UNPKG

@microsoft/botbuilder-m365

Version:

M365 extensions for Microsoft BotBuilder, Alpha release.

353 lines 21.8 kB
"use strict"; /** * @module botbuilder-m365 */ /** * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. */ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.MessageExtensions = void 0; const botbuilder_1 = require("botbuilder"); const ANONYMOUS_QUERY_LINK_INVOKE_NAME = `composeExtension/anonymousQueryLink`; const FETCH_TASK_INVOKE_NAME = `composeExtension/fetchTask`; const QUERY_INVOKE_NAME = `composeExtension/query`; const QUERY_LINK_INVOKE_NAME = `composeExtension/queryLink`; const SELECT_ITEM_INVOKE_NAME = `composeExtension/selectItem`; const SUBMIT_ACTION_INVOKE_NAME = `composeExtension/submitAction`; class MessageExtensions { constructor(app) { this._app = app; } anonymousQueryLink(commandId, handler) { (Array.isArray(commandId) ? commandId : [commandId]).forEach((cid) => { const selector = createTaskSelector(cid, ANONYMOUS_QUERY_LINK_INVOKE_NAME); this._app.addRoute(selector, (context, state) => __awaiter(this, void 0, void 0, function* () { var _a, _b, _c; // Insure that we're in an invoke as expected if (((_a = context === null || context === void 0 ? void 0 : context.activity) === null || _a === void 0 ? void 0 : _a.type) !== botbuilder_1.ActivityTypes.Invoke || ((_b = context === null || context === void 0 ? void 0 : context.activity) === null || _b === void 0 ? void 0 : _b.name) !== ANONYMOUS_QUERY_LINK_INVOKE_NAME) { throw new Error(`Unexpected MessageExtensions.anonymousQueryLink() triggered for activity type: ${(_c = context === null || context === void 0 ? void 0 : context.activity) === null || _c === void 0 ? void 0 : _c.type}`); } // Call handler and then check to see if an invoke response has already been added const result = yield handler(context, state); if (!context.turnState.get(botbuilder_1.INVOKE_RESPONSE_KEY)) { // Format invoke response const response = { composeExtension: result }; // Queue up invoke response yield context.sendActivity({ value: { body: response, status: 200 }, type: botbuilder_1.ActivityTypes.InvokeResponse }); } }), true); }); return this._app; } botMessagePreviewEdit(commandId, handler) { (Array.isArray(commandId) ? commandId : [commandId]).forEach((cid) => { const selector = createTaskSelector(cid, SUBMIT_ACTION_INVOKE_NAME, 'edit'); this._app.addRoute(selector, (context, state) => __awaiter(this, void 0, void 0, function* () { var _a, _b, _c, _d, _e, _f, _g; // Insure that we're in an invoke as expected if (((_a = context === null || context === void 0 ? void 0 : context.activity) === null || _a === void 0 ? void 0 : _a.type) !== botbuilder_1.ActivityTypes.Invoke || ((_b = context === null || context === void 0 ? void 0 : context.activity) === null || _b === void 0 ? void 0 : _b.name) !== SUBMIT_ACTION_INVOKE_NAME || ((_d = (_c = context === null || context === void 0 ? void 0 : context.activity) === null || _c === void 0 ? void 0 : _c.value) === null || _d === void 0 ? void 0 : _d.botMessagePreviewAction) !== 'edit') { throw new Error(`Unexpected MessageExtensions.botMessagePreviewEdit() triggered for activity type: ${(_e = context === null || context === void 0 ? void 0 : context.activity) === null || _e === void 0 ? void 0 : _e.type}`); } // Call handler and then check to see if an invoke response has already been added const result = yield handler(context, state, (_g = (_f = context.activity.value) === null || _f === void 0 ? void 0 : _f.botActivityPreview[0]) !== null && _g !== void 0 ? _g : {}); yield this.returnSubmitActionResponse(context, result); }), true); }); return this._app; } botMessagePreviewSend(commandId, handler) { (Array.isArray(commandId) ? commandId : [commandId]).forEach((cid) => { const selector = createTaskSelector(cid, SUBMIT_ACTION_INVOKE_NAME, 'send'); this._app.addRoute(selector, (context, state) => __awaiter(this, void 0, void 0, function* () { var _a, _b, _c, _d, _e, _f, _g; // Insure that we're in an invoke as expected if (((_a = context === null || context === void 0 ? void 0 : context.activity) === null || _a === void 0 ? void 0 : _a.type) !== botbuilder_1.ActivityTypes.Invoke || ((_b = context === null || context === void 0 ? void 0 : context.activity) === null || _b === void 0 ? void 0 : _b.name) !== SUBMIT_ACTION_INVOKE_NAME || ((_d = (_c = context === null || context === void 0 ? void 0 : context.activity) === null || _c === void 0 ? void 0 : _c.value) === null || _d === void 0 ? void 0 : _d.botMessagePreviewAction) !== 'send') { throw new Error(`Unexpected MessageExtensions.botMessagePreviewSend() triggered for activity type: ${(_e = context === null || context === void 0 ? void 0 : context.activity) === null || _e === void 0 ? void 0 : _e.type}`); } // Call handler and then check to see if an invoke response has already been added yield handler(context, state, (_g = (_f = context.activity.value) === null || _f === void 0 ? void 0 : _f.botActivityPreview[0]) !== null && _g !== void 0 ? _g : {}); // Queue up invoke response if (!context.turnState.get(botbuilder_1.INVOKE_RESPONSE_KEY)) { yield context.sendActivity({ value: { body: {}, status: 200 }, type: botbuilder_1.ActivityTypes.InvokeResponse }); } }), true); }); return this._app; } fetchTask(commandId, handler) { (Array.isArray(commandId) ? commandId : [commandId]).forEach((cid) => { const selector = createTaskSelector(cid, FETCH_TASK_INVOKE_NAME); this._app.addRoute(selector, (context, state) => __awaiter(this, void 0, void 0, function* () { var _a, _b, _c; // Insure that we're in an invoke as expected if (((_a = context === null || context === void 0 ? void 0 : context.activity) === null || _a === void 0 ? void 0 : _a.type) !== botbuilder_1.ActivityTypes.Invoke || ((_b = context === null || context === void 0 ? void 0 : context.activity) === null || _b === void 0 ? void 0 : _b.name) !== FETCH_TASK_INVOKE_NAME) { throw new Error(`Unexpected MessageExtensions.fetchTask() triggered for activity type: ${(_c = context === null || context === void 0 ? void 0 : context.activity) === null || _c === void 0 ? void 0 : _c.type}`); } // Call handler and then check to see if an invoke response has already been added const result = yield handler(context, state); if (!context.turnState.get(botbuilder_1.INVOKE_RESPONSE_KEY)) { // Format invoke response let response; if (typeof result == 'string') { // Return message response = { task: { type: 'message', value: result } }; } else { // Return card response = { task: { type: 'continue', value: result } }; } // Queue up invoke response yield context.sendActivity({ value: { body: response, status: 200 }, type: botbuilder_1.ActivityTypes.InvokeResponse }); } }), true); }); return this._app; } query(commandId, handler) { (Array.isArray(commandId) ? commandId : [commandId]).forEach((cid) => { const selector = createTaskSelector(cid, QUERY_INVOKE_NAME); this._app.addRoute(selector, (context, state) => __awaiter(this, void 0, void 0, function* () { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k; // Insure that we're in an invoke as expected if (((_a = context === null || context === void 0 ? void 0 : context.activity) === null || _a === void 0 ? void 0 : _a.type) !== botbuilder_1.ActivityTypes.Invoke || ((_b = context === null || context === void 0 ? void 0 : context.activity) === null || _b === void 0 ? void 0 : _b.name) !== QUERY_INVOKE_NAME) { throw new Error(`Unexpected MessageExtensions.query() triggered for activity type: ${(_c = context === null || context === void 0 ? void 0 : context.activity) === null || _c === void 0 ? void 0 : _c.type}`); } // Flatten query options const meQuery = (_e = (_d = context === null || context === void 0 ? void 0 : context.activity) === null || _d === void 0 ? void 0 : _d.value) !== null && _e !== void 0 ? _e : {}; const query = { count: (_g = (_f = meQuery === null || meQuery === void 0 ? void 0 : meQuery.queryOptions) === null || _f === void 0 ? void 0 : _f.count) !== null && _g !== void 0 ? _g : 25, skip: (_j = (_h = meQuery === null || meQuery === void 0 ? void 0 : meQuery.queryOptions) === null || _h === void 0 ? void 0 : _h.skip) !== null && _j !== void 0 ? _j : 0, parameters: {} }; // Flatten query parameters ((_k = meQuery.parameters) !== null && _k !== void 0 ? _k : []).forEach((param) => { if (param.name) { query.parameters[param.name] = param.value; } }); // Call handler and then check to see if an invoke response has already been added const result = yield handler(context, state, query); if (!context.turnState.get(botbuilder_1.INVOKE_RESPONSE_KEY)) { // Format invoke response const response = { composeExtension: result }; // Queue up invoke response yield context.sendActivity({ value: { body: response, status: 200 }, type: botbuilder_1.ActivityTypes.InvokeResponse }); } }), true); }); return this._app; } queryLink(commandId, handler) { (Array.isArray(commandId) ? commandId : [commandId]).forEach((cid) => { const selector = createTaskSelector(cid, QUERY_LINK_INVOKE_NAME); this._app.addRoute(selector, (context, state) => __awaiter(this, void 0, void 0, function* () { var _a, _b, _c; // Insure that we're in an invoke as expected if (((_a = context === null || context === void 0 ? void 0 : context.activity) === null || _a === void 0 ? void 0 : _a.type) !== botbuilder_1.ActivityTypes.Invoke || ((_b = context === null || context === void 0 ? void 0 : context.activity) === null || _b === void 0 ? void 0 : _b.name) !== QUERY_LINK_INVOKE_NAME) { throw new Error(`Unexpected MessageExtensions.queryLink() triggered for activity type: ${(_c = context === null || context === void 0 ? void 0 : context.activity) === null || _c === void 0 ? void 0 : _c.type}`); } // Call handler and then check to see if an invoke response has already been added const result = yield handler(context, state); if (!context.turnState.get(botbuilder_1.INVOKE_RESPONSE_KEY)) { // Format invoke response const response = { composeExtension: result }; // Queue up invoke response yield context.sendActivity({ value: { body: response, status: 200 }, type: botbuilder_1.ActivityTypes.InvokeResponse }); } }), true); }); return this._app; } selectItem(handler) { // Define static route selector const selector = (context) => { var _a; return Promise.resolve(((_a = context === null || context === void 0 ? void 0 : context.activity) === null || _a === void 0 ? void 0 : _a.type) == botbuilder_1.ActivityTypes.Invoke && (context === null || context === void 0 ? void 0 : context.activity.name) === SELECT_ITEM_INVOKE_NAME); }; // Add route this._app.addRoute(selector, (context, state) => __awaiter(this, void 0, void 0, function* () { var _a, _b; // Call handler and then check to see if an invoke response has already been added const result = yield handler(context, state, (_b = (_a = context === null || context === void 0 ? void 0 : context.activity) === null || _a === void 0 ? void 0 : _a.value) !== null && _b !== void 0 ? _b : {}); if (!context.turnState.get(botbuilder_1.INVOKE_RESPONSE_KEY)) { // Format invoke response const response = { composeExtension: result }; // Queue up invoke response yield context.sendActivity({ value: { body: response, status: 200 }, type: botbuilder_1.ActivityTypes.InvokeResponse }); } }), true); return this._app; } submitAction(commandId, handler) { (Array.isArray(commandId) ? commandId : [commandId]).forEach((cid) => { const selector = createTaskSelector(cid, SUBMIT_ACTION_INVOKE_NAME); this._app.addRoute(selector, (context, state) => __awaiter(this, void 0, void 0, function* () { var _a, _b, _c, _d, _e; // Insure that we're in an invoke as expected if (((_a = context === null || context === void 0 ? void 0 : context.activity) === null || _a === void 0 ? void 0 : _a.type) !== botbuilder_1.ActivityTypes.Invoke || ((_b = context === null || context === void 0 ? void 0 : context.activity) === null || _b === void 0 ? void 0 : _b.name) !== SUBMIT_ACTION_INVOKE_NAME) { throw new Error(`Unexpected MessageExtensions.submitAction() triggered for activity type: ${(_c = context === null || context === void 0 ? void 0 : context.activity) === null || _c === void 0 ? void 0 : _c.type}`); } // Call handler and then check to see if an invoke response has already been added const result = yield handler(context, state, (_e = (_d = context.activity.value) === null || _d === void 0 ? void 0 : _d.data) !== null && _e !== void 0 ? _e : {}); yield this.returnSubmitActionResponse(context, result); }), true); }); return this._app; } returnSubmitActionResponse(context, result) { return __awaiter(this, void 0, void 0, function* () { if (!context.turnState.get(botbuilder_1.INVOKE_RESPONSE_KEY)) { // Format invoke response let response; if (typeof result == 'string') { // Return message response = { task: { type: 'message', value: result } }; } else if (typeof result == 'object') { if (result.card) { // Return another task module response = { task: { type: 'continue', value: result } }; } else { // Return card to user response = { composeExtension: result }; } } else { // No action taken response = { composeExtension: undefined }; } // Queue up invoke response yield context.sendActivity({ value: { body: response, status: 200 }, type: botbuilder_1.ActivityTypes.InvokeResponse }); } }); } } exports.MessageExtensions = MessageExtensions; /** * * @param {string | RegExp | RouteSelector[]} commandId Name of the commandId * @param {boolean} invokeName Whether or not the commandId a Teams invokable action * @param {string} botMessagePreviewAction Message Extension preview action 'edit' or 'send' * @returns {RouteSelector} Route selector function */ function createTaskSelector(commandId, invokeName, botMessagePreviewAction) { if (typeof commandId == 'function') { // Return the passed in selector function return commandId; } else if (commandId instanceof RegExp) { // Return a function that matches the commandId using a RegExp return (context) => { var _a, _b, _c, _d; const isInvoke = ((_a = context === null || context === void 0 ? void 0 : context.activity) === null || _a === void 0 ? void 0 : _a.type) == botbuilder_1.ActivityTypes.Invoke && ((_b = context === null || context === void 0 ? void 0 : context.activity) === null || _b === void 0 ? void 0 : _b.name) == invokeName; if (isInvoke && typeof ((_d = (_c = context === null || context === void 0 ? void 0 : context.activity) === null || _c === void 0 ? void 0 : _c.value) === null || _d === void 0 ? void 0 : _d.commandId) == 'string' && matchesPreviewAction(context.activity, botMessagePreviewAction)) { return Promise.resolve(commandId.test(context.activity.value.commandId)); } else { return Promise.resolve(false); } }; } else { // Return a function that attempts to match commandId return (context) => { var _a, _b, _c, _d; const isInvoke = ((_a = context === null || context === void 0 ? void 0 : context.activity) === null || _a === void 0 ? void 0 : _a.type) == botbuilder_1.ActivityTypes.Invoke && ((_b = context === null || context === void 0 ? void 0 : context.activity) === null || _b === void 0 ? void 0 : _b.name) == invokeName; return Promise.resolve(isInvoke && ((_d = (_c = context === null || context === void 0 ? void 0 : context.activity) === null || _c === void 0 ? void 0 : _c.value) === null || _d === void 0 ? void 0 : _d.commandId) === commandId && matchesPreviewAction(context.activity, botMessagePreviewAction)); }; } } /** * Checks if the activity is a bot message preview action. * * @param {Activity} activity The activity / communication type that is being checked. * @param {string} botMessagePreviewAction Name of the preview action * @returns {boolean} True if the activity is a bot message preview action, false if it is not. */ function matchesPreviewAction(activity, botMessagePreviewAction) { var _a; if (typeof ((_a = activity === null || activity === void 0 ? void 0 : activity.value) === null || _a === void 0 ? void 0 : _a.botMessagePreviewAction) == 'string') { return activity.value.botMessagePreviewAction == botMessagePreviewAction; } else { return botMessagePreviewAction == undefined; } } //# sourceMappingURL=MessageExtensions.js.map