@microsoft/teamsfx 0.6.2-alpha.444739e17.0 → 0.6.2-alpha.52f41e8aa.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,9 +3,10 @@ import { ConfidentialClientApplication } from '@azure/msal-node';
3
3
  import { createHash } from 'crypto';
4
4
  import { Client } from '@microsoft/microsoft-graph-client';
5
5
  import { ManagedIdentityCredential } from '@azure/identity';
6
- import { ActivityTypes, Channels, TeamsInfo, CardFactory, ActionTypes, MessageFactory, StatusCodes, verifyStateOperationName, tokenExchangeOperationName, TurnContext } from 'botbuilder';
6
+ import { ActivityTypes, Channels, TeamsInfo, CardFactory, ActionTypes, MessageFactory, StatusCodes, verifyStateOperationName, tokenExchangeOperationName, TurnContext, BotFrameworkAdapter } from 'botbuilder';
7
7
  import { Dialog } from 'botbuilder-dialogs';
8
8
  import { v4 } from 'uuid';
9
+ import axios from 'axios';
9
10
  import * as path from 'path';
10
11
  import * as fs from 'fs';
11
12
 
@@ -1387,6 +1388,69 @@ class TeamsBotSsoPrompt extends Dialog {
1387
1388
  }
1388
1389
  }
1389
1390
 
1391
+ // Copyright (c) Microsoft Corporation.
1392
+ /**
1393
+ * Initializes new Axios instance with specific auth provider
1394
+ *
1395
+ * @param apiEndpoint - Base url of the API
1396
+ * @param authProvider - Auth provider that injects authentication info to each request
1397
+ * @returns axios instance configured with specfic auth provider
1398
+ *
1399
+ * @example
1400
+ * ```typescript
1401
+ * const client = createApiClient("https://my-api-endpoint-base-url", new BasicAuthProvider("xxx","xxx"));
1402
+ * ```
1403
+ *
1404
+ * @beta
1405
+ */
1406
+ function createApiClient(apiEndpoint, authProvider) {
1407
+ // Add a request interceptor
1408
+ const instance = axios.create({
1409
+ baseURL: apiEndpoint,
1410
+ });
1411
+ instance.interceptors.request.use(async function (config) {
1412
+ return await authProvider.AddAuthenticationInfo(config);
1413
+ });
1414
+ return instance;
1415
+ }
1416
+
1417
+ // Copyright (c) Microsoft Corporation.
1418
+ // Licensed under the MIT license.
1419
+ /**
1420
+ * Provider that handles Bearer Token authentication
1421
+ *
1422
+ * @beta
1423
+ */
1424
+ class BearerTokenAuthProvider {
1425
+ /**
1426
+ * @param getToken Function that returns the content of bearer token used in http request
1427
+ *
1428
+ * @beta
1429
+ */
1430
+ constructor(getToken) {
1431
+ this.getToken = getToken;
1432
+ }
1433
+ /**
1434
+ * Adds authentication info to http requests
1435
+ *
1436
+ * @param config - Contains all the request information and can be updated to include extra authentication info.
1437
+ * Refer https://axios-http.com/docs/req_config for detailed document.
1438
+ *
1439
+ * @beta
1440
+ */
1441
+ async AddAuthenticationInfo(config) {
1442
+ const token = await this.getToken();
1443
+ if (!config.headers) {
1444
+ config.headers = {};
1445
+ }
1446
+ if (config.headers["Authorization"]) {
1447
+ throw new Error("Authorization header already exists!");
1448
+ }
1449
+ config.headers["Authorization"] = `Bearer ${token}`;
1450
+ return config;
1451
+ }
1452
+ }
1453
+
1390
1454
  // Copyright (c) Microsoft Corporation.
