UNPKG

3.16 kBPlain TextView Raw
1/**
2 * @module botkit
3 */
4/**
5 * Copyright (c) Microsoft Corporation. All rights reserved.
6 * Licensed under the MIT License.
7 */
8import { BotFrameworkAdapter, TurnContext, TeamsInfo, ChannelInfo } from 'botbuilder';
9import { ConnectorClient, TokenApiClient } from 'botframework-connector';
10import { TeamsBotWorker } from './teamsHelpers';
11import * as os from 'os';
12
13const pjson: any = require('../package.json'); // eslint-disable-line @typescript-eslint/no-var-requires
14
15// Retrieve additional information, i.e., host operating system, host OS release, architecture, Node.js version
16const ARCHITECTURE: any = os.arch();
17const TYPE: any = os.type();
18const RELEASE: any = os.release();
19const NODE_VERSION: any = process.version;
20const USER_AGENT: string = `Microsoft-BotFramework/3.1 Botkit/${ pjson.version } ` +
21 `(Node.js,Version=${ NODE_VERSION }; ${ TYPE } ${ RELEASE }; ${ ARCHITECTURE })`;
22
23/**
24 * 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.
25 * * Changes userAgent to reflect Botkit instead of BotBuilder
26 * * Adds getChannels() (MS Teams)
27 * * Adds middleware for adjusting location of tenant id field (MS Teams)
28 */
29export class BotkitBotFrameworkAdapter extends BotFrameworkAdapter {
30 public botkit_worker = TeamsBotWorker;
31
32 /**
33 * Allows for mocking of the connector client in unit tests.
34 * Overridden by Botkit in order to change userAgent.
35 * @ignore
36 * @param serviceUrl Clients service url.
37 */
38 public createConnectorClient(serviceUrl: string): ConnectorClient {
39 const client: ConnectorClient = new ConnectorClient(this.credentials, { baseUri: serviceUrl, userAgent: USER_AGENT });
40 return client;
41 }
42
43 /**
44 * Allows for mocking of the OAuth API Client in unit tests.
45 * Overridden by Botkit in order to change userAgent.
46 * @ignore
47 * @param serviceUrl Clients service url.
48 */
49 protected createTokenApiClient(serviceUrl: string): TokenApiClient {
50 const client = new TokenApiClient(this.credentials, { baseUri: serviceUrl, userAgent: USER_AGENT });
51 return client;
52 }
53
54 /**
55 * Get the list of channels in a MS Teams team.
56 * 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.
57 * @param context A TurnContext object representing a message or event from a user in Teams
58 * @returns an array of channels in the format [{name: string, id: string}]
59 */
60 public async getChannels(context: TurnContext): Promise<ChannelInfo[]> {
61 if (context.activity.channelData && context.activity.channelData.team) {
62 const channels = await TeamsInfo.getTeamChannels(context);
63 return channels ? channels.map((c) => { if (!c.name) { c.name = 'General'; } return c; }) : [];
64 } else {
65 console.error('getChannels cannot be called from unknown team');
66 return [];
67 }
68 }
69}