如何解决BotFramework DateTimePrompt语言环境不起作用
我正在nodejs上使用bitframework 4。我无法获取DateTimePrompt来使用日期格式为dd / mm(不是mm / dd)的语言环境“ en-GB”,我在3个地方都没有设置语言环境:
adapter.processActivity(req,res,(context) => {
context.activity.locale = 'en-GB';
return myChatBot.run(context);
};
this.addDialog(new DateTimePrompt('startDatePrompt',undefined,'en-GB'));
以及在网络聊天框架中:
window.WebChat.renderWebChat(
{
directLine: window.WebChat.createDirectLine({ token: token }),locale: 'en-GB',webSpeechPonyfillFactory
},document.getElementById('webchat')
);
我只希望日期解析器解析为dd / mm而不是mm / dd有什么想法?
解决方法
使用正则表达式创建客户提示。这将为您提供所需的灵活性。
在构造函数中,使用TextPrompt并传递hintvalidator
this.addDialog(new TextPrompt(DATE_PROMPT,this.datePromptValidator));
在出现提示的对话框中。
async dateStep(stepContext) {
await helper.say(stepContext,'Please enter a date');
const promptOptions = { retryPrompt: 'This is not a valid date. Please use mm/dd' };
return await stepContext.prompt(WHEN_PROMPT,promptOptions);
}
由于提示验证器将使用turnState(对我有用)与dialogstep共享结果,因此我们在下一个dialogStep中对此进行检查
async stepafterDateStep(stepContext) {
stepContext.values.date = stepContext.context.turnState.match;
}
然后,提示符验证器使用正则表达式检查正确的格式。这些正则表达式表达趋于变得复杂。这个检查dd / mm / yyyy。有可用的库,例如https://regexlib.com/
async datePromptValidator(promptContext) {
const regex = new RegExp(/(((0[1-9]|[12][0-9]|3[01])([-./])(0[13578]|10|12)([-./])(\d{4}))|(([0][1-9]|[12][0-9]|30)([-./])(0[469]|11)([-./])(\d{4}))|((0[1-9]|1[0-9]|2[0-8])([-./])(02)([-./])(\d{4}))|((29)(\.|-|\/)(02)([-./])([02468][048]00))|((29)([-./])(02)([-./])([13579][26]00))|((29)([-./])(02)([-./])([0-9][0-9][0][48]))|((29)([-./])(02)([-./])([0-9][0-9][2468][048]))|((29)([-./])(02)([-./])([0-9][0-9][13579][26])))/);
var matches = promptContext.recognized.value.match(regex);
if (matches) {
promptContext.context.turnState.match = matches[0];
return true;
} else { return false; };
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。