I am working on a chatbot and want to deploy it in Django but I am using a standalone frontend. I have little to no knowledge about JavaScript and copied this Script from somewhere:
onSendButton(chatbox) {
var textField=chatbox.querySelector('input');
let text1=textField.value
if(text1==="") {
return;
}
let msg1={name: "User", message: text1}
this.messages.push(msg1);
this.updateChatText(chatbox)
textField.value=''
fetch('http://localhost:8000/chat', {
method: 'POST',
body: JSON.stringify({message: text1}),
mode: 'cors',
headers: {
'Content-Type': 'application/json'
},
})
.then(r => r.json())
.then(r => {
console.log(r)
let msg2={name: "CareAll", message: r.answer};
this.messages.push(msg2);
if("follow_up" in r) {
let msg3={name: "CareAll", message: r.follow_up};
this.messages.push(msg3)
}
this.updateChatText(chatbox)
textField.value=''
}).catch((error) => {
console.error('Error:', error);
this.updateChatText(chatbox)
textField.value=''
});
}
This function is for /chat route
def chat_bot_response(request):
if request.method == "POST":
u_msg = json.loads(request.body)["message"]
ints = predict_class(u_msg, cb_model)
resp = {"answer": getResponse(ints, intents)}
maxConf = max(ints, key=lambda x: x["probability"])
if maxConf["intent"] not in [
"greeting",
"farewell",
"about_self",
"about_self_function",
"question",
"unknown",
"yes_to_symptom",
"no_to_symptom"]:
RESP_LIST.append(resp["answer"])
resp["answer"] = "Do You Have other symptoms?"
if maxConf["intent"] == "yes_to_symptom":
print(RESP_LIST)
elif maxConf["intent"] == "no_to_symptom":
resp["answer"] = RESP_LIST[0]
return JsonResponse(resp)
What I am trying to do is, I want to stay in this function when the chatbox is opened until the user leaves the page. Will I need to improve JavaScript? use Django template? what is the possible solution.