let kara = new Vue({ el: '#kara', data: { messages: [], name: 'Kara', chatbox: null, isTyping: false, templates: { initialGreeting: "Hi! I'm Kara. :)" }, addModal: { includeAll: false, keywords: '', responses: [ 'Answer #1' ] }, 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.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(() => { let answer = this.getAnswer(message); console.log(answer); this.aiMessage(answer); this.scrollDown(); }, 3000); }, scrollDown() { $('#chat-box').stop().animate({ scrollTop: $('#chat-box')[0].scrollHeight }, 800); }, oneOf(answers) { let amountOfAnswers = answers.length; 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; }, 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; 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 }); } } })