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