这实际上取决于您的前端应用程序是如何构建以调用 IBM Watson API。为了更通用,您需要这样做:
首先,您需要将 html 语法添加到 Watson Conversation 上的应答/响应节点中:
Please select your date: <br />
<input type="date" id="birthday" name="birthday">
在您的前端代码(可能包含您的 UI 的 index.html)中,您需要一个函数来识别选择的内容,例如:
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 接受带有 context 变量的 payload,这正是您所需要的。我建议先检查API docs 以了解更多信息。但据我了解,您的有效载荷可能类似于:
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);
});
注意只有当您在消息请求中return_context=true 时,context 才会包含在消息响应中。
重要链接: