I working on a chatbot where I want to increase the progress bar depending on which intents the chatbot response comes from. The dataset is a JSON file containing responses for the chatbot. I want the progress bar to move accordingly by a certain percent depending on which “tag” the chatbot response comes from. I have used the fetch method to get the JSON data, looped through intents, and put them in different lists. The problem seems to be that the progress bar only moves once and then stops no matter what I type.
function botResponse(rawText) {
const bot_rep = [];
const bot_tag_1 = [];
const bot_tag_2 = [];
let counter = 0;
let percent = document.getElementById('percent');
let bar = document.getElementById('bar');
//const i=0;
fetch("http://127.0.0.1:5000/json") //fetch the json file
.then(response=>response.json())
.then(data=>{
for (i in data.intents[0].responses){ //loop through intetnts
bot_tag_1.push(data.intents[0].responses[i]); //push all responses from intents "greeting" [0] to list bot_tag_1
//console.log(data.intents[0].responses)
console.log(bot_tag_1.length);
}
if(bot_rep.indexOf(bot_tag_1 )){ //if the chatbot's response comes from/exists in bot_tag_1 increase by 2%
//var count = 1;
//console.log(bot_tag_2);
console.log("from tag greeting "+ "points increase 2 pts");
counter = 2;//count * 2;
percent.innerHTML = `${counter}%`;
bar.style.width = `${counter}%`;
}
for (i in data.intents[1].responses){ //loop through intents
bot_tag_2.push(data.intents[1].responses[i]); //push all responses from intents "goodbye" [1] to list bot_tag_2
//console.log(data.intents[1].tag)
console.log(bot_tag_2.length);
}
if(bot_rep.indexOf(bot_tag_2 )){ //if the chatbot's response comes from/exists in bot_tag_2 increase by 10%
//var count = 1;
//console.log(bot_tag_2);
console.log("from tag goodbye "+ "points increase 10pts");
counter = 10;
percent.innerHTML = `${counter}%`;
bar.style.width = `${counter}%`;
}
//console.log(data);
//console.log(data.intents[1].responses);
//console.log(data.intents[0].tag);
})
#progress {
width: 500px;
border: 1px solid black;
position: relative;
padding: 3px;
top: 16px;
}
#percent {
position: absolute;
left: 50%;
bottom: 25px;
}
#bar {
height: 20px;
background-color: green;
width: 0%;
}
<!-- JSON file
{"intents": [
{"tag": "greeting",
"patterns": ["Hi", "How are you?", "Hello", "Whats up?", "hiii"],
"responses": ["Hello!", "Hi there, how can I help?"],
"context_set": ""
},
{"tag": "goodbye",
"patterns": ["cya", "See you later!", "Bye Bye!", "Goodbye", "Peace out"],
"responses": ["Will miss you!", "Okay, talk to you later!", "Goodbye!"],
"context_set": ""
},
{"tag": "language",
"patterns": ["What language are you written in??"],
"responses": ["Im written in Python"],
"context_set": ""
},
<html lang="en">
<head>
</head>
<body>
<div class="progress">
progress Bar
<div id="progress">
<span id="percent">0%</span>
<div id="bar"></div>
</div>
</body>
</html>