1391
1455
  /**
1392
1456
  * A class providing credential and configuration.
@@ -1580,8 +1644,7 @@ var ActivityType;
1580
1644
  ActivityType[ActivityType["CurrentBotUninstalled"] = 2] = "CurrentBotUninstalled";
1581
1645
  ActivityType[ActivityType["TeamDeleted"] = 3] = "TeamDeleted";
1582
1646
  ActivityType[ActivityType["TeamRestored"] = 4] = "TeamRestored";
1583
- ActivityType[ActivityType["CommandReceived"] = 5] = "CommandReceived";
1584
- ActivityType[ActivityType["Unknown"] = 6] = "Unknown";
1647
+ ActivityType[ActivityType["Unknown"] = 5] = "Unknown";
1585
1648
  })(ActivityType || (ActivityType = {}));
1586
1649
  /**
1587
1650
  * @internal
@@ -1651,47 +1714,55 @@ class CommandResponseMiddleware {
1651
1714
  }
1652
1715
  async onTurn(context, next) {
1653
1716
  const type = this.classifyActivity(context.activity);
1654
- let handlers = [];
1655
1717
  switch (type) {
1656
- case ActivityType.CommandReceived:
1718
+ case ActivityType.CurrentBotMessaged:
1657
1719
  // Invoke corresponding command handler for the command response
1658
1720
  const commandText = this.getActivityText(context.activity);
1659
- handlers = this.filterCommandHandler(commandText, this.commandHandlers);
1660
- if (handlers.length > 0) {
1661
- const response = await handlers[0].handleCommandReceived(context, commandText);
1662
- await context.sendActivity(response);
1721
+ const message = {
1722
+ text: commandText,
1723
+ };
1724
+ for (const handler of this.commandHandlers) {
1725
+ const matchResult = this.shouldTrigger(handler.triggerPatterns, commandText);
1726
+ // It is important to note that the command bot will stop processing handlers
1727
+ // when the first command handler is matched.
1728
+ if (!!matchResult) {
1729
+ message.matches = Array.isArray(matchResult) ? matchResult : void 0;
1730
+ const response = await handler.handleCommandReceived(context, message);
1731
+ await context.sendActivity(response);
1732
+ break;
1733
+ }
1663
1734
  }
1664
1735
  break;
1665
1736
  }
1666
1737
  await next();
1667
1738
  }
1668
1739
  classifyActivity(activity) {
1669
- if (this.isCommandReceived(activity)) {
1670
- return ActivityType.CommandReceived;
1740
+ if (activity.type === ActivityTypes.Message) {
1741
+ return ActivityType.CurrentBotMessaged;
1671
1742
  }
1672
1743
  return ActivityType.Unknown;
1673
1744
  }
1674
- isCommandReceived(activity) {
1675
- if (this.commandHandlers) {
1676
- const commandText = this.getActivityText(activity);
1677
- const handlers = this.filterCommandHandler(commandText, this.commandHandlers);
1678
- return handlers.length > 0;
1679
- }
1680
- else {
1681
- return false;
1682
- }
1683
- }
1684
- filterCommandHandler(commandText, commandHandlers) {
1685
- const handlers = commandHandlers.filter((handler) => {
1686
- var _a;
1687
- if (typeof handler.commandNameOrPattern === "string") {
1688
- return handler.commandNameOrPattern.toLocaleLowerCase() === commandText;
1745
+ matchPattern(pattern, text) {
1746
+ if (text) {
1747
+ if (typeof pattern === "string") {
1748
+ const regExp = new RegExp(pattern, "i");
1749
+ return regExp.test(text);
1689
1750
  }
1690
- else {
1691
- return (_a = handler.commandNameOrPattern) === null || _a === void 0 ? void 0 : _a.test(commandText);
1751
+ if (pattern instanceof RegExp) {
1752
+ const matches = text.match(pattern);
1753
+ return matches !== null && matches !== void 0 ? matches : false;
1692
1754
  }
1693
- });
1694
- return handlers;
1755
+ }
1756
+ return false;
1757
+ }
1758
+ shouldTrigger(patterns, text) {
1759
+ const expressions = Array.isArray(patterns) ? patterns : [patterns];
1760
+ for (const ex of expressions) {
1761
+ const arg = this.matchPattern(ex, text);
1762
+ if (arg)
1763
+ return arg;
1764
+ }
1765
+ return false;
1695
1766
  }
1696
1767
  getActivityText(activity) {
1697
1768
  let text = activity.text;
@@ -1706,6 +1777,54 @@ class CommandResponseMiddleware {
1706
1777
  }
1707
1778
  }
1708
1779
 
1780
+ // Copyright (c) Microsoft Corporation.
1781
+ /**
1782
+ * A command bot for receiving commands and sending responses in Teams.
1783
+ *
1784
+ * @remarks
1785
+ * Ensure each command should ONLY be registered with the command once, otherwise it'll cause unexpected behavior if you register the same command more than once.
1786
+ *
1787
+ * @beta
1788
+ */
1789
+ class CommandBot {
1790
+ /**
1791
+ * Creates a new instance of the `CommandBot`.
1792
+ *
1793
+ * @param adapter The bound `BotFrameworkAdapter`.
1794
+ * @param options - initialize options
1795
+ *
1796
+ * @beta
1797
+ */
1798
+ constructor(adapter, options) {
1799
+ this.middleware = new CommandResponseMiddleware(options === null || options === void 0 ? void 0 : options.commands);
1800
+ this.adapter = adapter.use(this.middleware);
1801
+ }
1802
+ /**
1803
+ * Registers a command into the command bot.
1804
+ *
1805
+ * @param command The command to registered.
1806
+ *
1807
+ * @beta
1808
+ */
1809
+ registerCommand(command) {
1810
+ if (command) {
1811
+ this.middleware.commandHandlers.push(command);
1812
+ }
1813
+ }
1814
+ /**
1815
+ * Registers commands into the command bot.
1816
+ *
1817
+ * @param commands The command to registered.
1818
+ *
1819
+ * @beta
1820
+ */
1821
+ registerCommands(commands) {
1822
+ if (commands) {
1823
+ this.middleware.commandHandlers.push(...commands);
1824
+ }
1825
+ }
1826
+ }
1827
+
1709
1828
  // Copyright (c) Microsoft Corporation.
