-
Notifications
You must be signed in to change notification settings - Fork 9
/
actions.js
343 lines (307 loc) · 11.8 KB
/
actions.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import fs from 'fs'
import axios from 'axios'
import OpenAI from 'openai'
import config from './config.js'
import { state, cache, cacheTTL } from './store.js'
// Initialize OpenAI client
const ai = new OpenAI({ apiKey: config.openaiKey })
// Base URL API endpoint. Do not edit!
const API_URL = config.apiBaseUrl
// Function to send a message using the Wassenger API
export async function sendMessage ({ phone, message, media, device, ...fields }) {
const url = `${API_URL}/messages`
const body = {
phone,
message,
media,
device,
...fields,
enqueue: 'never'
}
let retries = 3
while (retries) {
retries -= 1
try {
const res = await axios.post(url, body, {
headers: { Authorization: config.apiKey }
})
console.log('[info] Message sent:', phone, res.data.id, res.data.status)
return res.data
} catch (err) {
console.error('[error] failed to send message:', phone, message || (body.list ? body.list.description : '<no message>'), err.response ? err.response.data : err)
}
}
return false
}
export async function pullMembers (device) {
if (cache.members && +cache.members.time && (Date.now() - +cache.members.time) < cacheTTL) {
return cache.members.data
}
const url = `${API_URL}/devices/${device.id}/team`
const { data: members } = await axios.get(url, { headers: { Authorization: config.apiKey } })
cache.members = { data: members, time: Date.now() }
return members
}
export async function validateMembers (device, members) {
const validateMembers = (config.teamWhitelist || []).concat(config.teamBlacklist || [])
for (const id of validateMembers) {
if (typeof id !== 'string' || id.length !== 24) {
return exit('Team user ID in config.teamWhitelist and config.teamBlacklist must be a 24 characters hexadecimal value:', id)
}
const exists = members.some(user => user.id === id)
if (!exists) {
return exit('Team user ID in config.teamWhitelist or config.teamBlacklist does not exist:', id)
}
}
}
export async function createLabels (device) {
const labels = cache.labels.data || []
const requiredLabels = (config.setLabelsOnUserAssignment || []).concat(config.setLabelsOnBotChats || [])
const missingLabels = requiredLabels.filter(label => labels.every(l => l.name !== label))
for (const label of missingLabels) {
console.log('[info] creating missing label:', label)
const url = `${API_URL}/devices/${device.id}/labels`
const body = {
name: label.slice(0, 30).trim(),
color: [
'tomato', 'orange', 'sunflower', 'bubble',
'rose', 'poppy', 'rouge', 'raspberry',
'purple', 'lavender', 'violet', 'pool',
'emerald', 'kelly', 'apple', 'turquoise',
'aqua', 'gold', 'latte', 'cocoa'
][Math.floor(Math.random() * 20)],
description: 'Automatically created label for the chatbot'
}
try {
await axios.post(url, body, { headers: { Authorization: config.apiKey } })
} catch (err) {
console.error('[error] failed to create label:', label, err.message)
}
}
if (missingLabels.length) {
await pullLabels(device, { force: true })
}
}
export async function pullLabels (device, { force } = {}) {
if (!force && cache.labels && +cache.labels.time && (Date.now() - +cache.labels.time) < cacheTTL) {
return cache.labels.data
}
const url = `${API_URL}/devices/${device.id}/labels`
const { data: labels } = await axios.get(url, { headers: { Authorization: config.apiKey } })
cache.labels = { data: labels, time: Date.now() }
return labels
}
export async function updateChatLabels ({ data, device, labels }) {
const url = `${API_URL}/chat/${device.id}/chats/${data.chat.id}/labels`
const newLabels = (data.chat.labels || [])
for (const label of labels) {
if (newLabels.includes(label)) {
newLabels.push(label)
}
}
if (newLabels.length) {
console.log('[info] update chat labels:', data.chat.id, newLabels)
await axios.patch(url, newLabels, { headers: { Authorization: config.apiKey } })
}
}
export async function updateChatMetadata ({ data, device, metadata }) {
const url = `${API_URL}/chat/${device.id}/contacts/${data.chat.id}/metadata`
const entries = []
const contactMetadata = data.chat.contact.metadata
for (const entry of metadata) {
if (entry && entry.key && entry.value) {
const value = typeof entry.value === 'function' ? entry.value() : entry.value
if (!entry.key || !value || typeof entry.key !== 'string' || typeof value !== 'string') {
continue
}
if (contactMetadata && contactMetadata.some(e => e.key === entry.key && e.value === value)) {
continue // skip if metadata entry is already present
}
entries.push({
key: entry.key.slice(0, 30).trim(),
value: value.slice(0, 1000).trim()
})
}
}
if (entries.length) {
await axios.patch(url, entries, { headers: { Authorization: config.apiKey } })
}
}
export async function selectAssignMember (device) {
const members = await pullMembers(device)
const isMemberEligible = (member) => {
if (config.teamBlacklist.length && config.teamBlacklist.includes(member.id)) {
return false
}
if (config.teamWhitelist.length && !config.teamWhitelist.includes(member.id)) {
return false
}
if (config.assignOnlyToOnlineMembers && (member.availability.mode !== 'auto' || ((Date.now() - +new Date(member.lastSeenAt)) > 30 * 60 * 1000))) {
return false
}
if (config.skipTeamRolesFromAssignment && config.skipTeamRolesFromAssignment.some(role => member.role === role)) {
return false
}
return true
}
const activeMembers = members.filter(member => member.status === 'active' && isMemberEligible(member))
if (!activeMembers.length) {
return console.log('[warning] Unable to assign chat: no eligible team members')
}
const targetMember = activeMembers[activeMembers.length * Math.random() | 0]
return targetMember
}
async function assignChat ({ member, data, device }) {
const url = `${API_URL}/chat/${device.id}/chats/${data.chat.id}/owner`
const body = { agent: member.id }
await axios.patch(url, body, { headers: { Authorization: config.apiKey } })
if (config.setMetadataOnAssignment && config.setMetadataOnAssignment.length) {
const metadata = config.setMetadataOnAssignment.filter(entry => entry && entry.key && entry.value).map(({ key, value }) => ({ key, value }))
await updateChatMetadata({ data, device, metadata })
}
}
export async function assignChatToAgent ({ data, device, force }) {
if (!config.enableMemberChatAssignment && !force) {
return console.log('[debug] Unable to assign chat: member chat assignment is disabled. Enable it in config.enableMemberChatAssignment = true')
}
try {
const member = await selectAssignMember(device)
if (member) {
let updateLabels = []
// Remove labels before chat assigned, if required
if (config.removeLabelsAfterAssignment && config.setLabelsOnBotChats && config.setLabelsOnBotChats.length) {
const labels = (data.chat.labels || []).filter(label => !config.setLabelsOnBotChats.includes(label))
console.log('[info] remove labels before assiging chat to user', data.chat.id, labels)
if (labels.length) {
updateLabels = labels
}
}
// Set labels on chat assignment, if required
if (config.setLabelsOnUserAssignment && config.setLabelsOnUserAssignment.length) {
let labels = (data.chat.labels || [])
if (updateLabels.length) {
labels = labels.filter(label => !updateLabels.includes(label))
}
for (const label of config.setLabelsOnUserAssignment) {
if (!labels.includes(label)) {
updateLabels.push(label)
}
}
}
if (updateLabels.length) {
console.log('[info] set labels on chat assignment to user', data.chat.id, updateLabels)
await updateChatLabels({ data, device, labels: updateLabels })
}
console.log('[info] automatically assign chat to user:', data.chat.id, member.displayName, member.email)
await assignChat({ member, data, device })
} else {
console.log('[info] Unable to assign chat: no eligible or available team members based on the current configuration:', data.chat.id)
}
return member
} catch (err) {
console.error('[error] failed to assign chat:', data.id, data.chat.id, err)
}
}
export async function pullChatMessages ({ data, device }) {
try {
const url = `${API_URL}/chat/${device.id}/messages/?chat=${data.chat.id}&limit=25`
const res = await axios.get(url, { headers: { Authorization: config.apiKey } })
state[data.chat.id] = res.data.reduce((acc, message) => {
acc[message.id] = message
return acc
}, state[data.chat.id] || {})
return res.data
} catch (err) {
console.error('[error] failed to pull chat messages history:', data.id, data.chat.id, err)
}
}
// Find an active WhatsApp device connected to the Wassenger API
export async function loadDevice () {
const url = `${API_URL}/devices`
const { data } = await axios.get(url, {
headers: { Authorization: config.apiKey }
})
if (config.device && !config.device.includes(' ')) {
if (/^[a-f0-9]{24}$/i.test(config.device) === false) {
return exit('Invalid WhatsApp device ID: must be 24 characers hexadecimal value. Get the device ID here: https://app.wassenger.com/number')
}
return data.find(device => device.id === config.device)
}
return data.find(device => device.status === 'operative')
}
// Function to register a Ngrok tunnel webhook for the chatbot
// Only used in local development mode
export async function registerWebhook (tunnel, device) {
const webhookUrl = `${tunnel}/webhook`
const url = `${API_URL}/webhooks`
const { data: webhooks } = await axios.get(url, {
headers: { Authorization: config.apiKey }
})
const findWebhook = webhook => {
return (
webhook.url === webhookUrl &&
webhook.device === device.id &&
webhook.status === 'active' &&
webhook.events.includes('message:in:new')
)
}
// If webhook already exists, return it
const existing = webhooks.find(findWebhook)
if (existing) {
return existing
}
for (const webhook of webhooks) {
// Delete previous ngrok webhooks
if (webhook.url.includes('ngrok-free.app') || webhook.url.startsWith(tunnel)) {
const url = `${API_URL}/webhooks/${webhook.id}`
await axios.delete(url, { headers: { Authorization: config.apiKey } })
}
}
await new Promise(resolve => setTimeout(resolve, 500))
const data = {
url: webhookUrl,
name: 'Chatbot',
events: ['message:in:new'],
device: device.id
}
const { data: webhook } = await axios.post(url, data, {
headers: { Authorization: config.apiKey }
})
return webhook
}
export async function transcribeAudio ({ message, device }) {
if (!message?.media?.id) {
return false
}
try {
const url = `${API_URL}/chat/${device.id}/files/${message.media.id}/download`
const response = await axios.get(url, {
headers: { Authorization: config.apiKey },
responseType: 'stream'
})
if (response.status !== 200) {
return false
}
const tmpFile = `${message.media.id}.mp3`
await new Promise((resolve, reject) => {
const writer = fs.createWriteStream(tmpFile)
response.data.pipe(writer)
writer.on('finish', () => resolve())
writer.on('error', reject)
})
const transcription = await ai.audio.transcriptions.create({
file: fs.createReadStream(tmpFile),
model: 'whisper-1',
response_format: 'text'
})
await fs.promises.unlink(tmpFile)
return transcription
} catch (err) {
console.error('[error] failed to transcribe audio:', message.fromNumber, message.media.id, err.message)
return false
}
}
export function exit (msg, ...args) {
console.error('[error]', msg, ...args)
process.exit(1)
}