【问题标题】:Bot framework (v4) - How to get state from custom prompt validationBot 框架 (v4) - 如何从自定义提示验证中获取状态
【发布时间】:2019-08-13 04:48:09
【问题描述】:

我正在实施自定义提示验证,我需要访问我的状态以与用户的输入进行比较。

我也做了很多搜索和微软文档以及一些示例,但我不知道该怎么做。

问题在于,为了能够获得状态,您需要像通常对对话框所做的那样将 StatePropertyAccessor 作为参数传递,但是当您扩展 Prompt 时,您不能这样做。

我怎样才能获得这段代码的状态? 请参阅 onRecognize() 上的评论。

class AddressTextPrompt extends TextPrompt {
  private userProfile: StatePropertyAccessor<State>;
  public defaultLocale: string | undefined;

  constructor(dialogId: string, validator?: PromptValidator<string>, defaultLocale?: string) {
    super(dialogId, validator);
    this.defaultLocale = defaultLocale;
  }

  protected async onPrompt(context: TurnContext, state: any, options: PromptOptions, isRetry: boolean): Promise<void> {
    if (isRetry && options.retryPrompt) {
      await context.sendActivity(options.retryPrompt, null, InputHints.ExpectingInput);
    } else if (options.prompt) {
      await context.sendActivity(options.prompt, null, InputHints.ExpectingInput);
    }
  }

  protected async onRecognize(context: TurnContext, state: any, options: PromptOptions): Promise<PromptRecognizerResult<string>> {
    const result: PromptRecognizerResult<string> = { succeeded: false };
    const activity: Activity = context.activity;

    // I can't access my state here and there's no way to pass StatePropertyAccessor through contructor
    const userState: State = await this.userProfile.get(context);

    result.succeeded = (userState.user.address === activity.text)

    return result;
  }
}

export { AddressTextPrompt };

向对话框添加提示

this.addDialog(new AddressTextPrompt(ADDRESS_TEXT_PROMPT));

使用它

  const messageText = `Some text ${hideStringPartially(userDetails.address)}`;
  const msg = MessageFactory.text(messageText, messageText, InputHints.ExpectingInput);
  return await step.prompt(ADDRESS_TEXT_PROMPT, { prompt: msg, retryPrompt: `Some text. ${messageText}` });

【问题讨论】:

  • 嗨,我还没有机会测试它。但这对我来说似乎很有意义。我只需要测试它。感谢您的帮助。

标签: javascript node.js typescript botframework


【解决方案1】:

如果AddressTextPrompt 扩展TextPrompt 的唯一原因是您可以进行验证,那么您真的应该将验证器传递给TextPrompt

Multi-Turn-Prompt Sample

...它passes in the validator:

this.addDialog(new NumberPrompt(NUMBER_PROMPT, this.agePromptValidator));

...然后performs the validation:

async agePromptValidator(promptContext) {
    // This condition is our validation rule. You can also change the value at this point.
    return promptContext.recognized.succeeded && promptContext.recognized.value > 0 && promptContext.recognized.value < 150;
}

如果验证器返回false,则retryPrompt 被触发。否则,activity.Text 会像往常一样传递到下一步。对您而言,验证器可能类似于:

async addressValidator(promptContext) {
    const userState: State = await this.userProfile.get(context);
    // This condition is our validation rule. You can also change the value at this point.
    return promptContext.recognized.succeeded && promptContext.recognized.value === userState.user.address;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-22
    • 2020-03-21
    • 1970-01-01
    • 2013-11-05
    • 1970-01-01
    相关资源
    最近更新 更多