我使用Watson助手创建了一个聊天机器人。我通过API调用使用它。
假设有两个不同的人想要同时使用我的聊天机器人。然后我要做的就是让Watson明白两个不同的人正在发送请求。
是否有任何额外的参数可以包含来处理这个问题?如果是的话,请提及。
//var objDiv = document.getElementById("chatbotid");
//objDiv.scrollTop = objDiv.scrollHeight;
document.querySelector("#input").addEventListener("keypress", function(e) {
var key = e.which || e.keyCode;
if (key === 13) { //Enter button
var input = document.getElementById("input").value; //storing input value
document.getElementById("input").value = "";
TempChatInput = document.getElementById("chatbot");
TempChatInput.innerHTML += "user: " + input + "<br/><br/>";
//document.getElementById("user").innerHTML = input;
output(input);
}
});
function output(input) {
//var xhr = new XMLHttpRequest();
var newData = "{\"input\": {\"text\":" + "\"" + input + "\"" + "}}";
//var input = $('input').val();
var dataText;
$.ajax({
url: "https://gateway.watsonplatform.net/assistant/api/v1/workspaces/myWorkspaceID/message?version=2018-02-16",
beforeSend: function(xhr) {
//xhr.setRequestHeader("Authorization", "Basic" + btoa(username + ":" + password));
xhr.setRequestHeader("Authorization", "Basic " + btoa("my username" + ":" + "my password"));
},
type: "POST",
dataType: "json",
contentType: "application/json",
processData: false,
//data: "{\"input\": {\"text\": \"location\"}}",
data: newData,
success: function(data) {
dataText = data["output"]["text"];
//console.log(dataText);
TempChat = document.getElementById("chatbot");
TempChat.innerHTML += "Chatbot: " + dataText + "<br/><br/>";
//document.getElementById("chatbot").innerHTML = dataText;
//alert(dataText);
var objDiv = document.getElementById("chatbotid");
objDiv.scrollTop = objDiv.scrollHeight;
},
error: function() {
alert("Cannot get data");
}
});
//document.getElementById("chatbot").innerHTML = dataText;
}
body {
color: #421;
font-weight: bold;
font-size: 18px;
background: #069597;
background-image: url("girl.png");
background-repeat: repeat-y;
}
span {
color: #104A70;
padding: 1px;
}
::-webkit-input-placeholder {
color: #104A70;
}
#main {
position: fixed;
top: 10%;
right: 60px;
width: 400px;
border: 0px solid #421;
padding: 40px;
}
#main div {
margin: 10px;
}
#input {
border: 0;
background: rgb(255, 255, 255);
padding: 5px;
border: 1px solid #421;
color: #104A70;
}
#line {
text-align: center;
background: #3BB2B4;
}
#chatbotid {
overflow: scroll;
width: 400px;
height: 400px;
background: rgb(230, 230, 225);
padding: 5px;
border: 2px solid #104A70;
}
<div id="line">
<hr/>
<h1 style="color:rgb(255,255,255);"> Vawsum Help Desk</h1>
<hr/>
</div>
<div id="main">
<!-- <div> <span id="user"></span></div> -->
<!--I have deleted user from this line -->
<div id="chatbotid"> <span id="chatbot"></span></div>
<!--I have deleted chatbot from this line -->
<div><input id="input" type="text" placeholder="say something..." autocomplete="off" /></div>
</div>
答案 0 :(得分:1)
当您与Watson Assistant建立连接时,您会收到一个带有响应的对话ID。
您可以使用它来确定两个不同的用户是否在同一时间通话。
根据评论更新:
沃森助理是无国籍人。它没有以前的电话记忆。
当您返回收到的上下文对象时,它将从上次停止的位置继续。如果您不提供会话ID或无效的会话ID,则会生成新的会话ID。