@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.
@@ -11,6 +11,7 @@ var identity = require('@azure/identity');
11
11
  var botbuilder = require('botbuilder');
12
12
  var botbuilderDialogs = require('botbuilder-dialogs');
13
13
  var uuid = require('uuid');
14
+ var axios = require('axios');
14
15
  var path = require('path');
15
16
  var fs = require('fs');
16
17
 
@@ -35,6 +36,7 @@ function _interopNamespace(e) {
35
36
  }
36
37
 
37
38
  var jwt_decode__default = /*#__PURE__*/_interopDefaultLegacy(jwt_decode);
39
+ var axios__default = /*#__PURE__*/_interopDefaultLegacy(axios);
38
40
  var path__namespace = /*#__PURE__*/_interopNamespace(path);
39
41
  var fs__namespace = /*#__PURE__*/_interopNamespace(fs);
40
42
 
@@ -1438,6 +1440,72 @@ class TeamsBotSsoPrompt extends botbuilderDialogs.Dialog {
1438
1440
  }
1439
1441
  }
1440
1442
 
1443
+ // Copyright (c) Microsoft Corporation.
1444
+ /**
1445
+ * Initializes new Axios instance with specific auth provider
1446
+ *
1447
+ * @param apiEndpoint - Base url of the API
1448
+ * @param authProvider - Auth provider that injects authentication info to each request
1449
+ * @returns axios instance configured with specfic auth provider
1450
+ *
1451
+ * @example
1452
+ * ```typescript
1453
+ * const client = createApiClient("https://my-api-endpoint-base-url", new BasicAuthProvider("xxx","xxx"));
1454
+ * ```
1455
+ *
1456
+ * @beta
1457
+ */
1458
+ function createApiClient(apiEndpoint, authProvider) {
1459
+ // Add a request interceptor
1460
+ const instance = axios__default["default"].create({
1461
+ baseURL: apiEndpoint,
1462
+ });
1463
+ instance.interceptors.request.use(function (config) {
1464
+ return tslib.__awaiter(this, void 0, void 0, function* () {
1465
+ return yield authProvider.AddAuthenticationInfo(config);
1466
+ });
1467
+ });
1468
+ return instance;
1469
+ }
1470
+
1471
+ // Copyright (c) Microsoft Corporation.
1472
+ /**
1473
+ * Provider that handles Bearer Token authentication
1474
+ *
1475
+ * @beta
1476
+ */
1477
+ class BearerTokenAuthProvider {
1478
+ /**
1479
+ * @param getToken Function that returns the content of bearer token used in http request
1480
+ *
1481
+ * @beta
1482
+ */
1483
+ constructor(getToken) {
1484
+ this.getToken = getToken;
1485
+ }
1486
+ /**
1487
+ * Adds authentication info to http requests
1488
+ *
1489
+ * @param config - Contains all the request information and can be updated to include extra authentication info.
1490
+ * Refer https://axios-http.com/docs/req_config for detailed document.
1491
+ *
1492
+ * @beta
1493
+ */
1494
+ AddAuthenticationInfo(config) {
1495
+ return tslib.__awaiter(this, void 0, void 0, function* () {
1496
+ const token = yield this.getToken();
1497
+ if (!config.headers) {
1498
+ config.headers = {};
1499
+ }
1500
+ if (config.headers["Authorization"]) {
1501
+ throw new Error("Authorization header already exists!");
1502
+ }
1503
+ config.headers["Authorization"] = `Bearer ${token}`;
1504
+ return config;
1505
+ });
1506
+ }
1507
+ }
1508
+
1441
1509
  // Copyright (c) Microsoft Corporation.
