Botkit for Twilio SMS Class Reference

May 20, 2019 · View on GitHub

← Botkit Documentation ← Class Index

This is a class reference for all the methods exposed by the botbuilder-adapter-twilio-sms package.

Classes

Interfaces


TwilioAdapter

Connect Botkit or BotBuilder to Twilio's SMS service.

To use this class in your application, first install the package:

npm install --save botbuilder-adapter-twilio-sms

Then import this and other classes into your code:

const { TwilioAdapter } = require('botbuilder-adapter-twilio-sms');

This class includes the following methods:

Create a new TwilioAdapter()

Parameters

ArgumentTypeDescription
optionsTwilioAdapterOptionsAn object containing API credentials, a webhook verification token and other options

Create an adapter to handle incoming messages from Twilio's SMS service and translate them into a standard format for processing by your bot.

Use with Botkit:

const adapter = new TwilioAdapter({
     twilio_number: process.env.TWILIO_NUMBER,
     account_sid: process.env.TWILIO_ACCOUNT_SID,
     auth_token: process.env.TWILIO_AUTH_TOKEN,
     validation_url: process.env.TWILIO_VALIDATION_URL
});
const controller = new Botkit({
     adapter: adapter,
     // ... other configuration options
});

Use with BotBuilder:

const adapter = new TwilioAdapter({
     twilio_number: process.env.TWILIO_NUMBER,
     account_sid: process.env.TWILIO_ACCOUNT_SID,
     auth_token: process.env.TWILIO_AUTH_TOKEN,
     validation_url: process.env.TWILIO_VALIDATION_URL
});
// set up restify...
const server = restify.createServer();
server.use(restify.plugins.bodyParser());
server.post('/api/messages', (req, res) => {
     adapter.processActivity(req, res, async(context) => {
         // do your bot logic here!
     });
});

TwilioAdapter Class Methods

continueConversation()

Standard BotBuilder adapter method for continuing an existing conversation based on a conversation reference. BotBuilder reference docs

Parameters

ArgumentTypedescription
referencePartial<ConversationReference>A conversation reference to be applied to future messages.
logicA bot logic function that will perform continuing action in the form async(context) => { ... }

processActivity()

Accept an incoming webhook request and convert it into a TurnContext which can be processed by the bot's logic.

Parameters

ArgumentTypedescription
reqanyA request object from Restify or Express
resanyA response object from Restify or Express
logicA bot logic function in the form async(context) => { ... }

sendActivities()

Standard BotBuilder adapter method to send a message from the bot to the messaging API. BotBuilder reference docs.

Parameters

ArgumentTypedescription
contextTurnContextA TurnContext representing the current incoming message and environment. (Not used)
activitiesAn array of outgoing activities to be sent back to the messaging API.

TwilioBotWorker

This is a specialized version of Botkit's core BotWorker class that includes additional methods for interacting with Twilio SMS. It includes all functionality from the base class, as well as the extension methods below.

When using the TwilioAdapter with Botkit, all bot objects passed to handler functions will include these extensions.

To use this class in your application, first install the package:

npm install --save botbuilder-adapter-twilio-sms

Then import this and other classes into your code:

const { TwilioBotWorker } = require('botbuilder-adapter-twilio-sms');

This class includes the following methods:

Properties and Accessors

NameTypeDescription
apiTwilio.TwilioA copy of the Twilio API client.

TwilioBotWorker Class Methods

startConversationWithUser()

Start a conversation with a given user identified by their phone number. Useful for sending pro-active messages:

Parameters

ArgumentTypedescription
userIdstringA phone number in the form +1XXXYYYZZZZ
let bot = await controller.spawn();
await bot.startConversationWithUser(MY_PHONE_NUMBER);
await bot.send('An important update!');

Interface TwilioAdapterOptions

Parameters passed to the TwilioAdapter constructor.

Fields

NameTypeDescription
account_sidstringThe account SID from the twilio account
auth_tokenstringAn api auth token associated with the twilio account
enable_incompletebooleanAllow the adapter to startup without a complete configuration.
This is risky as it may result in a non-functioning or insecure adapter.
This should only be used when getting started.
twilio_numberstringThe phone number associated with this Twilio app, in the format 1XXXYYYZZZZ
validation_urlstringAn optional url to override the automatically generated url signature used to validate incoming requests -- See Twilio docs about securing your endpoint.