@microsoft/teamsfx 0.6.2-alpha.957c90e1c.0 → 0.6.2-alpha.b7cedce9e.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,10 +11,32 @@ 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 path = require('path');
15
+ var fs = require('fs');
14
16
 
15
17
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
16
18
 
19
+ function _interopNamespace(e) {
20
+ if (e && e.__esModule) return e;
21
+ var n = Object.create(null);
22
+ if (e) {
23
+ Object.keys(e).forEach(function (k) {
24
+ if (k !== 'default') {
25
+ var d = Object.getOwnPropertyDescriptor(e, k);
26
+ Object.defineProperty(n, k, d.get ? d : {
27
+ enumerable: true,
28
+ get: function () { return e[k]; }
29
+ });
30
+ }
31
+ });
32
+ }
33
+ n["default"] = e;
34
+ return Object.freeze(n);
35
+ }
36
+
17
37
  var jwt_decode__default = /*#__PURE__*/_interopDefaultLegacy(jwt_decode);
38
+ var path__namespace = /*#__PURE__*/_interopNamespace(path);
39
+ var fs__namespace = /*#__PURE__*/_interopNamespace(fs);
18
40
 
19
41
  // Copyright (c) Microsoft Corporation.
20
42
  // Licensed under the MIT license.
@@ -1602,16 +1624,922 @@ class TeamsFx {
1602
1624
  }
1603
1625
  }
1604
1626
 
