mdominick
mdominick

Reputation: 1319

Send Twilio SMS in Microsoft Bot Framework

I'm having a hard time getting my bot to send a Twilio message. I keep getting 400 bad request I am looking out the msg object here as JSON --- I assume there's something wrong with the way I'm creating the address for Twilio? I have also already gone through and setup the Twilio channel in the admin tool

SMS == {"data":{"type":"message","agent":"botbuilder","text":"test msg","address":{"channelId":"sms","conversation":{"id":"4i4hlandl06ha5g3","isGroup":false},"serviceUrl":"https://sms.botframework.com","useAuth":true},"source":"sms"}} sendMsg - Session.sendBatch() sending 0 message(s)

So dding a little more details. I'm using Node.js, get no error in the console what you see above is the JSON version of sms from:

        bot.dialog('sendMsg', function(session, context) {
        var address = addresses.twilioAddress(session);
        console.log(JSON.stringify(address));
        var sms = new builder.Message().text('test msg').address(address);
        console.log("SMS == " + JSON.stringify(sms));
        bot.send(sms);
    }).triggerAction({ matches: 'Communication.SendMessage' })
}

My current thinking is that I am creating the address for Twilio wrong. Here's what that looks like:

module.exports = {
twilioAddress: function(session) {
    console.log('made it to twilio Address');
    var address = init("twilio test", session, false, 'https://sms.botframework.com', 'sms');
    return address;
}

}

function init(name, session, isGroup, serviceUrl, channelId) { var address = { channelId: channelId, conversation: { id: session.message.address.id, isGroup: !!isGroup }, serviceUrl: serviceUrl, useAuth: true }; console.log(JSON.stringify(address)); return address; }

Again, my instincts are telling me that I'm mangling creating the address somehow.

Upvotes: 1

Views: 390

Answers (1)

nwxdev
nwxdev

Reputation: 4625

Use session.send("Message content here"); instead of bot.send().

When you use session.send() it will handle all the address stuff for you.

For more information, see: https://learn.microsoft.com/en-us/bot-framework/nodejs/bot-builder-nodejs-use-default-message-handler

Upvotes: 0

Related Questions