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