Bot Framework(v4)-如何从自定义提示验证中获取状态 [英] Bot framework (v4) - How to get state from custom prompt validation

查看:58
本文介绍了Bot Framework(v4)-如何从自定义提示验证中获取状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm implementing a custom prompt validation where I need to access my state to compare with the user's input.

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

I did a lot of search and microsoft documentation and some samples too but I couldn't figure out how to do that.

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

The problem is that to be able to get the state you need to pass StatePropertyAccessor as a parameter as you would normally do with dialogs but when you extend a Prompt you can't do the same.

如何获取此代码的状态? 请查看onRecognize()上的评论.

How can I get my state on this code ? Please, see the comment on 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

If the only reason AddressTextPrompt extends TextPrompt is so that you can do validation, then you should really just pass a validator in to a 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像通常一样传递到下一步.对您来说,验证器可能类似于:

If the validator returns false, then the retryPrompt is fired. Otherwise, activity.Text is passed to the next step like normal. For you, the validator might look something like:

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

这篇关于Bot Framework(v4)-如何从自定义提示验证中获取状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