在IBM Watson对话对话框中使用HTML日期选择器从用户获取日期作为输入 [英] use HTML date picker in IBM watson conversation dialog to get date as input from user
问题描述
我正在尝试使用<输入类型=日期 id =生日 name =生日>
显示要由用户选择的日历在IBM对话框上。因此,当此日历在聊天机器人中要求输入日期,然后在用户选择日期之后,应将所选日期存储为变量或任何上下文变量。
如何在IBM Watson chatbot中实现它。
谢谢
I am trying to use <input type="date" id="birthday" name="birthday">
to display a calendar to be selected by user on IBM dialog. So, when this calendar will ask for date in the chat bot and then after user selecting the date, it should store the selected date as variable or any context variable.
How I have to implement it in IBM Watson chatbot.
Thanks
推荐答案
这实际上取决于您的前端应用程序如何构建以调用IBM Watson API。尝试变得更加通用,您需要执行以下操作:
It really depends on how your front-end app is built to call IBM Watson API. Trying to be more generic, you would need to do:
首先,您需要添加 html
语法进入您在Watson对话中的答案/响应节点:
First, you would need to add the html
syntax into your answer/response node on Watson Conversation:
Please select your date: <br />
<input type="date" id="birthday" name="birthday">
在您的前端代码中(可能是 index.html
包含您的UI),您将需要一个函数来识别所选内容,例如:
And in your front-end code (probably index.html
that contains your UI), you would need a function to identify what was selected, e.g:
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
函数。此外,如果未选择任何值,它将返回无效日期
You could also use querySelector
function. In addition, if no value is selected it will return "Invalid date"
记住所有这些,您还需要知道Watson API接受有效载荷
上具有 context
变量,这是您所需要的。我建议您检查 API文档首先了解更多。但是据我了解,您的有效负载可能类似于:
With all that in mind, you also need to know that Watson API accepts the payload
having the context
variables on it, which is what you need. I would recommend checking the API docs first to understand more. But according to what I understood, your payload might be similar to:
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
时才包含在消息响应中。
Note The context
is included in message responses only if you return_context=true
in the message request.
重要链接:
- Input type Date - MDN
- Watson Assistant API doc - IBM Watson
这篇关于在IBM Watson对话对话框中使用HTML日期选择器从用户获取日期作为输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!