@microsoft/teamsfx 1.1.1 → 1.1.2-alpha.0904fc821.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/dist/index.esm2017.js +293 -20
- package/dist/index.esm2017.js.map +1 -1
- package/dist/index.esm2017.mjs +1099 -124
- package/dist/index.esm2017.mjs.map +1 -1
- package/dist/index.esm5.js +309 -20
- package/dist/index.esm5.js.map +1 -1
- package/dist/index.node.cjs.js +1142 -123
- package/dist/index.node.cjs.js.map +1 -1
- package/package.json +8 -5
- package/types/teamsfx.d.ts +579 -23
package/dist/index.esm2017.mjs
CHANGED
|
@@ -3,13 +3,14 @@ import { ConfidentialClientApplication } from '@azure/msal-node';
|
|
|
3
3
|
import { createHash } from 'crypto';
|
|
4
4
|
import { Client } from '@microsoft/microsoft-graph-client';
|
|
5
5
|
import { ManagedIdentityCredential } from '@azure/identity';
|
|
6
|
-
import { ActivityTypes, Channels, TeamsInfo, CardFactory, ActionTypes, MessageFactory, StatusCodes, verifyStateOperationName, tokenExchangeOperationName, TurnContext, BotFrameworkAdapter } from 'botbuilder';
|
|
7
|
-
import { Dialog } from 'botbuilder-dialogs';
|
|
6
|
+
import { ActivityTypes, Channels, TeamsInfo, CardFactory, ActionTypes, MessageFactory, StatusCodes, verifyStateOperationName, tokenExchangeOperationName, TurnContext, TeamsActivityHandler, MemoryStorage, UserState, ConversationState, BotFrameworkAdapter } from 'botbuilder';
|
|
7
|
+
import { Dialog, ComponentDialog, WaterfallDialog, DialogSet, DialogTurnStatus } from 'botbuilder-dialogs';
|
|
8
8
|
import { v4 } from 'uuid';
|
|
9
9
|
import axios from 'axios';
|
|
10
10
|
import { Agent } from 'https';
|
|
11
11
|
import * as path from 'path';
|
|
12
12
|
import * as fs from 'fs';
|
|
13
|
+
import { __rest } from 'tslib';
|
|
13
14
|
import { AdaptiveCards } from '@microsoft/adaptivecards-tools';
|
|
14
15
|
|
|
15
16
|
// Copyright (c) Microsoft Corporation.
|
|
@@ -39,6 +40,30 @@ var ErrorCode;
|
|
|
39
40
|
* Channel is not supported error.
|
|
40
41
|
*/
|
|
41
42
|
ErrorCode["ChannelNotSupported"] = "ChannelNotSupported";
|
|
43
|
+
/**
|
|
44
|
+
* Failed to retrieve sso token
|
|
45
|
+
*/
|
|
46
|
+
ErrorCode["FailedToRetrieveSsoToken"] = "FailedToRetrieveSsoToken";
|
|
47
|
+
/**
|
|
48
|
+
* Failed to process sso handler
|
|
49
|
+
*/
|
|
50
|
+
ErrorCode["FailedToProcessSsoHandler"] = "FailedToProcessSsoHandler";
|
|
51
|
+
/**
|
|
52
|
+
* Cannot find command
|
|
53
|
+
*/
|
|
54
|
+
ErrorCode["CannotFindCommand"] = "CannotFindCommand";
|
|
55
|
+
/**
|
|
56
|
+
* Failed to run sso step
|
|
57
|
+
*/
|
|
58
|
+
ErrorCode["FailedToRunSsoStep"] = "FailedToRunSsoStep";
|
|
59
|
+
/**
|
|
60
|
+
* Failed to run dedup step
|
|
61
|
+
*/
|
|
62
|
+
ErrorCode["FailedToRunDedupStep"] = "FailedToRunDedupStep";
|
|
63
|
+
/**
|
|
64
|
+
* Sso activity handler is undefined
|
|
65
|
+
*/
|
|
66
|
+
ErrorCode["SsoActivityHandlerIsUndefined"] = "SsoActivityHandlerIsUndefined";
|
|
42
67
|
/**
|
|
43
68
|
* Runtime is not supported error.
|
|
44
69
|
*/
|
|
@@ -94,6 +119,15 @@ ErrorMessage.NodejsRuntimeNotSupported = "{0} is not supported in Node.";
|
|
|
94
119
|
ErrorMessage.FailToAcquireTokenOnBehalfOfUser = "Failed to acquire access token on behalf of user: {0}";
|
|
95
120
|
// ChannelNotSupported Error
|
|
96
121
|
ErrorMessage.OnlyMSTeamsChannelSupported = "{0} is only supported in MS Teams Channel";
|
|
122
|
+
ErrorMessage.FailedToProcessSsoHandler = "Failed to process sso handler: {0}";
|
|
123
|
+
// FailedToRetrieveSsoToken Error
|
|
124
|
+
ErrorMessage.FailedToRetrieveSsoToken = "Failed to retrieve sso token, user failed to finish the AAD consent flow.";
|
|
125
|
+
// CannotFindCommand Error
|
|
126
|
+
ErrorMessage.CannotFindCommand = "Cannot find command: {0}";
|
|
127
|
+
ErrorMessage.FailedToRunSsoStep = "Failed to run dialog to retrieve sso token: {0}";
|
|
128
|
+
ErrorMessage.FailedToRunDedupStep = "Failed to run dialog to remove duplicated messages: {0}";
|
|
129
|
+
// SsoActivityHandlerIsUndefined Error
|
|
130
|
+
ErrorMessage.SsoActivityHandlerIsNull = "Sso command can only be used or added when sso activity handler is not undefined";
|
|
97
131
|
// IdentityTypeNotSupported Error
|
|
98
132
|
ErrorMessage.IdentityTypeNotSupported = "{0} identity is not supported in {1}";
|
|
99
133
|
// AuthorizationInfoError
|
|
@@ -104,6 +138,7 @@ ErrorMessage.EmptyParameter = "Parameter {0} is empty";
|
|
|
104
138
|
ErrorMessage.DuplicateHttpsOptionProperty = "Axios HTTPS agent already defined value for property {0}";
|
|
105
139
|
ErrorMessage.DuplicateApiKeyInHeader = "The request already defined api key in request header with name {0}.";
|
|
106
140
|
ErrorMessage.DuplicateApiKeyInQueryParam = "The request already defined api key in query parameter with name {0}.";
|
|
141
|
+
ErrorMessage.OnlySupportInQueryActivity = "The handleMessageExtensionQueryWithToken only support in handleTeamsMessagingExtensionQuery with composeExtension/query type.";
|
|
107
142
|
/**
|
|
108
143
|
* Error class with code and message thrown by the SDK.
|
|
109
144
|
*/
|
|
@@ -694,10 +729,13 @@ class TeamsUserCredential {
|
|
|
694
729
|
}
|
|
695
730
|
/**
|
|
696
731
|
* Popup login page to get user's access token with specific scopes.
|
|
732
|
+
*
|
|
733
|
+
* @param {string[]} resources - The optional list of resources for full trust Teams apps.
|
|
734
|
+
*
|
|
697
735
|
* @remarks
|
|
698
736
|
* Can only be used within Teams.
|
|
699
737
|
*/
|
|
700
|
-
async login(scopes) {
|
|
738
|
+
async login(scopes, resources) {
|
|
701
739
|
throw new ErrorWithCode(formatString(ErrorMessage.NodejsRuntimeNotSupported, "TeamsUserCredential"), ErrorCode.RuntimeNotSupported);
|
|
702
740
|
}
|
|
703
741
|
/**
|
|
@@ -710,10 +748,13 @@ class TeamsUserCredential {
|
|
|
710
748
|
}
|
|
711
749
|
/**
|
|
712
750
|
* Get basic user info from SSO token
|
|
751
|
+
*
|
|
752
|
+
* @param {string[]} resources - The optional list of resources for full trust Teams apps.
|
|
753
|
+
*
|
|
713
754
|
* @remarks
|
|
714
755
|
* Can only be used within Teams.
|
|
715
756
|
*/
|
|
716
|
-
getUserInfo() {
|
|
757
|
+
getUserInfo(resources) {
|
|
717
758
|
throw new ErrorWithCode(formatString(ErrorMessage.NodejsRuntimeNotSupported, "TeamsUserCredential"), ErrorCode.RuntimeNotSupported);
|
|
718
759
|
}
|
|
719
760
|
}
|
|
@@ -1661,8 +1702,9 @@ class TeamsFx {
|
|
|
1661
1702
|
this.configuration = new Map();
|
|
1662
1703
|
this.loadFromEnv();
|
|
1663
1704
|
if (customConfig) {
|
|
1664
|
-
|
|
1665
|
-
|
|
1705
|
+
const myConfig = Object.assign({}, customConfig);
|
|
1706
|
+
for (const key of Object.keys(myConfig)) {
|
|
1707
|
+
const value = myConfig[key];
|
|
1666
1708
|
if (value) {
|
|
1667
1709
|
this.configuration.set(key, value);
|
|
1668
1710
|
}
|
|
@@ -1704,9 +1746,10 @@ class TeamsFx {
|
|
|
1704
1746
|
}
|
|
1705
1747
|
/**
|
|
1706
1748
|
* Get user information.
|
|
1749
|
+
* @param {string[]} resources - The optional list of resources for full trust Teams apps.
|
|
1707
1750
|
* @returns UserInfo object.
|
|
1708
1751
|
*/
|
|
1709
|
-
async getUserInfo() {
|
|
1752
|
+
async getUserInfo(resources) {
|
|
1710
1753
|
if (this.identityType !== IdentityType.User) {
|
|
1711
1754
|
const errorMsg = formatString(ErrorMessage.IdentityTypeNotSupported, this.identityType.toString(), "TeamsFx");
|
|
1712
1755
|
internalLogger.error(errorMsg);
|
|
@@ -1728,13 +1771,14 @@ class TeamsFx {
|
|
|
1728
1771
|
* await teamsfx.login("https://graph.microsoft.com/User.Read Calendars.Read"); // multiple scopes using string
|
|
1729
1772
|
* ```
|
|
1730
1773
|
* @param scopes - The list of scopes for which the token will have access, before that, we will request user to consent.
|
|
1774
|
+
* @param {string[]} resources - The optional list of resources for full trust Teams apps.
|
|
1731
1775
|
*
|
|
1732
1776
|
* @throws {@link ErrorCode|InternalError} when failed to login with unknown error.
|
|
1733
1777
|
* @throws {@link ErrorCode|ConsentFailed} when user canceled or failed to consent.
|
|
1734
1778
|
* @throws {@link ErrorCode|InvalidParameter} when scopes is not a valid string or string array.
|
|
1735
1779
|
* @throws {@link ErrorCode|RuntimeNotSupported} when runtime is nodeJS.
|
|
1736
1780
|
*/
|
|
1737
|
-
async login(scopes) {
|
|
1781
|
+
async login(scopes, resources) {
|
|
1738
1782
|
throw new ErrorWithCode(formatString(ErrorMessage.NodejsRuntimeNotSupported, "login"), ErrorCode.RuntimeNotSupported);
|
|
1739
1783
|
}
|
|
1740
1784
|
/**
|
|
@@ -1838,7 +1882,460 @@ var NotificationTargetType;
|
|
|
1838
1882
|
* The notification will be sent to a personal chat.
|
|
1839
1883
|
*/
|
|
1840
1884
|
NotificationTargetType["Person"] = "Person";
|
|
1841
|
-
})(NotificationTargetType || (NotificationTargetType = {}));
|
|
1885
|
+
})(NotificationTargetType || (NotificationTargetType = {}));
|
|
1886
|
+
/**
|
|
1887
|
+
* Options used to control how the response card will be sent to users.
|
|
1888
|
+
*/
|
|
1889
|
+
var AdaptiveCardResponse;
|
|
1890
|
+
(function (AdaptiveCardResponse) {
|
|
1891
|
+
/**
|
|
1892
|
+
* The response card will be replaced the current one for the interactor who trigger the action.
|
|
1893
|
+
*/
|
|
1894
|
+
AdaptiveCardResponse[AdaptiveCardResponse["ReplaceForInteractor"] = 0] = "ReplaceForInteractor";
|
|
1895
|
+
/**
|
|
1896
|
+
* The response card will be replaced the current one for all users in the chat.
|
|
1897
|
+
*/
|
|
1898
|
+
AdaptiveCardResponse[AdaptiveCardResponse["ReplaceForAll"] = 1] = "ReplaceForAll";
|
|
1899
|
+
/**
|
|
1900
|
+
* The response card will be sent as a new message for all users in the chat.
|
|
1901
|
+
*/
|
|
1902
|
+
AdaptiveCardResponse[AdaptiveCardResponse["NewForAll"] = 2] = "NewForAll";
|
|
1903
|
+
})(AdaptiveCardResponse || (AdaptiveCardResponse = {}));
|
|
1904
|
+
/**
|
|
1905
|
+
* Status code for an `application/vnd.microsoft.error` invoke response.
|
|
1906
|
+
*/
|
|
1907
|
+
var InvokeResponseErrorCode;
|
|
1908
|
+
(function (InvokeResponseErrorCode) {
|
|
1909
|
+
/**
|
|
1910
|
+
* Invalid request.
|
|
1911
|
+
*/
|
|
1912
|
+
InvokeResponseErrorCode[InvokeResponseErrorCode["BadRequest"] = 400] = "BadRequest";
|
|
1913
|
+
/**
|
|
1914
|
+
* Internal server error.
|
|
1915
|
+
*/
|
|
1916
|
+
InvokeResponseErrorCode[InvokeResponseErrorCode["InternalServerError"] = 500] = "InternalServerError";
|
|
1917
|
+
})(InvokeResponseErrorCode || (InvokeResponseErrorCode = {}));
|
|
1918
|
+
|
|
1919
|
+
/**
|
|
1920
|
+
* Available response type for an adaptive card invoke response.
|
|
1921
|
+
* @internal
|
|
1922
|
+
*/
|
|
1923
|
+
var InvokeResponseType;
|
|
1924
|
+
(function (InvokeResponseType) {
|
|
1925
|
+
InvokeResponseType["AdaptiveCard"] = "application/vnd.microsoft.card.adaptive";
|
|
1926
|
+
InvokeResponseType["Message"] = "application/vnd.microsoft.activity.message";
|
|
1927
|
+
InvokeResponseType["Error"] = "application/vnd.microsoft.error";
|
|
1928
|
+
})(InvokeResponseType || (InvokeResponseType = {}));
|
|
1929
|
+
/**
|
|
1930
|
+
* Provides methods for formatting various invoke responses a bot can send to respond to an invoke request.
|
|
1931
|
+
*
|
|
1932
|
+
* @remarks
|
|
1933
|
+
* All of these functions return an {@link InvokeResponse} object, which can be
|
|
1934
|
+
* passed as input to generate a new `invokeResponse` activity.
|
|
1935
|
+
*
|
|
1936
|
+
* This example sends an invoke response that contains an adaptive card.
|
|
1937
|
+
*
|
|
1938
|
+
* ```typescript
|
|
1939
|
+
*
|
|
1940
|
+
* const myCard: IAdaptiveCard = {
|
|
1941
|
+
* type: "AdaptiveCard",
|
|
1942
|
+
* body: [
|
|
1943
|
+
* {
|
|
1944
|
+
* "type": "TextBlock",
|
|
1945
|
+
* "text": "This is a sample card"
|
|
1946
|
+
* }],
|
|
1947
|
+
* $schema: "http://adaptivecards.io/schemas/adaptive-card.json",
|
|
1948
|
+
* version: "1.4"
|
|
1949
|
+
* };
|
|
1950
|
+
*
|
|
1951
|
+
* const invokeResponse = InvokeResponseFactory.adaptiveCard(myCard);
|
|
1952
|
+
* await context.sendActivity({
|
|
1953
|
+
* type: ActivityTypes.InvokeResponse,
|
|
1954
|
+
* value: invokeResponse,
|
|
1955
|
+
* });
|
|
1956
|
+
* ```
|
|
1957
|
+
*/
|
|
1958
|
+
class InvokeResponseFactory {
|
|
1959
|
+
/**
|
|
1960
|
+
* Create an invoke response from a text message.
|
|
1961
|
+
* The type of the invoke response is `application/vnd.microsoft.activity.message`
|
|
1962
|
+
* indicates the request was successfully processed.
|
|
1963
|
+
*
|
|
1964
|
+
* @param message A text message included in a invoke response.
|
|
1965
|
+
*
|
|
1966
|
+
* @returns {InvokeResponse} An InvokeResponse object.
|
|
1967
|
+
*/
|
|
1968
|
+
static textMessage(message) {
|
|
1969
|
+
if (!message) {
|
|
1970
|
+
throw new Error("The text message cannot be null or empty");
|
|
1971
|
+
}
|
|
1972
|
+
return {
|
|
1973
|
+
status: StatusCodes.OK,
|
|
1974
|
+
body: {
|
|
1975
|
+
statusCode: StatusCodes.OK,
|
|
1976
|
+
type: InvokeResponseType.Message,
|
|
1977
|
+
value: message,
|
|
1978
|
+
},
|
|
1979
|
+
};
|
|
1980
|
+
}
|
|
1981
|
+
/**
|
|
1982
|
+
* Create an invoke response from an adaptive card.
|
|
1983
|
+
*
|
|
1984
|
+
* The type of the invoke response is `application/vnd.microsoft.card.adaptive` indicates
|
|
1985
|
+
* the request was successfully processed, and the response includes an adaptive card
|
|
1986
|
+
* that the client should display in place of the current one.
|
|
1987
|
+
*
|
|
1988
|
+
* @param card The adaptive card JSON payload.
|
|
1989
|
+
*
|
|
1990
|
+
* @returns {InvokeResponse} An InvokeResponse object.
|
|
1991
|
+
*/
|
|
1992
|
+
static adaptiveCard(card) {
|
|
1993
|
+
if (!card) {
|
|
1994
|
+
throw new Error("The adaptive card content cannot be null or undefined");
|
|
1995
|
+
}
|
|
1996
|
+
return {
|
|
1997
|
+
status: StatusCodes.OK,
|
|
1998
|
+
body: {
|
|
1999
|
+
statusCode: StatusCodes.OK,
|
|
2000
|
+
type: InvokeResponseType.AdaptiveCard,
|
|
2001
|
+
value: card,
|
|
2002
|
+
},
|
|
2003
|
+
};
|
|
2004
|
+
}
|
|
2005
|
+
/**
|
|
2006
|
+
* Create an invoke response with error code and message.
|
|
2007
|
+
*
|
|
2008
|
+
* The type of the invoke response is `application/vnd.microsoft.error` indicates
|
|
2009
|
+
* the request was failed to processed.
|
|
2010
|
+
*
|
|
2011
|
+
* @param errorCode The status code indicates error, available values:
|
|
2012
|
+
* - 400 (BadRequest): indicate the incoming request was invalid.
|
|
2013
|
+
* - 500 (InternalServerError): indicate an unexpected error occurred.
|
|
2014
|
+
* @param errorMessage The error message.
|
|
2015
|
+
*
|
|
2016
|
+
* @returns {InvokeResponse} An InvokeResponse object.
|
|
2017
|
+
*/
|
|
2018
|
+
static errorResponse(errorCode, errorMessage) {
|
|
2019
|
+
return {
|
|
2020
|
+
status: StatusCodes.OK,
|
|
2021
|
+
body: {
|
|
2022
|
+
statusCode: errorCode,
|
|
2023
|
+
type: InvokeResponseType.Error,
|
|
2024
|
+
value: {
|
|
2025
|
+
code: errorCode.toString(),
|
|
2026
|
+
message: errorMessage,
|
|
2027
|
+
},
|
|
2028
|
+
},
|
|
2029
|
+
};
|
|
2030
|
+
}
|
|
2031
|
+
/**
|
|
2032
|
+
* Create an invoke response with status code and response value.
|
|
2033
|
+
* @param statusCode The status code.
|
|
2034
|
+
* @param body The value of the response body.
|
|
2035
|
+
*
|
|
2036
|
+
* @returns {InvokeResponse} An InvokeResponse object.
|
|
2037
|
+
*/
|
|
2038
|
+
static createInvokeResponse(statusCode, body) {
|
|
2039
|
+
return {
|
|
2040
|
+
status: statusCode,
|
|
2041
|
+
body: body,
|
|
2042
|
+
};
|
|
2043
|
+
}
|
|
2044
|
+
}
|
|
2045
|
+
|
|
2046
|
+
/**
|
|
2047
|
+
* @internal
|
|
2048
|
+
*/
|
|
2049
|
+
class CardActionMiddleware {
|
|
2050
|
+
constructor(handlers) {
|
|
2051
|
+
this.actionHandlers = [];
|
|
2052
|
+
this.defaultMessage = "Your response was sent to the app";
|
|
2053
|
+
if (handlers && handlers.length > 0) {
|
|
2054
|
+
this.actionHandlers.push(...handlers);
|
|
2055
|
+
}
|
|
2056
|
+
}
|
|
2057
|
+
async onTurn(context, next) {
|
|
2058
|
+
var _a, _b, _c;
|
|
2059
|
+
if (context.activity.name === "adaptiveCard/action") {
|
|
2060
|
+
const action = context.activity.value.action;
|
|
2061
|
+
const actionVerb = action.verb;
|
|
2062
|
+
for (const handler of this.actionHandlers) {
|
|
2063
|
+
if (((_a = handler.triggerVerb) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === (actionVerb === null || actionVerb === void 0 ? void 0 : actionVerb.toLowerCase())) {
|
|
2064
|
+
let response;
|
|
2065
|
+
try {
|
|
2066
|
+
response = await handler.handleActionInvoked(context, action.data);
|
|
2067
|
+
}
|
|
2068
|
+
catch (error) {
|
|
2069
|
+
const errorResponse = InvokeResponseFactory.errorResponse(InvokeResponseErrorCode.InternalServerError, error.message);
|
|
2070
|
+
await this.sendInvokeResponse(context, errorResponse);
|
|
2071
|
+
throw error;
|
|
2072
|
+
}
|
|
2073
|
+
const responseType = (_b = response.body) === null || _b === void 0 ? void 0 : _b.type;
|
|
2074
|
+
switch (responseType) {
|
|
2075
|
+
case InvokeResponseType.AdaptiveCard:
|
|
2076
|
+
const card = (_c = response.body) === null || _c === void 0 ? void 0 : _c.value;
|
|
2077
|
+
if (!card) {
|
|
2078
|
+
const errorMessage = "Adaptive card content cannot be found in the response body";
|
|
2079
|
+
await this.sendInvokeResponse(context, InvokeResponseFactory.errorResponse(InvokeResponseErrorCode.InternalServerError, errorMessage));
|
|
2080
|
+
throw new Error(errorMessage);
|
|
2081
|
+
}
|
|
2082
|
+
if (card.refresh && handler.adaptiveCardResponse !== AdaptiveCardResponse.NewForAll) {
|
|
2083
|
+
// Card won't be refreshed with AdaptiveCardResponse.ReplaceForInteractor.
|
|
2084
|
+
// So set to AdaptiveCardResponse.ReplaceForAll here.
|
|
2085
|
+
handler.adaptiveCardResponse = AdaptiveCardResponse.ReplaceForAll;
|
|
2086
|
+
}
|
|
2087
|
+
const activity = MessageFactory.attachment(CardFactory.adaptiveCard(card));
|
|
2088
|
+
if (handler.adaptiveCardResponse === AdaptiveCardResponse.NewForAll) {
|
|
2089
|
+
await this.sendInvokeResponse(context, InvokeResponseFactory.textMessage(this.defaultMessage));
|
|
2090
|
+
await context.sendActivity(activity);
|
|
2091
|
+
}
|
|
2092
|
+
else if (handler.adaptiveCardResponse === AdaptiveCardResponse.ReplaceForAll) {
|
|
2093
|
+
activity.id = context.activity.replyToId;
|
|
2094
|
+
await context.updateActivity(activity);
|
|
2095
|
+
await this.sendInvokeResponse(context, response);
|
|
2096
|
+
}
|
|
2097
|
+
else {
|
|
2098
|
+
await this.sendInvokeResponse(context, response);
|
|
2099
|
+
}
|
|
2100
|
+
break;
|
|
2101
|
+
case InvokeResponseType.Message:
|
|
2102
|
+
case InvokeResponseType.Error:
|
|
2103
|
+
default:
|
|
2104
|
+
await this.sendInvokeResponse(context, response);
|
|
2105
|
+
break;
|
|
2106
|
+
}
|
|
2107
|
+
break;
|
|
2108
|
+
}
|
|
2109
|
+
}
|
|
2110
|
+
}
|
|
2111
|
+
await next();
|
|
2112
|
+
}
|
|
2113
|
+
async sendInvokeResponse(context, response) {
|
|
2114
|
+
await context.sendActivity({
|
|
2115
|
+
type: ActivityTypes.InvokeResponse,
|
|
2116
|
+
value: response,
|
|
2117
|
+
});
|
|
2118
|
+
}
|
|
2119
|
+
}
|
|
2120
|
+
|
|
2121
|
+
/**
|
|
2122
|
+
* A card action bot to respond to adaptive card universal actions.
|
|
2123
|
+
*/
|
|
2124
|
+
class CardActionBot {
|
|
2125
|
+
/**
|
|
2126
|
+
* Creates a new instance of the `CardActionBot`.
|
|
2127
|
+
*
|
|
2128
|
+
* @param adapter The bound `BotFrameworkAdapter`.
|
|
2129
|
+
* @param options - initialize options
|
|
2130
|
+
*/
|
|
2131
|
+
constructor(adapter, options) {
|
|
2132
|
+
this.middleware = new CardActionMiddleware(options === null || options === void 0 ? void 0 : options.actions);
|
|
2133
|
+
this.adapter = adapter.use(this.middleware);
|
|
2134
|
+
}
|
|
2135
|
+
/**
|
|
2136
|
+
* Registers a card action handler to the bot.
|
|
2137
|
+
* @param actionHandler A card action handler to be registered.
|
|
2138
|
+
*/
|
|
2139
|
+
registerHandler(actionHandler) {
|
|
2140
|
+
if (actionHandler) {
|
|
2141
|
+
this.middleware.actionHandlers.push(actionHandler);
|
|
2142
|
+
}
|
|
2143
|
+
}
|
|
2144
|
+
/**
|
|
2145
|
+
* Registers card action handlers to the bot.
|
|
2146
|
+
* @param actionHandlers A set of card action handlers to be registered.
|
|
2147
|
+
*/
|
|
2148
|
+
registerHandlers(actionHandlers) {
|
|
2149
|
+
if (actionHandlers) {
|
|
2150
|
+
this.middleware.actionHandlers.push(...actionHandlers);
|
|
2151
|
+
}
|
|
2152
|
+
}
|
|
2153
|
+
}
|
|
2154
|
+
|
|
2155
|
+
// Copyright (c) Microsoft Corporation.
|
|
2156
|
+
/**
|
|
2157
|
+
* @internal
|
|
2158
|
+
*/
|
|
2159
|
+
class CommandResponseMiddleware {
|
|
2160
|
+
constructor(handlers, ssoHandlers, activityHandler) {
|
|
2161
|
+
this.commandHandlers = [];
|
|
2162
|
+
this.ssoCommandHandlers = [];
|
|
2163
|
+
handlers = handlers !== null && handlers !== void 0 ? handlers : [];
|
|
2164
|
+
ssoHandlers = ssoHandlers !== null && ssoHandlers !== void 0 ? ssoHandlers : [];
|
|
2165
|
+
this.hasSsoCommand = ssoHandlers.length > 0;
|
|
2166
|
+
this.ssoActivityHandler = activityHandler;
|
|
2167
|
+
if (this.hasSsoCommand && !this.ssoActivityHandler) {
|
|
2168
|
+
internalLogger.error(ErrorMessage.SsoActivityHandlerIsNull);
|
|
2169
|
+
throw new ErrorWithCode(ErrorMessage.SsoActivityHandlerIsNull, ErrorCode.SsoActivityHandlerIsUndefined);
|
|
2170
|
+
}
|
|
2171
|
+
this.commandHandlers.push(...handlers);
|
|
2172
|
+
for (const ssoHandler of ssoHandlers) {
|
|
2173
|
+
this.addSsoCommand(ssoHandler);
|
|
2174
|
+
}
|
|
2175
|
+
}
|
|
2176
|
+
addSsoCommand(ssoHandler) {
|
|
2177
|
+
var _a;
|
|
2178
|
+
(_a = this.ssoActivityHandler) === null || _a === void 0 ? void 0 : _a.addCommand(async (context, tokenResponse, message) => {
|
|
2179
|
+
const matchResult = this.shouldTrigger(ssoHandler.triggerPatterns, message.text);
|
|
2180
|
+
message.matches = Array.isArray(matchResult) ? matchResult : void 0;
|
|
2181
|
+
const response = await ssoHandler.handleCommandReceived(context, message, tokenResponse);
|
|
2182
|
+
await this.processResponse(context, response);
|
|
2183
|
+
}, ssoHandler.triggerPatterns);
|
|
2184
|
+
this.ssoCommandHandlers.push(ssoHandler);
|
|
2185
|
+
this.hasSsoCommand = true;
|
|
2186
|
+
}
|
|
2187
|
+
async onTurn(context, next) {
|
|
2188
|
+
var _a, _b;
|
|
2189
|
+
if (context.activity.type === ActivityTypes.Message) {
|
|
2190
|
+
// Invoke corresponding command handler for the command response
|
|
2191
|
+
const commandText = this.getActivityText(context.activity);
|
|
2192
|
+
let alreadyProcessed = false;
|
|
2193
|
+
for (const handler of this.commandHandlers) {
|
|
2194
|
+
const matchResult = this.shouldTrigger(handler.triggerPatterns, commandText);
|
|
2195
|
+
// It is important to note that the command bot will stop processing handlers
|
|
2196
|
+
// when the first command handler is matched.
|
|
2197
|
+
if (!!matchResult) {
|
|
2198
|
+
const message = {
|
|
2199
|
+
text: commandText,
|
|
2200
|
+
};
|
|
2201
|
+
message.matches = Array.isArray(matchResult) ? matchResult : void 0;
|
|
2202
|
+
const response = await handler.handleCommandReceived(context, message);
|
|
2203
|
+
await this.processResponse(context, response);
|
|
2204
|
+
alreadyProcessed = true;
|
|
2205
|
+
break;
|
|
2206
|
+
}
|
|
2207
|
+
}
|
|
2208
|
+
if (!alreadyProcessed) {
|
|
2209
|
+
for (const handler of this.ssoCommandHandlers) {
|
|
2210
|
+
const matchResult = this.shouldTrigger(handler.triggerPatterns, commandText);
|
|
2211
|
+
if (!!matchResult) {
|
|
2212
|
+
await ((_a = this.ssoActivityHandler) === null || _a === void 0 ? void 0 : _a.run(context));
|
|
2213
|
+
break;
|
|
2214
|
+
}
|
|
2215
|
+
}
|
|
2216
|
+
}
|
|
2217
|
+
}
|
|
2218
|
+
else {
|
|
2219
|
+
if (this.hasSsoCommand) {
|
|
2220
|
+
await ((_b = this.ssoActivityHandler) === null || _b === void 0 ? void 0 : _b.run(context));
|
|
2221
|
+
}
|
|
2222
|
+
}
|
|
2223
|
+
await next();
|
|
2224
|
+
}
|
|
2225
|
+
async processResponse(context, response) {
|
|
2226
|
+
if (typeof response === "string") {
|
|
2227
|
+
await context.sendActivity(response);
|
|
2228
|
+
}
|
|
2229
|
+
else {
|
|
2230
|
+
const replyActivity = response;
|
|
2231
|
+
if (replyActivity) {
|
|
2232
|
+
await context.sendActivity(replyActivity);
|
|
2233
|
+
}
|
|
2234
|
+
}
|
|
2235
|
+
}
|
|
2236
|
+
matchPattern(pattern, text) {
|
|
2237
|
+
if (text) {
|
|
2238
|
+
if (typeof pattern === "string") {
|
|
2239
|
+
const regExp = new RegExp(pattern, "i");
|
|
2240
|
+
return regExp.test(text);
|
|
2241
|
+
}
|
|
2242
|
+
if (pattern instanceof RegExp) {
|
|
2243
|
+
const matches = text.match(pattern);
|
|
2244
|
+
return matches !== null && matches !== void 0 ? matches : false;
|
|
2245
|
+
}
|
|
2246
|
+
}
|
|
2247
|
+
return false;
|
|
2248
|
+
}
|
|
2249
|
+
shouldTrigger(patterns, text) {
|
|
2250
|
+
const expressions = Array.isArray(patterns) ? patterns : [patterns];
|
|
2251
|
+
for (const ex of expressions) {
|
|
2252
|
+
const arg = this.matchPattern(ex, text);
|
|
2253
|
+
if (arg)
|
|
2254
|
+
return arg;
|
|
2255
|
+
}
|
|
2256
|
+
return false;
|
|
2257
|
+
}
|
|
2258
|
+
getActivityText(activity) {
|
|
2259
|
+
let text = activity.text;
|
|
2260
|
+
const removedMentionText = TurnContext.removeRecipientMention(activity);
|
|
2261
|
+
if (removedMentionText) {
|
|
2262
|
+
text = removedMentionText
|
|
2263
|
+
.toLowerCase()
|
|
2264
|
+
.replace(/\n|\r\n/g, "")
|
|
2265
|
+
.trim();
|
|
2266
|
+
}
|
|
2267
|
+
return text;
|
|
2268
|
+
}
|
|
2269
|
+
}
|
|
2270
|
+
|
|
2271
|
+
// Copyright (c) Microsoft Corporation.
|
|
2272
|
+
/**
|
|
2273
|
+
* A command bot for receiving commands and sending responses in Teams.
|
|
2274
|
+
*
|
|
2275
|
+
* @remarks
|
|
2276
|
+
* 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.
|
|
2277
|
+
*/
|
|
2278
|
+
class CommandBot {
|
|
2279
|
+
/**
|
|
2280
|
+
* Creates a new instance of the `CommandBot`.
|
|
2281
|
+
*
|
|
2282
|
+
* @param adapter The bound `BotFrameworkAdapter`.
|
|
2283
|
+
* @param options - initialize options
|
|
2284
|
+
*/
|
|
2285
|
+
constructor(adapter, options, ssoCommandActivityHandler, ssoConfig) {
|
|
2286
|
+
this.ssoConfig = ssoConfig;
|
|
2287
|
+
this.middleware = new CommandResponseMiddleware(options === null || options === void 0 ? void 0 : options.commands, options === null || options === void 0 ? void 0 : options.ssoCommands, ssoCommandActivityHandler);
|
|
2288
|
+
this.adapter = adapter.use(this.middleware);
|
|
2289
|
+
}
|
|
2290
|
+
/**
|
|
2291
|
+
* Registers a command into the command bot.
|
|
2292
|
+
*
|
|
2293
|
+
* @param command The command to register.
|
|
2294
|
+
*/
|
|
2295
|
+
registerCommand(command) {
|
|
2296
|
+
if (command) {
|
|
2297
|
+
this.middleware.commandHandlers.push(command);
|
|
2298
|
+
}
|
|
2299
|
+
}
|
|
2300
|
+
/**
|
|
2301
|
+
* Registers commands into the command bot.
|
|
2302
|
+
*
|
|
2303
|
+
* @param commands The commands to register.
|
|
2304
|
+
*/
|
|
2305
|
+
registerCommands(commands) {
|
|
2306
|
+
if (commands) {
|
|
2307
|
+
this.middleware.commandHandlers.push(...commands);
|
|
2308
|
+
}
|
|
2309
|
+
}
|
|
2310
|
+
/**
|
|
2311
|
+
* Registers a sso command into the command bot.
|
|
2312
|
+
*
|
|
2313
|
+
* @param command The command to register.
|
|
2314
|
+
*/
|
|
2315
|
+
registerSsoCommand(ssoCommand) {
|
|
2316
|
+
this.validateSsoActivityHandler();
|
|
2317
|
+
this.middleware.addSsoCommand(ssoCommand);
|
|
2318
|
+
}
|
|
2319
|
+
/**
|
|
2320
|
+
* Registers commands into the command bot.
|
|
2321
|
+
*
|
|
2322
|
+
* @param commands The commands to register.
|
|
2323
|
+
*/
|
|
2324
|
+
registerSsoCommands(ssoCommands) {
|
|
2325
|
+
if (ssoCommands.length > 0) {
|
|
2326
|
+
this.validateSsoActivityHandler();
|
|
2327
|
+
for (const ssoCommand of ssoCommands) {
|
|
2328
|
+
this.middleware.addSsoCommand(ssoCommand);
|
|
2329
|
+
}
|
|
2330
|
+
}
|
|
2331
|
+
}
|
|
2332
|
+
validateSsoActivityHandler() {
|
|
2333
|
+
if (!this.middleware.ssoActivityHandler) {
|
|
2334
|
+
internalLogger.error(ErrorMessage.SsoActivityHandlerIsNull);
|
|
2335
|
+
throw new ErrorWithCode(ErrorMessage.SsoActivityHandlerIsNull, ErrorCode.SsoActivityHandlerIsUndefined);
|
|
2336
|
+
}
|
|
2337
|
+
}
|
|
2338
|
+
}
|
|
1842
2339
|
|
|
1843
2340
|
// Copyright (c) Microsoft Corporation.
|
|
1844
2341
|
/**
|
|
@@ -1962,115 +2459,6 @@ class NotificationMiddleware {
|
|
|
1962
2459
|
}
|
|
1963
2460
|
}
|
|
1964
2461
|
}
|
|
1965
|
-
}
|
|
1966
|
-
class CommandResponseMiddleware {
|
|
1967
|
-
constructor(handlers) {
|
|
1968
|
-
this.commandHandlers = [];
|
|
1969
|
-
if (handlers && handlers.length > 0) {
|
|
1970
|
-
this.commandHandlers.push(...handlers);
|
|
1971
|
-
}
|
|
1972
|
-
}
|
|
1973
|
-
async onTurn(context, next) {
|
|
1974
|
-
if (context.activity.type === ActivityTypes.Message) {
|
|
1975
|
-
// Invoke corresponding command handler for the command response
|
|
1976
|
-
const commandText = this.getActivityText(context.activity);
|
|
1977
|
-
const message = {
|
|
1978
|
-
text: commandText,
|
|
1979
|
-
};
|
|
1980
|
-
for (const handler of this.commandHandlers) {
|
|
1981
|
-
const matchResult = this.shouldTrigger(handler.triggerPatterns, commandText);
|
|
1982
|
-
// It is important to note that the command bot will stop processing handlers
|
|
1983
|
-
// when the first command handler is matched.
|
|
1984
|
-
if (!!matchResult) {
|
|
1985
|
-
message.matches = Array.isArray(matchResult) ? matchResult : void 0;
|
|
1986
|
-
const response = await handler.handleCommandReceived(context, message);
|
|
1987
|
-
if (typeof response === "string") {
|
|
1988
|
-
await context.sendActivity(response);
|
|
1989
|
-
}
|
|
1990
|
-
else {
|
|
1991
|
-
const replyActivity = response;
|
|
1992
|
-
if (replyActivity) {
|
|
1993
|
-
await context.sendActivity(replyActivity);
|
|
1994
|
-
}
|
|
1995
|
-
}
|
|
1996
|
-
}
|
|
1997
|
-
}
|
|
1998
|
-
}
|
|
1999
|
-
await next();
|
|
2000
|
-
}
|
|
2001
|
-
matchPattern(pattern, text) {
|
|
2002
|
-
if (text) {
|
|
2003
|
-
if (typeof pattern === "string") {
|
|
2004
|
-
const regExp = new RegExp(pattern, "i");
|
|
2005
|
-
return regExp.test(text);
|
|
2006
|
-
}
|
|
2007
|
-
if (pattern instanceof RegExp) {
|
|
2008
|
-
const matches = text.match(pattern);
|
|
2009
|
-
return matches !== null && matches !== void 0 ? matches : false;
|
|
2010
|
-
}
|
|
2011
|
-
}
|
|
2012
|
-
return false;
|
|
2013
|
-
}
|
|
2014
|
-
shouldTrigger(patterns, text) {
|
|
2015
|
-
const expressions = Array.isArray(patterns) ? patterns : [patterns];
|
|
2016
|
-
for (const ex of expressions) {
|
|
2017
|
-
const arg = this.matchPattern(ex, text);
|
|
2018
|
-
if (arg)
|
|
2019
|
-
return arg;
|
|
2020
|
-
}
|
|
2021
|
-
return false;
|
|
2022
|
-
}
|
|
2023
|
-
getActivityText(activity) {
|
|
2024
|
-
let text = activity.text;
|
|
2025
|
-
const removedMentionText = TurnContext.removeRecipientMention(activity);
|
|
2026
|
-
if (removedMentionText) {
|
|
2027
|
-
text = removedMentionText
|
|
2028
|
-
.toLowerCase()
|
|
2029
|
-
.replace(/\n|\r\n/g, "")
|
|
2030
|
-
.trim();
|
|
2031
|
-
}
|
|
2032
|
-
return text;
|
|
2033
|
-
}
|
|
2034
|
-
}
|
|
2035
|
-
|
|
2036
|
-
// Copyright (c) Microsoft Corporation.
|
|
2037
|
-
/**
|
|
2038
|
-
* A command bot for receiving commands and sending responses in Teams.
|
|
2039
|
-
*
|
|
2040
|
-
* @remarks
|
|
2041
|
-
* 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.
|
|
2042
|
-
*/
|
|
2043
|
-
class CommandBot {
|
|
2044
|
-
/**
|
|
2045
|
-
* Creates a new instance of the `CommandBot`.
|
|
2046
|
-
*
|
|
2047
|
-
* @param adapter The bound `BotFrameworkAdapter`.
|
|
2048
|
-
* @param options - initialize options
|
|
2049
|
-
*/
|
|
2050
|
-
constructor(adapter, options) {
|
|
2051
|
-
this.middleware = new CommandResponseMiddleware(options === null || options === void 0 ? void 0 : options.commands);
|
|
2052
|
-
this.adapter = adapter.use(this.middleware);
|
|
2053
|
-
}
|
|
2054
|
-
/**
|
|
2055
|
-
* Registers a command into the command bot.
|
|
2056
|
-
*
|
|
2057
|
-
* @param command The command to registered.
|
|
2058
|
-
*/
|
|
2059
|
-
registerCommand(command) {
|
|
2060
|
-
if (command) {
|
|
2061
|
-
this.middleware.commandHandlers.push(command);
|
|
2062
|
-
}
|
|
2063
|
-
}
|
|
2064
|
-
/**
|
|
2065
|
-
* Registers commands into the command bot.
|
|
2066
|
-
*
|
|
2067
|
-
* @param commands The command to registered.
|
|
2068
|
-
*/
|
|
2069
|
-
registerCommands(commands) {
|
|
2070
|
-
if (commands) {
|
|
2071
|
-
this.middleware.commandHandlers.push(...commands);
|
|
2072
|
-
}
|
|
2073
|
-
}
|
|
2074
2462
|
}
|
|
2075
2463
|
|
|
2076
2464
|
// Copyright (c) Microsoft Corporation.
|
|
@@ -2421,6 +2809,10 @@ class TeamsBotInstallation {
|
|
|
2421
2809
|
* @returns an array of channels if bot is installed into a team, otherwise returns an empty array.
|
|
2422
2810
|
*/
|
|
2423
2811
|
async channels() {
|
|
2812
|
+
const channels = [];
|
|
2813
|
+
if (this.type !== NotificationTargetType.Channel) {
|
|
2814
|
+
return channels;
|
|
2815
|
+
}
|
|
2424
2816
|
let teamsChannels = [];
|
|
2425
2817
|
await this.adapter.continueConversation(this.conversationReference, async (context) => {
|
|
2426
2818
|
const teamId = getTeamsBotInstallationId(context);
|
|
@@ -2428,7 +2820,6 @@ class TeamsBotInstallation {
|
|
|
2428
2820
|
teamsChannels = await TeamsInfo.getTeamChannels(context, teamId);
|
|
2429
2821
|
}
|
|
2430
2822
|
});
|
|
2431
|
-
const channels = [];
|
|
2432
2823
|
for (const channel of teamsChannels) {
|
|
2433
2824
|
channels.push(new Channel(this, channel));
|
|
2434
2825
|
}
|
|
@@ -2453,6 +2844,24 @@ class TeamsBotInstallation {
|
|
|
2453
2844
|
});
|
|
2454
2845
|
return members;
|
|
2455
2846
|
}
|
|
2847
|
+
/**
|
|
2848
|
+
* Get team details from this bot installation
|
|
2849
|
+
*
|
|
2850
|
+
* @returns the team details if bot is installed into a team, otherwise returns undefined.
|
|
2851
|
+
*/
|
|
2852
|
+
async getTeamDetails() {
|
|
2853
|
+
if (this.type !== NotificationTargetType.Channel) {
|
|
2854
|
+
return undefined;
|
|
2855
|
+
}
|
|
2856
|
+
let teamDetails;
|
|
2857
|
+
await this.adapter.continueConversation(this.conversationReference, async (context) => {
|
|
2858
|
+
const teamId = getTeamsBotInstallationId(context);
|
|
2859
|
+
if (teamId !== undefined) {
|
|
2860
|
+
teamDetails = await TeamsInfo.getTeamDetails(context, teamId);
|
|
2861
|
+
}
|
|
2862
|
+
});
|
|
2863
|
+
return teamDetails;
|
|
2864
|
+
}
|
|
2456
2865
|
}
|
|
2457
2866
|
/**
|
|
2458
2867
|
* Provide utilities to send notification to varies targets (e.g., member, group, channel).
|
|
@@ -2512,6 +2921,459 @@ class NotificationBot {
|
|
|
2512
2921
|
}
|
|
2513
2922
|
return targets;
|
|
2514
2923
|
}
|
|
2924
|
+
/**
|
|
2925
|
+
* Returns the first {@link Member} where predicate is true, and undefined otherwise.
|
|
2926
|
+
*
|
|
2927
|
+
* @param predicate find calls predicate once for each member of the installation,
|
|
2928
|
+
* until it finds one where predicate returns true. If such a member is found, find
|
|
2929
|
+
* immediately returns that member. Otherwise, find returns undefined.
|
|
2930
|
+
* @param scope the scope to find members from the installations
|
|
2931
|
+
* (personal chat, group chat, Teams channel).
|
|
2932
|
+
* @returns the first {@link Member} where predicate is true, and undefined otherwise.
|
|
2933
|
+
*/
|
|
2934
|
+
async findMember(predicate, scope) {
|
|
2935
|
+
for (const target of await this.installations()) {
|
|
2936
|
+
if (this.matchSearchScope(target, scope)) {
|
|
2937
|
+
for (const member of await target.members()) {
|
|
2938
|
+
if (await predicate(member)) {
|
|
2939
|
+
return member;
|
|
2940
|
+
}
|
|
2941
|
+
}
|
|
2942
|
+
}
|
|
2943
|
+
}
|
|
2944
|
+
return;
|
|
2945
|
+
}
|
|
2946
|
+
/**
|
|
2947
|
+
* Returns the first {@link Channel} where predicate is true, and undefined otherwise.
|
|
2948
|
+
*
|
|
2949
|
+
* @param predicate find calls predicate once for each channel of the installation,
|
|
2950
|
+
* until it finds one where predicate returns true. If such a channel is found, find
|
|
2951
|
+
* immediately returns that channel. Otherwise, find returns undefined.
|
|
2952
|
+
* @returns the first {@link Channel} where predicate is true, and undefined otherwise.
|
|
2953
|
+
*/
|
|
2954
|
+
async findChannel(predicate) {
|
|
2955
|
+
for (const target of await this.installations()) {
|
|
2956
|
+
if (target.type === NotificationTargetType.Channel) {
|
|
2957
|
+
const teamDetails = await target.getTeamDetails();
|
|
2958
|
+
for (const channel of await target.channels()) {
|
|
2959
|
+
if (await predicate(channel, teamDetails)) {
|
|
2960
|
+
return channel;
|
|
2961
|
+
}
|
|
2962
|
+
}
|
|
2963
|
+
}
|
|
2964
|
+
}
|
|
2965
|
+
return;
|
|
2966
|
+
}
|
|
2967
|
+
/**
|
|
2968
|
+
* Returns all {@link Member} where predicate is true, and empty array otherwise.
|
|
2969
|
+
*
|
|
2970
|
+
* @param predicate find calls predicate for each member of the installation.
|
|
2971
|
+
* @param scope the scope to find members from the installations
|
|
2972
|
+
* (personal chat, group chat, Teams channel).
|
|
2973
|
+
* @returns an array of {@link Member} where predicate is true, and empty array otherwise.
|
|
2974
|
+
*/
|
|
2975
|
+
async findAllMembers(predicate, scope) {
|
|
2976
|
+
const members = [];
|
|
2977
|
+
for (const target of await this.installations()) {
|
|
2978
|
+
if (this.matchSearchScope(target, scope)) {
|
|
2979
|
+
for (const member of await target.members()) {
|
|
2980
|
+
if (await predicate(member)) {
|
|
2981
|
+
members.push(member);
|
|
2982
|
+
}
|
|
2983
|
+
}
|
|
2984
|
+
}
|
|
2985
|
+
}
|
|
2986
|
+
return members;
|
|
2987
|
+
}
|
|
2988
|
+
/**
|
|
2989
|
+
* Returns all {@link Channel} where predicate is true, and empty array otherwise.
|
|
2990
|
+
*
|
|
2991
|
+
* @param predicate find calls predicate for each channel of the installation.
|
|
2992
|
+
* @returns an array of {@link Channel} where predicate is true, and empty array otherwise.
|
|
2993
|
+
*/
|
|
2994
|
+
async findAllChannels(predicate) {
|
|
2995
|
+
const channels = [];
|
|
2996
|
+
for (const target of await this.installations()) {
|
|
2997
|
+
if (target.type === NotificationTargetType.Channel) {
|
|
2998
|
+
const teamDetails = await target.getTeamDetails();
|
|
2999
|
+
for (const channel of await target.channels()) {
|
|
3000
|
+
if (await predicate(channel, teamDetails)) {
|
|
3001
|
+
channels.push(channel);
|
|
3002
|
+
}
|
|
3003
|
+
}
|
|
3004
|
+
}
|
|
3005
|
+
}
|
|
3006
|
+
return channels;
|
|
3007
|
+
}
|
|
3008
|
+
matchSearchScope(target, scope) {
|
|
3009
|
+
scope = scope !== null && scope !== void 0 ? scope : SearchScope.All;
|
|
3010
|
+
return ((target.type === NotificationTargetType.Channel && (scope & SearchScope.Channel) !== 0) ||
|
|
3011
|
+
(target.type === NotificationTargetType.Group && (scope & SearchScope.Group) !== 0) ||
|
|
3012
|
+
(target.type === NotificationTargetType.Person && (scope & SearchScope.Person) !== 0));
|
|
3013
|
+
}
|
|
3014
|
+
}
|
|
3015
|
+
/**
|
|
3016
|
+
* The search scope when calling {@link NotificationBot.findMember} and {@link NotificationBot.findAllMembers}.
|
|
3017
|
+
* The search scope is a flagged enum and it can be combined with `|`.
|
|
3018
|
+
* For example, to search from personal chat and group chat, use `SearchScope.Person | SearchScope.Group`.
|
|
3019
|
+
*/
|
|
3020
|
+
var SearchScope;
|
|
3021
|
+
(function (SearchScope) {
|
|
3022
|
+
/**
|
|
3023
|
+
* Search members from the installations in personal chat only.
|
|
3024
|
+
*/
|
|
3025
|
+
SearchScope[SearchScope["Person"] = 1] = "Person";
|
|
3026
|
+
/**
|
|
3027
|
+
* Search members from the installations in group chat only.
|
|
3028
|
+
*/
|
|
3029
|
+
SearchScope[SearchScope["Group"] = 2] = "Group";
|
|
3030
|
+
/**
|
|
3031
|
+
* Search members from the installations in Teams channel only.
|
|
3032
|
+
*/
|
|
3033
|
+
SearchScope[SearchScope["Channel"] = 4] = "Channel";
|
|
3034
|
+
/**
|
|
3035
|
+
* Search members from all installations including personal chat, group chat and Teams channel.
|
|
3036
|
+
*/
|
|
3037
|
+
SearchScope[SearchScope["All"] = 7] = "All";
|
|
3038
|
+
})(SearchScope || (SearchScope = {}));
|
|
3039
|
+
|
|
3040
|
+
// Copyright (c) Microsoft Corporation.
|
|
3041
|
+
let DIALOG_NAME = "BotSsoExecutionDialog";
|
|
3042
|
+
let TEAMS_SSO_PROMPT_ID = "TeamsFxSsoPrompt";
|
|
3043
|
+
let COMMAND_ROUTE_DIALOG = "CommandRouteDialog";
|
|
3044
|
+
/**
|
|
3045
|
+
* Sso execution dialog, use to handle sso command
|
|
3046
|
+
*/
|
|
3047
|
+
class BotSsoExecutionDialog extends ComponentDialog {
|
|
3048
|
+
/**
|
|
3049
|
+
* Creates a new instance of the BotSsoExecutionDialog.
|
|
3050
|
+
* @param dedupStorage Helper storage to remove duplicated messages
|
|
3051
|
+
* @param settings The list of scopes for which the token will have access
|
|
3052
|
+
* @param teamsfx {@link TeamsFx} instance for authentication
|
|
3053
|
+
*/
|
|
3054
|
+
constructor(dedupStorage, ssoPromptSettings, teamsfx, dialogName) {
|
|
3055
|
+
super(dialogName !== null && dialogName !== void 0 ? dialogName : DIALOG_NAME);
|
|
3056
|
+
this.dedupStorageKeys = [];
|
|
3057
|
+
// Map to store the commandId and triggerPatterns, key: commandId, value: triggerPatterns
|
|
3058
|
+
this.commandMapping = new Map();
|
|
3059
|
+
if (dialogName) {
|
|
3060
|
+
DIALOG_NAME = dialogName;
|
|
3061
|
+
TEAMS_SSO_PROMPT_ID = dialogName + TEAMS_SSO_PROMPT_ID;
|
|
3062
|
+
COMMAND_ROUTE_DIALOG = dialogName + COMMAND_ROUTE_DIALOG;
|
|
3063
|
+
}
|
|
3064
|
+
this.initialDialogId = COMMAND_ROUTE_DIALOG;
|
|
3065
|
+
this.dedupStorage = dedupStorage;
|
|
3066
|
+
this.dedupStorageKeys = [];
|
|
3067
|
+
const ssoDialog = new TeamsBotSsoPrompt(teamsfx, TEAMS_SSO_PROMPT_ID, ssoPromptSettings);
|
|
3068
|
+
this.addDialog(ssoDialog);
|
|
3069
|
+
const commandRouteDialog = new WaterfallDialog(COMMAND_ROUTE_DIALOG, [
|
|
3070
|
+
this.commandRouteStep.bind(this),
|
|
3071
|
+
]);
|
|
3072
|
+
this.addDialog(commandRouteDialog);
|
|
3073
|
+
}
|
|
3074
|
+
/**
|
|
3075
|
+
* Add TeamsFxBotSsoCommandHandler instance
|
|
3076
|
+
* @param handler {@link BotSsoExecutionDialogHandler} callback function
|
|
3077
|
+
* @param triggerPatterns The trigger pattern
|
|
3078
|
+
*/
|
|
3079
|
+
addCommand(handler, triggerPatterns) {
|
|
3080
|
+
const commandId = this.getCommandHash(triggerPatterns);
|
|
3081
|
+
const dialog = new WaterfallDialog(commandId, [
|
|
3082
|
+
this.ssoStep.bind(this),
|
|
3083
|
+
this.dedupStep.bind(this),
|
|
3084
|
+
async (stepContext) => {
|
|
3085
|
+
const tokenResponse = stepContext.result.tokenResponse;
|
|
3086
|
+
const context = stepContext.context;
|
|
3087
|
+
const message = stepContext.result.message;
|
|
3088
|
+
try {
|
|
3089
|
+
if (tokenResponse) {
|
|
3090
|
+
await handler(context, tokenResponse, message);
|
|
3091
|
+
}
|
|
3092
|
+
else {
|
|
3093
|
+
throw new Error(ErrorMessage.FailedToRetrieveSsoToken);
|
|
3094
|
+
}
|
|
3095
|
+
return await stepContext.endDialog();
|
|
3096
|
+
}
|
|
3097
|
+
catch (error) {
|
|
3098
|
+
const errorMsg = formatString(ErrorMessage.FailedToProcessSsoHandler, error.message);
|
|
3099
|
+
internalLogger.error(errorMsg);
|
|
3100
|
+
return await stepContext.endDialog(new ErrorWithCode(errorMsg, ErrorCode.FailedToProcessSsoHandler));
|
|
3101
|
+
}
|
|
3102
|
+
},
|
|
3103
|
+
]);
|
|
3104
|
+
this.commandMapping.set(commandId, triggerPatterns);
|
|
3105
|
+
this.addDialog(dialog);
|
|
3106
|
+
}
|
|
3107
|
+
getCommandHash(patterns) {
|
|
3108
|
+
const expressions = Array.isArray(patterns) ? patterns : [patterns];
|
|
3109
|
+
const patternStr = expressions.join();
|
|
3110
|
+
const patternStrWithoutSpecialChar = patternStr.replace(/[^a-zA-Z0-9]/g, "");
|
|
3111
|
+
const hash = createHash("sha256").update(patternStr).digest("hex").toLowerCase();
|
|
3112
|
+
return patternStrWithoutSpecialChar + hash;
|
|
3113
|
+
}
|
|
3114
|
+
/**
|
|
3115
|
+
* The run method handles the incoming activity (in the form of a DialogContext) and passes it through the dialog system.
|
|
3116
|
+
*
|
|
3117
|
+
* @param context The context object for the current turn.
|
|
3118
|
+
* @param accessor The instance of StatePropertyAccessor for dialog system.
|
|
3119
|
+
*/
|
|
3120
|
+
async run(context, accessor) {
|
|
3121
|
+
const dialogSet = new DialogSet(accessor);
|
|
3122
|
+
dialogSet.add(this);
|
|
3123
|
+
const dialogContext = await dialogSet.createContext(context);
|
|
3124
|
+
this.ensureMsTeamsChannel(dialogContext);
|
|
3125
|
+
const results = await dialogContext.continueDialog();
|
|
3126
|
+
if (results && results.status === DialogTurnStatus.empty) {
|
|
3127
|
+
await dialogContext.beginDialog(this.id);
|
|
3128
|
+
}
|
|
3129
|
+
else if (results &&
|
|
3130
|
+
results.status === DialogTurnStatus.complete &&
|
|
3131
|
+
results.result instanceof Error) {
|
|
3132
|
+
throw results.result;
|
|
3133
|
+
}
|
|
3134
|
+
}
|
|
3135
|
+
getActivityText(activity) {
|
|
3136
|
+
let text = activity.text;
|
|
3137
|
+
const removedMentionText = TurnContext.removeRecipientMention(activity);
|
|
3138
|
+
if (removedMentionText) {
|
|
3139
|
+
text = removedMentionText
|
|
3140
|
+
.toLowerCase()
|
|
3141
|
+
.replace(/\n|\r\n/g, "")
|
|
3142
|
+
.trim();
|
|
3143
|
+
}
|
|
3144
|
+
return text;
|
|
3145
|
+
}
|
|
3146
|
+
async commandRouteStep(stepContext) {
|
|
3147
|
+
const turnContext = stepContext.context;
|
|
3148
|
+
const text = this.getActivityText(turnContext.activity);
|
|
3149
|
+
const commandId = this.getMatchesCommandId(text);
|
|
3150
|
+
if (commandId) {
|
|
3151
|
+
return await stepContext.beginDialog(commandId);
|
|
3152
|
+
}
|
|
3153
|
+
const errorMsg = formatString(ErrorMessage.CannotFindCommand, turnContext.activity.text);
|
|
3154
|
+
internalLogger.error(errorMsg);
|
|
3155
|
+
throw new ErrorWithCode(errorMsg, ErrorCode.CannotFindCommand);
|
|
3156
|
+
}
|
|
3157
|
+
async ssoStep(stepContext) {
|
|
3158
|
+
try {
|
|
3159
|
+
const turnContext = stepContext.context;
|
|
3160
|
+
const text = this.getActivityText(turnContext.activity);
|
|
3161
|
+
const message = {
|
|
3162
|
+
text,
|
|
3163
|
+
};
|
|
3164
|
+
stepContext.options.commandMessage = message;
|
|
3165
|
+
return await stepContext.beginDialog(TEAMS_SSO_PROMPT_ID);
|
|
3166
|
+
}
|
|
3167
|
+
catch (error) {
|
|
3168
|
+
const errorMsg = formatString(ErrorMessage.FailedToRunSsoStep, error.message);
|
|
3169
|
+
internalLogger.error(errorMsg);
|
|
3170
|
+
return await stepContext.endDialog(new ErrorWithCode(errorMsg, ErrorCode.FailedToRunSsoStep));
|
|
3171
|
+
}
|
|
3172
|
+
}
|
|
3173
|
+
async dedupStep(stepContext) {
|
|
3174
|
+
const tokenResponse = stepContext.result;
|
|
3175
|
+
if (!tokenResponse) {
|
|
3176
|
+
internalLogger.error(ErrorMessage.FailedToRetrieveSsoToken);
|
|
3177
|
+
return await stepContext.endDialog(new ErrorWithCode(ErrorMessage.FailedToRetrieveSsoToken, ErrorCode.FailedToRunSsoStep));
|
|
3178
|
+
}
|
|
3179
|
+
try {
|
|
3180
|
+
// Only dedup after ssoStep to make sure that all Teams client would receive the login request
|
|
3181
|
+
if (tokenResponse && (await this.shouldDedup(stepContext.context))) {
|
|
3182
|
+
return Dialog.EndOfTurn;
|
|
3183
|
+
}
|
|
3184
|
+
return await stepContext.next({
|
|
3185
|
+
tokenResponse,
|
|
3186
|
+
message: stepContext.options.commandMessage,
|
|
3187
|
+
});
|
|
3188
|
+
}
|
|
3189
|
+
catch (error) {
|
|
3190
|
+
const errorMsg = formatString(ErrorMessage.FailedToRunDedupStep, error.message);
|
|
3191
|
+
internalLogger.error(errorMsg);
|
|
3192
|
+
return await stepContext.endDialog(new ErrorWithCode(errorMsg, ErrorCode.FailedToRunDedupStep));
|
|
3193
|
+
}
|
|
3194
|
+
}
|
|
3195
|
+
/**
|
|
3196
|
+
* Called when the component is ending.
|
|
3197
|
+
*
|
|
3198
|
+
* @param context Context for the current turn of conversation.
|
|
3199
|
+
*/
|
|
3200
|
+
async onEndDialog(context) {
|
|
3201
|
+
const conversationId = context.activity.conversation.id;
|
|
3202
|
+
const currentDedupKeys = this.dedupStorageKeys.filter((key) => key.indexOf(conversationId) > 0);
|
|
3203
|
+
await this.dedupStorage.delete(currentDedupKeys);
|
|
3204
|
+
this.dedupStorageKeys = this.dedupStorageKeys.filter((key) => key.indexOf(conversationId) < 0);
|
|
3205
|
+
}
|
|
3206
|
+
/**
|
|
3207
|
+
* If a user is signed into multiple Teams clients, the Bot might receive a "signin/tokenExchange" from each client.
|
|
3208
|
+
* Each token exchange request for a specific user login will have an identical activity.value.Id.
|
|
3209
|
+
* Only one of these token exchange requests should be processed by the bot. For a distributed bot in production,
|
|
3210
|
+
* this requires a distributed storage to ensure only one token exchange is processed.
|
|
3211
|
+
* @param context Context for the current turn of conversation.
|
|
3212
|
+
* @returns boolean value indicate whether the message should be removed
|
|
3213
|
+
*/
|
|
3214
|
+
async shouldDedup(context) {
|
|
3215
|
+
const storeItem = {
|
|
3216
|
+
eTag: context.activity.value.id,
|
|
3217
|
+
};
|
|
3218
|
+
const key = this.getStorageKey(context);
|
|
3219
|
+
const storeItems = { [key]: storeItem };
|
|
3220
|
+
try {
|
|
3221
|
+
await this.dedupStorage.write(storeItems);
|
|
3222
|
+
this.dedupStorageKeys.push(key);
|
|
3223
|
+
}
|
|
3224
|
+
catch (err) {
|
|
3225
|
+
if (err instanceof Error && err.message.indexOf("eTag conflict")) {
|
|
3226
|
+
return true;
|
|
3227
|
+
}
|
|
3228
|
+
throw err;
|
|
3229
|
+
}
|
|
3230
|
+
return false;
|
|
3231
|
+
}
|
|
3232
|
+
getStorageKey(context) {
|
|
3233
|
+
if (!context || !context.activity || !context.activity.conversation) {
|
|
3234
|
+
throw new Error("Invalid context, can not get storage key!");
|
|
3235
|
+
}
|
|
3236
|
+
const activity = context.activity;
|
|
3237
|
+
const channelId = activity.channelId;
|
|
3238
|
+
const conversationId = activity.conversation.id;
|
|
3239
|
+
if (activity.type !== ActivityTypes.Invoke || activity.name !== tokenExchangeOperationName) {
|
|
3240
|
+
throw new Error("TokenExchangeState can only be used with Invokes of signin/tokenExchange.");
|
|
3241
|
+
}
|
|
3242
|
+
const value = activity.value;
|
|
3243
|
+
if (!value || !value.id) {
|
|
3244
|
+
throw new Error("Invalid signin/tokenExchange. Missing activity.value.id.");
|
|
3245
|
+
}
|
|
3246
|
+
return `${channelId}/${conversationId}/${value.id}`;
|
|
3247
|
+
}
|
|
3248
|
+
matchPattern(pattern, text) {
|
|
3249
|
+
if (text) {
|
|
3250
|
+
if (typeof pattern === "string") {
|
|
3251
|
+
const regExp = new RegExp(pattern, "i");
|
|
3252
|
+
return regExp.test(text);
|
|
3253
|
+
}
|
|
3254
|
+
if (pattern instanceof RegExp) {
|
|
3255
|
+
const matches = text.match(pattern);
|
|
3256
|
+
return matches !== null && matches !== void 0 ? matches : false;
|
|
3257
|
+
}
|
|
3258
|
+
}
|
|
3259
|
+
return false;
|
|
3260
|
+
}
|
|
3261
|
+
isPatternMatched(patterns, text) {
|
|
3262
|
+
const expressions = Array.isArray(patterns) ? patterns : [patterns];
|
|
3263
|
+
for (const ex of expressions) {
|
|
3264
|
+
const matches = this.matchPattern(ex, text);
|
|
3265
|
+
return !!matches;
|
|
3266
|
+
}
|
|
3267
|
+
return false;
|
|
3268
|
+
}
|
|
3269
|
+
getMatchesCommandId(text) {
|
|
3270
|
+
for (const command of this.commandMapping) {
|
|
3271
|
+
const pattern = command[1];
|
|
3272
|
+
if (this.isPatternMatched(pattern, text)) {
|
|
3273
|
+
return command[0];
|
|
3274
|
+
}
|
|
3275
|
+
}
|
|
3276
|
+
return undefined;
|
|
3277
|
+
}
|
|
3278
|
+
/**
|
|
3279
|
+
* Ensure bot is running in MS Teams since TeamsBotSsoPrompt is only supported in MS Teams channel.
|
|
3280
|
+
* @param dc dialog context
|
|
3281
|
+
* @throws {@link ErrorCode|ChannelNotSupported} if bot channel is not MS Teams
|
|
3282
|
+
* @internal
|
|
3283
|
+
*/
|
|
3284
|
+
ensureMsTeamsChannel(dc) {
|
|
3285
|
+
if (dc.context.activity.channelId != Channels.Msteams) {
|
|
3286
|
+
const errorMsg = formatString(ErrorMessage.OnlyMSTeamsChannelSupported, "SSO execution dialog");
|
|
3287
|
+
internalLogger.error(errorMsg);
|
|
3288
|
+
throw new ErrorWithCode(errorMsg, ErrorCode.ChannelNotSupported);
|
|
3289
|
+
}
|
|
3290
|
+
}
|
|
3291
|
+
}
|
|
3292
|
+
|
|
3293
|
+
// Copyright (c) Microsoft Corporation.
|
|
3294
|
+
/**
|
|
3295
|
+
* Default SSO execution activity handler
|
|
3296
|
+
*/
|
|
3297
|
+
class DefaultBotSsoExecutionActivityHandler extends TeamsActivityHandler {
|
|
3298
|
+
/**
|
|
3299
|
+
* Creates a new instance of the DefaultBotSsoExecutionActivityHandler.
|
|
3300
|
+
* @param ssoConfig configuration for SSO command bot
|
|
3301
|
+
*
|
|
3302
|
+
* @remarks
|
|
3303
|
+
* In the constructor, it uses BotSsoConfig parameter which from {@link ConversationBot} options to initialize {@link BotSsoExecutionDialog}.
|
|
3304
|
+
* It also need to register an event handler for the message event which trigger {@link BotSsoExecutionDialog} instance.
|
|
3305
|
+
*/
|
|
3306
|
+
constructor(ssoConfig) {
|
|
3307
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
|
|
3308
|
+
super();
|
|
3309
|
+
const memoryStorage = new MemoryStorage();
|
|
3310
|
+
const userState = (_b = (_a = ssoConfig.dialog) === null || _a === void 0 ? void 0 : _a.userState) !== null && _b !== void 0 ? _b : new UserState(memoryStorage);
|
|
3311
|
+
const conversationState = (_d = (_c = ssoConfig.dialog) === null || _c === void 0 ? void 0 : _c.conversationState) !== null && _d !== void 0 ? _d : new ConversationState(memoryStorage);
|
|
3312
|
+
const dedupStorage = (_f = (_e = ssoConfig.dialog) === null || _e === void 0 ? void 0 : _e.dedupStorage) !== null && _f !== void 0 ? _f : memoryStorage;
|
|
3313
|
+
const _l = ssoConfig.aad, { scopes } = _l, customConfig = __rest(_l, ["scopes"]);
|
|
3314
|
+
const settings = {
|
|
3315
|
+
scopes: scopes,
|
|
3316
|
+
timeout: (_h = (_g = ssoConfig.dialog) === null || _g === void 0 ? void 0 : _g.ssoPromptConfig) === null || _h === void 0 ? void 0 : _h.timeout,
|
|
3317
|
+
endOnInvalidMessage: (_k = (_j = ssoConfig.dialog) === null || _j === void 0 ? void 0 : _j.ssoPromptConfig) === null || _k === void 0 ? void 0 : _k.endOnInvalidMessage,
|
|
3318
|
+
};
|
|
3319
|
+
const teamsfx = new TeamsFx(IdentityType.User, Object.assign({}, customConfig));
|
|
3320
|
+
this.ssoExecutionDialog = new BotSsoExecutionDialog(dedupStorage, settings, teamsfx);
|
|
3321
|
+
this.conversationState = conversationState;
|
|
3322
|
+
this.dialogState = conversationState.createProperty("DialogState");
|
|
3323
|
+
this.userState = userState;
|
|
3324
|
+
this.onMessage(async (context, next) => {
|
|
3325
|
+
await this.ssoExecutionDialog.run(context, this.dialogState);
|
|
3326
|
+
await next();
|
|
3327
|
+
});
|
|
3328
|
+
}
|
|
3329
|
+
/**
|
|
3330
|
+
* Add TeamsFxBotSsoCommandHandler instance to SSO execution dialog
|
|
3331
|
+
* @param handler {@link BotSsoExecutionDialogHandler} callback function
|
|
3332
|
+
* @param triggerPatterns The trigger pattern
|
|
3333
|
+
*
|
|
3334
|
+
* @remarks
|
|
3335
|
+
* This function is used to add SSO command to {@link BotSsoExecutionDialog} instance.
|
|
3336
|
+
*/
|
|
3337
|
+
addCommand(handler, triggerPatterns) {
|
|
3338
|
+
this.ssoExecutionDialog.addCommand(handler, triggerPatterns);
|
|
3339
|
+
}
|
|
3340
|
+
/**
|
|
3341
|
+
* Called to initiate the event emission process.
|
|
3342
|
+
* @param context The context object for the current turn.
|
|
3343
|
+
*/
|
|
3344
|
+
async run(context) {
|
|
3345
|
+
try {
|
|
3346
|
+
await super.run(context);
|
|
3347
|
+
}
|
|
3348
|
+
finally {
|
|
3349
|
+
await this.conversationState.saveChanges(context, false);
|
|
3350
|
+
await this.userState.saveChanges(context, false);
|
|
3351
|
+
}
|
|
3352
|
+
}
|
|
3353
|
+
/**
|
|
3354
|
+
* Receives invoke activities with Activity name of 'signin/verifyState'.
|
|
3355
|
+
* @param context A context object for this turn.
|
|
3356
|
+
* @param query Signin state (part of signin action auth flow) verification invoke query.
|
|
3357
|
+
* @returns A promise that represents the work queued.
|
|
3358
|
+
*
|
|
3359
|
+
* @remarks
|
|
3360
|
+
* It should trigger {@link BotSsoExecutionDialog} instance to handle signin process
|
|
3361
|
+
*/
|
|
3362
|
+
async handleTeamsSigninVerifyState(context, query) {
|
|
3363
|
+
await this.ssoExecutionDialog.run(context, this.dialogState);
|
|
3364
|
+
}
|
|
3365
|
+
/**
|
|
3366
|
+
* Receives invoke activities with Activity name of 'signin/tokenExchange'
|
|
3367
|
+
* @param context A context object for this turn.
|
|
3368
|
+
* @param query Signin state (part of signin action auth flow) verification invoke query
|
|
3369
|
+
* @returns A promise that represents the work queued.
|
|
3370
|
+
*
|
|
3371
|
+
* @remark
|
|
3372
|
+
* It should trigger {@link BotSsoExecutionDialog} instance to handle signin process
|
|
3373
|
+
*/
|
|
3374
|
+
async handleTeamsSigninTokenExchange(context, query) {
|
|
3375
|
+
await this.ssoExecutionDialog.run(context, this.dialogState);
|
|
3376
|
+
}
|
|
2515
3377
|
}
|
|
2516
3378
|
|
|
2517
3379
|
// Copyright (c) Microsoft Corporation.
|
|
@@ -2576,19 +3438,32 @@ class ConversationBot {
|
|
|
2576
3438
|
* @param options - initialize options
|
|
2577
3439
|
*/
|
|
2578
3440
|
constructor(options) {
|
|
2579
|
-
var _a, _b;
|
|
3441
|
+
var _a, _b, _c, _d;
|
|
2580
3442
|
if (options.adapter) {
|
|
2581
3443
|
this.adapter = options.adapter;
|
|
2582
3444
|
}
|
|
2583
3445
|
else {
|
|
2584
3446
|
this.adapter = this.createDefaultAdapter(options.adapterConfig);
|
|
2585
3447
|
}
|
|
2586
|
-
|
|
2587
|
-
|
|
3448
|
+
let ssoCommandActivityHandler;
|
|
3449
|
+
if (options === null || options === void 0 ? void 0 : options.ssoConfig) {
|
|
3450
|
+
if ((_a = options.ssoConfig.dialog) === null || _a === void 0 ? void 0 : _a.CustomBotSsoExecutionActivityHandler) {
|
|
3451
|
+
ssoCommandActivityHandler =
|
|
3452
|
+
new options.ssoConfig.dialog.CustomBotSsoExecutionActivityHandler(options.ssoConfig);
|
|
3453
|
+
}
|
|
3454
|
+
else {
|
|
3455
|
+
ssoCommandActivityHandler = new DefaultBotSsoExecutionActivityHandler(options.ssoConfig);
|
|
3456
|
+
}
|
|
3457
|
+
}
|
|
3458
|
+
if ((_b = options.command) === null || _b === void 0 ? void 0 : _b.enabled) {
|
|
3459
|
+
this.command = new CommandBot(this.adapter, options.command, ssoCommandActivityHandler, options.ssoConfig);
|
|
2588
3460
|
}
|
|
2589
|
-
if ((
|
|
3461
|
+
if ((_c = options.notification) === null || _c === void 0 ? void 0 : _c.enabled) {
|
|
2590
3462
|
this.notification = new NotificationBot(this.adapter, options.notification);
|
|
2591
3463
|
}
|
|
3464
|
+
if ((_d = options.cardAction) === null || _d === void 0 ? void 0 : _d.enabled) {
|
|
3465
|
+
this.cardAction = new CardActionBot(this.adapter, options.cardAction);
|
|
3466
|
+
}
|
|
2592
3467
|
}
|
|
2593
3468
|
createDefaultAdapter(adapterConfig) {
|
|
2594
3469
|
const adapter = adapterConfig === undefined
|
|
@@ -2775,5 +3650,105 @@ class MessageBuilder {
|
|
|
2775
3650
|
}
|
|
2776
3651
|
}
|
|
2777
3652
|
|
|
2778
|
-
|
|
3653
|
+
// Copyright (c) Microsoft Corporation.
|
|
3654
|
+
/**
|
|
3655
|
+
* Retrieve the OAuth Sign in Link to use in the MessagingExtensionResult Suggested Actions.
|
|
3656
|
+
* This method only work on MessageExtension with Query now.
|
|
3657
|
+
*
|
|
3658
|
+
* @param {TeamsFx} teamsfx - Used to provide configuration and auth.
|
|
3659
|
+
* @param {string | string[]} scopes - The list of scopes for which the token will have access.
|
|
3660
|
+
*
|
|
3661
|
+
* @returns SignIn link CardAction with 200 status code.
|
|
3662
|
+
*/
|
|
3663
|
+
function getSignInResponseForMessageExtension(teamsfx, scopes) {
|
|
3664
|
+
const scopesArray = getScopesArray(scopes);
|
|
3665
|
+
const signInLink = `${teamsfx.getConfig("initiateLoginEndpoint")}?scope=${encodeURI(scopesArray.join(" "))}&clientId=${teamsfx.getConfig("clientId")}&tenantId=${teamsfx.getConfig("tenantId")}`;
|
|
3666
|
+
return {
|
|
3667
|
+
composeExtension: {
|
|
3668
|
+
type: "silentAuth",
|
|
3669
|
+
suggestedActions: {
|
|
3670
|
+
actions: [
|
|
3671
|
+
{
|
|
3672
|
+
type: "openUrl",
|
|
3673
|
+
value: signInLink,
|
|
3674
|
+
title: "Message Extension OAuth",
|
|
3675
|
+
},
|
|
3676
|
+
],
|
|
3677
|
+
},
|
|
3678
|
+
},
|
|
3679
|
+
};
|
|
3680
|
+
}
|
|
3681
|
+
/**
|
|
3682
|
+
* execution in message extension with SSO token.
|
|
3683
|
+
*
|
|
3684
|
+
* @param {TurnContext} context - The context object for the current turn.
|
|
3685
|
+
* @param {AuthenticationConfiguration} config - User custom the message extension authentication configuration.
|
|
3686
|
+
* @param {string[]} scopes - The list of scopes for which the token will have access.
|
|
3687
|
+
* @param {function} logic - Business logic when executing the query in message extension with SSO or access token.
|
|
3688
|
+
*
|
|
3689
|
+
* @throws {@link ErrorCode|InternalError} when failed to get access token with unknown error.
|
|
3690
|
+
* @throws {@link ErrorCode|TokenExpiredError} when SSO token has already expired.
|
|
3691
|
+
* @throws {@link ErrorCode|ServiceError} when failed to get access token from simple auth server.
|
|
3692
|
+
* @throws {@link ErrorCode|InvalidParameter} when scopes is not a valid string or string array.
|
|
3693
|
+
* @throws {@link ErrorCode|RuntimeNotSupported} when runtime is nodeJS.
|
|
3694
|
+
*
|
|
3695
|
+
* @returns A MessageExtension Response for the activity. If the logic not return any, return void instead.
|
|
3696
|
+
*/
|
|
3697
|
+
async function executionWithToken(context, config, scopes, logic) {
|
|
3698
|
+
const valueObj = context.activity.value;
|
|
3699
|
+
if (!valueObj.authentication || !valueObj.authentication.token) {
|
|
3700
|
+
internalLogger.verbose("No AccessToken in request, return silentAuth for AccessToken");
|
|
3701
|
+
return getSignInResponseForMessageExtension(new TeamsFx(IdentityType.User, config), scopes);
|
|
3702
|
+
}
|
|
3703
|
+
try {
|
|
3704
|
+
const teamsfx = new TeamsFx(IdentityType.User, config).setSsoToken(valueObj.authentication.token);
|
|
3705
|
+
const token = await teamsfx.getCredential().getToken(scopes);
|
|
3706
|
+
const ssoTokenExpiration = parseJwt(valueObj.authentication.token).exp;
|
|
3707
|
+
const tokenRes = {
|
|
3708
|
+
ssoToken: valueObj.authentication.token,
|
|
3709
|
+
ssoTokenExpiration: new Date(ssoTokenExpiration * 1000).toISOString(),
|
|
3710
|
+
token: token.token,
|
|
3711
|
+
expiration: token.expiresOnTimestamp.toString(),
|
|
3712
|
+
connectionName: "",
|
|
3713
|
+
};
|
|
3714
|
+
if (logic) {
|
|
3715
|
+
return await logic(tokenRes);
|
|
3716
|
+
}
|
|
3717
|
+
}
|
|
3718
|
+
catch (err) {
|
|
3719
|
+
if (err instanceof ErrorWithCode && err.code === ErrorCode.UiRequiredError) {
|
|
3720
|
+
internalLogger.verbose("User not consent yet, return 412 to user consent first.");
|
|
3721
|
+
const response = { status: 412 };
|
|
3722
|
+
await context.sendActivity({ value: response, type: ActivityTypes.InvokeResponse });
|
|
3723
|
+
return;
|
|
3724
|
+
}
|
|
3725
|
+
throw err;
|
|
3726
|
+
}
|
|
3727
|
+
}
|
|
3728
|
+
/**
|
|
3729
|
+
* Users execute query in message extension with SSO or access token.
|
|
3730
|
+
*
|
|
3731
|
+
* @param {TurnContext} context - The context object for the current turn.
|
|
3732
|
+
* @param {AuthenticationConfiguration} config - User custom the message extension authentication configuration.
|
|
3733
|
+
* @param {string| string[]} scopes - The list of scopes for which the token will have access.
|
|
3734
|
+
* @param {function} logic - Business logic when executing the query in message extension with SSO or access token.
|
|
3735
|
+
*
|
|
3736
|
+
* @throws {@link ErrorCode|InternalError} when User invoke not response to message extension query.
|
|
3737
|
+
* @throws {@link ErrorCode|InternalError} when failed to get access token with unknown error.
|
|
3738
|
+
* @throws {@link ErrorCode|TokenExpiredError} when SSO token has already expired.
|
|
3739
|
+
* @throws {@link ErrorCode|ServiceError} when failed to get access token from simple auth server.
|
|
3740
|
+
* @throws {@link ErrorCode|InvalidParameter} when scopes is not a valid string or string array.
|
|
3741
|
+
* @throws {@link ErrorCode|RuntimeNotSupported} when runtime is nodeJS.
|
|
3742
|
+
*
|
|
3743
|
+
* @returns A MessageExtension Response for the activity. If the logic not return any, return void instead.
|
|
3744
|
+
*/
|
|
3745
|
+
async function handleMessageExtensionQueryWithToken(context, config, scopes, logic) {
|
|
3746
|
+
if (context.activity.name != "composeExtension/query") {
|
|
3747
|
+
internalLogger.error(ErrorMessage.OnlySupportInQueryActivity);
|
|
3748
|
+
throw new ErrorWithCode(formatString(ErrorMessage.OnlySupportInQueryActivity), ErrorCode.FailedOperation);
|
|
3749
|
+
}
|
|
3750
|
+
return await executionWithToken(context, config !== null && config !== void 0 ? config : {}, scopes, logic);
|
|
3751
|
+
}
|
|
3752
|
+
|
|
3753
|
+
export { AdaptiveCardResponse, ApiKeyLocation, ApiKeyProvider, AppCredential, BasicAuthProvider, BearerTokenAuthProvider, BotSsoExecutionDialog, CardActionBot, CertificateAuthProvider, Channel, CommandBot, ConversationBot, ErrorCode, ErrorWithCode, IdentityType, InvokeResponseErrorCode, InvokeResponseFactory, LogLevel, Member, MessageBuilder, MsGraphAuthProvider, NotificationBot, NotificationTargetType, OnBehalfOfUserCredential, SearchScope, TeamsBotInstallation, TeamsBotSsoPrompt, TeamsFx, TeamsUserCredential, createApiClient, createMicrosoftGraphClient, createPemCertOption, createPfxCertOption, getLogLevel, getTediousConnectionConfig, handleMessageExtensionQueryWithToken, sendAdaptiveCard, sendMessage, setLogFunction, setLogLevel, setLogger };
|
|
2779
3754
|
//# sourceMappingURL=index.esm2017.mjs.map
|