我试图通过添加机智/日期时间来扩展机智天气示例。
例如,用户可能会输入" 1小时内在柏林会有多冷?"天气机器人将在柏林1小时内恢复天气数据。
到目前为止,这是有效的,但是当我尝试设置missingDate
以询问日期是否遗漏时,它表现得很有趣。
对话将是:
- How cold will it be in Berlin?
- In what time?
- In 1 hour.
相反,在1小时后,我再次被问到上下文中的位置,而是再次触发。
我的操作名为getForecast({context, entities})
,我将其定义如下:
actions:
[...],
getForecast({ context, entities }) {
console.log(`The current context is: ${JSON.stringify(context)}`);
console.log(`Wit extracted ${JSON.stringify(entities)}`);
// extract entity
var location = firstEntityValue(entities, "location");
var date = firstEntityValue(entities, "datetime");
// if the entity exists, do a remote weather call.
if (date) {
context.date = date;
delete context.missingDate;
} else {
context.missingDate = true;
delete context.date;
delete context.forecast;
}
if (location) {
context.forecast = '38 degrees';
context.location = location;
delete context.missingLocation;
} else {
context.missingLocation = true;
delete context.forecast;
}
// return the context object
return Promise.resolve(context);
}