我正在实现自定义的提示验证,在这里我需要访问自己的状态以与用户输入进行比较。
我做了很多搜索和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}` });
答案 0 :(得分:2)
如果AddressTextPrompt
扩展TextPrompt
的唯一原因是您可以进行验证,那么您实际上应该只是将验证器传递给TextPrompt
。
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;
}