How to send SMS (using Twilio channel) from Microsoft Bot Framework?

我的未来我决定 提交于 2021-02-08 09:43:11

问题


Currently my bot is on Facebook messenger, used by employees. I'd like my bot to send one SMS to a person to welcome him / her to our team and with its credentials.

I know Microsoft Bot Framework integrates Twilio, so I integrated Twilio channel following this: https://docs.microsoft.com/en-us/bot-framework/channel-connect-twilio, so I have a phone, and everything is well configured because I can send manually SMS (from the Twilio's dashboard), it works.

Problem is that I don't know how to use it right now, in the bot.

const confirmPerson = (session, results) => {
  try {
    if (results.response && session.userData.required) {

      // Here I want to send SMS

      session.endDialog('SMS sent ! (TODO)');
    } else {
      session.endDialog('SMS cancelled !');
    }
  } catch (e) {
    console.error(e);
    session.endDialog('I had a problem while sending SMS :/');
  }
};

How to achieve this ?

EDIT: Precision, the person welcoming employee is a coach, just sending SMS from bot with the credentials to use in the webapp the bot connects after first usage by the user welcomed


回答1:


Twilio developer evangelist here.

You can do this in bot framework by sending an ad-hoc proactive message. It seems you'd need to create an address for the user that you want to send messages to though and I can't find in the documentation what an address should look like.

Since you're in a Node environment, you could use Twilio's API wrapper to this though. Just install twilio to your project with:

npm install twilio

Then gather your account credentials and use the module like so:

const Twilio = require('twilio');

const confirmPerson = (session, results) => {
  try {
    if (results.response && session.userData.required) {

      const client = new Twilio('your_account_sid','your_auth_token');

      client.messages.create({
        to: session.userData.phoneNumber,   // or whereever it's stored.
        from: 'your_twilio_number',
        body: 'Your body here'
      }).then(function() {
        session.endDialog('SMS sent ! (TODO)');
      }).catch(function() {
        session.endDialog('SMS could not be sent.');
      })

    } else {
      session.endDialog('SMS cancelled !');
    }
  } catch (e) {
    console.error(e);
    session.endDialog('I had a problem while sending SMS :/');
  }
};

Let me know how this goes.



来源:https://stackoverflow.com/questions/45999559/how-to-send-sms-using-twilio-channel-from-microsoft-bot-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!