UNPKG

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