我使用 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>