-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat.js
82 lines (66 loc) · 2.14 KB
/
chat.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
const application = require('./index')
const secrets = require('./secrets')
const apiai = require('apiai')
const randomstring = require('randomstring')
const chatState = require('./chatstate')
const server = application.server
const io = require('socket.io').listen(server)
let sessions = []
io.on('connection', (socket) => {
console.log('User connected')
socket.on('disconnect', () => {
console.log('User disconnected')
})
const session = newSession(socket)
sessions.push(session)
socket.on('message', (message) => {
console.log('User: ', message.message)
socket.emit('message-success', { })
sendToAPI(message.message,session,function(data) {
const response = data.result.fulfillment.speech
console.log('Desiree: ' + response)
socket.emit('message',newMessage(response))
})
})
socket.emit('message', newMessage('Hi, I\'m Desiree. I\'m built to get you better grades. I\'m going to be asking some questions so please bear with me.'))
setTimeout(function(){
sendToAPI('start-data-collect',session,function(data) {
const response = data.result.fulfillment.speech
console.log('Desiree: ' + response)
socket.emit('message',newMessage(response))
})
},2000)
})
function newMessage(text) {
return {
message: text,
isSenderServer: true,
sent: true
}
}
function newSession(socket) {
return {
sessionId: randomstring.generate(16),
dbObjectId: null,
socket: socket,
state: 0,
rawData: null,
}
}
const desiree = apiai(secrets.apiai_token)
function sendToAPI(text,session,callback) {
var request = desiree.textRequest(text, {
sessionId: session.sessionId,
})
request.on('response', function(response) {
if(chatState.handleResponse(response,session)) return;
callback(response)
})
request.on('error', function(error) {
callback(error)
})
request.end()
}
module.exports.desiree = desiree
module.exports.sendToAPI = sendToAPI
module.exports.newMessage = newMessage