Skip to content

Commit 9819216

Browse files
committed
Switched from userProfile to dialog-values.
1 parent 67282de commit 9819216

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

labs/06-dialog-bot/bot.js

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ const NAME_PROMPT = 'name_prompt';
1818
const CONFIRM_PROMPT = 'confirm_prompt';
1919
const AGE_PROMPT = 'age_prompt';
2020

21+
// just a constant string to access the user values
22+
const USER_INFO = 'user_info';
23+
2124
/**
2225
* We will use compromise NLP library for text-matching.
2326
* http://compromise.cool
@@ -36,8 +39,10 @@ class DailogBot {
3639
this.conversationState = conversationState;
3740
this.userState = userState;
3841

42+
// create accessor for the conversation state. This enables us to capture and store conversation specific properties.
43+
// Also create an accessor for userProfile. This enables us to capture and store user-specific properties.
44+
// For more info either see here: https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-howto-v4-state?view=azure-bot-service-4.0&tabs=javascript
3945
this.dialogState = this.conversationState.createProperty(DIALOG_STATE_PROPERTY);
40-
4146
this.userProfile = this.userState.createProperty(USER_PROFILE_PROPERTY);
4247

4348
this.dialogs = new DialogSet(this.dialogState);
@@ -78,14 +83,14 @@ class DailogBot {
7883

7984
// This step in the dialog prompts the user for their name.
8085
async promptForName(step) {
86+
// create new object to store the user_info into
87+
step.values[USER_INFO] = {};
8188
return await step.prompt(NAME_PROMPT, `What is your name, human?`);
8289
}
8390

8491
// This step captures the user's name, then prompts whether or not to collect an age.
8592
async confirmAgePrompt(step) {
86-
const user = await this.userProfile.get(step.context, {});
87-
user.name = step.result;
88-
await this.userProfile.set(step.context, user);
93+
step.values[USER_INFO].name = step.result;
8994
await step.prompt(CONFIRM_PROMPT, 'Do you want to give your age?', ['yes', 'no']);
9095
}
9196

@@ -105,19 +110,34 @@ class DailogBot {
105110

106111
// This step captures the user's age.
107112
async captureAge(step) {
108-
const user = await this.userProfile.get(step.context, {});
109-
if (step.result !== -1) {
110-
user.age = step.result;
111-
await this.userProfile.set(step.context, user);
113+
if (step.result !== -1) {
114+
// store the age in the info-object
115+
step.values[USER_INFO].age = step.result;
112116
await step.context.sendActivity(`I will remember that you are ${ step.result } years old.`);
113117
} else {
114118
await step.context.sendActivity(`No age given.`);
115119
}
120+
121+
// finally save all this informationen into the user-specific memory
122+
// tipp: if you are going to use all the information anyways,
123+
// just store it into the profile right away in each step :)
124+
125+
// First: Get the state properties from the turn context.
126+
const user = await this.userProfile.get(step.context, {});
127+
128+
// then copy the properties directly from the dialog-object
129+
user.age = step.values[USER_INFO].age;
130+
user.name = step.values[USER_INFO].name;
131+
132+
// finally store it all back into the userProfile-memory
133+
await this.userProfile.set(step.context, user);
134+
116135
return await step.endDialog();
117136
}
118137

119138
// This step displays the captured information back to the user.
120139
async displayProfile(step) {
140+
// Get the state properties from the turn context.
121141
const user = await this.userProfile.get(step.context, {});
122142
if (user.age) {
123143
await step.context.sendActivity(`Your name is ${ user.name } and you are ${ user.age } years old.`);

0 commit comments

Comments
 (0)