1627
+ // Copyright (c) Microsoft Corporation.
1628
+ /**
1629
+ * @internal
1630
+ */
1631
+ var ActivityType;
1632
+ (function (ActivityType) {
1633
+ ActivityType[ActivityType["CurrentBotInstalled"] = 0] = "CurrentBotInstalled";
1634
+ ActivityType[ActivityType["CurrentBotMessaged"] = 1] = "CurrentBotMessaged";
1635
+ ActivityType[ActivityType["CurrentBotUninstalled"] = 2] = "CurrentBotUninstalled";
1636
+ ActivityType[ActivityType["TeamDeleted"] = 3] = "TeamDeleted";
1637
+ ActivityType[ActivityType["TeamRestored"] = 4] = "TeamRestored";
1638
+ ActivityType[ActivityType["CommandReceived"] = 5] = "CommandReceived";
1639
+ ActivityType[ActivityType["Unknown"] = 6] = "Unknown";
1640
+ })(ActivityType || (ActivityType = {}));
1641
+ /**
1642
+ * @internal
1643
+ */
1644
+ class NotificationMiddleware {
1645
+ constructor(options) {
1646
+ this.conversationReferenceStore = options.conversationReferenceStore;
1647
+ }
1648
+ onTurn(context, next) {
1649
+ return tslib.__awaiter(this, void 0, void 0, function* () {
1650
+ const type = this.classifyActivity(context.activity);
1651
+ switch (type) {
1652
+ case ActivityType.CurrentBotInstalled:
1653
+ case ActivityType.TeamRestored: {
1654
+ const reference = botbuilder.TurnContext.getConversationReference(context.activity);
1655
+ yield this.conversationReferenceStore.set(reference);
1656
+ break;
1657
+ }
1658
+ case ActivityType.CurrentBotMessaged: {
1659
+ const reference = botbuilder.TurnContext.getConversationReference(context.activity);
1660
+ if (!(yield this.conversationReferenceStore.check(reference))) {
1661
+ yield this.conversationReferenceStore.set(reference);
1662
+ }
1663
+ break;
1664
+ }
1665
+ case ActivityType.CurrentBotUninstalled:
1666
+ case ActivityType.TeamDeleted: {
1667
+ const reference = botbuilder.TurnContext.getConversationReference(context.activity);
1668
+ yield this.conversationReferenceStore.delete(reference);
1669
+ break;
1670
+ }
1671
+ }
1672
+ yield next();
1673
+ });
1674
+ }
1675
+ classifyActivity(activity) {
1676
+ var _a, _b;
1677
+ const activityType = activity.type;
1678
+ if (activityType === "installationUpdate") {
1679
+ const action = (_a = activity.action) === null || _a === void 0 ? void 0 : _a.toLowerCase();
1680
+ if (action === "add") {
1681
+ return ActivityType.CurrentBotInstalled;
1682
+ }
1683
+ else {
1684
+ return ActivityType.CurrentBotUninstalled;
1685
+ }
1686
+ }
1687
+ else if (activityType === "message") {
1688
+ return ActivityType.CurrentBotMessaged;
1689
+ }
1690
+ else if (activityType === "conversationUpdate") {
1691
+ const eventType = (_b = activity.channelData) === null || _b === void 0 ? void 0 : _b.eventType;
1692
+ if (eventType === "teamDeleted") {
1693
+ return ActivityType.TeamDeleted;
1694
+ }
1695
+ else if (eventType === "teamRestored") {
1696
+ return ActivityType.TeamRestored;
1697
+ }
1698
+ }
1699
+ return ActivityType.Unknown;
1700
+ }
1701
+ }
1702
+ class CommandResponseMiddleware {
1703
+ constructor(handlers) {
1704
+ this.commandHandlers = [];
1705
+ if (handlers && handlers.length > 0) {
1706
+ this.commandHandlers.push(...handlers);
1707
+ }
1708
+ }
1709
+ onTurn(context, next) {
1710
+ return tslib.__awaiter(this, void 0, void 0, function* () {
1711
+ const type = this.classifyActivity(context.activity);
1712
+ let handlers = [];
1713
+ switch (type) {
1714
+ case ActivityType.CommandReceived:
1715
+ // Invoke corresponding command handler for the command response
1716
+ 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);
1721
+ }
1722
+ break;
1723
+ }
1724
+ yield next();
1725
+ });
1726
+ }
1727
+ classifyActivity(activity) {
1728
+ if (this.isCommandReceived(activity)) {
1729
+ return ActivityType.CommandReceived;
1730
+ }
1731
+ return ActivityType.Unknown;
1732
+ }
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;
1748
+ }
1749
+ else {
1750
+ return (_a = handler.commandNameOrPattern) === null || _a === void 0 ? void 0 : _a.test(commandText);
1751
+ }
1752
+ });
1753
+ return handlers;
1754
+ }
1755
+ getActivityText(activity) {
1756
+ let text = activity.text;
1757
+ const removedMentionText = botbuilder.TurnContext.removeRecipientMention(activity);
1758
+ if (removedMentionText) {
1759
+ text = removedMentionText
1760
+ .toLowerCase()
1761
+ .replace(/\n|\r\n/g, "")
1762
+ .trim();
1763
+ }
1764
+ return text;
1765
+ }
1766
+ }
1767
+
1768
+ // Copyright (c) Microsoft Corporation.
1769
+ /**
1770
+ * @internal
1771
+ */
1772
+ class LocalFileStorage {
1773
+ constructor(fileDir) {
1774
+ this.localFileName = ".notification.localstore.json";
1775
+ this.filePath = path__namespace.resolve(fileDir, this.localFileName);
1776
+ }
1777
+ read(key) {
1778
+ return tslib.__awaiter(this, void 0, void 0, function* () {
1779
+ if (!(yield this.storeFileExists())) {
1780
+ return undefined;
1781
+ }
1782
+ const data = yield this.readFromFile();
1783
+ return data[key];
1784
+ });
1785
+ }
1786
+ list() {
1787
+ return tslib.__awaiter(this, void 0, void 0, function* () {
1788
+ if (!(yield this.storeFileExists())) {
1789
+ return [];
1790
+ }
1791
+ const data = yield this.readFromFile();
1792
+ return Object.entries(data).map((entry) => entry[1]);
1793
+ });
1794
+ }
1795
+ write(key, object) {
1796
+ return tslib.__awaiter(this, void 0, void 0, function* () {
1797
+ if (!(yield this.storeFileExists())) {
1798
+ yield this.writeToFile({ [key]: object });
1799
+ return;
1800
+ }
1801
+ const data = yield this.readFromFile();
1802
+ yield this.writeToFile(Object.assign(data, { [key]: object }));
1803
+ });
1804
+ }
1805
+ delete(key) {
1806
+ return tslib.__awaiter(this, void 0, void 0, function* () {
1807
+ if (yield this.storeFileExists()) {
1808
+ const data = yield this.readFromFile();
1809
+ if (data[key] !== undefined) {
1810
+ delete data[key];
1811
+ yield this.writeToFile(data);
1812
+ }
1813
+ }
1814
+ });
1815
+ }
1816
+ storeFileExists() {
1817
+ return new Promise((resolve) => {
1818
+ try {
1819
+ fs__namespace.access(this.filePath, (err) => {
1820
+ if (err) {
1821
+ resolve(false);
1822
+ }
1823
+ else {
1824
+ resolve(true);
1825
+ }
1826
+ });
1827
+ }
1828
+ catch (error) {
1829
+ resolve(false);
1830
+ }
1831
+ });
1832
+ }
1833
+ readFromFile() {
1834
+ return new Promise((resolve, reject) => {
1835
+ try {
1836
+ fs__namespace.readFile(this.filePath, { encoding: "utf-8" }, (err, rawData) => {
1837
+ if (err) {
1838
+ reject(err);
1839
+ }
1840
+ else {
1841
+ resolve(JSON.parse(rawData));
1842
+ }
1843
+ });
1844
+ }
1845
+ catch (error) {
1846
+ reject(error);
1847
+ }
1848
+ });
1849
+ }
1850
+ writeToFile(data) {
1851
+ return tslib.__awaiter(this, void 0, void 0, function* () {
1852
+ return new Promise((resolve, reject) => {
1853
+ try {
1854
+ const rawData = JSON.stringify(data, undefined, 2);
1855
+ fs__namespace.writeFile(this.filePath, rawData, { encoding: "utf-8" }, (err) => {
1856
+ if (err) {
1857
+ reject(err);
1858
+ }
1859
+ else {
1860
+ resolve();
1861
+ }
1862
+ });
1863
+ }
1864
+ catch (error) {
1865
+ reject(error);
1866
+ }
1867
+ });
1868
+ });
1869
+ }
1870
+ }
1871
+ /**
1872
+ * @internal
1873
+ */
1874
+ class ConversationReferenceStore {
1875
+ constructor(storage) {
1876
+ this.storage = storage;
1877
+ }
1878
+ check(reference) {
1879
+ return tslib.__awaiter(this, void 0, void 0, function* () {
1880
+ const ref = yield this.storage.read(this.getKey(reference));
1881
+ return ref !== undefined;
1882
+ });
1883
+ }
1884
+ getAll() {
1885
+ return this.storage.list();
1886
+ }
1887
+ set(reference) {
1888
+ return this.storage.write(this.getKey(reference), reference);
1889
+ }
1890
+ delete(reference) {
1891
+ return this.storage.delete(this.getKey(reference));
1892
+ }
1893
+ getKey(reference) {
1894
+ var _a, _b;
1895
+ return `_${(_a = reference.conversation) === null || _a === void 0 ? void 0 : _a.tenantId}_${(_b = reference.conversation) === null || _b === void 0 ? void 0 : _b.id}`;
1896
+ }
1897
+ }
1898
+
1899
+ // Copyright (c) Microsoft Corporation.
1900
+ // Licensed under the MIT license.
1901
+ /**
1902
+ * @internal
1903
+ */
1904
+ function cloneConversation(conversation) {
1905
+ return Object.assign({}, conversation);
1906
+ }
1907
+ /**
1908
+ * @internal
1909
+ */
1910
+ function getTargetType(conversationReference) {
1911
+ var _a;
1912
+ const conversationType = (_a = conversationReference.conversation) === null || _a === void 0 ? void 0 : _a.conversationType;
1913
+ if (conversationType === "personal") {
1914
+ return "Person";
1915
+ }
1916
+ else if (conversationType === "groupChat") {
1917
+ return "Group";
1918
+ }
1919
+ else if (conversationType === "channel") {
1920
+ return "Channel";
1921
+ }
1922
+ else {
1923
+ return undefined;
1924
+ }
1925
+ }
1926
+ /**
1927
+ * @internal
1928
+ */
1929
+ function getTeamsBotInstallationId(context) {
1930
+ var _a, _b, _c, _d;
1931
+ return (_d = (_c = (_b = (_a = context.activity) === null || _a === void 0 ? void 0 : _a.channelData) === null || _b === void 0 ? void 0 : _b.team) === null || _c === void 0 ? void 0 : _c.id) !== null && _d !== void 0 ? _d : context.activity.conversation.id;
1932
+ }
1933
+
1934
+ // Copyright (c) Microsoft Corporation.
1935
+ /**
1936
+ * Send a plain text message to a notification target.
1937
+ *
1938
+ * @param target - the notification target.
1939
+ * @param text - the plain text message.
1940
+ * @returns A `Promise` representing the asynchronous operation.
1941
+ *
1942
+ * @beta
1943
+ */
1944
+ function sendMessage(target, text) {
1945
+ return target.sendMessage(text);
1946
+ }
1947
+ /**
1948
+ * Send an adaptive card message to a notification target.
1949
+ *
1950
+ * @param target - the notification target.
1951
+ * @param card - the adaptive card raw JSON.
1952
+ * @returns A `Promise` representing the asynchronous operation.
1953
+ *
1954
+ * @beta
1955
+ */
1956
+ function sendAdaptiveCard(target, card) {
1957
+ return target.sendAdaptiveCard(card);
1958
+ }
1959
+ /**
1960
+ * A {@link NotificationTarget} that represents a team channel.
1961
+ *
1962
+ * @remarks
1963
+ * It's recommended to get channels from {@link TeamsBotInstallation.channels()}.
1964
+ *
1965
+ * @beta
1966
+ */
1967
+ class Channel {
1968
+ /**
1969
+ * Constuctor.
1970
+ *
1971
+ * @remarks
1972
+ * It's recommended to get channels from {@link TeamsBotInstallation.channels()}, instead of using this constructor.
1973
+ *
1974
+ * @param parent - The parent {@link TeamsBotInstallation} where this channel is created from.
1975
+ * @param info - Detailed channel information.
1976
+ *
1977
+ * @beta
1978
+ */
1979
+ constructor(parent, info) {
1980
+ /**
1981
+ * Notification target type. For channel it's always "Channel".
1982
+ *
1983
+ * @beta
1984
+ */
1985
+ this.type = "Channel";
1986
+ this.parent = parent;
1987
+ this.info = info;
1988
+ }
1989
+ /**
1990
+ * Send a plain text message.
1991
+ *
1992
+ * @param text - the plain text message.
1993
+ * @returns A `Promise` representing the asynchronous operation.
1994
+ *
1995
+ * @beta
1996
+ */
1997
+ sendMessage(text) {
1998
+ return this.parent.adapter.continueConversation(this.parent.conversationReference, (context) => tslib.__awaiter(this, void 0, void 0, function* () {
1999
+ const conversation = yield this.newConversation(context);
2000
+ yield this.parent.adapter.continueConversation(conversation, (ctx) => tslib.__awaiter(this, void 0, void 0, function* () {
2001
+ yield ctx.sendActivity(text);
2002
+ }));
2003
+ }));
2004
+ }
2005
+ /**
2006
+ * Send an adaptive card message.
2007
+ *
2008
+ * @param card - the adaptive card raw JSON.
2009
+ * @returns A `Promise` representing the asynchronous operation.
2010
+ *
2011
+ * @beta
2012
+ */
2013
+ sendAdaptiveCard(card) {
2014
+ return tslib.__awaiter(this, void 0, void 0, function* () {
2015
+ return this.parent.adapter.continueConversation(this.parent.conversationReference, (context) => tslib.__awaiter(this, void 0, void 0, function* () {
2016
+ const conversation = yield this.newConversation(context);
2017
+ yield this.parent.adapter.continueConversation(conversation, (ctx) => tslib.__awaiter(this, void 0, void 0, function* () {
2018
+ yield ctx.sendActivity({
2019
+ attachments: [botbuilder.CardFactory.adaptiveCard(card)],
2020
+ });
2021
+ }));
2022
+ }));
2023
+ });
2024
+ }
2025
+ /**
2026
+ * @internal
2027
+ */
2028
+ newConversation(context) {
2029
+ return tslib.__awaiter(this, void 0, void 0, function* () {
2030
+ const reference = botbuilder.TurnContext.getConversationReference(context.activity);
2031
+ const channelConversation = cloneConversation(reference);
2032
+ channelConversation.conversation.id = this.info.id || "";
2033
+ return channelConversation;
2034
+ });
2035
+ }
2036
+ }
2037
+ /**
2038
+ * A {@link NotificationTarget} that represents a team member.
2039
+ *
2040
+ * @remarks
2041
+ * It's recommended to get members from {@link TeamsBotInstallation.members()}.
2042
+ *
2043
+ * @beta
2044
+ */
2045
+ class Member {
2046
+ /**
2047
+ * Constuctor.
2048
+ *
2049
+ * @remarks
2050
+ * It's recommended to get members from {@link TeamsBotInstallation.members()}, instead of using this constructor.
2051
+ *
2052
+ * @param parent - The parent {@link TeamsBotInstallation} where this member is created from.
2053
+ * @param account - Detailed member account information.
2054
+ *
2055
+ * @beta
2056
+ */
2057
+ constructor(parent, account) {
2058
+ /**
2059
+ * Notification target type. For member it's always "Person".
2060
+ *
2061
+ * @beta
2062
+ */
2063
+ this.type = "Person";
2064
+ this.parent = parent;
2065
+ this.account = account;
2066
+ }
2067
+ /**
2068
+ * Send a plain text message.
2069
+ *
2070
+ * @param text - the plain text message.
2071
+ * @returns A `Promise` representing the asynchronous operation.
2072
+ *
2073
+ * @beta
2074
+ */
2075
+ sendMessage(text) {
2076
+ return this.parent.adapter.continueConversation(this.parent.conversationReference, (context) => tslib.__awaiter(this, void 0, void 0, function* () {
2077
+ const conversation = yield this.newConversation(context);
2078
+ yield this.parent.adapter.continueConversation(conversation, (ctx) => tslib.__awaiter(this, void 0, void 0, function* () {
2079
+ yield ctx.sendActivity(text);
2080
+ }));
2081
+ }));
2082
+ }
2083
+ /**
2084
+ * Send an adaptive card message.
2085
+ *
2086
+ * @param card - the adaptive card raw JSON.
2087
+ * @returns A `Promise` representing the asynchronous operation.
2088
+ *
2089
+ * @beta
2090
+ */
2091
+ sendAdaptiveCard(card) {
2092
+ return tslib.__awaiter(this, void 0, void 0, function* () {
2093
+ return this.parent.adapter.continueConversation(this.parent.conversationReference, (context) => tslib.__awaiter(this, void 0, void 0, function* () {
2094
+ const conversation = yield this.newConversation(context);
2095
+ yield this.parent.adapter.continueConversation(conversation, (ctx) => tslib.__awaiter(this, void 0, void 0, function* () {
2096
+ yield ctx.sendActivity({
2097
+ attachments: [botbuilder.CardFactory.adaptiveCard(card)],
2098
+ });
2099
+ }));
2100
+ }));
2101
+ });
2102
+ }
2103
+ /**
2104
+ * @internal
2105
+ */
2106
+ newConversation(context) {
2107
+ return tslib.__awaiter(this, void 0, void 0, function* () {
2108
+ const reference = botbuilder.TurnContext.getConversationReference(context.activity);
2109
+ const personalConversation = cloneConversation(reference);
2110
+ const connectorClient = context.turnState.get(this.parent.adapter.ConnectorClientKey);
2111
+ const conversation = yield connectorClient.conversations.createConversation({
2112
+ isGroup: false,
2113
+ tenantId: context.activity.conversation.tenantId,
2114
+ bot: context.activity.recipient,
2115
+ members: [this.account],
2116
+ channelData: {},
2117
+ });
2118
+ personalConversation.conversation.id = conversation.id;
2119
+ return personalConversation;
2120
+ });
2121
+ }
2122
+ }
2123
+ /**
2124
+ * A {@link NotificationTarget} that represents a bot installation. Teams Bot could be installed into
2125
+ * - Personal chat
2126
+ * - Group chat
2127
+ * - Team (by default the `General` channel)
2128
+ *
2129
+ * @remarks
2130
+ * It's recommended to get bot installations from {@link ConversationBot.installations()}.
2131
+ *
2132
+ * @beta
2133
+ */
2134
+ class TeamsBotInstallation {
2135
+ /**
2136
+ * Constructor
2137
+ *
2138
+ * @remarks
2139
+ * It's recommended to get bot installations from {@link ConversationBot.installations()}, instead of using this constructor.
2140
+ *
2141
+ * @param adapter - the bound `BotFrameworkAdapter`.
2142
+ * @param conversationReference - the bound `ConversationReference`.
2143
+ *
2144
+ * @beta
2145
+ */
2146
+ constructor(adapter, conversationReference) {
2147
+ this.adapter = adapter;
2148
+ this.conversationReference = conversationReference;
2149
+ this.type = getTargetType(conversationReference);
2150
+ }
2151
+ /**
2152
+ * Send a plain text message.
2153
+ *
2154
+ * @param text - the plain text message.
2155
+ * @returns A `Promise` representing the asynchronous operation.
2156
+ *
2157
+ * @beta
2158
+ */
2159
+ sendMessage(text) {
2160
+ return this.adapter.continueConversation(this.conversationReference, (context) => tslib.__awaiter(this, void 0, void 0, function* () {
2161
+ yield context.sendActivity(text);
2162
+ }));
2163
+ }
2164
+ /**
2165
+ * Send an adaptive card message.
2166
+ *
2167
+ * @param card - the adaptive card raw JSON.
2168
+ * @returns A `Promise` representing the asynchronous operation.
2169
+ *
2170
+ * @beta
2171
+ */
2172
+ sendAdaptiveCard(card) {
2173
+ return this.adapter.continueConversation(this.conversationReference, (context) => tslib.__awaiter(this, void 0, void 0, function* () {
2174
+ yield context.sendActivity({
2175
+ attachments: [botbuilder.CardFactory.adaptiveCard(card)],
2176
+ });
2177
+ }));
2178
+ }
2179
+ /**
2180
+ * Get channels from this bot installation.
2181
+ *
2182
+ * @returns an array of channels if bot is installed into a team, otherwise returns an empty array.
2183
+ *
2184
+ * @beta
2185
+ */
2186
+ channels() {
2187
+ return tslib.__awaiter(this, void 0, void 0, function* () {
2188
+ let teamsChannels = [];
2189
+ yield this.adapter.continueConversation(this.conversationReference, (context) => tslib.__awaiter(this, void 0, void 0, function* () {
2190
+ const teamId = getTeamsBotInstallationId(context);
2191
+ if (teamId !== undefined) {
2192
+ teamsChannels = yield botbuilder.TeamsInfo.getTeamChannels(context, teamId);
2193
+ }
2194
+ }));
2195
+ const channels = [];
2196
+ for (const channel of teamsChannels) {
2197
+ channels.push(new Channel(this, channel));
2198
+ }
2199
+ return channels;
2200
+ });
2201
+ }
2202
+ /**
2203
+ * Get members from this bot installation.
2204
+ *
2205
+ * @returns an array of members from where the bot is installed.
2206
+ *
2207
+ * @beta
2208
+ */
2209
+ members() {
2210
+ return tslib.__awaiter(this, void 0, void 0, function* () {
2211
+ let teamsMembers = [];
2212
+ yield this.adapter.continueConversation(this.conversationReference, (context) => tslib.__awaiter(this, void 0, void 0, function* () {
2213
+ teamsMembers = yield botbuilder.TeamsInfo.getMembers(context);
2214
+ }));
2215
+ const members = [];
2216
+ for (const member of teamsMembers) {
2217
+ members.push(new Member(this, member));
2218
+ }
2219
+ return members;
2220
+ });
2221
+ }
2222
+ }
2223
+ /**
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
+ * ```
2246
+ *
2247
+ * @beta
2248
+ */
2249
+ class NotificationBot {
2250
+ /**
2251
+ * constructor of the notification bot.
2252
+ *
2253
+ * @remarks
2254
+ * To ensure accuracy, it's recommended to initialize before handling any message.
2255
+ *
2256
+ * @param adapter - the bound `BotFrameworkAdapter`
2257
+ * @param options - initialize options
2258
+ *
2259
+ * @beta
2260
+ */
2261
+ constructor(adapter, options) {
2262
+ var _a, _b;
2263
+ const storage = (_a = options === null || options === void 0 ? void 0 : options.storage) !== null && _a !== void 0 ? _a : new LocalFileStorage(path__namespace.resolve(process.env.RUNNING_ON_AZURE === "1" ? (_b = process.env.TEMP) !== null && _b !== void 0 ? _b : "./" : "./"));
2264
+ this.conversationReferenceStore = new ConversationReferenceStore(storage);
2265
+ this.adapter = adapter.use(new NotificationMiddleware({
2266
+ conversationReferenceStore: this.conversationReferenceStore,
2267
+ }));
2268
+ }
2269
+ /**
2270
+ * Get all targets where the bot is installed.
2271
+ *
2272
+ * @remarks
2273
+ * The result is retrieving from the persisted storage.
2274
+ *
2275
+ * @returns - an array of {@link TeamsBotInstallation}.
2276
+ *
2277
+ * @beta
2278
+ */
2279
+ installations() {
2280
+ return tslib.__awaiter(this, void 0, void 0, function* () {
2281
+ if (this.conversationReferenceStore === undefined || this.adapter === undefined) {
2282
+ throw new Error("NotificationBot has not been initialized.");
2283
+ }
2284
+ const references = (yield this.conversationReferenceStore.getAll()).values();
2285
+ const targets = [];
2286
+ for (const reference of references) {
2287
+ // validate connection
2288
+ let valid = true;
2289
+ this.adapter.continueConversation(reference, (context) => tslib.__awaiter(this, void 0, void 0, function* () {
2290
+ try {
2291
+ // try get member to see if the installation is still valid
2292
+ yield botbuilder.TeamsInfo.getPagedMembers(context, 1);
2293
+ }
2294
+ catch (error) {
2295
+ if (error.code === "BotNotInConversationRoster") {
2296
+ valid = false;
2297
+ }
2298
+ }
2299
+ }));
2300
+ if (valid) {
2301
+ targets.push(new TeamsBotInstallation(this.adapter, reference));
2302
+ }
2303
+ else {
2304
+ this.conversationReferenceStore.delete(reference);
2305
+ }
2306
+ }
2307
+ return targets;
2308
+ });
2309
+ }
2310
+ }
2311
+
2312
+ // Copyright (c) Microsoft Corporation.
2313
+ /**
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.
2318
+ *
2319
+ * @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.
2321
+ *
2322
+ * ```typescript
2323
+ * // register through constructor
2324
+ * const commandBot = new CommandBot(adapter, [ new HelloWorldCommandHandler() ]);
2325
+ *
2326
+ * // register through `register*` API
2327
+ * commandBot.registerCommand(new HelpCommandHandler());
2328
+ * ```
2329
+ *
2330
+ * @beta
2331
+ */
2332
+ class CommandBot {
2333
+ /**
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.
2347
+ *
2348
+ * @param command The command to registered.
2349
+ *
2350
+ * @beta
2351
+ */
2352
+ registerCommand(command) {
2353
+ if (command) {
2354
+ this.middleware.commandHandlers.push(command);
2355
+ }
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);
2367
+ }
2368
+ }
2369
+ }
2370
+
2371
+ // Copyright (c) Microsoft Corporation.
2372
+ const { AdaptiveCards } = require("@microsoft/adaptivecards-tools");
2373
+ /**
2374
+ * Provides utility method to build bot message with cards that supported in Teams.
2375
+ */
2376
+ class MessageBuilder {
2377
+ /**
2378
+ * Build a bot message activity attached with adaptive card.
2379
+ *
2380
+ * @param getCardData Function to prepare your card data.
2381
+ * @param cardTemplate The adaptive card template.
2382
+ * @returns A bot message activity attached with an adaptive card.
2383
+ *
2384
+ * @example
2385
+ * ```javascript
2386
+ * const cardTemplate = {
2387
+ * type: "AdaptiveCard",
2388
+ * body: [
2389
+ * {
2390
+ * "type": "TextBlock",
2391
+ * "text": "${title}",
2392
+ * "size": "Large"
2393
+ * },
2394
+ * {
2395
+ * "type": "TextBlock",
2396
+ * "text": "${description}"
2397
+ * }],
2398
+ * $schema: "http://adaptivecards.io/schemas/adaptive-card.json",
2399
+ * version: "1.4"
2400
+ * };
2401
+ *
2402
+ * type CardData = {
2403
+ * title: string,
2404
+ * description: string
2405
+ * };
2406
+ * const card = MessageBuilder.attachAdaptiveCard<CardData>(() => {
2407
+ * return {
2408
+ * title: "sample card title",
2409
+ * description: "sample card description"
2410
+ * }}, cardTemplate);
2411
+ * ```
2412
+ *
2413
+ * @beta
2414
+ */
2415
+ static attachAdaptiveCard(getCardData, cardTemplate) {
2416
+ const cardData = getCardData();
2417
+ return {
2418
+ attachments: [botbuilder.CardFactory.adaptiveCard(AdaptiveCards.declare(cardTemplate).render(cardData))],
2419
+ };
2420
+ }
2421
+ /**
2422
+ * Build a bot message activity attached with an adaptive card.
2423
+ *
2424
+ * @param card The adaptive card content.
2425
+ * @returns A bot message activity attached with an adaptive card.
2426
+ *
2427
+ * @beta
2428
+ */
2429
+ static attachAdaptiveCardWithoutData(card) {
2430
+ return {
2431
+ attachments: [botbuilder.CardFactory.adaptiveCard(AdaptiveCards.declareWithoutData(card).render())],
2432
+ };
2433
+ }
2434
+ /**
2435
+ * Build a bot message activity attached with an hero card.
2436
+ *
2437
+ * @param title The card title.
2438
+ * @param images Optional. The array of images to include on the card.
2439
+ * @param buttons Optional. The array of buttons to include on the card. Each `string` in the array
2440
+ * is converted to an `imBack` button with a title and value set to the value of the string.
2441
+ * @param other Optional. Any additional properties to include on the card.
2442
+ *
2443
+ * @returns A bot message activity attached with a hero card.
2444
+ *
2445
+ * @example
2446
+ * ```javascript
2447
+ * const message = MessageBuilder.attachHeroCard(
2448
+ * 'sample title',
2449
+ * ['https://example.com/sample.jpg'],
2450
+ * ['action']
2451
+ * );
2452
+ * ```
2453
+ *
2454
+ * @beta
2455
+ */
2456
+ static attachHeroCard(title, images, buttons, other) {
2457
+ return MessageBuilder.attachContent(botbuilder.CardFactory.heroCard(title, images, buttons, other));
2458
+ }
2459
+ /**
2460
+ * Returns an attachment for a sign-in card.
2461
+ *
2462
+ * @param title The title for the card's sign-in button.
2463
+ * @param url The URL of the sign-in page to use.
2464
+ * @param text Optional. Additional text to include on the card.
2465
+ *
2466
+ * @returns A bot message activity attached with a sign-in card.
2467
+ *
2468
+ * @remarks
2469
+ * For channels that don't natively support sign-in cards, an alternative message is rendered.
2470
+ *
2471
+ * @beta
2472
+ */
2473
+ static attachSigninCard(title, url, text) {
2474
+ return MessageBuilder.attachContent(botbuilder.CardFactory.signinCard(title, url, text));
2475
+ }
2476
+ /**
2477
+ * Build a bot message activity attached with an Office 365 connector card.
2478
+ *
2479
+ * @param card A description of the Office 365 connector card.
2480
+ * @returns A bot message activity attached with an Office 365 connector card.
2481
+ *
2482
+ * @beta
2483
+ */
2484
+ static attachO365ConnectorCard(card) {
2485
+ return MessageBuilder.attachContent(botbuilder.CardFactory.o365ConnectorCard(card));
2486
+ }
2487
+ /**
2488
+ * Build a message activity attached with a receipt card.
2489
+ * @param card A description of the receipt card.
2490
+ * @returns A message activity attached with a receipt card.
2491
+ *
2492
+ * @beta
2493
+ */
2494
+ static AttachReceiptCard(card) {
2495
+ return MessageBuilder.attachContent(botbuilder.CardFactory.receiptCard(card));
2496
+ }
2497
+ /**
2498
+ *
2499
+ * @param title The card title.
2500
+ * @param images Optional. The array of images to include on the card.
2501
+ * @param buttons Optional. The array of buttons to include on the card. Each `string` in the array
2502
+ * is converted to an `imBack` button with a title and value set to the value of the string.
2503
+ * @param other Optional. Any additional properties to include on the card.
2504
+ * @returns A message activity attached with a thumbnail card
2505
+ *
2506
+ * @beta
2507
+ */
2508
+ static attachThumbnailCard(title, images, buttons, other) {
2509
+ return MessageBuilder.attachContent(botbuilder.CardFactory.thumbnailCard(title, images, buttons, other));
2510
+ }
2511
+ /**
2512
+ * Add an attachement to a bot activity.
2513
+ * @param attachement The attachment object to attach.
2514
+ * @returns A message activity with an attachment.
2515
+ *
2516
+ * @beta
2517
+ */
2518
+ static attachContent(attachement) {
2519
+ return {
2520
+ attachments: [attachement],
2521
+ };
2522
+ }
2523
+ }
2524
+
1605
2525
  exports.AppCredential = AppCredential;
