UNPKG

8.59 kBTypeScriptView Raw
1/**
2 * @module botkit
3 */
4/**
5 * Copyright (c) Microsoft Corporation. All rights reserved.
6 * Licensed under the MIT License.
7 */
8import { Botkit, BotkitMessage } from './core';
9import { Activity, ConversationReference } from 'botbuilder';
10import { DialogTurnResult, Dialog } from 'botbuilder-dialogs';
11/**
12 * A base class for a `bot` instance, an object that contains the information and functionality for taking action in response to an incoming message.
13 * Note that adapters are likely to extend this class with additional platform-specific methods - refer to the adapter documentation for these extensions.
14 */
15export declare class BotWorker {
16 private _controller;
17 private _config;
18 /**
19 * Create a new BotWorker instance. Do not call this directly - instead, use [controller.spawn()](#spawn).
20 * @param controller A pointer to the main Botkit controller
21 * @param config An object typically containing { dialogContext, reference, context, activity }
22 */
23 constructor(controller: Botkit, config: any);
24 /**
25 * Get a reference to the main Botkit controller.
26 */
27 get controller(): Botkit;
28 /**
29 * Get a value from the BotWorker's configuration.
30 *
31 * ```javascript
32 * let original_context = bot.getConfig('context');
33 * await original_context.sendActivity('send directly using the adapter instead of Botkit');
34 * ```
35 *
36 * @param {string} key The name of a value stored in the configuration
37 * @returns {any} The value stored in the configuration (or null if absent)
38 */
39 getConfig(key?: string): any;
40 /**
41 * Send a message using whatever context the `bot` was spawned in or set using [changeContext()](#changecontext) --
42 * or more likely, one of the platform-specific helpers like
43 * [startPrivateConversation()](../reference/slack.md#startprivateconversation) (Slack),
44 * [startConversationWithUser()](../reference/twilio-sms.md#startconversationwithuser) (Twilio SMS),
45 * and [startConversationWithUser()](../reference/facebook.md#startconversationwithuser) (Facebook Messenger).
46 * Be sure to check the platform documentation for others - most adapters include at least one.
47 *
48 * Simple use in event handler (acts the same as bot.reply)
49 * ```javascript
50 * controller.on('event', async(bot, message) => {
51 *
52 * await bot.say('I received an event!');
53 *
54 * });
55 * ```
56 *
57 * Use with a freshly spawned bot and bot.changeContext:
58 * ```javascript
59 * let bot = controller.spawn(OPTIONS);
60 * bot.changeContext(REFERENCE);
61 * bot.say('ALERT! I have some news.');
62 * ```
63 *
64 * Use with multi-field message object:
65 * ```javascript
66 * controller.on('event', async(bot, message) => {
67 * bot.say({
68 * text: 'I heard an event',
69 * attachments: [
70 * title: message.type,
71 * text: `The message was of type ${ message.type }`,
72 * // ...
73 * ]
74 * });
75 * });
76 * ```
77 *
78 * @param message A string containing the text of a reply, or more fully formed message object
79 * @returns Return value will contain the results of the send action, typically `{id: <id of message>}`
80 */
81 say(message: Partial<BotkitMessage> | string): Promise<any>;
82 /**
83 * Reply to an incoming message.
84 * Message will be sent using the context of the source message, which may in some cases be different than the context used to spawn the bot.
85 *
86 * Note that like [bot.say()](#say), `reply()` can take a string or a message object.
87 *
88 * ```javascript
89 * controller.on('event', async(bot, message) => {
90 *
91 * await bot.reply(message, 'I received an event and am replying to it.');
92 *
93 * });
94 * ```
95 *
96 * @param src An incoming message, usually passed in to a handler function
97 * @param resp A string containing the text of a reply, or more fully formed message object
98 * @returns Return value will contain the results of the send action, typically `{id: <id of message>}`
99 */
100 reply(src: Partial<BotkitMessage>, resp: Partial<BotkitMessage> | string): Promise<any>;
101 /**
102 * Begin a pre-defined dialog by specifying its id. The dialog will be started in the same context (same user, same channel) in which the original incoming message was received.
103 * [See "Using Dialogs" in the core documentation.](../index.md#using-dialogs)
104 *
105 * ```javascript
106 * controller.hears('hello', 'message', async(bot, message) => {
107 * await bot.beginDialog(GREETINGS_DIALOG);
108 * });
109 * ```
110 * @param id id of dialog
111 * @param options object containing options to be passed into the dialog
112 */
113 beginDialog(id: string, options?: any): Promise<void>;
114 /**
115 * Cancel any and all active dialogs for the current user/context.
116 */
117 cancelAllDialogs(): Promise<DialogTurnResult>;
118 /**
119 * Get a reference to the active dialog
120 * @returns a reference to the active dialog or undefined if no dialog is active
121 */
122 getActiveDialog(): Dialog | undefined;
123 /**
124 * Check if any dialog is active or not
125 * @returns true if there is an active dialog, otherwise false
126 */
127 hasActiveDialog(): boolean;
128 /**
129 * Check to see if a given dialog is currently active in the stack
130 * @param id The id of a dialog to look for in the dialog stack
131 * @returns true if dialog with id is located anywhere in the dialog stack
132 */
133 isDialogActive(id: string): boolean;
134 /**
135 * Replace any active dialogs with a new a pre-defined dialog by specifying its id. The dialog will be started in the same context (same user, same channel) in which the original incoming message was received.
136 * [See "Using Dialogs" in the core documentation.](../index.md#using-dialogs)
137 *
138 * ```javascript
139 * controller.hears('hello', 'message', async(bot, message) => {
140 * await bot.replaceDialog(GREETINGS_DIALOG);
141 * });
142 * ```
143 * @param id id of dialog
144 * @param options object containing options to be passed into the dialog
145 */
146 replaceDialog(id: string, options?: any): Promise<void>;
147 /**
148 * Alter the context in which a bot instance will send messages.
149 * Use this method to create or adjust a bot instance so that it can send messages to a predefined user/channel combination.
150 *
151 * ```javascript
152 * // get the reference field and store it.
153 * const saved_reference = message.reference;
154 *
155 * // later on...
156 * let bot = await controller.spawn();
157 * bot.changeContext(saved_reference);
158 * bot.say('Hello!');
159 * ```
160 *
161 * @param reference A [ConversationReference](https://docs.microsoft.com/en-us/javascript/api/botframework-schema/conversationreference?view=botbuilder-ts-latest), most likely captured from an incoming message and stored for use in proactive messaging scenarios.
162 */
163 changeContext(reference: Partial<ConversationReference>): Promise<BotWorker>;
164 startConversationWithUser(reference: any): Promise<void>;
165 /**
166 * Take a crudely-formed Botkit message with any sort of field (may just be a string, may be a partial message object)
167 * and map it into a beautiful BotFramework Activity.
168 * Any fields not found in the Activity definition will be moved to activity.channelData.
169 * @params message a string or partial outgoing message object
170 * @returns a properly formed Activity object
171 */
172 ensureMessageFormat(message: Partial<BotkitMessage> | string): Partial<Activity>;
173 /**
174 * Set the http response status code for this turn
175 *
176 * ```javascript
177 * controller.on('event', async(bot, message) => {
178 * // respond with a 500 error code for some reason!
179 * bot.httpStatus(500);
180 * });
181 * ```
182 *
183 * @param status {number} a valid http status code like 200 202 301 500 etc
184 */
185 httpStatus(status: number): void;
186 /**
187 * Set the http response body for this turn.
188 * Use this to define the response value when the platform requires a synchronous response to the incoming webhook.
189 *
190 * Example handling of a /slash command from Slack:
191 * ```javascript
192 * controller.on('slash_command', async(bot, message) => {
193 * bot.httpBody('This is a reply to the slash command.');
194 * })
195 * ```
196 *
197 * @param body (any) a value that will be returned as the http response body
198 */
199 httpBody(body: any): void;
200}