1442
1510
  /**
1443
1511
  * A class providing credential and configuration.
@@ -1635,8 +1703,7 @@ var ActivityType;
1635
1703
  ActivityType[ActivityType["CurrentBotUninstalled"] = 2] = "CurrentBotUninstalled";
1636
1704
  ActivityType[ActivityType["TeamDeleted"] = 3] = "TeamDeleted";
1637
1705
  ActivityType[ActivityType["TeamRestored"] = 4] = "TeamRestored";
1638
- ActivityType[ActivityType["CommandReceived"] = 5] = "CommandReceived";
1639
- ActivityType[ActivityType["Unknown"] = 6] = "Unknown";
1706
+ ActivityType[ActivityType["Unknown"] = 5] = "Unknown";
1640
1707
  })(ActivityType || (ActivityType = {}));
1641
1708
  /**
1642
1709
  * @internal
@@ -1709,15 +1776,23 @@ class CommandResponseMiddleware {
1709
1776
  onTurn(context, next) {
1710
1777
  return tslib.__awaiter(this, void 0, void 0, function* () {
1711
1778
  const type = this.classifyActivity(context.activity);
1712
- let handlers = [];
1713
1779
  switch (type) {
1714
- case ActivityType.CommandReceived:
1780
+ case ActivityType.CurrentBotMessaged:
1715
1781
  // Invoke corresponding command handler for the command response
1716
1782
  const commandText = this.getActivityText(context.activity);
1717
- handlers = this.filterCommandHandler(commandText, this.commandHandlers);
1718
- if (handlers.length > 0) {
1719
- const response = yield handlers[0].handleCommandReceived(context, commandText);
1720
- yield context.sendActivity(response);
1783
+ const message = {
1784
+ text: commandText,
1785
+ };
1786
+ for (const handler of this.commandHandlers) {
1787
+ const matchResult = this.shouldTrigger(handler.triggerPatterns, commandText);
1788
+ // It is important to note that the command bot will stop processing handlers
1789
+ // when the first command handler is matched.
1790
+ if (!!matchResult) {
1791
+ message.matches = Array.isArray(matchResult) ? matchResult : void 0;
1792
+ const response = yield handler.handleCommandReceived(context, message);
1793
+ yield context.sendActivity(response);
1794
+ break;
1795
+ }
1721
1796
  }
1722
1797
  break;
1723
1798
  }
@@ -1725,32 +1800,32 @@ class CommandResponseMiddleware {
1725
1800
  });
1726
1801
  }
1727
1802
  classifyActivity(activity) {
1728
- if (this.isCommandReceived(activity)) {
1729
- return ActivityType.CommandReceived;
1803
+ if (activity.type === botbuilder.ActivityTypes.Message) {
1804
+ return ActivityType.CurrentBotMessaged;
1730
1805
  }
1731
1806
  return ActivityType.Unknown;
1732
1807
  }
1733
- isCommandReceived(activity) {
1734
- if (this.commandHandlers) {
1735
- const commandText = this.getActivityText(activity);
1736
- const handlers = this.filterCommandHandler(commandText, this.commandHandlers);
1737
- return handlers.length > 0;
1738
- }
1739
- else {
1740
- return false;
1741
- }
1742
- }
1743
- filterCommandHandler(commandText, commandHandlers) {
1744
- const handlers = commandHandlers.filter((handler) => {
1745
- var _a;
1746
- if (typeof handler.commandNameOrPattern === "string") {
1747
- return handler.commandNameOrPattern.toLocaleLowerCase() === commandText;
1808
+ matchPattern(pattern, text) {
1809
+ if (text) {
1810
+ if (typeof pattern === "string") {
1811
+ const regExp = new RegExp(pattern, "i");
1812
+ return regExp.test(text);
1748
1813
  }
1749
- else {
1750
- return (_a = handler.commandNameOrPattern) === null || _a === void 0 ? void 0 : _a.test(commandText);
1814
+ if (pattern instanceof RegExp) {
1815
+ const matches = text.match(pattern);
1816
+ return matches !== null && matches !== void 0 ? matches : false;
1751
1817
  }
1752
- });
1753
- return handlers;
1818
+ }
1819
+ return false;
1820
+ }
1821
+ shouldTrigger(patterns, text) {
1822
+ const expressions = Array.isArray(patterns) ? patterns : [patterns];
1823
+ for (const ex of expressions) {
1824
+ const arg = this.matchPattern(ex, text);
1825
+ if (arg)
1826
+ return arg;
1827
+ }
1828
+ return false;
1754
1829
  }
1755
1830
  getActivityText(activity) {
1756
1831
  let text = activity.text;
@@ -1765,6 +1840,54 @@ class CommandResponseMiddleware {
1765
1840
  }
1766
1841
  }
1767
1842
 
1843
+ // Copyright (c) Microsoft Corporation.
1844
+ /**
1845
+ * A command bot for receiving commands and sending responses in Teams.
1846
+ *
1847
+ * @remarks
1848
+ * 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.
1849
+ *
1850
+ * @beta
1851
+ */
1852
+ class CommandBot {
1853
+ /**
1854
+ * Creates a new instance of the `CommandBot`.
1855
+ *
1856
+ * @param adapter The bound `BotFrameworkAdapter`.
1857
+ * @param options - initialize options
1858
+ *
1859
+ * @beta
1860
+ */
1861
+ constructor(adapter, options) {
1862
+ this.middleware = new CommandResponseMiddleware(options === null || options === void 0 ? void 0 : options.commands);
1863
+ this.adapter = adapter.use(this.middleware);
1864
+ }
1865
+ /**
1866
+ * Registers a command into the command bot.
1867
+ *
1868
+ * @param command The command to registered.
1869
+ *
1870
+ * @beta
1871
+ */
1872
+ registerCommand(command) {
1873
+ if (command) {
1874
+ this.middleware.commandHandlers.push(command);
1875
+ }
1876
+ }
1877
+ /**
1878
+ * Registers commands into the command bot.
1879
+ *
1880
+ * @param commands The command to registered.
1881
+ *
1882
+ * @beta
1883
+ */
1884
+ registerCommands(commands) {
1885
+ if (commands) {
1886
+ this.middleware.commandHandlers.push(...commands);
1887
+ }
1888
+ }
1889
+ }
1890
+
1768
1891
  // Copyright (c) Microsoft Corporation.
