|
|
|
|
let kara = new Vue({
|
|
|
|
|
el: '#kara',
|
|
|
|
|
data: {
|
|
|
|
|
messages: [],
|
|
|
|
|
name: 'Kara',
|
|
|
|
|
chatbox: null,
|
|
|
|
|
isTyping: false,
|
|
|
|
|
templates: {
|
|
|
|
|
initialGreeting: "Hi! I'm Kara. :)"
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
mounted() {
|
|
|
|
|
this.initialGreeting();
|
|
|
|
|
document.getElementById('chatinput').focus();
|
|
|
|
|
},
|
|
|
|
|
methods: {
|
|
|
|
|
addMessage(body, ai) {
|
|
|
|
|
this.messages.push({
|
|
|
|
|
body: body,
|
|
|
|
|
ai: ai,
|
|
|
|
|
time: Date.now()
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
aiMessage(body) {
|
|
|
|
|
this.addMessage(body, true);
|
|
|
|
|
},
|
|
|
|
|
userMessage(body) {
|
|
|
|
|
this.addMessage(body, false);
|
|
|
|
|
},
|
|
|
|
|
initialGreeting() {
|
|
|
|
|
this.aiMessage(this.templates.initialGreeting);
|
|
|
|
|
},
|
|
|
|
|
sendMessage() {
|
|
|
|
|
if (this.chatbox.trim() === '') {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.userMessage(this.chatbox);
|
|
|
|
|
this.scrollDown();
|
|
|
|
|
this.react(this.chatbox);
|
|
|
|
|
this.chatbox = '';
|
|
|
|
|
},
|
|
|
|
|
react(message) {
|
|
|
|
|
this.isTyping = true;
|
|
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
this.isTyping = false;
|
|
|
|
|
document.getElementById('chatinput').focus();
|
|
|
|
|
}, 2500);
|
|
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
this.aiMessage(
|
|
|
|
|
this.getAnswer(message)
|
|
|
|
|
);
|
|
|
|
|
this.scrollDown();
|
|
|
|
|
}, 3000);
|
|
|
|
|
},
|
|
|
|
|
scrollDown() {
|
|
|
|
|
$('#chat-box').stop().animate({
|
|
|
|
|
scrollTop: $('#chat-box')[0].scrollHeight
|
|
|
|
|
}, 800);
|
|
|
|
|
},
|
|
|
|
|
oneOf(answers) {
|
|
|
|
|
let amountOfAnswers = answers.length;
|
|
|
|
|
console.log(answers);
|
|
|
|
|
let randomIndex = Math.floor(Math.random() * (amountOfAnswers));
|
|
|
|
|
console.log(randomIndex);
|
|
|
|
|
|
|
|
|
|
console.log(answers[randomIndex]);
|
|
|
|
|
return answers[randomIndex];
|
|
|
|
|
},
|
|
|
|
|
includesOneOf(phrases, wordsToSearch) {
|
|
|
|
|
let includes = false;
|
|
|
|
|
|
|
|
|
|
wordsToSearch.forEach((searchWord) => {
|
|
|
|
|
if (phrases.includes(searchWord)) {
|
|
|
|
|
includes = true;
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return includes;
|
|
|
|
|
},
|
|
|
|
|
includesAllOf(phrases, wordsToSearch) {
|
|
|
|
|
let includesAllPhrases = true;
|
|
|
|
|
|
|
|
|
|
wordsToSearch.forEach((searchWord) => {
|
|
|
|
|
if (!phrases.includes(searchWord)) {
|
|
|
|
|
includesAllPhrases = false;
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return includesAllPhrases;
|
|
|
|
|
},
|
|
|
|
|
clearMessage(message) {
|
|
|
|
|
return message.replace('?', '')
|
|
|
|
|
.replace('!', '')
|
|
|
|
|
.replace('.', '')
|
|
|
|
|
.replace(',', '')
|
|
|
|
|
.replace('-', '')
|
|
|
|
|
.replace('_', '')
|
|
|
|
|
.replace('#', '')
|
|
|
|
|
.replace('\'', '')
|
|
|
|
|
.replace('"', '')
|
|
|
|
|
.replace('+', '')
|
|
|
|
|
.replace('*', '')
|
|
|
|
|
.replace('§', '')
|
|
|
|
|
.replace('$', '')
|
|
|
|
|
.replace('%', '')
|
|
|
|
|
.replace('&', '')
|
|
|
|
|
.replace('/', '')
|
|
|
|
|
.replace('(', '')
|
|
|
|
|
.replace(')', '')
|
|
|
|
|
.replace('=', '')
|
|
|
|
|
.replace('\\', '')
|
|
|
|
|
.replace('@', '')
|
|
|
|
|
.replace('~', '')
|
|
|
|
|
.replace('…', '');
|
|
|
|
|
},
|
|
|
|
|
getAnswer(message) {
|
|
|
|
|
let isQuestion = (message.indexOf('?') > 0);
|
|
|
|
|
|
|
|
|
|
message = message.toLowerCase();
|
|
|
|
|
message = this.clearMessage(message);
|
|
|
|
|
let phrases = message.split(' ');
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
this.includesOneOf(phrases,['hi', 'hallo', 'servas', 'servas', 'servus', 'hello'])
|
|
|
|
|
) {
|
|
|
|
|
return this.oneOf([
|
|
|
|
|
'Hey! :)',
|
|
|
|
|
'Hello!',
|
|
|
|
|
'How are you?'
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
this.includesAllOf(phrases,['how', 'are', 'you'])
|
|
|
|
|
) {
|
|
|
|
|
return this.oneOf([
|
|
|
|
|
'I\'m good! Thanks for asking! :) How about you?',
|
|
|
|
|
'A bit tired from trying to do that think called "thinking".. i\'ll figure it out one day.',
|
|
|
|
|
'Wooo! I\'m hyped for party! You\'re in?'
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 'I don\'t know what to say..';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|