Botkit for the Web Class Reference

March 17, 2020 · View on GitHub

← Botkit Documentation ← Class Index

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

Classes


WebAdapter

Connect Botkit or BotBuilder to the Web. It offers both websocket and webhook capabilities. To use this adapter, you will need a compatible chat client - generate one using the Botkit yeoman generator, or use the one included in the project repo here.

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

npm install --save botbuilder-adapter-web

Then import this and other classes into your code:

const { WebAdapter } = require('botbuilder-adapter-web');

This class includes the following methods:

Create a new WebAdapter()

Parameters

ArgumentTypeDescription
socketServerOptionsan optional object containing parameters to send to a call to WebSocket.server.

Create an adapter to handle incoming messages from a websocket and/or webhook and translate them into a standard format for processing by your bot.

To use with Botkit:

const adapter = new WebAdapter();
const controller = new Botkit({
     adapter: adapter,
     // other options
});

To use with BotBuilder:

const adapter = new WebAdapter();
const server = restify.createServer();
server.use(restify.plugins.bodyParser());
// instead of binding processActivity to the incoming request, pass in turn handler logic to createSocketServer
let options = {}; // socket server configuration options
adapter.createSocketServer(server, options, async(context) => {
 // handle turn here
});

Properties and Accessors

NameTypeDescription
wssanyThe websocket server.

WebAdapter 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) => { ... }

createSocketServer()

Bind a websocket listener to an existing webserver object. Note: Create the server using Node's http.createServer

Parameters

ArgumentTypedescription
serveranyan http server
socketOptionsanyadditional options passed when creating the websocket server with WebSocket.server
logicanya turn handler function in the form async(context)=>{ ... } that will handle the bot's logic.

getConnection()

Returns websocket connection of given user Example: if (message.action === 'disconnect') bot.controller.adapter.getConnection(message.user).terminate()

Parameters

ArgumentTypedescription
userstring

init()

Botkit-only: Initialization function called automatically when used with Botkit. * Calls createSocketServer to bind a websocket listener to Botkit's pre-existing webserver.

Parameters

ArgumentTypedescription
botkitany

isConnected()

Is given user currently connected? Use this to test the websocket connection between the bot and a given user before sending messages, particularly in cases where a long period of time may have passed.

Parameters

ArgumentTypedescription
userstringthe id of a user, typically from message.user

Example: bot.controller.adapter.isConnected(message.user)

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.