2526
+ exports.Channel = Channel;
2527
+ exports.CommandBot = CommandBot;
1606
2528
  exports.ErrorWithCode = ErrorWithCode;
2529
+ exports.Member = Member;
2530
+ exports.MessageBuilder = MessageBuilder;
1607
2531
  exports.MsGraphAuthProvider = MsGraphAuthProvider;
2532
+ exports.NotificationBot = NotificationBot;
1608
2533
  exports.OnBehalfOfUserCredential = OnBehalfOfUserCredential;
2534
+ exports.TeamsBotInstallation = TeamsBotInstallation;
1609
2535
  exports.TeamsBotSsoPrompt = TeamsBotSsoPrompt;
1610
2536
  exports.TeamsFx = TeamsFx;
1611
2537
  exports.TeamsUserCredential = TeamsUserCredential;
1612
2538
  exports.createMicrosoftGraphClient = createMicrosoftGraphClient;
1613
2539
  exports.getLogLevel = getLogLevel;
1614
2540
  exports.getTediousConnectionConfig = getTediousConnectionConfig;
2541
+ exports.sendAdaptiveCard = sendAdaptiveCard;
2542
+ exports.sendMessage = sendMessage;
1615
2543
  exports.setLogFunction = setLogFunction;
1616
2544
  exports.setLogLevel = setLogLevel;
1617
2545
  exports.setLogger = setLogger;