forked from microsoft/BotBuilder-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
116 lines (97 loc) · 4.62 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
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
const { ActivityTypes } = require('botbuilder');
const { DialogSet, TextPrompt, WaterfallDialog } = require('botbuilder-dialogs');
const DIALOG_STATE_PROPERTY = 'dialogState';
const USER_NAME_PROP = 'user_name';
const WHO_ARE_YOU = 'who_are_you';
const HELLO_USER = 'hello_user';
const NAME_PROMPT = 'name_prompt';
class SimplePromptBot {
/**
*
* @param {Object} conversationState
* @param {Object} userState
*/
constructor(conversationState, userState) {
// creates a new state accessor property.
// See https://aka.ms/about-bot-state-accessors to learn more about the bot state and state accessors
this.conversationState = conversationState;
this.userState = userState;
this.dialogState = this.conversationState.createProperty(DIALOG_STATE_PROPERTY);
this.userName = this.userState.createProperty(USER_NAME_PROP);
this.dialogs = new DialogSet(this.dialogState);
// Add prompts
this.dialogs.add(new TextPrompt(NAME_PROMPT));
// Create a dialog that asks the user for their name.
this.dialogs.add(new WaterfallDialog(WHO_ARE_YOU, [
this.askForName.bind(this),
this.collectAndDisplayName.bind(this)
]));
// Create a dialog that displays a user name after it has been collected.
this.dialogs.add(new WaterfallDialog(HELLO_USER, [
this.displayName.bind(this)
]));
}
// The first step in this waterfall asks the user for their name.
async askForName(dc) {
await dc.prompt(NAME_PROMPT, `What is your name, human?`);
}
// The second step in this waterfall collects the response, stores it in
// the state accessor, then displays it.
async collectAndDisplayName(step) {
await this.userName.set(step.context, step.result);
await step.context.sendActivity(`Got it. You are ${ step.result }.`);
await step.endDialog();
}
// This step loads the user's name from state and displays it.
async displayName(step) {
const userName = await this.userName.get(step.context, null);
await step.context.sendActivity(`Your name is ${ userName }.`);
await step.endDialog();
}
/**
*
* @param {Object} context on turn context object.
*/
async onTurn(turnContext) {
// See https://aka.ms/about-bot-activity-message to learn more about the message and other activity types.
if (turnContext.activity.type === ActivityTypes.Message) {
// Create dialog context
const dc = await this.dialogs.createContext(turnContext);
// Continue the current dialog
if (!turnContext.responded) {
await dc.continueDialog();
}
// Show menu if no response sent
if (!turnContext.responded) {
const userName = await this.userName.get(dc.context, null);
if (userName) {
await dc.beginDialog(HELLO_USER);
} else {
await dc.beginDialog(WHO_ARE_YOU);
}
}
} else if (turnContext.activity.type === ActivityTypes.ConversationUpdate) {
// Do we have any new members added to the conversation?
if (turnContext.activity.membersAdded.length !== 0) {
// Iterate over all new members added to the conversation
for (let idx in turnContext.activity.membersAdded) {
// Greet anyone that was not the target (recipient) of this message.
// Since the bot is the recipient for events from the channel,
// context.activity.membersAdded === context.activity.recipient.Id indicates the
// bot was added to the conversation, and the opposite indicates this is a user.
if (turnContext.activity.membersAdded[idx].id !== turnContext.activity.recipient.id) {
// Send a "this is what the bot does" message to this user.
await turnContext.sendActivity('I am a bot that demonstrates the TextPrompt class to collect your name, store it in UserState, and display it. Say anything to continue.');
}
}
}
}
// Save changes to the user name.
await this.userState.saveChanges(turnContext);
// End this turn by saving changes to the conversation state.
await this.conversationState.saveChanges(turnContext);
}
}
module.exports.SimplePromptBot = SimplePromptBot;