1710
1829
  /**
1711
1830
  * @internal
@@ -2126,40 +2245,22 @@ class TeamsBotInstallation {
2126
2245
  * @beta
2127
2246
  */
2128
2247
  async members() {
2129
- let teamsMembers = [];
2248
+ const members = [];
2130
2249
  await this.adapter.continueConversation(this.conversationReference, async (context) => {
2131
- teamsMembers = await TeamsInfo.getMembers(context);
2250
+ let continuationToken;
2251
+ do {
2252
+ const pagedMembers = await TeamsInfo.getPagedMembers(context, undefined, continuationToken);
2253
+ continuationToken = pagedMembers.continuationToken;
2254
+ for (const member of pagedMembers.members) {
2255
+ members.push(new Member(this, member));
2256
+ }
2257
+ } while (continuationToken !== undefined);
2132
2258
  });
2133
- const members = [];
2134
- for (const member of teamsMembers) {
2135
- members.push(new Member(this, member));
2136
- }
2137
2259
  return members;
2138
2260
  }
2139
2261
  }
2140
2262
  /**
2141
- * Provide static utilities for bot conversation, including
2142
- * - send notification to varies targets (e.g., member, channel, incoming wehbook)
2143
- * - handle command and response.
2144
- *
2145
- * @example
2146
- * Here's an example on how to send notification via Teams Bot.
2147
- * ```typescript
2148
- * // initialize (it's recommended to be called before handling any bot message)
2149
- * const notificationBot = new NotificationBot(adapter);
2150
- *
2151
- * // get all bot installations and send message
2152
- * for (const target of await notificationBot.installations()) {
2153
- * await target.sendMessage("Hello Notification");
2154
- * }
2155
- *
2156
- * // alternative - send message to all members
2157
- * for (const target of await notificationBot.installations()) {
2158
- * for (const member of await target.members()) {
2159
- * await member.sendMessage("Hello Notification");
2160
- * }
2161
- * }
2162
- * ```
2263
+ * Provide utilities to send notification to varies targets (e.g., member, channel, incoming wehbook).
2163
2264
  *
2164
2265
  * @beta
2165
2266
  */
