This repository was archived by the owner on Nov 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathBot.ts
More file actions
240 lines (205 loc) · 11.8 KB
/
Bot.ts
File metadata and controls
240 lines (205 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import * as builder from "botbuilder";
import { RootDialog } from "./dialogs/RootDialog";
import { SetLocaleFromTeamsSetting } from "./middleware/SetLocaleFromTeamsSetting";
import { AdaptiveCardSubmitActionHandler } from "./middleware/AdaptiveCardSubmitActionHandler";
import { StripBotAtMentions } from "./middleware/StripBotAtMentions";
import { RestrictIncomingMessagesToTenants } from "./middleware/RestrictIncomingMessagesToTenants";
import { LoadBotChannelData } from "./middleware/LoadBotChannelData";
import { SimulateResetBotChat } from "./middleware/SimulateResetBotChat";
import { Strings } from "./locale/locale";
import { loadSessionAsync } from "./utils/DialogUtils";
import * as teams from "botbuilder-teams";
import { ComposeExtensionHandlers } from "./composeExtension/ComposeExtensionHandlers";
import {fetchTemplates, cardTemplates} from "./dialogs/Templates/CardTemplates";
import {renderACAttachment} from "./utils/CardUtils";
// =========================================================
// Bot Setup
// =========================================================
export class Bot extends builder.UniversalBot {
constructor(
private _connector: teams.TeamsChatConnector,
private botSettings: any,
) {
super(_connector, botSettings);
this.set("persistConversationData", true);
// Root dialog
new RootDialog(this).createChildDialogs();
// Add middleware
this.use(
// currently this middleware cannot be used because there is an error using it
// with updating messages examples
// builder.Middleware.sendTyping(),
// set on "receive" of incoming payload
new SetLocaleFromTeamsSetting(),
new AdaptiveCardSubmitActionHandler(),
// set on "botbuilder" (after session created)
new SimulateResetBotChat(this), // We recommend having this only in non-prod environments, for testing your 1:1 first-run experience
new StripBotAtMentions(),
new RestrictIncomingMessagesToTenants(),
new LoadBotChannelData(this.get("channelStorage")),
);
// setup invoke payload handler
this._connector.onInvoke(this.getInvokeHandler(this));
// setup O365ConnectorCard action handler
this._connector.onO365ConnectorCardAction(this.getO365ConnectorCardActionHandler(this));
// setup conversation update handler for things such as a memberAdded event
this.on("conversationUpdate", this.getConversationUpdateHandler(this));
// setup message reaction handler for like and remove like message
this.on("messageReaction", (event: builder.IMessageUpdate) => {
this.handleMessageReaction(event);
});
// setup popup signin incoming request
this._connector.onSigninStateVerification((event, query, callback) => {
this.verifySigninState(event, query, callback);
});
// setup compose extension handlers
// onQuery is for events that come through the compose extension itself including
// config and auth responses from popups that were started in the compose extension
// onQuerySettingsUrl is only used when the user selects "Settings" from the three dot option
// next to the compose extension's name on the list of compose extensions
// onSettingsUpdate is only used for the response from the popup created by the
// onQuerySettingsUrl event
this._connector.onQuery("search123", ComposeExtensionHandlers.getOnQueryHandler(this));
this._connector.onQuerySettingsUrl(ComposeExtensionHandlers.getOnQuerySettingsUrlHandler());
this._connector.onSettingsUpdate(ComposeExtensionHandlers.getOnSettingsUpdateHandler(this));
}
// Handle incoming invoke
private getInvokeHandler(bot: builder.UniversalBot): (event: builder.IEvent, callback: (err: Error, body: any, status?: number) => void) => void {
return async function (
event: builder.IEvent,
callback: (err: Error, body: any, status?: number) => void,
): Promise<void>
{
let session = await loadSessionAsync(bot, event);
if (session) {
// Clear the stack on invoke, as many builtin dialogs don't play well with invoke
// Invoke messages should carry the necessary information to perform their action
session.clearDialogStack();
let payload = (event as any).value;
let invokeType = (event as any).name;
// Invokes don't participate in middleware
// If payload has an address, then it is from a button to update a message so we do not what to send typing
if (!payload.address) {
session.sendTyping();
}
if (payload && payload.dialog) {
session.beginDialog(payload.dialog, payload);
}
switch (invokeType) {
case "task/fetch":
let taskModule = payload.data.taskModule.toLowerCase();
if (fetchTemplates[taskModule] !== undefined) {
// Return the specified task module response to the bot
callback(null, fetchTemplates[taskModule], 200);
}
else {
callback(new Error(`Error: task module template for ${(payload.taskModule === undefined ? "<undefined>" : payload.taskModule)} not found.`), null, 500);
}
break;
case "task/submit":
if (payload.data !== undefined) {
switch (payload.data.taskResponse) {
case "message":
// Echo the results to the chat stream
session.send("**task/submit results from the Adaptive card:**\n```" + JSON.stringify(payload) + "```");
break;
case "continue":
let fetchResponse = fetchTemplates.submitResponse;
fetchResponse.task.value.card = renderACAttachment(cardTemplates.adaptiveCardSubmitResponse, { results: JSON.stringify(payload.data) });
callback(null, fetchResponse, 200);
break;
case "final":
// do nothing
default:
if (payload.data.levelType !== undefined && payload.data.levelType === "multistep") {
callback(null, fetchTemplates.submitMessageResponse, 200);
}
if (payload.data.levelType === undefined) {
session.send("**task/submit results from Task Module adaptive card:**\n\n```" + JSON.stringify(payload) + "```");
}
else {
session.send("**task/submit results from Task Module Html:**\n\n```" + JSON.stringify(payload) + "```");
}
}
}
break;
}
}
callback(null, "", 200);
};
}
// set incoming event to any because membersAdded is not a field in builder.IEvent
private getConversationUpdateHandler(bot: builder.UniversalBot): (event: builder.IConversationUpdate) => void {
return async function(event: builder.IConversationUpdate): Promise<void> {
// For sending a welcome message, we are only interested in member add events
if (!event.membersAdded || (event.membersAdded.length === 0)) {
return;
}
let session = await loadSessionAsync(bot, event);
// Determine if the bot was added to the conversation
let botId = event.address.bot.id;
let botWasAdded = event.membersAdded && event.membersAdded.find(member => (member.id === botId));
if (!event.address.conversation.isGroup) {
// 1:1 conversation event
// If the user hasn't received a first-run message YET, send a message to the user,
// introducing your bot and what it can do. Do NOT send this blindly, as you can receive
// spurious conversationUpdate events, especially if you use proactive messaging.
if (botWasAdded) {
if (!session.userData.freSent) {
session.userData.freSent = true;
session.send(Strings.bot_introduction);
} else {
// First-run message has already been sent, so skip sending it again
// Do not remove the check for "freSent" above. Your bot can receive spurious conversationUpdate
// activities from chat service, so if you always respond to all of them, you will send random
// welcome messages to users who have already received the welcome.
}
}
} else {
// Not 1:1 event (bot or user was added to a team or group chat)
if (botWasAdded) {
// Bot was added to the team
// Send a message to the team's channel, introducing your bot and what you can do
session.send(Strings.bot_introduction);
} else {
// Other users were added to the team
}
}
};
}
// handler for handling incoming payloads from O365ConnectorCard actions
private getO365ConnectorCardActionHandler(bot: builder.UniversalBot): (event: builder.IEvent, query: teams.IO365ConnectorCardActionQuery, callback: (err: Error, result: any, statusCode: number) => void) => void {
return async function (event: builder.IEvent, query: teams.IO365ConnectorCardActionQuery, callback: (err: Error, result: any, statusCode: number) => void): Promise<void> {
let session = await loadSessionAsync(bot, event);
let userName = event.address.user.name;
let body = JSON.parse(query.body);
let msg = new builder.Message(session)
.text(Strings.o365connectorcard_action_response, userName, query.actionId, JSON.stringify(body, null, 2));
session.send(msg);
callback(null, null, 200);
};
}
// method for handling incoming payloads from message reactions
private async handleMessageReaction(event: builder.IMessageUpdate): Promise<void>
{
let session = await loadSessionAsync(this, event);
if (event.reactionsAdded && event.reactionsAdded[0].type === "like") {
session.send(Strings.like_message);
}
if (event.reactionsRemoved && event.reactionsRemoved[0].type === "like") {
session.send(Strings.remove_like_message);
}
}
// method for handling incoming payloads from popup signin
private async verifySigninState (event: builder.IEvent, query: teams.ISigninStateVerificationQuery, callback: (err: Error, body: any, status?: number) => void): Promise<void>
{
let session = await loadSessionAsync(this, event);
if (session)
{
let magicNumber = query.state;
session.clearDialogStack();
session.send(session.gettext(Strings.popupsignin_successful, magicNumber));
}
callback(null, "", 200);
}
}