UNPKG

5.33 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4 return new (P || (P = Promise))(function (resolve, reject) {
5 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8 step((generator = generator.apply(thisArg, _arguments || [])).next());
9 });
10};
11Object.defineProperty(exports, "__esModule", { value: true });
12exports.TeamsInvokeMiddleware = exports.TeamsBotWorker = void 0;
13const botworker_1 = require("./botworker");
14const botbuilder_1 = require("botbuilder");
15/**
16 * This is a specialized version of [Botkit's core BotWorker class](core.md#BotWorker) that includes additional methods for interacting with Microsoft Teams.
17 * It includes all functionality from the base class, as well as the extension methods below.
18 * This BotWorker is used with the built-in Bot Framework adapter.
19 * @noInheritDoc
20 */
21class TeamsBotWorker extends botworker_1.BotWorker {
22 constructor() {
23 super(...arguments);
24 /**
25 * Grants access to the TeamsInfo helper class
26 * See: https://docs.microsoft.com/en-us/javascript/api/botbuilder/teamsinfo?view=botbuilder-ts-latest
27 */
28 this.teams = botbuilder_1.TeamsInfo;
29 }
30 /**
31 * Reply to a Teams task module task/fetch or task/submit with a task module response.
32 * See https://docs.microsoft.com/en-us/microsoftteams/platform/task-modules-and-cards/task-modules/task-modules-bots
33 * @param message
34 * @param taskInfo an object in the form {type, value}
35 */
36 replyWithTaskInfo(message, taskInfo) {
37 return __awaiter(this, void 0, void 0, function* () {
38 if (!taskInfo || taskInfo === {}) {
39 // send a null response back
40 taskInfo = {
41 type: 'message',
42 value: ''
43 };
44 }
45 return new Promise((resolve, reject) => {
46 this.controller.middleware.send.run(this, taskInfo, (err, bot, taskInfo) => __awaiter(this, void 0, void 0, function* () {
47 if (err) {
48 return reject(err);
49 }
50 resolve(yield this.getConfig('context').sendActivity({
51 type: 'invokeResponse',
52 value: {
53 status: 200,
54 body: {
55 task: taskInfo
56 }
57 }
58 }));
59 }));
60 });
61 });
62 }
63}
64exports.TeamsBotWorker = TeamsBotWorker;
65/**
66 * When used, causes Botkit to emit special events for teams "invokes"
67 * Based on https://github.com/microsoft/botbuilder-js/blob/master/libraries/botbuilder/src/teamsActivityHandler.ts
68 * This allows Botkit bots to respond directly to task/fetch or task/submit events, as an example.
69 * To use this, bind it to the adapter before creating the Botkit controller:
70 * ```javascript
71 * const Botkit = new Botkit({...});
72 * botkit.adapter.use(new TeamsInvokeMiddleware());
73 *
74 * // can bind directly to task/fetch, task/submit and other invoke types used by teams
75 * controller.on('task/fetch', async(bot, message) => {
76 * await bot.replyWithTaskInfo(message, taskInfo);
77 * });
78 * ```
79 */
80class TeamsInvokeMiddleware extends botbuilder_1.MiddlewareSet {
81 /**
82 * Not for direct use - implements the MiddlewareSet's required onTurn function used to process the event
83 * @param context
84 * @param next
85 */
86 onTurn(context, next) {
87 return __awaiter(this, void 0, void 0, function* () {
88 if (context.activity.type === 'invoke') {
89 if (!context.activity.name && context.activity.channelId === 'msteams') {
90 context.activity.channelData.botkitEventType = 'cardAction';
91 }
92 else {
93 switch (context.activity.name) {
94 case 'fileConsent/invoke':
95 case 'actionableMessage/executeAction':
96 case 'composeExtension/queryLink':
97 case 'composeExtension/query':
98 case 'composeExtension/selectItem':
99 case 'composeExtension/submitAction':
100 case 'composeExtension/fetchTask':
101 case 'composeExtension/querySettingUrl':
102 case 'composeExtension/setting':
103 case 'composeExtension/onCardButtonClicked':
104 case 'task/fetch':
105 case 'task/submit':
106 context.activity.channelData.botkitEventType = context.activity.name;
107 break;
108 }
109 }
110 }
111 yield next();
112 });
113 }
114}
115exports.TeamsInvokeMiddleware = TeamsInvokeMiddleware;
116//# sourceMappingURL=teamsHelpers.js.map
\No newline at end of file