|
|
var sendForm = document.querySelector('#chatform'), |
|
|
textInput = document.querySelector('.chatbox'), |
|
|
chatList = document.querySelector('.chatlist'), |
|
|
userBubble = document.querySelectorAll('.userInput'), |
|
|
botBubble = document.querySelectorAll('.bot__output'), |
|
|
animateBotBubble = document.querySelectorAll('.bot__input--animation'), |
|
|
overview = document.querySelector('.chatbot__overview'), |
|
|
hasCorrectInput, |
|
|
imgLoader = false, |
|
|
animationCounter = 1, |
|
|
animationBubbleDelay = 600, |
|
|
input, |
|
|
previousInput, |
|
|
isReaction = false, |
|
|
unkwnCommReaction = "I didn't quite get that.", |
|
|
chatbotButton = document.querySelector(".submit-button") |
|
|
|
|
|
let printVoice = false; |
|
|
|
|
|
let bar = document.getElementById("loadding_bar") |
|
|
|
|
|
|
|
|
sendForm.onkeydown = function(e){ |
|
|
if(e.keyCode == 13){ |
|
|
e.preventDefault(); |
|
|
|
|
|
|
|
|
var input = textInput.value.toLowerCase(); |
|
|
|
|
|
|
|
|
if(input.length > 0) { |
|
|
createBubble(input) |
|
|
} |
|
|
} |
|
|
}; |
|
|
|
|
|
sendForm.addEventListener('submit', function(e) { |
|
|
|
|
|
|
|
|
e.preventDefault(); |
|
|
|
|
|
|
|
|
var input = textInput.value.toLowerCase(); |
|
|
|
|
|
|
|
|
if(input.length > 0) { |
|
|
createBubble(input) |
|
|
} |
|
|
}) |
|
|
|
|
|
var createBubble = function(input) { |
|
|
|
|
|
|
|
|
var chatBubble = document.createElement('li'); |
|
|
chatBubble.classList.add('userInput'); |
|
|
|
|
|
|
|
|
chatBubble.innerHTML = input; |
|
|
|
|
|
|
|
|
chatList.appendChild(chatBubble) |
|
|
|
|
|
|
|
|
checkInput(input) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function asyncGetAnswer(input) { |
|
|
return new Promise((resolve, reject) => { |
|
|
let state = getAnswer(input) |
|
|
resolve(state) |
|
|
}); |
|
|
} |
|
|
|
|
|
|
|
|
function asyncGetVoice(input) { |
|
|
return new Promise((resolve, reject) => { |
|
|
let audio_src = getVoice(input) |
|
|
resolve(audio_src) |
|
|
}); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async function executeGetAnswer(input) { |
|
|
|
|
|
if (!input){ |
|
|
return false |
|
|
} |
|
|
|
|
|
console.log("asyncGetAnswer ๋๊ธฐ ํจ์ ์คํ ์์"); |
|
|
|
|
|
|
|
|
await asyncGetAnswer(input); |
|
|
|
|
|
console.log("asyncGetAnswer ๋๊ธฐ ํจ์ ์คํ ์ข
๋ฃ"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async function executeGetVoice(input) { |
|
|
|
|
|
if (!input){ |
|
|
return false |
|
|
} |
|
|
|
|
|
console.log("asyncGetVoice ๋๊ธฐ ํจ์ ์คํ ์์"); |
|
|
|
|
|
|
|
|
await asyncGetVoice(input); |
|
|
|
|
|
console.log("asyncGetVoice ๋๊ธฐ ํจ์ ์คํ ์ข
๋ฃ"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async function checkInput(input) { |
|
|
|
|
|
|
|
|
|
|
|
isReaction = true; |
|
|
|
|
|
await executeGetAnswer(input) |
|
|
|
|
|
|
|
|
console.log("answer: ", answer); |
|
|
console.log("jeju_answer: ", jeju_answer); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let voice_check = document.getElementById("voice_yn"); |
|
|
let is_checked = voice_check.checked |
|
|
console.log("is_checked:", is_checked) |
|
|
if (is_checked) { |
|
|
console.log("์๋ฆฌO") |
|
|
|
|
|
await executeGetVoice(jeju_answer) |
|
|
|
|
|
|
|
|
|
|
|
}else{ |
|
|
console.log("์๋ฆฌX") |
|
|
} |
|
|
|
|
|
if (answer == ""){ |
|
|
|
|
|
responseCommand("๋ค์ ์
๋ ฅํด์ฃผ์ธ์.", "๋ค์ ์
๋ ฅํด์ฃผ์ธ์."); |
|
|
|
|
|
}else{ |
|
|
|
|
|
responseCommand(answer, jeju_answer); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function getAnswer(input){ |
|
|
$.ajax({ |
|
|
type:"get", |
|
|
url: "/process_input/"+input, |
|
|
timeout:100000, |
|
|
async:false, |
|
|
|
|
|
success:function(result){ |
|
|
console.log("success getAnswer() ํจ์ ์ฑ๊ณต" + input); |
|
|
answer = result.answer |
|
|
jeju_answer = result.jeju_answer |
|
|
console.log("answer " + answer); |
|
|
console.log("jeju_answer " + jeju_answer); |
|
|
}, |
|
|
error:function(request,error){ |
|
|
alert("fail getAnswer() ํจ์ ์คํจ " + input); |
|
|
|
|
|
} |
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
function getVoice(sentence){ |
|
|
$.ajax({ |
|
|
type:"get", |
|
|
url: "/voice/"+sentence, |
|
|
timeout:100000, |
|
|
async:false, |
|
|
|
|
|
success:function(audio_src){ |
|
|
console.log("success " + audio_src); |
|
|
print_voice(audio_src) |
|
|
|
|
|
return audio_src |
|
|
}, |
|
|
error:function(request,error){ |
|
|
alert("fail " + sentence); |
|
|
|
|
|
return "error" |
|
|
} |
|
|
}) |
|
|
} |
|
|
|
|
|
|
|
|
function print_voice(audio_src){ |
|
|
let hidden_area = document.querySelector("#hidden_area"); |
|
|
voice_tag_html = ` |
|
|
<audio src="../${audio_src}" autoplay></audio>`; |
|
|
hidden_area.insertAdjacentHTML("beforeend", voice_tag_html); |
|
|
printVoice = true; |
|
|
} |
|
|
|
|
|
|
|
|
function responseCommand(comm, jeju_comm) { |
|
|
|
|
|
|
|
|
|
|
|
var successResponse = document.createElement('li'); |
|
|
|
|
|
successResponse.classList.add('bot__output'); |
|
|
successResponse.classList.add('bot__output--failed'); |
|
|
|
|
|
|
|
|
response_html = `<p>${comm}</p> |
|
|
<p style="font-size:11px;color:lightslategray">${jeju_comm}</p>` |
|
|
|
|
|
|
|
|
|
|
|
successResponse.innerHTML = response_html; |
|
|
|
|
|
|
|
|
chatList.appendChild(successResponse) |
|
|
|
|
|
animateBotOutput(); |
|
|
|
|
|
|
|
|
textInput.value = ""; |
|
|
|
|
|
|
|
|
chatList.scrollTop = chatList.scrollHeight; |
|
|
|
|
|
animationCounter = 1; |
|
|
|
|
|
} |
|
|
|
|
|
function responseText(e) { |
|
|
|
|
|
var response = document.createElement('li'); |
|
|
|
|
|
response.classList.add('bot__output'); |
|
|
|
|
|
|
|
|
response.innerHTML = e; |
|
|
|
|
|
chatList.appendChild(response); |
|
|
|
|
|
animateBotOutput(); |
|
|
|
|
|
console.log(response.clientHeight); |
|
|
|
|
|
|
|
|
setTimeout(function(){ |
|
|
chatList.scrollTop = chatList.scrollHeight; |
|
|
console.log(response.clientHeight); |
|
|
}, 0) |
|
|
} |
|
|
|
|
|
function responseImg(e) { |
|
|
var image = new Image(); |
|
|
|
|
|
image.classList.add('bot__output'); |
|
|
|
|
|
image.classList.add('bot__outputImage'); |
|
|
|
|
|
image.src = "/images/"+e; |
|
|
chatList.appendChild(image); |
|
|
|
|
|
animateBotOutput() |
|
|
if(image.completed) { |
|
|
chatList.scrollTop = chatList.scrollTop + image.scrollHeight; |
|
|
} |
|
|
else { |
|
|
image.addEventListener('load', function(){ |
|
|
chatList.scrollTop = chatList.scrollTop + image.scrollHeight; |
|
|
}) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
function animateBotOutput() { |
|
|
chatList.lastElementChild.style.animationDelay= (animationCounter * animationBubbleDelay)+"ms"; |
|
|
animationCounter++; |
|
|
chatList.lastElementChild.style.animationPlayState = "running"; |
|
|
} |
|
|
|
|
|
function commandReset(e){ |
|
|
animationCounter = 1; |
|
|
previousInput = Object.keys(possibleInput)[e]; |
|
|
} |
|
|
|
|
|
|
|
|
var reactionInput = { |
|
|
"best work" : function(){ |
|
|
|
|
|
responseText("On this GitHub page you'll find everything about Navvy"); |
|
|
responseText("<a href='https://github.com/meesrutten/chatbot'>Navvy on GitHub</a>") |
|
|
animationCounter = 1; |
|
|
return |
|
|
}, |
|
|
"about" : function(){ |
|
|
responseText("Things I want to learn or do:"); |
|
|
responseText("Get great at CSS & JS animation"); |
|
|
responseText("Create 3D browser experiences"); |
|
|
responseText("Learn Three.js and WebGL"); |
|
|
responseText("Combine Motion Design with Front-End"); |
|
|
animationCounter = 1; |
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|