-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
138 lines (120 loc) · 3.43 KB
/
index.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
const debug = require('debug')('botium-connector-botkit-websocket')
const WebSocket = require('ws')
const Capabilities = {
BOTKIT_SERVER_URL: 'BOTKIT_SERVER_URL'
}
class BotiumConnectorBotkitWebsocket {
constructor ({ queueBotSays, caps }) {
this.queueBotSays = queueBotSays
this.caps = caps
this.counter = 1
this.sessionIdPrefix = 'test' + new Date().getTime()
// buttons returned by the last bot response
this.activeButtons = {}
}
Validate () {
debug('Validate called')
if (!this.caps[Capabilities.BOTKIT_SERVER_URL]) {
throw new Error('BOTKIT_SERVER_URL capability required')
}
return Promise.resolve()
}
Build () {
debug('Build called')
}
Start () {
debug('Start called')
this.sessionId = this.sessionIdPrefix + this.counter
this.counter++
const socket = new WebSocket(this.caps[Capabilities.BOTKIT_SERVER_URL])
this.socket = socket
socket.on('message', (encodedMessage) => {
const message = JSON.parse(encodedMessage)
const messageText = message.text
const buttons = message.quick_replies && message.quick_replies.map(r => {
return {
text: r.title,
payload: r.payload
}
})
this.activeButtons = buttons
if (messageText || buttons) {
if (messageText) {
debug('Bot says ' + messageText)
}
if (buttons) {
const buttonText = buttons && buttons.map(b => {
return b.text
}).join(',')
debug('Bot displays buttons ' + buttonText)
}
const botMsg = { sender: 'bot', sourceData: message, messageText, buttons }
this.queueBotSays(botMsg)
} else {
debug('Received Websocket Message without text: ' + encodedMessage)
}
})
return new Promise((resolve, reject) => {
socket.on('open', function () {
resolve()
})
socket.on('error', function (err) {
reject(err)
})
})
}
UserSays ({ messageText, buttons, SET_FIELD }) {
if (buttons && buttons.length > 0) {
const buttonText = buttons[0].text.toLowerCase()
const matchingButton = this.activeButtons.find(button => {
return button.text.toLowerCase() === buttonText
})
if (matchingButton === undefined) {
return Promise.reject(new Error('There is no button ' + buttonText))
}
messageText = matchingButton.payload
debug('User clicked button "' + buttons[0].text + '" which sends text ' + messageText)
} else {
debug('User says ' + messageText)
}
const message = {
type: 'message',
text: messageText,
user: this.sessionId,
channel: 'socket'
}
if (typeof SET_FIELD === 'object') {
debug('User sets message fields ' + JSON.stringify(SET_FIELD))
Object.assign(message, SET_FIELD)
}
return new Promise((resolve, reject) => {
this.socket.send(JSON.stringify(message), {}, (err) => {
if (err) {
reject(err)
}
resolve()
})
})
}
Stop () {
debug('Stop called')
return new Promise((resolve, reject) => {
setTimeout(() => {
try {
this.socket.terminate()
resolve()
} catch (err) {
reject(err)
}
}, 1000)
})
}
Clean () {
debug('Clean called')
return Promise.resolve()
}
}
module.exports = {
PluginVersion: 1,
PluginClass: BotiumConnectorBotkitWebsocket
}