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

标签 javascript node.js typescript botframework

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

我做了很多搜索和微软文档以及一些示例,但我不知道如何做到这一点。

问题是,为了能够获取状态,您需要将 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 >.

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

关于javascript - Bot 框架 (v4) - 如何从自定义提示验证中获取状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57441392/

相关文章:

javascript - Polymer 检查元素是否打开并将 css 类分配给父级

javascript - 为什么 React 的 onClick 函数需要独特的语法?与其他事件触发器相比?

node.js - 架构 : Multiple Mongo databases+connections vs multiple collections with Express

node.js - npm 错误! cb.apply 不是函数

javascript - 增加缓冲区的推荐方法?

javascript - Mat0-Slider ngModel 不工作 Angular 5

Angular 2 - 重定向登录页面

Angular:在 [innerHtml] 内绑定(bind)变量

javascript - 如何监听指令中文本区域的变化?

javascript - 用于在现有 DOM 元素上动态设置属性的 Angular 指令