Skip to content
This repository has been archived by the owner on Sep 20, 2024. It is now read-only.

Latest commit

 

History

History
192 lines (131 loc) · 6.29 KB

twilio-sms.md

File metadata and controls

192 lines (131 loc) · 6.29 KB

Botkit for Twilio SMS Class Reference

← 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

Argument Type Description
options TwilioAdapterOptions An 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

Argument Type description
reference Partial<ConversationReference> A conversation reference to be applied to future messages.
logic A 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

Argument Type description
req any A request object from Restify or Express
res any A response object from Restify or Express
logic A 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

Argument Type description
context TurnContext A TurnContext representing the current incoming message and environment. (Not used)
activities An 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

Name Type Description
api Twilio.Twilio A 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

Argument Type description
userId string A 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

Name Type Description
account_sid string The account SID from the twilio account
auth_token string An api auth token associated with the twilio account
enable_incomplete boolean Allow 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_number string The phone number associated with this Twilio app, in the format 1XXXYYYZZZZ
validation_url string An optional url to override the automatically generated url signature used to validate incoming requests -- See Twilio docs about securing your endpoint.