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

Latest commit

 

History

History
182 lines (121 loc) · 5.67 KB

web.md

File metadata and controls

182 lines (121 loc) · 5.67 KB

Botkit for the Web Class Reference

← 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

Argument Type Description
socketServerOptions an 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

Name Type Description
wss any The websocket server.

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

createSocketServer()

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

Parameters

Argument Type description
server any an http server
socketOptions any additional options passed when creating the websocket server with WebSocket.server
logic any a 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

Argument Type description
user string

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

Argument Type description
botkit any

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

Argument Type description
user string the 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

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.