@@ -2226,59 +2327,83 @@ class NotificationBot {
2226
2327
 
2227
2328
  // Copyright (c) Microsoft Corporation.
2228
2329
  /**
2229
- * A command bot for receiving commands and sending responses in Teams.
2230
- *
2231
- * @remarks
2232
- * Ensure each command should ONLY be registered with the command once, otherwise it'll cause unexpected behavior if you register the same command more than once.
2330
+ * Provide utilities for bot conversation, including:
2331
+ * - handle command and response.
2332
+ * - send notification to varies targets (e.g., member, channel, incoming wehbook).
2233
2333
  *
2234
2334
  * @example
2235
- * You can register your commands through the constructor of the {@link CommandBot}, or use the `registerCommand` and `registerCommands` API to add commands after creating the `CommandBot` instance.
2335
+ * For command and response, you can register your commands through the constructor, or use the `registerCommand` and `registerCommands` API to add commands later.
2236
2336
  *
2237
2337
  * ```typescript
2238
2338
  * // register through constructor
2239
- * const commandBot = new CommandBot(adapter, [ new HelloWorldCommandHandler() ]);
2339
+ * const conversationBot = new ConversationBot({
2340
+ * command: {
2341
+ * enabled: true,
2342
+ * options: {
2343
+ * commands: [ new HelloWorldCommandHandler() ],
2344
+ * },
2345
+ * },
2346
+ * });
2240
2347
  *
2241
2348
  * // register through `register*` API
2242
- * commandBot.registerCommand(new HelpCommandHandler());
2349
+ * conversationBot.command.registerCommand(new HelpCommandHandler());
2243
2350
  * ```
2244
2351
  *
2352
+ * For notification, you can enable notification at initialization, then send notificaations at any time.
2353
+ *
2354
+ * ```typescript
2355
+ * // enable through constructor
2356
+ * const conversationBot = new ConversationBot({
2357
+ * notification: {
2358
+ * enabled: true,
2359
+ * },
2360
+ * });
2361
+ *
2362
+ * // get all bot installations and send message
2363
+ * for (const target of await conversationBot.notification.installations()) {
2364
+ * await target.sendMessage("Hello Notification");
2365
+ * }
2366
+ *
2367
+ * // alternative - send message to all members
2368
+ * for (const target of await conversationBot.notification.installations()) {
2369
+ * for (const member of await target.members()) {
2370
+ * await member.sendMessage("Hello Notification");
2371
+ * }
2372
+ * }
2373
+ * ```
2374
+ *
2375
+ * @remarks
2376
+ * Set `adapter` in {@link ConversationOptions} to use your own bot adapter.
2377
+ *
2378
+ * For command and response, ensure each command should ONLY be registered with the command once, otherwise it'll cause unexpected behavior if you register the same command more than once.
2379
+ *
2380
+ * For notification, set `notification.options.storage` in {@link ConversationOptions} to use your own storage implementation.
2381
+ *
2245
2382
  * @beta
2246
2383
  */
2247
- class CommandBot {
2384
+ class ConversationBot {
2248
2385
  /**
2249
- * Creates a new instance of the `CommandBot`.
2250
- *
2251
- * @param adapter The bound `BotFrameworkAdapter`.
2252
- * @param commands The commands to registered with the command bot. Each command should implement the interface {@link TeamsFxBotCommandHandler} so that it can be correctly handled by this command bot.
2253
- *
2254
- * @beta
2255
- */
2256
- constructor(adapter, commands) {
2257
- this.middleware = new CommandResponseMiddleware(commands);
2258
- this.adapter = adapter.use(this.middleware);
2259
- }
2260
- /**
2261
- * Registers a command into the command bot.
2386
+ * Creates new instance of the `ConversationBot`.
2262
2387
  *
2263
- * @param command The command to registered.
2388
+ * @param options - initialize options
2264
2389
  *
2265
2390
  * @beta
2266
2391
  */
2267
- registerCommand(command) {
2268
- if (command) {
2269
- this.middleware.commandHandlers.push(command);
2392
+ constructor(options) {
2393
+ if (options.adapter) {
2394
+ this.adapter = options.adapter;
2270
2395
  }
2271
- }
2272
- /**
2273
- * Registers commands into the command bot.
2274
- *
2275
- * @param commands The command to registered.
2276
- *
2277
- * @beta
2278
- */
2279
- registerCommands(commands) {
2280
- if (commands) {
2281
- this.middleware.commandHandlers.push(...commands);
2396
+ else {
2397
+ this.adapter = new BotFrameworkAdapter({
2398
+ appId: process.env.BOT_ID,
2399
+ appPassword: process.env.BOT_PASSWORD,
2400
+ });
2401
+ }
2402
+ if (options.command.enabled) {
2403
+ this.command = new CommandBot(this.adapter, options.command.options);
2404
+ }
2405
+ if (options.notification.enabled) {
2406
+ this.notification = new NotificationBot(this.adapter, options.notification.options);
2282
2407
  }
2283
2408
  }
2284
2409
  }
@@ -2437,5 +2562,5 @@ class MessageBuilder {
2437
2562
  }
2438
2563
  }
2439
2564
 
2440
- export { AppCredential, Channel, CommandBot, ErrorCode, ErrorWithCode, IdentityType, LogLevel, Member, MessageBuilder, MsGraphAuthProvider, NotificationBot, OnBehalfOfUserCredential, TeamsBotInstallation, TeamsBotSsoPrompt, TeamsFx, TeamsUserCredential, createMicrosoftGraphClient, getLogLevel, getTediousConnectionConfig, sendAdaptiveCard, sendMessage, setLogFunction, setLogLevel, setLogger };
2565
+ export { AppCredential, BearerTokenAuthProvider, Channel, CommandBot, ConversationBot, ErrorCode, ErrorWithCode, IdentityType, LogLevel, Member, MessageBuilder, MsGraphAuthProvider, NotificationBot, OnBehalfOfUserCredential, TeamsBotInstallation, TeamsBotSsoPrompt, TeamsFx, TeamsUserCredential, createApiClient, createMicrosoftGraphClient, getLogLevel, getTediousConnectionConfig, sendAdaptiveCard, sendMessage, setLogFunction, setLogLevel, setLogger };
2441
2566
  //# sourceMappingURL=index.esm2017.mjs.map