问题
I implemented a bot with the Microsoft BotFramework. For gathering user data, I use ChoicePrompts. When a user doesn't choose one of the proposed options, the ChoicePrompt repeats until the user enters a valid option (this is default behavior from the prompt method).
Unfortunately, the stepContext seems to refresh, if the ChoicePrompt reprompts the user. I used this documentation to store user data. This means that I lose the data that has been prompted from the user until then.
This is my dialog:
// Create dialog for prompting user for profile data
this.dialogSet.add(new WaterfallDialog('createProfile', [
this.promptForName.bind(this),
this.promptForAge.bind(this),
this.promptForGender.bind(this),
this.promptForEducation.bind(this),
this.promptForMajor.bind(this),
this.promptForOtherMajor.bind(this),
this.completeProfile.bind(this)
]));
Those are the methods in which the problem appears. In "promptForEducation" the ChoicePrompt is used.
async promptForEducation (step) {
console.log("Education Prompt");
// Retrieve user object from UserState storage
const user = await this.userData.get(step.context, {});
// Before saving entry, check if it already exists
if(!user.gender){
user.gender = step.result.value;
// Give user object back to UserState storage
await this.userData.set(step.context, user);
}
// Before prompting, check if value already exists
if (!user.education){
const user = await this.userData.get(step.context, {});
console.log(user);
// Prompt for highest education with list of education options
return await step.prompt('choicePrompt', 'What is your highest education', ['Bachelor', 'Master']);
} else {
return await step.next();
}
}
async promptForMajor (step) {
console.log("Major Prompt");
// Retrieve user object from UserState storage
const user = await this.userData.get(step.context, {});
console.log(user);
// Before saving entry, check if it already exists
if(!user.education){
user.education = step.result.value;
// Give user object back to UserState storage
await this.userData.set(step.context, user);
console.log(user);
}
// Before prompting, check if value already exists
if (!user.major){
// Copy List of majors and add "Other" entry
let majorsOther = majors.slice(0,majors.length);
majorsOther.push("Einen anderen Studiengang");
return await step.prompt('choicePrompt', userData.major.prompt, majorsOther);
} else {
return await step.next();
}
}
As you can see, I retrieve the user data as follows:
// Retrieve user object from UserState storage
const user = await this.userData.get(step.context, {});
And I save it like this:
// Give user object back to UserState storage
await this.userData.set(step.context, user);
When directly choosing a valid option from the "education" array, everything is fine. This is how my console output looks like (I log the "user"-object 3 times in the snipped. Once before and twice after the ChoicePrompt)
User added Name Prompt Age Prompt Gender Prompt Education Prompt { name: 'John Doe', age: 18, gender: 'Männlich' } Major Prompt { name: 'John Doe', age: 18, gender: 'Männlich' } { name: 'John Doe', age: 18, gender: 'Männlich', education: 'Bachelor' }
As soon as I answer the ChoicePrompt with an invalid message, I get reprompted but then the "StepContext" seems to refresh and my "user" Object is empty:
User added Name Prompt Age Prompt Gender Prompt Education Prompt { name: 'John Doe 2', age: 18, gender: 'Männlich' } Major Prompt {} { education: 'Bachelor' }
Is there any way to fix this? Am I using some method differently from how it's intended? Thank you in advance.
来源:https://stackoverflow.com/questions/54635967/choiceprompt-clears-userstate-or-stepcontext