-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
bot.js
54 lines (44 loc) · 2.2 KB
/
bot.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
48
49
50
51
52
53
54
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
const { ActivityHandler } = require('botbuilder');
class IntersectionBot extends ActivityHandler {
constructor(conversationState, userState) {
super();
this.conversationState = conversationState;
this.conversationStateAccessor = this.conversationState.createProperty('test-conversation-state');
this.userState = userState;
this.userStateAccessor = this.userState.createProperty('test-user-state');
this.onMessage(async (context, next) => {
// get the state objects
const testConversationState = await this.conversationStateAccessor.get(context, { count: 0 });
const testUserState = await this.userStateAccessor.get(context, { count: 0 });
// print the current state to the reply to show we are incrementing it
await context.sendActivity(`You said '${ context.activity.text }' conversation-state: ${ testConversationState.count } user-state: ${ testUserState.count }`);
// do the actual increment
testConversationState.count++;
testUserState.count++;
// By calling next() you ensure that the next BotHandler is run.
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) {
await context.sendActivity('Hello and welcome!');
}
}
// By calling next() you ensure that the next BotHandler is run.
await next();
});
}
/**
* Override the ActivityHandler.run() method to save state changes after the bot logic completes.
*/
async run(context) {
await super.run(context);
// Save any state changes. The load happened during the execution of the Dialog.
await this.conversationState.saveChanges(context, false);
await this.userState.saveChanges(context, false);
}
}
module.exports.IntersectionBot = IntersectionBot;