我正在尝试使用 <input type="date" id="birthday" name="birthday">
来在IBM对话框中显示一个日历,供用户选择,因此,当这个日历在聊天机器人中要求输入日期时,在用户选择日期后,它应该将选择的日期存储为变量或任何上下文变量。所以,当这个日历会在聊天机器人中询问日期,然后在用户选择日期后,它应该将选择的日期存储为变量或任何上下文变量。我如何在IBM Watson聊天机器人中实现它呢.谢谢。
这真的取决于你的前端应用是如何构建调用IBM Watson API的。试图更通用,你需要做的。
首先,你需要添加 html
语法到你在Watson Conversation上的answerresponse节点中。
Please select your date: <br />
<input type="date" id="birthday" name="birthday">
在你的前端代码中(可能是 index.html
包含你的UI),你将需要一个函数来识别被选择的内容,如
document.getElementById("birthday").addEventListener("change", function() {
let inputDate = this.value;
let ifYouWantEntireDateFormat = new Date(inputDate);
console.log(inputDate); // 2020-04-20
console.log(ifYouWantEntireDateFormat); //e.g. Mon April 20 2020 00:00:00 etc
});
你也可以使用 querySelector
函数。此外,如果没有选择数值,将返回 "无效日期"
考虑到这些,你还需要知道,Watson API接受了 payload
具有 context
变量,这是你需要的。我建议检查 API文档 先来了解一下。但据我了解,你的有效载荷可能类似于。
const AssistantV2 = require('ibm-watson/assistant/v2');
const { IamAuthenticator } = require('ibm-watson/auth');
const assistant = new AssistantV2({
version: '2020-04-01',
authenticator: new IamAuthenticator({
apikey: '{apikey}',
}),
url: '{url}',
});
assistant.message({
assistantId: '{assistant_id}',
sessionId: '{session_id}',
input: {
'message_type': 'text',
'text': 'Hello',
'options': {
'return_context': true
}
},
context: {
'global': {
'myDatePicker': inputDate,
'system': {
'user_id': 'my_user_id'
}
},
'skills': {
'main skill': {
'user_defined': {
'account_number': '123456'
}
}
}
}
})
.then(res => {
console.log(JSON.stringify(res.result, null, 2));
})
.catch(err => {
console.log(err);
});
笔记 该 context
只有当您 return_context=true
的消息请求中。
重要环节。