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
141 lines (120 loc) · 6.22 KB
/
Bot.ts
File metadata and controls
141 lines (120 loc) · 6.22 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
import * as builder from "botbuilder";
import * as config from "config";
import { RootDialog } from "./dialogs/RootDialog";
import { SetLocaleFromTeamsSetting } from "./middleware/SetLocaleFromTeamsSetting";
import { StripBotAtMentions } from "./middleware/StripBotAtMentions";
// import { SetAADObjectId } from "./middleware/SetAADObjectId";
import { LoadBotChannelData } from "./middleware/LoadBotChannelData";
import { Strings } from "./locale/locale";
import { loadSessionAsync } from "./utils/DialogUtils";
import * as teams from "botbuilder-teams";
import { ComposeExtensionHandlers } from "./composeExtension/ComposeExtensionHandlers";
// =========================================================
// 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 message
new SetLocaleFromTeamsSetting(),
// set on "botbuilder" (after session created)
new StripBotAtMentions(),
// new SetAADObjectId(),
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));
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;
// 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);
}
}
callback(null, "", 200);
};
}
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);
let magicNumber = '';
if (session)
{
magicNumber = query.state;
session.clearDialogStack();
session.send(session.gettext(Strings.popupsignin_successful) + magicNumber);
}
callback(null, "", 200);
}
// set incoming event to any because membersAdded is not a field in builder.IEvent
private getConversationUpdateHandler(bot: builder.UniversalBot): (event: any) => void {
return async function(event: any): Promise<void> {
let session = await loadSessionAsync(bot, event);
if (event.membersAdded && event.membersAdded[0].id && event.membersAdded[0].id.endsWith(config.get("bot.botId"))) {
session.send(Strings.bot_introduction); // probably only works in Teams
} else {
session.send(Strings.bot_welcome_to_new_person);
}
};
}
// 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);
};
}
}