Kara is a digital assistent aimed to help in any daily situation. => Moved to /LunaDev/luna-development
https://luna-development.net/kara/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
322 lines
9.6 KiB
322 lines
9.6 KiB
let kara = new Vue({ |
|
el: '#kara', |
|
data: { |
|
messages: [], |
|
name: 'Kara', |
|
chatbox: null, |
|
isTyping: false, |
|
askedForName: false, |
|
username: '', |
|
addModal: { |
|
includeAll: false, |
|
keywords: '', |
|
responses: [ |
|
'Answer #1' |
|
] |
|
}, |
|
settingsModal: { |
|
name: null, |
|
username: null |
|
}, |
|
answers: [ |
|
{ |
|
includeAll: false, |
|
keywords: [ |
|
'hi', 'hallo', 'servas', 'servus', 'hello' |
|
], |
|
responses: [ |
|
'Hey! :)', |
|
'Hello!', |
|
'How are you?' |
|
] |
|
}, |
|
{ |
|
includeAll: true, |
|
keywords: [ |
|
'how', 'are', 'you' |
|
], |
|
responses: [ |
|
'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?' |
|
] |
|
} |
|
] |
|
}, |
|
mounted() { |
|
this.getSavedData(); |
|
|
|
this.settingsModal = { |
|
name: this.name, |
|
username: this.username |
|
}; |
|
|
|
if (!this.username) { |
|
this.initialGreeting(); |
|
this.askForName(); |
|
|
|
this.isTyping = false; |
|
} else { |
|
this.welcomeBack(); |
|
} |
|
|
|
document.getElementById('chatinput').focus(); |
|
document.title = this.name; |
|
|
|
this.scrollDown(); |
|
}, |
|
methods: { |
|
addMessage(body, bot) { |
|
this.messages.push({ |
|
body: body, |
|
bot: bot, |
|
time: Date.now() |
|
}) |
|
}, |
|
botMessage(body) { |
|
this.addMessage(body, true); |
|
}, |
|
userMessage(body) { |
|
this.addMessage(body, false); |
|
|
|
this.updateStorage(); |
|
}, |
|
initialGreeting() { |
|
this.botMessage( |
|
this.oneOf( |
|
[ |
|
"Hi! I'm " + this.name + ". :)", |
|
"Hi, nice to meet you! My name is " + this.name + ". :)" |
|
] |
|
) |
|
); |
|
|
|
this.updateStorage(); |
|
}, |
|
welcomeBack() { |
|
this.botMessage( |
|
this.oneOf( |
|
[ |
|
"Hi! Haven't heard of you in a while. :)", |
|
"Welcome back! :)", |
|
"Hey :) Good to see you :)", |
|
] |
|
) |
|
); |
|
}, |
|
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(() => { |
|
if (this.askedForName === true) { |
|
this.setName(message) |
|
} else { |
|
let answer = this.getAnswer(message); |
|
if (answer) { |
|
this.botMessage(answer); |
|
} |
|
|
|
this.updateStorage(); |
|
} |
|
|
|
this.isTyping = false; |
|
this.scrollDown(); |
|
}, 3000); |
|
}, |
|
scrollDown() { |
|
$('#chat-box').stop().animate({ |
|
scrollTop: ($('#chat-box')[0].scrollHeight * 10) |
|
}, 800); |
|
}, |
|
oneOf(answers) { |
|
let amountOfAnswers = answers.length; |
|
let randomIndex = Math.floor(Math.random() * (amountOfAnswers)); |
|
console.log(randomIndex); |
|
|
|
console.log(answers[randomIndex]); |
|
return this.convertWildcards(answers[randomIndex]); |
|
}, |
|
convertWildcards(message) { |
|
message = message.replace('$name$', this.username); |
|
message = message.replace('$botname$', this.name); |
|
|
|
return message; |
|
}, |
|
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; |
|
}, |
|
cleanupMessage(message) { |
|
message = message.toLowerCase(); |
|
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) { |
|
message = this.cleanupMessage(message); |
|
let phrases = message.split(' '); |
|
let answer = undefined; |
|
|
|
if (this.includesAllOf(phrases, ['change', 'my', 'name'])) { |
|
this.askedForName = true; |
|
return "Please tell me how i should call you."; |
|
} |
|
|
|
if (this.includesAllOf(phrases, ['clear', 'chat'])) { |
|
this.clearChat() |
|
return false; |
|
} |
|
|
|
this.answers.forEach((answerOption) => { |
|
if (answerOption.includeAll === true) { |
|
if ( |
|
this.includesAllOf(phrases, answerOption.keywords) |
|
) { |
|
answer = this.oneOf(answerOption.responses); |
|
} |
|
} else { |
|
if ( |
|
this.includesOneOf(phrases, answerOption.keywords) |
|
) { |
|
answer = this.oneOf(answerOption.responses); |
|
} |
|
} |
|
}); |
|
|
|
if (answer) { |
|
return answer; |
|
} |
|
|
|
return 'I don\'t know what to say..'; |
|
}, |
|
addResponseToInput() { |
|
this.addModal.responses.push(''); |
|
}, |
|
addNewResponseContainer() { |
|
let includeAll = this.addModal.includeAll; |
|
let keywords = this.cleanupMessage(this.addModal.keywords).split(','); |
|
let responses = this.addModal.responses; |
|
console.log(this.addModal.responses); |
|
|
|
this.answers.push({ |
|
includeAll: includeAll, |
|
keywords: keywords, |
|
responses: responses |
|
}); |
|
|
|
this.addModal.includeAll = false; |
|
this.addModal.keywords = ''; |
|
this.addModal.responses = ['']; |
|
|
|
this.updateStorage(); |
|
}, |
|
updateStorage() { |
|
localStorage.setItem('name', this.name); |
|
localStorage.setItem('username', this.username); |
|
localStorage.setItem('messages', JSON.stringify(this.messages)); |
|
localStorage.setItem('answers', JSON.stringify(this.answers)); |
|
}, |
|
getSavedData() { |
|
let savedName = localStorage.getItem('name'); |
|
|
|
this.name = savedName ? savedName : this.name; |
|
|
|
let savedUsername = localStorage.getItem('username'); |
|
this.username = savedUsername ? savedUsername : null; |
|
|
|
let savedMessages = JSON.parse(localStorage.getItem('messages')); |
|
this.messages = savedMessages ? savedMessages : []; |
|
|
|
let savedAnswers = JSON.parse(localStorage.getItem('answers')); |
|
this.answers = savedAnswers ? savedAnswers : this.answers; |
|
|
|
this.scrollDown(); |
|
}, |
|
askForName() { |
|
this.botMessage( |
|
this.oneOf([ |
|
'May i ask for your name?', |
|
'Whats your name? :)', |
|
'How can i call you?', |
|
'How did your developers call you? :)' |
|
]) |
|
); |
|
|
|
this.askedForName = true; |
|
}, |
|
setName(message) { |
|
this.username = message.trim(); |
|
this.settingsModal.username = this.username; |
|
this.askedForName = false; |
|
this.updateStorage(); |
|
|
|
this.botMessage( |
|
this.oneOf([ |
|
"That's a beautiful name!", |
|
"Okay, i'll call you " + this.username + " from now on :)", |
|
"Nice to meet you, " + this.username + ". :D" |
|
]) |
|
) |
|
}, |
|
saveSettings() { |
|
this.name = this.settingsModal.name; |
|
this.username = this.settingsModal.username; |
|
this.updateStorage(); |
|
|
|
this.botMessage('Settings saved! :)'); |
|
this.scrollDown(); |
|
}, |
|
clearChat() { |
|
this.messages = []; |
|
this.botMessage('Chat cleared.'); |
|
} |
|
} |
|
}) |