繁体   English   中英

Bot Framework(v4)-如何从自定义提示验证中获取状态

[英]Bot framework (v4) - How to get state from custom prompt validation

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

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

问题在于,要获取状态,您需要像通常使用对话框那样传递StatePropertyAccessor作为参数,但是当您扩展提示时,您不能这样做。

如何获得此代码的状态? 请查看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}` });

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

在“ 多回转提示”示例中

... 通过验证器

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

...然后执行验证

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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM