-
Notifications
You must be signed in to change notification settings - Fork 0
/
dialogBot.js
48 lines (40 loc) · 1.87 KB
/
dialogBot.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
const { TeamsActivityHandler } = require('botbuilder');
class DialogBot extends TeamsActivityHandler {
/**
*
* @param {ConversationState} conversationState
* @param {UserState} userState
* @param {Dialog} dialog
*/
constructor(conversationState, userState, dialog) {
super();
if (!conversationState) throw new Error('[DialogBot]: Missing parameter. conversationState is required');
if (!userState) throw new Error('[DialogBot]: Missing parameter. userState is required');
if (!dialog) throw new Error('[DialogBot]: Missing parameter. dialog is required');
this.conversationState = conversationState;
this.userState = userState;
this.dialog = dialog;
this.dialogState = this.conversationState.createProperty('DialogState');
this.userProfileAccessor = this.userState.createProperty('UserProfile');
this.onMessage(async (context, next) => {
console.log('Running dialog with Message Activity.');
const userProfile = await this.userProfileAccessor.get(context, {});
userProfile.userid = context.activity.from.id;
// Run the Dialog with the new message Activity.
await this.dialog.run(context, this.dialogState);
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.DialogBot = DialogBot;