-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
proactiveBot.js
47 lines (36 loc) · 1.82 KB
/
proactiveBot.js
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
const { ActivityHandler, TurnContext } = require('botbuilder');
class ProactiveBot extends ActivityHandler {
constructor(conversationReferences) {
super();
// Dependency injected dictionary for storing ConversationReference objects used in NotifyController to proactively message users
this.conversationReferences = conversationReferences;
this.onConversationUpdate(async (context, next) => {
this.addConversationReference(context.activity);
await next();
});
this.onMembersAdded(async (context, next) => {
const membersAdded = context.activity.membersAdded;
for (let cnt = 0; cnt < membersAdded.length; cnt++) {
if (membersAdded[cnt].id !== context.activity.recipient.id) {
const welcomeMessage = 'Welcome to the Proactive Bot sample. Navigate to http://localhost:3978/api/notify to proactively message everyone who has previously messaged this bot.';
await context.sendActivity(welcomeMessage);
}
}
// By calling next() you ensure that the next BotHandler is run.
await next();
});
this.onMessage(async (context, next) => {
this.addConversationReference(context.activity);
// Echo back what the user said
await context.sendActivity(`You sent '${ context.activity.text }'`);
await next();
});
}
addConversationReference(activity) {
const conversationReference = TurnContext.getConversationReference(activity);
this.conversationReferences[conversationReference.conversation.id] = conversationReference;
}
}
module.exports.ProactiveBot = ProactiveBot;