UNPKG

4.12 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.BotkitBotFrameworkAdapter = void 0;
13/**
14 * @module botkit
15 */
16/**
17 * Copyright (c) Microsoft Corporation. All rights reserved.
18 * Licensed under the MIT License.
19 */
20const botbuilder_1 = require("botbuilder");
21const botframework_connector_1 = require("botframework-connector");
22const teamsHelpers_1 = require("./teamsHelpers");
23const os = require("os");
24const pjson = require('../package.json'); // eslint-disable-line @typescript-eslint/no-var-requires
25// Retrieve additional information, i.e., host operating system, host OS release, architecture, Node.js version
26const ARCHITECTURE = os.arch();
27const TYPE = os.type();
28const RELEASE = os.release();
29const NODE_VERSION = process.version;
30const USER_AGENT = `Microsoft-BotFramework/3.1 Botkit/${pjson.version} ` +
31 `(Node.js,Version=${NODE_VERSION}; ${TYPE} ${RELEASE}; ${ARCHITECTURE})`;
32/**
33 * This class extends the [BotFrameworkAdapter](https://docs.microsoft.com/en-us/javascript/api/botbuilder/botframeworkadapter?view=botbuilder-ts-latest) with a few additional features to support Microsoft Teams.
34 * * Changes userAgent to reflect Botkit instead of BotBuilder
35 * * Adds getChannels() (MS Teams)
36 * * Adds middleware for adjusting location of tenant id field (MS Teams)
37 */
38class BotkitBotFrameworkAdapter extends botbuilder_1.BotFrameworkAdapter {
39 constructor() {
40 super(...arguments);
41 this.botkit_worker = teamsHelpers_1.TeamsBotWorker;
42 }
43 /**
44 * Allows for mocking of the connector client in unit tests.
45 * Overridden by Botkit in order to change userAgent.
46 * @ignore
47 * @param serviceUrl Clients service url.
48 */
49 createConnectorClient(serviceUrl) {
50 const client = new botframework_connector_1.ConnectorClient(this.credentials, { baseUri: serviceUrl, userAgent: USER_AGENT });
51 return client;
52 }
53 /**
54 * Allows for mocking of the OAuth API Client in unit tests.
55 * Overridden by Botkit in order to change userAgent.
56 * @ignore
57 * @param serviceUrl Clients service url.
58 */
59 createTokenApiClient(serviceUrl) {
60 const client = new botframework_connector_1.TokenApiClient(this.credentials, { baseUri: serviceUrl, userAgent: USER_AGENT });
61 return client;
62 }
63 /**
64 * Get the list of channels in a MS Teams team.
65 * Can only be called with a TurnContext that originated in a team conversation - 1:1 conversations happen _outside a team_ and thus do not contain the required information to call this API.
66 * @param context A TurnContext object representing a message or event from a user in Teams
67 * @returns an array of channels in the format [{name: string, id: string}]
68 */
69 getChannels(context) {
70 return __awaiter(this, void 0, void 0, function* () {
71 if (context.activity.channelData && context.activity.channelData.team) {
72 const channels = yield botbuilder_1.TeamsInfo.getTeamChannels(context);
73 return channels ? channels.map((c) => { if (!c.name) {
74 c.name = 'General';
75 } return c; }) : [];
76 }
77 else {
78 console.error('getChannels cannot be called from unknown team');
79 return [];
80 }
81 });
82 }
83}
84exports.BotkitBotFrameworkAdapter = BotkitBotFrameworkAdapter;
85//# sourceMappingURL=adapter.js.map
\No newline at end of file