1769
1892
  /**
1770
1893
  * @internal
@@ -2208,41 +2331,23 @@ class TeamsBotInstallation {
2208
2331
  */
2209
2332
  members() {
2210
2333
  return tslib.__awaiter(this, void 0, void 0, function* () {
2211
- let teamsMembers = [];
2334
+ const members = [];
2212
2335
  yield this.adapter.continueConversation(this.conversationReference, (context) => tslib.__awaiter(this, void 0, void 0, function* () {
2213
- teamsMembers = yield botbuilder.TeamsInfo.getMembers(context);
2336
+ let continuationToken;
2337
+ do {
2338
+ const pagedMembers = yield botbuilder.TeamsInfo.getPagedMembers(context, undefined, continuationToken);
2339
+ continuationToken = pagedMembers.continuationToken;
2340
+ for (const member of pagedMembers.members) {
2341
+ members.push(new Member(this, member));
2342
+ }
2343
+ } while (continuationToken !== undefined);
2214
2344
  }));
2215
- const members = [];
2216
- for (const member of teamsMembers) {
2217
- members.push(new Member(this, member));
2218
- }
2219
2345
  return members;
2220
2346
  });
2221
2347
  }
2222
2348
  }
2223
2349
  /**
2224
- * Provide static utilities for bot conversation, including
2225
- * - send notification to varies targets (e.g., member, channel, incoming wehbook)
2226
- * - handle command and response.
2227
- *
2228
- * @example
2229
- * Here's an example on how to send notification via Teams Bot.
2230
- * ```typescript
2231
- * // initialize (it's recommended to be called before handling any bot message)
2232
- * const notificationBot = new NotificationBot(adapter);
2233
- *
2234
- * // get all bot installations and send message
2235
- * for (const target of await notificationBot.installations()) {
2236
- * await target.sendMessage("Hello Notification");
2237
- * }
2238
- *
2239
- * // alternative - send message to all members
2240
- * for (const target of await notificationBot.installations()) {
2241
- * for (const member of await target.members()) {
2242
- * await member.sendMessage("Hello Notification");
2243
- * }
2244
- * }
2245
- * ```
2350
+ * Provide utilities to send notification to varies targets (e.g., member, channel, incoming wehbook).
2246
2351
  *
2247
2352
  * @beta
2248
2353
  */
