【发布时间】: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