首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >重复ChoicePrompt清除UserState

重复ChoicePrompt清除UserState
EN

Stack Overflow用户
提问于 2019-02-22 11:17:12
回答 1查看 51关注 0票数 0

我用微软的BotFramework实现了一个机器人。为了收集用户数据,我使用ChoicePrompts。当用户没有选择所建议的选项之一时,ChoicePrompt会重复执行,直到用户输入一个有效选项(这是提示方法中的默认行为)。

不幸的是,在没有选择一个有效的选择选项后,用户状态将被刷新。这意味着,在此之前,我将丢失所有收集的用户数据。

这种行为是有意的还是有办法防止这种情况发生?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-19 00:16:03

您提供的链接上的代码有几个问题。由于我看不到您的完整代码,所以我对某些部分进行了猜测。

  1. 双重检查userState和userData是正确的设置。我的构造函数如下所示:
代码语言:javascript
复制
const DIALOG_STATE_PROPERTY = 'dialogState';
const USER_PROFILE_PROPERTY = 'user';

const EDUCATION_PROMPT = 'education_prompt';
const MAJOR_PROMPT = 'major_prompt';

constructor(conversationState, userState) {
    this.conversationState = conversationState;
    this.userState = userState;

    this.dialogState = this.conversationState.createProperty(DIALOG_STATE_PROPERTY);
    this.userData = this.userState.createProperty(USER_PROFILE_PROPERTY);

    this.dialogs = new DialogSet(this.dialogState);

    // Add prompts that will be used by the main dialogs.
    this.dialogs
        .add(new TextPrompt(NAME_PROMPT))
        .add(new TextPrompt(AGE_PROMPT))
        .add(new TextPrompt(GENDER_PROMPT))
        .add(new ChoicePrompt(EDUCATION_PROMPT))
        .add(new ChoicePrompt(MAJOR_PROMPT));

    // Create dialog for prompting user for profile data
    this.dialogs.add(new WaterfallDialog(START_DIALOG, [
        this.promptForName.bind(this),
        this.promptForAge.bind(this),
        this.promptForGender.bind(this),
        this.promptForEducation.bind(this),
        this.promptForMajor.bind(this),
        this.returnUser.bind(this)
    ]));

    this.majors = ['English', 'History', 'Computer Science'];
}
  1. 请记住,TextPrompt返回step.result中的值。ChoicePrompt以step.result.value的形式返回值 我假设在您的"promptForEducation“步骤中,您将性别值分配给用户,该值来自选择提示。否则,您将失去价值。再次检查您是否指定了正确的源。
代码语言:javascript
复制
.add(new TextPrompt(GENDER_PROMPT))
.add(new ChoicePrompt(EDUCATION_PROMPT))

...

if (!user.gender) {
    user.gender = step.result;
    // Give user object back to UserState storage
    await this.userData.set(step.context, user);
    console.log(user);
}
  1. 在"promptForMajor“步骤中,step.Prompt中的第二个参数接受一个字符串并表示选择的对话框部分。您的代码应该如下所示,并将产生以下输出。在本例中,我在构造函数中将值分配给"this.majors“(如上文所示)。
代码语言:javascript
复制
this.majors = ['English', 'History', 'Computer Science'];

...

if (!user.major) {
    // Copy List of majors and add "Other" entry
    let majorsOther = this.majors.slice(0, this.majors.length);
    majorsOther.push('Einen anderen Studiengang');
    // return await step.prompt(MAJOR_PROMPT, this.userData.major, majorsOther);
    return await step.prompt(MAJOR_PROMPT, 'List of majors:', majorsOther);
}

  1. 检查您是否保存在"onTurn“末尾的状态。
代码语言:javascript
复制
// Save changes to the user state.
await this.userState.saveChanges(turnContext);

// End this turn by saving changes to the conversation state.
await this.conversationState.saveChanges(turnContext);

如果您实现了上述功能,则应该设置。我能够在没有任何问题和国家损失的情况下竞选。此外,在最后提供适当的响应之前,反复不响应ChoicePrompt并不会破坏状态。

希望得到帮助!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54825916

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档