This repository has been archived by the owner on Feb 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vcu-altlab-online-chatbot.js
101 lines (93 loc) · 3.47 KB
/
vcu-altlab-online-chatbot.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
(function(Vue) {
Vue.component('ChatMessage', {
props: ['message'],
template: `
<div class="vcu-online-message">
<span v-if="message.from === 'bot' " class="from-title from-bot"> Online@VCU </span>
<span v-if="message.from === 'you' " class="from-title from-human"> You </span>
<span v-if="message.initialMessage === true" v-html="message.content"> </span>
<span v-if="!message.initialMessage" v-html="message.content"></span>
</div>
`
})
let virtualAssistant = new Vue({
el: '#vcu-online-virtual-assistant',
data () {
return {
chatText: '',
messages: [],
isVisible: false,
timestamp: null,
userID: null,
url: 'https://fqtjet7xb6.execute-api.us-east-1.amazonaws.com/default/lexBotIntegration',
autoQuestions: {
'blackboard': 'I am having trouble with blackboard',
'tuitionAndFees': 'I need information about tuition and fees'
}
}
},
methods: {
toggleVirtualAssistant: function () {
this.isVisible = !this.isVisible
},
updateBotState: function (state) {
this.botState = state
},
scrollChatWindow: function () {
setTimeout(function ( ){
let chatWindow = document.querySelector('.vcu-online-chat-window')
chatWindow.scrollTop = chatWindow.scrollHeight
}, 500)
},
clearChatInput: function () {
this.chatText = ''
},
autoSubmitQuestion: function (question) {
this.chatText = this.autoQuestions[question]
this.sendTextChatMessage()
},
sendTextChatMessage: function ( ) {
if (this.userID == null) {
this.userID = Date.now()
}
this.timestamp = Date.now()
let newChat = this.chatText
console.log(newChat)
this.postChatMessage(newChat)
},
postChatMessage: function (newChat) {
this.displayResponse(newChat,'you')
let body = {
"body-json": {
"timestamp": this.timestamp,
"body": newChat,
"userID": this.userID
}
}
fetch(this.url, {
method: 'POST',
body: JSON.stringify(body),
headers: new Headers({
'Content-Type': 'application/json'
})
}).then(res => res.json())
.catch(error => console.error('Error:', error))
.then(response => console.log('Success:', this.displayResponse(response, 'bot') ))
this.clearChatInput()
},
displayResponse: function (message, from) {
let messageObject = {
'from': from,
'content': message,
'time': new Date().toLocaleTimeString()
}
this.messages.push(messageObject)
this.scrollChatWindow()
}
},
created () {
},
mounted () {
}
})
})(Vue)