如何从Microsoft Bot Framework发送SMS(使用Twilio通道)?

如何从Microsoft Bot Framework发送SMS(使用Twilio通道)?

问题描述:

目前,我的机器人在Facebook Messenger上,供员工使用.我希望我的机器人将一条短信发送给某人,以欢迎他/她加入我们的团队并获得其凭据.

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.

我知道Microsoft Bot Framework集成了Twilio,因此我按照以下方法集成了Twilio渠道: https://docs.microsoft.com/zh-cn/bot-framework/channel-connect-twilio ,所以我有一部电话,并且所有配置都很好,因为我可以手动发送短信(在Twilio的仪表板上),它可以正常工作.

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.

问题是我现在不知道如何在bot中使用它.

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 :/');
  }
};

如何实现这一目标?

精确,欢迎员工是一位教练,只是从机器人发送带有凭据的短信到机器人,该Web凭证将在机器人首次使用后被机器人连接的网络应用中使用

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

此处是Twilio开发人员的福音.

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.

由于您处于Node环境中,因此可以使用Twilio的API包装器.只需使用以下命令将 twilio 安装到您的项目中即可:

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.