@@ -2311,59 +2416,83 @@ class NotificationBot {
2311
2416
 
2312
2417
  // Copyright (c) Microsoft Corporation.
2313
2418
  /**
2314
- * A command bot for receiving commands and sending responses in Teams.
2315
- *
2316
- * @remarks
2317
- * 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.
2419
+ * Provide utilities for bot conversation, including:
2420
+ * - handle command and response.
2421
+ * - send notification to varies targets (e.g., member, channel, incoming wehbook).
2318
2422
  *
2319
2423
  * @example
2320
- * 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.
2424
+ * For command and response, you can register your commands through the constructor, or use the `registerCommand` and `registerCommands` API to add commands later.
2321
2425
  *
2322
2426
  * ```typescript
2323
2427
  * // register through constructor
2324
- * const commandBot = new CommandBot(adapter, [ new HelloWorldCommandHandler() ]);
2428
+ * const conversationBot = new ConversationBot({
2429
+ * command: {
2430
+ * enabled: true,
2431
+ * options: {
2432
+ * commands: [ new HelloWorldCommandHandler() ],
2433
+ * },
2434
+ * },
2435
+ * });
2325
2436
  *
2326
2437
  * // register through `register*` API
2327
- * commandBot.registerCommand(new HelpCommandHandler());
2438
+ * conversationBot.command.registerCommand(new HelpCommandHandler());
2328
2439
  * ```
2329
2440
  *
2441
+ * For notification, you can enable notification at initialization, then send notificaations at any time.
2442
+ *
2443
+ * ```typescript
2444
+ * // enable through constructor
2445
+ * const conversationBot = new ConversationBot({
2446
+ * notification: {
2447
+ * enabled: true,
2448
+ * },
2449
+ * });
2450
+ *
2451
+ * // get all bot installations and send message
2452
+ * for (const target of await conversationBot.notification.installations()) {
2453
+ * await target.sendMessage("Hello Notification");
2454
+ * }
2455
+ *
2456
+ * // alternative - send message to all members
2457
+ * for (const target of await conversationBot.notification.installations()) {
2458
+ * for (const member of await target.members()) {
2459
+ * await member.sendMessage("Hello Notification");
2460
+ * }
2461
+ * }
2462
+ * ```
2463
+ *
2464
+ * @remarks
2465
+ * Set `adapter` in {@link ConversationOptions} to use your own bot adapter.
2466
+ *
2467
+ * 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.
2468
+ *
2469
+ * For notification, set `notification.options.storage` in {@link ConversationOptions} to use your own storage implementation.
2470
+ *
2330
2471
  * @beta
2331
2472
  */
2332
- class CommandBot {
2473
+ class ConversationBot {
2333
2474
  /**
2334
- * Creates a new instance of the `CommandBot`.
2335
- *
2336
- * @param adapter The bound `BotFrameworkAdapter`.
2337
- * @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.
2338
- *
2339
- * @beta
2340
- */
2341
- constructor(adapter, commands) {
2342
- this.middleware = new CommandResponseMiddleware(commands);
2343
- this.adapter = adapter.use(this.middleware);
2344
- }
2345
- /**
2346
- * Registers a command into the command bot.
2475
+ * Creates new instance of the `ConversationBot`.
2347
2476
  *
2348
- * @param command The command to registered.
2477
+ * @param options - initialize options
2349
2478
  *
2350
2479
  * @beta
2351
2480
  */
2352
- registerCommand(command) {
2353
- if (command) {
2354
- this.middleware.commandHandlers.push(command);
2481
+ constructor(options) {
2482
+ if (options.adapter) {
2483
+ this.adapter = options.adapter;
2355
2484
  }
2356
- }
2357
- /**
2358
- * Registers commands into the command bot.
2359
- *
2360
- * @param commands The command to registered.
2361
- *
2362
- * @beta
2363
- */
2364
- registerCommands(commands) {
2365
- if (commands) {
2366
- this.middleware.commandHandlers.push(...commands);
2485
+ else {
2486
+ this.adapter = new botbuilder.BotFrameworkAdapter({
2487
+ appId: process.env.BOT_ID,
2488
+ appPassword: process.env.BOT_PASSWORD,
2489
+ });
2490
+ }
2491
+ if (options.command.enabled) {
2492
+ this.command = new CommandBot(this.adapter, options.command.options);
2493
+ }
2494
+ if (options.notification.enabled) {
2495
+ this.notification = new NotificationBot(this.adapter, options.notification.options);
2367
2496
  }
2368
2497
  }
2369
2498
  }
@@ -2523,8 +2652,10 @@ class MessageBuilder {
2523
2652
  }
2524
2653
 
2525
2654
  exports.AppCredential = AppCredential;
2655
+ exports.BearerTokenAuthProvider = BearerTokenAuthProvider;
2526
2656
  exports.Channel = Channel;
2527
2657
  exports.CommandBot = CommandBot;
2658
+ exports.ConversationBot = ConversationBot;
2528
2659
  exports.ErrorWithCode = ErrorWithCode;
2529
2660
  exports.Member = Member;
2530
2661
  exports.MessageBuilder = MessageBuilder;
@@ -2535,6 +2666,7 @@ exports.TeamsBotInstallation = TeamsBotInstallation;
2535
2666
  exports.TeamsBotSsoPrompt = TeamsBotSsoPrompt;
2536
2667
  exports.TeamsFx = TeamsFx;
2537
2668
  exports.TeamsUserCredential = TeamsUserCredential;
2669
+ exports.createApiClient = createApiClient;
2538
2670
  exports.createMicrosoftGraphClient = createMicrosoftGraphClient;
2539
2671
  exports.getLogLevel = getLogLevel;
2540
2672
  exports.getTediousConnectionConfig = getTediousConnectionConfig;