@tiledesk/tiledesk-whatsapp-connector 0.1.30 → 0.1.32
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +257 -402
- package/package.json +1 -1
- package/template/detail.html +27 -11
- package/tiledesk/MessageHandler.js +1 -1
- package/tiledesk/TiledeskBotTester.js +137 -0
- package/tiledesk/TiledeskChannel.js +0 -1
package/index.js
CHANGED
|
@@ -18,6 +18,7 @@ const { TiledeskWhatsapp } = require('./tiledesk/TiledeskWhatsapp');
|
|
|
18
18
|
const { TiledeskChannel } = require('./tiledesk/TiledeskChannel');
|
|
19
19
|
const { TiledeskAppsClient } = require('./tiledesk/TiledeskAppsClient');
|
|
20
20
|
const { MessageHandler } = require('./tiledesk/MessageHandler');
|
|
21
|
+
const { TiledeskBotTester } = require('./tiledesk/TiledeskBotTester');
|
|
21
22
|
|
|
22
23
|
// mongo
|
|
23
24
|
const { KVBaseMongo } = require('@tiledesk/tiledesk-kvbasemongo');
|
|
@@ -42,7 +43,6 @@ let REDIS_PORT = null;
|
|
|
42
43
|
let REDIS_PASSWORD = null;
|
|
43
44
|
let log = false;
|
|
44
45
|
|
|
45
|
-
|
|
46
46
|
router.get('/', async (req, res) => {
|
|
47
47
|
res.send('Welcome to Tiledesk-WhatsApp Business connector!')
|
|
48
48
|
})
|
|
@@ -63,7 +63,7 @@ router.get('/detail', async (req, res) => {
|
|
|
63
63
|
|
|
64
64
|
readHTMLFile('/detail.html', (err, html) => {
|
|
65
65
|
if (err) {
|
|
66
|
-
console.log("(
|
|
66
|
+
console.log("(whatsapp) error read html file: ", err);
|
|
67
67
|
}
|
|
68
68
|
|
|
69
69
|
var template = handlebars.compile(html);
|
|
@@ -74,9 +74,6 @@ router.get('/detail', async (req, res) => {
|
|
|
74
74
|
app_id: app_id,
|
|
75
75
|
installed: installed
|
|
76
76
|
}
|
|
77
|
-
if (log) {
|
|
78
|
-
console.log("Replacements: ", replacements);
|
|
79
|
-
}
|
|
80
77
|
var html = template(replacements);
|
|
81
78
|
res.send(html);
|
|
82
79
|
})
|
|
@@ -88,28 +85,25 @@ router.post('/install', async (req, res) => {
|
|
|
88
85
|
let app_id = req.body.app_id;
|
|
89
86
|
let token = req.body.token;
|
|
90
87
|
|
|
91
|
-
console.log("Install app " + app_id + " for project id " + project_id);
|
|
88
|
+
console.log("(whatsapp) Install app " + app_id + " for project id " + project_id);
|
|
92
89
|
let installation_info = {
|
|
93
90
|
project_id: project_id,
|
|
94
91
|
app_id: app_id,
|
|
95
92
|
createdAt: Date.now()
|
|
96
93
|
};
|
|
97
94
|
|
|
98
|
-
console.log("installation info: ", installation_info)
|
|
99
|
-
|
|
100
95
|
const appClient = new TiledeskAppsClient({ APPS_API_URL: APPS_API_URL });
|
|
101
|
-
//let installation = await appClient.install(installation_info);
|
|
102
96
|
appClient.install(installation_info).then((installation) => {
|
|
103
97
|
|
|
104
98
|
if (log) {
|
|
105
|
-
console.log("installation response: ", installation);
|
|
99
|
+
console.log("(whatsapp) installation response: ", installation);
|
|
106
100
|
}
|
|
107
101
|
|
|
108
102
|
let installed = true;
|
|
109
103
|
|
|
110
104
|
readHTMLFile('/detail.html', (err, html) => {
|
|
111
105
|
if (err) {
|
|
112
|
-
console.log("(
|
|
106
|
+
console.log("(whatsapp) error read html file: ", err);
|
|
113
107
|
}
|
|
114
108
|
|
|
115
109
|
var template = handlebars.compile(html);
|
|
@@ -120,15 +114,12 @@ router.post('/install', async (req, res) => {
|
|
|
120
114
|
app_id: app_id,
|
|
121
115
|
installed: installed
|
|
122
116
|
}
|
|
123
|
-
if (log) {
|
|
124
|
-
console.log("Replacements: ", replacements);
|
|
125
|
-
}
|
|
126
117
|
var html = template(replacements);
|
|
127
118
|
res.send(html);
|
|
128
119
|
})
|
|
129
120
|
|
|
130
121
|
}).catch((err) => {
|
|
131
|
-
console.error("installation error: ", err.data)
|
|
122
|
+
console.error("(whatsapp) installation error: ", err.data)
|
|
132
123
|
res.send("An error occurred during the installation");
|
|
133
124
|
})
|
|
134
125
|
|
|
@@ -143,14 +134,14 @@ router.post('/uninstall', async (req, res) => {
|
|
|
143
134
|
appClient.uninstall(project_id, app_id).then((response) => {
|
|
144
135
|
|
|
145
136
|
if (log) {
|
|
146
|
-
console.log("uninstallation response: ", response);
|
|
137
|
+
console.log("(whatsapp) uninstallation response: ", response);
|
|
147
138
|
}
|
|
148
139
|
|
|
149
140
|
let installed = false;
|
|
150
141
|
|
|
151
142
|
readHTMLFile('/detail.html', (err, html) => {
|
|
152
143
|
if (err) {
|
|
153
|
-
console.log("(
|
|
144
|
+
console.log("(whatsapp) error read html file: ", err);
|
|
154
145
|
}
|
|
155
146
|
|
|
156
147
|
var template = handlebars.compile(html);
|
|
@@ -161,23 +152,20 @@ router.post('/uninstall', async (req, res) => {
|
|
|
161
152
|
app_id: app_id,
|
|
162
153
|
installed: installed
|
|
163
154
|
}
|
|
164
|
-
if (log) {
|
|
165
|
-
console.log("Replacements: ", replacements);
|
|
166
|
-
}
|
|
167
155
|
var html = template(replacements);
|
|
168
156
|
res.send(html);
|
|
169
157
|
})
|
|
170
158
|
|
|
171
159
|
}).catch((err) => {
|
|
172
|
-
console.error("uninsallation error: ", err.data)
|
|
160
|
+
console.error("(whatsapp) uninsallation error: ", err.data)
|
|
173
161
|
res.send("An error occurred during the uninstallation");
|
|
174
162
|
})
|
|
175
163
|
})
|
|
176
164
|
|
|
177
165
|
router.get('/configure', async (req, res) => {
|
|
178
|
-
console.log("\n/configure");
|
|
166
|
+
console.log("\n(whatsapp) /configure");
|
|
179
167
|
if (log) {
|
|
180
|
-
console.log("/configure query: ", req.query);
|
|
168
|
+
console.log("(whatsapp) /configure query: ", req.query);
|
|
181
169
|
}
|
|
182
170
|
|
|
183
171
|
let projectId = "";
|
|
@@ -191,13 +179,15 @@ router.get('/configure', async (req, res) => {
|
|
|
191
179
|
let CONTENT_KEY = "whatsapp-" + projectId;
|
|
192
180
|
|
|
193
181
|
let settings = await db.get(CONTENT_KEY);
|
|
194
|
-
|
|
182
|
+
if (log) {
|
|
183
|
+
console.log("(whatsapp) kvdb settings: ", settings);
|
|
184
|
+
}
|
|
195
185
|
|
|
196
186
|
if (settings) {
|
|
197
187
|
|
|
198
188
|
readHTMLFile('/configure.html', (err, html) => {
|
|
199
189
|
if (err) {
|
|
200
|
-
console.log("(
|
|
190
|
+
console.log("(whatsapp) error read html file: ", err);
|
|
201
191
|
}
|
|
202
192
|
|
|
203
193
|
var template = handlebars.compile(html);
|
|
@@ -210,9 +200,6 @@ router.get('/configure', async (req, res) => {
|
|
|
210
200
|
verify_token: settings.verify_token,
|
|
211
201
|
subscription_id: settings.subscriptionId,
|
|
212
202
|
}
|
|
213
|
-
if (log) {
|
|
214
|
-
console.log("Replacements: ", replacements);
|
|
215
|
-
}
|
|
216
203
|
var html = template(replacements);
|
|
217
204
|
res.send(html);
|
|
218
205
|
|
|
@@ -223,7 +210,7 @@ router.get('/configure', async (req, res) => {
|
|
|
223
210
|
readHTMLFile('/configure.html', (err, html) => {
|
|
224
211
|
|
|
225
212
|
if (err) {
|
|
226
|
-
console.log("(
|
|
213
|
+
console.log("(whatsapp) error read html file: ", err);
|
|
227
214
|
}
|
|
228
215
|
|
|
229
216
|
var template = handlebars.compile(html);
|
|
@@ -233,9 +220,6 @@ router.get('/configure', async (req, res) => {
|
|
|
233
220
|
token: token,
|
|
234
221
|
proxy_url: proxy_url,
|
|
235
222
|
}
|
|
236
|
-
if (log) {
|
|
237
|
-
console.log("Replacements: ", replacements);
|
|
238
|
-
}
|
|
239
223
|
var html = template(replacements);
|
|
240
224
|
res.send(html);
|
|
241
225
|
|
|
@@ -244,9 +228,9 @@ router.get('/configure', async (req, res) => {
|
|
|
244
228
|
})
|
|
245
229
|
|
|
246
230
|
router.post('/update', async (req, res) => {
|
|
247
|
-
console.log("\n/update");
|
|
231
|
+
console.log("\n(whatsapp) /update");
|
|
248
232
|
if (log) {
|
|
249
|
-
console.log("/update body: ", req.body);
|
|
233
|
+
console.log("(whatsapp) /update body: ", req.body);
|
|
250
234
|
}
|
|
251
235
|
|
|
252
236
|
let projectId = req.body.project_id;
|
|
@@ -268,7 +252,7 @@ router.post('/update', async (req, res) => {
|
|
|
268
252
|
|
|
269
253
|
readHTMLFile('/configure.html', (err, html) => {
|
|
270
254
|
if (err) {
|
|
271
|
-
console.log("(
|
|
255
|
+
console.log("(whatsapp) error read html file: ", err);
|
|
272
256
|
}
|
|
273
257
|
|
|
274
258
|
var template = handlebars.compile(html);
|
|
@@ -282,10 +266,6 @@ router.post('/update', async (req, res) => {
|
|
|
282
266
|
verify_token: settings.verify_token,
|
|
283
267
|
subscription_id: settings.subscriptionId,
|
|
284
268
|
}
|
|
285
|
-
if (log) {
|
|
286
|
-
console.log("Replacements: ", replacements);
|
|
287
|
-
}
|
|
288
|
-
|
|
289
269
|
var html = template(replacements);
|
|
290
270
|
res.send(html);
|
|
291
271
|
})
|
|
@@ -310,7 +290,7 @@ router.post('/update', async (req, res) => {
|
|
|
310
290
|
tdClient.subscribe(subscription_info).then((data) => {
|
|
311
291
|
let subscription = data;
|
|
312
292
|
if (log) {
|
|
313
|
-
console.log("\
|
|
293
|
+
console.log("\n(whatsapp) Subscription: ", subscription)
|
|
314
294
|
}
|
|
315
295
|
|
|
316
296
|
let settings = {
|
|
@@ -328,7 +308,7 @@ router.post('/update', async (req, res) => {
|
|
|
328
308
|
|
|
329
309
|
readHTMLFile('/configure.html', (err, html) => {
|
|
330
310
|
if (err) {
|
|
331
|
-
console.log("(
|
|
311
|
+
console.log("(whatsapp) error read html file: ", err);
|
|
332
312
|
}
|
|
333
313
|
|
|
334
314
|
var template = handlebars.compile(html);
|
|
@@ -342,10 +322,6 @@ router.post('/update', async (req, res) => {
|
|
|
342
322
|
verify_token: settings.verify_token,
|
|
343
323
|
subscription_id: settings.subscriptionId,
|
|
344
324
|
}
|
|
345
|
-
if (log) {
|
|
346
|
-
console.log("Replacements: ", replacements);
|
|
347
|
-
}
|
|
348
|
-
|
|
349
325
|
var html = template(replacements);
|
|
350
326
|
res.send(html);
|
|
351
327
|
})
|
|
@@ -354,7 +330,7 @@ router.post('/update', async (req, res) => {
|
|
|
354
330
|
|
|
355
331
|
readHTMLFile('/configure.html', (err, html) => {
|
|
356
332
|
if (err) {
|
|
357
|
-
console.log("(
|
|
333
|
+
console.log("(whatsapp) error read html file: ", err);
|
|
358
334
|
}
|
|
359
335
|
|
|
360
336
|
var template = handlebars.compile(html);
|
|
@@ -365,10 +341,6 @@ router.post('/update', async (req, res) => {
|
|
|
365
341
|
proxy_url: proxy_url,
|
|
366
342
|
show_error_modal: true
|
|
367
343
|
}
|
|
368
|
-
if (log) {
|
|
369
|
-
console.log("Replacements: ", replacements);
|
|
370
|
-
}
|
|
371
|
-
|
|
372
344
|
var html = template(replacements);
|
|
373
345
|
res.send(html);
|
|
374
346
|
})
|
|
@@ -378,20 +350,19 @@ router.post('/update', async (req, res) => {
|
|
|
378
350
|
})
|
|
379
351
|
|
|
380
352
|
router.post('/disconnect', async (req, res) => {
|
|
381
|
-
console.log("\n/disconnect")
|
|
353
|
+
console.log("\n(whatsapp) /disconnect")
|
|
382
354
|
if (log) {
|
|
383
|
-
console.log("/disconnect body: ", req.body)
|
|
355
|
+
console.log("(whatsapp) /disconnect body: ", req.body)
|
|
384
356
|
}
|
|
385
357
|
|
|
386
358
|
let projectId = req.body.project_id;
|
|
387
359
|
let token = req.body.token;
|
|
388
360
|
let subscriptionId = req.body.subscription_id;
|
|
389
361
|
|
|
390
|
-
console.log("uninstall --> subscriptionId: ", subscriptionId)
|
|
391
362
|
|
|
392
363
|
let CONTENT_KEY = "whatsapp-" + projectId;
|
|
393
364
|
await db.remove(CONTENT_KEY);
|
|
394
|
-
console.log("Content deleted.");
|
|
365
|
+
console.log("(whatsapp) Content deleted.");
|
|
395
366
|
|
|
396
367
|
let proxy_url = BASE_URL + "/webhook/" + projectId;
|
|
397
368
|
|
|
@@ -409,7 +380,7 @@ router.post('/disconnect', async (req, res) => {
|
|
|
409
380
|
readHTMLFile('/configure.html', (err, html) => {
|
|
410
381
|
|
|
411
382
|
if (err) {
|
|
412
|
-
console.log("(
|
|
383
|
+
console.log("(whatsapp) error read html file: ", err);
|
|
413
384
|
}
|
|
414
385
|
|
|
415
386
|
var template = handlebars.compile(html);
|
|
@@ -419,31 +390,25 @@ router.post('/disconnect', async (req, res) => {
|
|
|
419
390
|
token: token,
|
|
420
391
|
proxy_url: proxy_url,
|
|
421
392
|
}
|
|
422
|
-
if (log) {
|
|
423
|
-
console.log("Replacements: ", replacements);
|
|
424
|
-
}
|
|
425
|
-
|
|
426
393
|
var html = template(replacements);
|
|
427
394
|
res.send(html);
|
|
428
395
|
})
|
|
429
396
|
|
|
430
397
|
}).catch((err) => {
|
|
431
|
-
console.error("(
|
|
398
|
+
console.error("(whatsapp) error unsubscribe: ", err);
|
|
432
399
|
})
|
|
433
400
|
|
|
434
401
|
})
|
|
435
402
|
|
|
436
403
|
router.post('/tiledesk', async (req, res) => {
|
|
437
|
-
console.log("\n/tiledesk")
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
404
|
+
console.log("\n(whatsapp) /tiledesk")
|
|
405
|
+
if (log) {
|
|
406
|
+
console.log("(whatsapp) /tiledesk ---> tiledeskChannelMessage: " + JSON.stringify(req.body.payload));
|
|
407
|
+
}
|
|
441
408
|
|
|
442
409
|
var tiledeskChannelMessage = req.body.payload;
|
|
443
|
-
|
|
444
|
-
|
|
410
|
+
|
|
445
411
|
var projectId = req.body.payload.id_project;
|
|
446
|
-
console.log("(TILEDESK) PROJECT ID: ", projectId);
|
|
447
412
|
|
|
448
413
|
// get settings from mongo
|
|
449
414
|
let CONTENT_KEY = "whatsapp-" + projectId;
|
|
@@ -451,21 +416,22 @@ router.post('/tiledesk', async (req, res) => {
|
|
|
451
416
|
let wab_token = settings.wab_token;
|
|
452
417
|
|
|
453
418
|
var text = req.body.payload.text;
|
|
454
|
-
|
|
419
|
+
if (log) {
|
|
420
|
+
console.log("(whatsapp) /tiledesk text: ", text);
|
|
421
|
+
}
|
|
455
422
|
|
|
456
423
|
let attributes = req.body.payload.attributes;
|
|
457
|
-
console.log("(TILEDESK) ATTRIBUTES: ", JSON.stringify(attributes, null, 2))
|
|
458
424
|
if (log) {
|
|
425
|
+
console.log("(whatsapp) /tiledesk attributes: ", JSON.stringify(attributes))
|
|
459
426
|
}
|
|
460
427
|
|
|
461
428
|
let commands;
|
|
462
429
|
if (attributes && attributes.commands) {
|
|
463
430
|
commands = attributes.commands;
|
|
464
|
-
console.log("commands: ", JSON.stringify(commands, null, 2));
|
|
465
431
|
}
|
|
466
432
|
|
|
467
433
|
var sender_id = req.body.payload.sender;
|
|
468
|
-
console.log("(
|
|
434
|
+
console.log("(whatsapp) /tiledesk sender_id: ", sender_id);
|
|
469
435
|
|
|
470
436
|
if (sender_id.indexOf("wab") > -1) {
|
|
471
437
|
console.log("Skip same sender");
|
|
@@ -473,69 +439,58 @@ router.post('/tiledesk', async (req, res) => {
|
|
|
473
439
|
}
|
|
474
440
|
|
|
475
441
|
if (attributes && attributes.subtype === "info") {
|
|
476
|
-
console.log("(
|
|
442
|
+
console.log("(whatsapp) /tiledesk Skip subtype (info)");
|
|
477
443
|
return res.send(200);
|
|
478
444
|
}
|
|
479
445
|
|
|
480
446
|
if (attributes && attributes.subtype === 'info/support') {
|
|
481
|
-
console.log("Skip subtype: ", attributes.subtype);
|
|
447
|
+
console.log("(whatsapp) /tiledesk Skip subtype: ", attributes.subtype);
|
|
482
448
|
return res.send(200);
|
|
483
449
|
}
|
|
484
450
|
|
|
485
|
-
// Redirect for testing bots
|
|
486
|
-
//if (text.startsWith("#td")) {
|
|
487
|
-
// return res.redirect('/testitout');
|
|
488
|
-
//}
|
|
489
|
-
|
|
490
|
-
//console.log("/tiledesk ---> tiledeskChannelMessage: " + JSON.stringify(req.body.payload));
|
|
491
|
-
|
|
492
451
|
let recipient_id = tiledeskChannelMessage.recipient;
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
452
|
+
if (log) {
|
|
453
|
+
console.log("(whatsapp) /tiledesk Recipient_id: ", recipient_id);
|
|
454
|
+
}
|
|
496
455
|
|
|
497
456
|
let whatsapp_receiver = recipient_id.substring(recipient_id.lastIndexOf("-") + 1);
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
457
|
+
if (log) {
|
|
458
|
+
console.log("(whatsapp) /tiledesk whatsapp_receiver: ", whatsapp_receiver);
|
|
459
|
+
}
|
|
501
460
|
|
|
502
461
|
let phone_number_id = recipient_id.substring(recipient_id.lastIndexOf("wab-") + 4, recipient_id.lastIndexOf("-"));
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
462
|
+
if (log) {
|
|
463
|
+
console.log("(Tiledesk) phone_number_id: ", phone_number_id)
|
|
464
|
+
}
|
|
506
465
|
|
|
507
466
|
const messageHandler = new MessageHandler({ tiledeskChannelMessage: tiledeskChannelMessage });
|
|
508
467
|
const tlr = new TiledeskWhatsappTranslator();
|
|
509
468
|
|
|
510
469
|
if (commands) {
|
|
511
|
-
const that = this;
|
|
512
470
|
let i = 0;
|
|
513
471
|
async function execute(command) {
|
|
514
472
|
// message
|
|
515
|
-
console.log/("----> next command: ", command);
|
|
516
473
|
if (command.type === "message") {
|
|
517
474
|
let tiledeskCommandMessage = await messageHandler.generateMessageObject(command);
|
|
518
|
-
console.log("message generated
|
|
475
|
+
console.log("(whatsapp) message generated\ntiledeskCommandMessage: ", JSON.stringify(tiledeskCommandMessage))
|
|
519
476
|
|
|
520
477
|
let whatsappJsonMessage = await tlr.toWhatsapp(tiledeskCommandMessage, whatsapp_receiver);
|
|
521
|
-
console.log(
|
|
478
|
+
console.log("🟢 whatsappJsonMessage", JSON.stringify(whatsappJsonMessage))
|
|
522
479
|
|
|
523
|
-
|
|
524
480
|
if (whatsappJsonMessage) {
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
//console.log("Send message response: ", response.status, response.statusText);
|
|
481
|
+
const twClient = new TiledeskWhatsapp({ token: settings.wab_token, GRAPH_URL: GRAPH_URL });
|
|
482
|
+
twClient.sendMessage(phone_number_id, whatsappJsonMessage).then((response) => {
|
|
528
483
|
i += 1;
|
|
529
484
|
if (i < commands.length) {
|
|
530
485
|
execute(commands[i]);
|
|
531
486
|
} else {
|
|
532
|
-
console.log("End of commands")
|
|
487
|
+
console.log("(whatsapp) End of commands")
|
|
533
488
|
}
|
|
534
489
|
}).catch((err) => {
|
|
535
|
-
console.error("
|
|
490
|
+
console.error("(whatsapp) error send message: ", err);
|
|
536
491
|
})
|
|
537
492
|
} else {
|
|
538
|
-
console.error("Whatsapp Json Message is undefined!")
|
|
493
|
+
console.error("(whatsapp) Whatsapp Json Message is undefined!")
|
|
539
494
|
}
|
|
540
495
|
|
|
541
496
|
}
|
|
@@ -543,12 +498,11 @@ router.post('/tiledesk', async (req, res) => {
|
|
|
543
498
|
//wait
|
|
544
499
|
if (command.type === "wait") {
|
|
545
500
|
setTimeout(() => {
|
|
546
|
-
console.log("wait for " + command.time + " s")
|
|
547
501
|
i += 1;
|
|
548
502
|
if (i < commands.length) {
|
|
549
503
|
execute(commands[i]);
|
|
550
504
|
} else {
|
|
551
|
-
console.log("End of commands")
|
|
505
|
+
console.log("(whatsapp) End of commands")
|
|
552
506
|
}
|
|
553
507
|
}, command.time)
|
|
554
508
|
}
|
|
@@ -558,104 +512,26 @@ router.post('/tiledesk', async (req, res) => {
|
|
|
558
512
|
|
|
559
513
|
else if (tiledeskChannelMessage.text) {
|
|
560
514
|
|
|
561
|
-
console.log("tiledeskChannelMessage.text: ", tiledeskChannelMessage.text)
|
|
562
|
-
|
|
563
515
|
let whatsappJsonMessage = await tlr.toWhatsapp(tiledeskChannelMessage, whatsapp_receiver);
|
|
564
|
-
console.log(
|
|
565
|
-
//console.log("whatsappJsonMessage: ", whatsappJsonMessage);
|
|
516
|
+
console.log("🟢 whatsappJsonMessage", JSON.stringify(whatsappJsonMessage))
|
|
566
517
|
|
|
567
518
|
if (whatsappJsonMessage) {
|
|
568
519
|
const twClient = new TiledeskWhatsapp({ token: settings.wab_token, GRAPH_URL: GRAPH_URL });
|
|
569
520
|
|
|
570
521
|
twClient.sendMessage(phone_number_id, whatsappJsonMessage).then((response) => {
|
|
571
|
-
console.log("Send message response: ", response.status, response.statusText);
|
|
522
|
+
console.log("(whatsapp) Send message response: ", response.status, response.statusText);
|
|
572
523
|
}).catch((err) => {
|
|
573
|
-
console.error("
|
|
524
|
+
console.error("(whatsapp) error send message: ", err);
|
|
574
525
|
})
|
|
575
526
|
|
|
576
|
-
/*
|
|
577
|
-
sendMessage(phone_number_id, whatsappJsonMessage, wab_token).then((response) => {
|
|
578
|
-
console.log("sendMessage() response: ", response);
|
|
579
|
-
}).catch((err) => {
|
|
580
|
-
console.log("ERROR sendMessage(): ", err);
|
|
581
|
-
})
|
|
582
|
-
*/
|
|
583
527
|
} else {
|
|
584
|
-
console.error("Whatsapp Json Message is undefined!")
|
|
528
|
+
console.error("(whatsapp) Whatsapp Json Message is undefined!")
|
|
585
529
|
}
|
|
586
530
|
|
|
587
531
|
} else {
|
|
588
|
-
console.log("no command, no text --> skip")
|
|
532
|
+
console.log("(whatsapp) no command, no text --> skip")
|
|
589
533
|
}
|
|
590
534
|
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
/*
|
|
595
|
-
if (commands) {
|
|
596
|
-
commands.forEach( async (command) => {
|
|
597
|
-
console.log("\ncommand: ", command);
|
|
598
|
-
if (command.type === "message") {
|
|
599
|
-
let tiledeskCommandMessage = await messageHandler.generateMessageObject(command);
|
|
600
|
-
console.log("message generated!\ntiledeskCommandMessage: ", tiledeskCommandMessage)
|
|
601
|
-
|
|
602
|
-
let whatsappJsonMessage = await tlr.toWhatsapp(tiledeskCommandMessage, whatsapp_receiver);
|
|
603
|
-
console.log('\x1b[34m%s\x1b[0m', 'whatsappJsonMessage', whatsappJsonMessage)
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
if (whatsappJsonMessage) {
|
|
607
|
-
const twClient = new TiledeskWhatsapp({ token: settings.wab_token, GRAPH_URL: GRAPH_URL });
|
|
608
|
-
twClient.sendMessage(phone_number_id, whatsappJsonMessage).then((response) => {
|
|
609
|
-
console.log("Send message response: ", response.status, response.statusText);
|
|
610
|
-
}).catch((err) => {
|
|
611
|
-
console.error("ERROR Send message: ", err);
|
|
612
|
-
})
|
|
613
|
-
} else {
|
|
614
|
-
console.error("Whatsapp Json Message is undefined!")
|
|
615
|
-
}
|
|
616
|
-
|
|
617
|
-
}
|
|
618
|
-
if (command.type === "wait") {
|
|
619
|
-
setTimeout(() => {
|
|
620
|
-
console.log("wait for " + command.time + " s")
|
|
621
|
-
console.log("Next command")
|
|
622
|
-
}, command.time)
|
|
623
|
-
}
|
|
624
|
-
})
|
|
625
|
-
}
|
|
626
|
-
*/
|
|
627
|
-
//new_tiledesk_message = addCommandmessage(commands[0]);
|
|
628
|
-
|
|
629
|
-
/*
|
|
630
|
-
let whatsappJsonMessage = await tlr.toWhatsapp(tiledeskChannelMessage, whatsapp_receiver);
|
|
631
|
-
console.log('\x1b[34m%s\x1b[0m', 'whatsappJsonMessage', whatsappJsonMessage)
|
|
632
|
-
//console.log("whatsappJsonMessage: ", whatsappJsonMessage);
|
|
633
|
-
|
|
634
|
-
if (whatsappJsonMessage) {
|
|
635
|
-
const twClient = new TiledeskWhatsapp({ token: settings.wab_token, GRAPH_URL: GRAPH_URL });
|
|
636
|
-
|
|
637
|
-
twClient.sendMessage(phone_number_id, whatsappJsonMessage).then((response) => {
|
|
638
|
-
console.log("Send message response: ", response.status, response.statusText);
|
|
639
|
-
}).catch((err) => {
|
|
640
|
-
console.error("ERROR Send message: ", err);
|
|
641
|
-
})
|
|
642
|
-
|
|
643
|
-
/*
|
|
644
|
-
sendMessage(phone_number_id, whatsappJsonMessage, wab_token).then((response) => {
|
|
645
|
-
console.log("sendMessage() response: ", response);
|
|
646
|
-
}).catch((err) => {
|
|
647
|
-
console.log("ERROR sendMessage(): ", err);
|
|
648
|
-
})
|
|
649
|
-
*/
|
|
650
|
-
|
|
651
|
-
/*
|
|
652
|
-
} else {
|
|
653
|
-
console.error("Whatsapp Json Message is undefined!")
|
|
654
|
-
}
|
|
655
|
-
*/
|
|
656
|
-
// richiamo toWhatsapp;
|
|
657
|
-
// sendMessage
|
|
658
|
-
//callSendAPI(project_id, phone_number_id, to, payload);
|
|
659
535
|
return res.send(200);
|
|
660
536
|
})
|
|
661
537
|
|
|
@@ -664,12 +540,11 @@ router.post('/tiledesk', async (req, res) => {
|
|
|
664
540
|
router.post("/webhook/:project_id", async (req, res) => {
|
|
665
541
|
|
|
666
542
|
// Parse the request body from the POST
|
|
667
|
-
let body = req.body;
|
|
668
543
|
let projectId = req.params.project_id;
|
|
544
|
+
//console.log("/webhook req.body: ", JSON.stringify(req.body, null, 2))
|
|
669
545
|
|
|
670
546
|
// Check the Incoming webhook message
|
|
671
547
|
// console.log("\n***Body: ", JSON.stringify(req.body, null, 2));
|
|
672
|
-
|
|
673
548
|
// info on WhatsApp text message payload: https://developers.facebook.com/docs/whatsapp/cloud-api/webhooks/payload-examples#text-messages
|
|
674
549
|
if (req.body.object) {
|
|
675
550
|
if (
|
|
@@ -680,135 +555,138 @@ router.post("/webhook/:project_id", async (req, res) => {
|
|
|
680
555
|
req.body.entry[0].changes[0].value.messages[0]
|
|
681
556
|
) {
|
|
682
557
|
|
|
683
|
-
console.log("\n
|
|
558
|
+
console.log("\n(whatsapp) /whatsapp Body: ", JSON.stringify(req.body));
|
|
684
559
|
|
|
685
560
|
if (req.body.entry[0].changes[0].value.messages[0].type == "system") {
|
|
686
|
-
console.log("Skip system message")
|
|
561
|
+
console.log("(whatsapp) Skip system message")
|
|
687
562
|
return res.sendStatus(200);
|
|
688
563
|
}
|
|
689
564
|
|
|
690
|
-
//let originalWhatsappMessage = req.body;
|
|
691
565
|
let whatsappChannelMessage = req.body.entry[0].changes[0].value.messages[0];
|
|
692
566
|
|
|
693
|
-
if (whatsappChannelMessage.text && whatsappChannelMessage.text.body.startsWith("#td")) {
|
|
694
|
-
return res.redirect(307, '/testitout/' + projectId);
|
|
695
|
-
}
|
|
696
|
-
|
|
697
|
-
let firstname = req.body.entry[0].changes[0].value.contacts[0].profile.name;
|
|
698
|
-
console.log("(WAB) firstname: ", firstname)
|
|
699
|
-
|
|
700
|
-
let message_info = {
|
|
701
|
-
channel: "whatsapp",
|
|
702
|
-
whatsapp: {
|
|
703
|
-
phone_number_id: req.body.entry[0].changes[0].value.metadata.phone_number_id,
|
|
704
|
-
from: req.body.entry[0].changes[0].value.messages[0].from,
|
|
705
|
-
firstname: req.body.entry[0].changes[0].value.contacts[0].profile.name,
|
|
706
|
-
lastname: " "
|
|
707
|
-
}
|
|
708
|
-
}
|
|
709
|
-
|
|
710
567
|
let CONTENT_KEY = "whatsapp-" + projectId;
|
|
711
|
-
|
|
712
568
|
let settings = await db.get(CONTENT_KEY);
|
|
713
569
|
if (log) {
|
|
714
|
-
console.log("
|
|
570
|
+
console.log("(whatsapp) kvdb settings: ", settings);
|
|
715
571
|
}
|
|
716
|
-
|
|
717
572
|
if (!settings) {
|
|
718
|
-
console.log("No settings found. Exit..");
|
|
719
|
-
res.sendStatus(200);
|
|
720
|
-
return;
|
|
573
|
+
console.log("(whatsapp) No settings found. Exit..");
|
|
574
|
+
return res.sendStatus(200);
|
|
721
575
|
}
|
|
722
576
|
|
|
723
|
-
let tiledeskJsonMessage;
|
|
724
|
-
|
|
725
577
|
const tlr = new TiledeskWhatsappTranslator();
|
|
578
|
+
const tdChannel = new TiledeskChannel({ settings: settings, API_URL: API_URL })
|
|
726
579
|
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
tiledeskJsonMessage = tlr.toTiledesk(whatsappChannelMessage, firstname);
|
|
730
|
-
|
|
731
|
-
}
|
|
732
|
-
else if (whatsappChannelMessage.type == 'interactive') {
|
|
733
|
-
console.log("message type interactive")
|
|
734
|
-
tiledeskJsonMessage = tlr.toTiledesk(whatsappChannelMessage, firstname);
|
|
735
|
-
}
|
|
736
|
-
else if ((whatsappChannelMessage.type == 'image') || (whatsappChannelMessage.type == 'video') || (whatsappChannelMessage.type == 'document')) {
|
|
737
|
-
let media;
|
|
738
|
-
const util = new TiledeskWhatsapp({ token: settings.wab_token, GRAPH_URL: GRAPH_URL })
|
|
739
|
-
|
|
740
|
-
if (whatsappChannelMessage.type == 'image') {
|
|
741
|
-
media = whatsappChannelMessage.image;
|
|
742
|
-
console.log("media_id: ", media.id);
|
|
743
|
-
|
|
744
|
-
const filename = await util.downloadMedia(media.id);
|
|
745
|
-
console.log("File position: ", filename);
|
|
746
|
-
if (!filename) {
|
|
747
|
-
console.log("Unable to download media. Message not sent.");
|
|
748
|
-
return res.status(500).send({ success: false, error: "unable to download media" })
|
|
749
|
-
}
|
|
750
|
-
let file_path = path.join(__dirname, 'tmp', filename);
|
|
751
|
-
|
|
752
|
-
const image_url = await util.uploadMedia(file_path, "images");
|
|
753
|
-
console.log("image_url: ", image_url)
|
|
580
|
+
// Initialize conversation with chatbot
|
|
581
|
+
if (whatsappChannelMessage.text && whatsappChannelMessage.text.body.startsWith("#td")) {
|
|
754
582
|
|
|
755
|
-
|
|
583
|
+
const bottester = new TiledeskBotTester({project_id: projectId, redis_client: redis_client, db: db, tdChannel: tdChannel, tlr: tlr});
|
|
584
|
+
bottester.startBotConversation(req.body).then((result) => {
|
|
585
|
+
return res.status(200).send('testitout');
|
|
586
|
+
}).catch((err) => {
|
|
587
|
+
return res.status(500).send(err);
|
|
588
|
+
})
|
|
756
589
|
|
|
590
|
+
// Standard message
|
|
591
|
+
} else {
|
|
592
|
+
|
|
593
|
+
let firstname = req.body.entry[0].changes[0].value.contacts[0].profile.name;
|
|
594
|
+
if (log) {
|
|
595
|
+
console.log("(whatsapp) /whatsapp firstname: ", firstname)
|
|
757
596
|
}
|
|
758
597
|
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
return res.status(500).send({ success: false, error: "unable to download media" })
|
|
598
|
+
let message_info = {
|
|
599
|
+
channel: "whatsapp",
|
|
600
|
+
whatsapp: {
|
|
601
|
+
phone_number_id: req.body.entry[0].changes[0].value.metadata.phone_number_id,
|
|
602
|
+
from: req.body.entry[0].changes[0].value.messages[0].from,
|
|
603
|
+
firstname: req.body.entry[0].changes[0].value.contacts[0].profile.name,
|
|
604
|
+
lastname: " "
|
|
767
605
|
}
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
tiledeskJsonMessage = tlr.toTiledesk(whatsappChannelMessage, firstname
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
let tiledeskJsonMessage;
|
|
609
|
+
|
|
610
|
+
if ((whatsappChannelMessage.type == 'text')) {
|
|
611
|
+
console.log("(whatsapp) /whatsapp message type text")
|
|
612
|
+
tiledeskJsonMessage = tlr.toTiledesk(whatsappChannelMessage, firstname);
|
|
613
|
+
|
|
614
|
+
}
|
|
615
|
+
else if (whatsappChannelMessage.type == 'interactive') {
|
|
616
|
+
console.log("(whatsapp) /whatsapp message type interactive")
|
|
617
|
+
tiledeskJsonMessage = tlr.toTiledesk(whatsappChannelMessage, firstname);
|
|
775
618
|
}
|
|
776
619
|
|
|
777
|
-
if (whatsappChannelMessage.type == 'document') {
|
|
778
|
-
media
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
console.log("
|
|
784
|
-
|
|
620
|
+
else if ((whatsappChannelMessage.type == 'image') || (whatsappChannelMessage.type == 'video') || (whatsappChannelMessage.type == 'document')) {
|
|
621
|
+
let media;
|
|
622
|
+
const util = new TiledeskWhatsapp({ token: settings.wab_token, GRAPH_URL: GRAPH_URL })
|
|
623
|
+
|
|
624
|
+
if (whatsappChannelMessage.type == 'image') {
|
|
625
|
+
media = whatsappChannelMessage.image;
|
|
626
|
+
console.log("(whatsapp) /whatsapp media_id: ", media.id);
|
|
627
|
+
|
|
628
|
+
const filename = await util.downloadMedia(media.id);
|
|
629
|
+
if (!filename) {
|
|
630
|
+
console.log("(whatsapp) /whatsapp Unable to download media. Message not sent.");
|
|
631
|
+
return res.status(500).send({ success: false, error: "unable to download media" })
|
|
632
|
+
}
|
|
633
|
+
let file_path = path.join(__dirname, 'tmp', filename);
|
|
634
|
+
|
|
635
|
+
const image_url = await util.uploadMedia(file_path, "images");
|
|
636
|
+
console.log("(whatsapp) /whatsapp image_url: ", image_url)
|
|
637
|
+
|
|
638
|
+
tiledeskJsonMessage = tlr.toTiledesk(whatsappChannelMessage, firstname, image_url);
|
|
785
639
|
}
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
640
|
+
|
|
641
|
+
if (whatsappChannelMessage.type == 'video') {
|
|
642
|
+
media = whatsappChannelMessage.video;
|
|
643
|
+
|
|
644
|
+
const filename = await util.downloadMedia(media.id);
|
|
645
|
+
if (!filename) {
|
|
646
|
+
console.log("(whatsapp) /whatsapp Unable to download media. Message not sent.");
|
|
647
|
+
return res.status(500).send({ success: false, error: "unable to download media" })
|
|
648
|
+
}
|
|
649
|
+
let file_path = path.join(__dirname, 'tmp', filename);
|
|
650
|
+
|
|
651
|
+
|
|
652
|
+
const media_url = await util.uploadMedia(file_path, "files");
|
|
653
|
+
console.log("(whatsapp) /whatsapp media_url: ", media_url)
|
|
654
|
+
|
|
655
|
+
tiledeskJsonMessage = tlr.toTiledesk(whatsappChannelMessage, firstname, media_url);
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
if (whatsappChannelMessage.type == 'document') {
|
|
659
|
+
media = whatsappChannelMessage.document;
|
|
660
|
+
|
|
661
|
+
const filename = await util.downloadMedia(media.id);
|
|
662
|
+
if (!filename) {
|
|
663
|
+
console.log("(whatsapp) /whatsapp Unable to download media. Message not sent.");
|
|
664
|
+
return res.status(500).send({ success: false, error: "unable to download media" })
|
|
665
|
+
}
|
|
666
|
+
let file_path = path.join(__dirname, 'tmp', filename);
|
|
667
|
+
|
|
668
|
+
const media_url = await util.uploadMedia(file_path, "files");
|
|
669
|
+
console.log("(whatsapp) /whatsapp media_url: ", media_url)
|
|
670
|
+
|
|
671
|
+
tiledeskJsonMessage = tlr.toTiledesk(whatsappChannelMessage, firstname, media_url);
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
} else {
|
|
675
|
+
// unsupported. Try anyway to send something.
|
|
792
676
|
}
|
|
793
677
|
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
// tdClient.signInWithCustomToken
|
|
801
|
-
// tdClient.getRequests
|
|
802
|
-
const tdChannel = new TiledeskChannel({ settings: settings, API_URL: API_URL })
|
|
803
|
-
const response = await tdChannel.send(tiledeskJsonMessage, message_info);
|
|
804
|
-
if (log) {
|
|
805
|
-
console.log("Send response: ", response)
|
|
678
|
+
console.log("🟠 tiledeskJsonMessage: ", JSON.stringify(tiledeskJsonMessage));
|
|
679
|
+
const response = await tdChannel.send(tiledeskJsonMessage, message_info);
|
|
680
|
+
if (log) {
|
|
681
|
+
console.log("(whatsapp) /whatsapp Send response: ", response)
|
|
682
|
+
}
|
|
683
|
+
|
|
806
684
|
}
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
res.sendStatus(200);
|
|
685
|
+
}
|
|
686
|
+
res.sendStatus(200);
|
|
810
687
|
} else {
|
|
811
688
|
// Return a '404 Not Found' if event is not from a WhatsApp API
|
|
689
|
+
console.log("event not from whatsapp")
|
|
812
690
|
res.sendStatus(404);
|
|
813
691
|
}
|
|
814
692
|
});
|
|
@@ -820,33 +698,28 @@ router.get("/webhook/:project_id", async (req, res) => {
|
|
|
820
698
|
* UPDATE YOUR VERIFY TOKEN
|
|
821
699
|
*This will be the Verify Token value when you set up webhook
|
|
822
700
|
**/
|
|
823
|
-
console.log("\
|
|
824
|
-
console.log("\req.query: ", req.query);
|
|
701
|
+
console.log("\n(whatsapp) Verify the webhook... ");
|
|
702
|
+
console.log("\(whatsapp) req.query: ", req.query);
|
|
825
703
|
|
|
826
704
|
// Parse params from the webhook verification request
|
|
827
705
|
let mode = req.query["hub.mode"];
|
|
828
706
|
let token = req.query["hub.verify_token"];
|
|
829
707
|
let challenge = req.query["hub.challenge"];
|
|
830
708
|
|
|
831
|
-
console.log("**********")
|
|
832
|
-
console.log("mode: ", mode)
|
|
833
|
-
console.log("token: ", token)
|
|
834
|
-
console.log("challenge: ", challenge)
|
|
835
|
-
console.log("**********")
|
|
836
|
-
|
|
837
709
|
let CONTENT_KEY = "whatsapp-" + req.params.project_id;
|
|
838
|
-
console.log("\CONTENT_KEY: ", CONTENT_KEY);
|
|
839
710
|
|
|
840
711
|
let settings = await db.get(CONTENT_KEY);
|
|
841
712
|
|
|
842
713
|
if (!settings || !settings.verify_token) {
|
|
843
|
-
console.error("No settings found! Unable to verify token.")
|
|
714
|
+
console.error("(whatsapp) No settings found! Unable to verify token.")
|
|
844
715
|
res.sendStatus(403);
|
|
845
716
|
} else {
|
|
846
717
|
let VERIFY_TOKEN = settings.verify_token;
|
|
847
718
|
|
|
848
|
-
|
|
849
|
-
|
|
719
|
+
if (log) {
|
|
720
|
+
console.log("token: ", token);
|
|
721
|
+
console.log("verify token: ", VERIFY_TOKEN);
|
|
722
|
+
}
|
|
850
723
|
|
|
851
724
|
// Check if a token and mode were sent
|
|
852
725
|
if (mode && token) {
|
|
@@ -854,15 +727,15 @@ router.get("/webhook/:project_id", async (req, res) => {
|
|
|
854
727
|
if (mode === "subscribe" && token === VERIFY_TOKEN) {
|
|
855
728
|
|
|
856
729
|
// Respond with 200 OK and challenge token from the request
|
|
857
|
-
console.log("WEBHOOK_VERIFIED");
|
|
730
|
+
console.log("(whatsapp) WEBHOOK_VERIFIED");
|
|
858
731
|
res.status(200).send(challenge);
|
|
859
732
|
} else {
|
|
860
733
|
// Responds with '403 Forbidden' if verify tokens do not match
|
|
861
|
-
console.error("mode is not 'subscribe' or token do not match");
|
|
734
|
+
console.error("(whatsapp) mode is not 'subscribe' or token do not match");
|
|
862
735
|
res.sendStatus(403);
|
|
863
736
|
}
|
|
864
737
|
} else {
|
|
865
|
-
console.error("mode or token undefined");
|
|
738
|
+
console.error("(whatsapp) mode or token undefined");
|
|
866
739
|
res.status(400).send("impossible to verify the webhook: mode or token undefined.")
|
|
867
740
|
}
|
|
868
741
|
|
|
@@ -870,24 +743,10 @@ router.get("/webhook/:project_id", async (req, res) => {
|
|
|
870
743
|
|
|
871
744
|
});
|
|
872
745
|
|
|
873
|
-
function getRandomName() {
|
|
874
|
-
|
|
875
|
-
let hexString = uuidv4();
|
|
876
|
-
console.log("hex: ", hexString);
|
|
877
|
-
|
|
878
|
-
// remove decoration
|
|
879
|
-
hexString = hexString.replace(/-/g, '');
|
|
880
|
-
|
|
881
|
-
let base64String = Buffer.from(hexString, 'hex').toString('base64')
|
|
882
|
-
console.log("base64: ", base64String);
|
|
883
|
-
|
|
884
|
-
return base64String;
|
|
885
|
-
}
|
|
886
|
-
|
|
887
746
|
|
|
888
747
|
router.post("/newtest", async (req, res) => {
|
|
889
|
-
|
|
890
|
-
console.log("/newtest body: ", JSON.stringify(req.body
|
|
748
|
+
|
|
749
|
+
console.log("(whatsapp) /newtest body: ", JSON.stringify(req.body));
|
|
891
750
|
let project_id = req.body.project_id;
|
|
892
751
|
let bot_id = req.body.bot_id;
|
|
893
752
|
|
|
@@ -896,7 +755,6 @@ router.post("/newtest", async (req, res) => {
|
|
|
896
755
|
bot_id: bot_id
|
|
897
756
|
}
|
|
898
757
|
|
|
899
|
-
//let short_uid = getRandomName();
|
|
900
758
|
let short_uid = uuidv4().substring(0, 8)
|
|
901
759
|
let key = "bottest:" + short_uid;
|
|
902
760
|
|
|
@@ -904,96 +762,19 @@ router.post("/newtest", async (req, res) => {
|
|
|
904
762
|
return res.status(500).send({ message: "Test it out on Whatsapp not available. Redis not ready."})
|
|
905
763
|
}
|
|
906
764
|
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
await redis_client.set(key, JSON.stringify(info));
|
|
910
|
-
//const value = await redis_client.get(key)
|
|
911
|
-
//console.log("get --> value: ", value)
|
|
765
|
+
await redis_client.set(key, JSON.stringify(info), 'EX', 604800);
|
|
912
766
|
redis_client.get(key, (err, value) => {
|
|
913
767
|
if (err) {
|
|
914
|
-
console.log("get err: ", err)
|
|
915
|
-
return res.status(500).send({success: "false", message: "
|
|
768
|
+
console.log("(whatsapp) redis get err: ", err)
|
|
769
|
+
return res.status(500).send({success: "false", message: "Testing info could not be saved"});
|
|
916
770
|
} else {
|
|
917
|
-
console.log("get value: ", value)
|
|
771
|
+
console.log("redis get value: ", value)
|
|
918
772
|
return res.status(200).send({short_uid: short_uid});
|
|
919
773
|
}
|
|
920
774
|
})
|
|
921
|
-
/*
|
|
922
|
-
const value = await redis_client.json.get(key, {
|
|
923
|
-
// JSON Path: .node = the element called 'node' at root level.
|
|
924
|
-
path: '.nodell',
|
|
925
|
-
});
|
|
926
|
-
*/
|
|
927
|
-
//res.status(200).send({short_uid: short_uid});
|
|
928
|
-
})
|
|
929
|
-
|
|
930
|
-
router.post("/testitout/:project_id", async (req, res) => {
|
|
931
|
-
|
|
932
|
-
//console.log("\n/(testitout): ", JSON.stringify(req.body, null, 2));
|
|
933
|
-
console.log("\n/(testitout)");
|
|
934
|
-
let projectId = req.params.project_id;
|
|
935
|
-
console.log("(testitout) projectId: ", projectId)
|
|
936
|
-
|
|
937
|
-
let whatsappChannelMessage = req.body.entry[0].changes[0].value.messages[0];
|
|
938
|
-
console.log("(testitout) whatsappChannelMessage: ", whatsappChannelMessage);
|
|
939
|
-
|
|
940
|
-
let whatsappContact = req.body.entry[0].changes[0].value.contacts[0];
|
|
941
|
-
|
|
942
|
-
let key = "bottest:" + whatsappChannelMessage.text.body.substring(3);
|
|
943
|
-
console.log("(testitout) key: ", key);
|
|
944
|
-
|
|
945
|
-
/*
|
|
946
|
-
const info = await redis_client.json.get(REDIS_KEY);
|
|
947
|
-
console.log("(testitout) info: ", info);
|
|
948
|
-
*/
|
|
949
|
-
let test_info;
|
|
950
|
-
redis_client.get(key, (err, value) => {
|
|
951
|
-
if (err) {
|
|
952
|
-
console.log("No info found on redis. Exit..");
|
|
953
|
-
res.sendStatus(200);
|
|
954
|
-
} else {
|
|
955
|
-
test_info = JSON.parse(value)
|
|
956
|
-
console.log("test_info: ", test_info)
|
|
957
|
-
}
|
|
958
|
-
})
|
|
959
|
-
|
|
960
|
-
let message_info = {
|
|
961
|
-
channel: "whatsapp",
|
|
962
|
-
whatsapp: {
|
|
963
|
-
phone_number_id: req.body.entry[0].changes[0].value.metadata.phone_number_id,
|
|
964
|
-
from: whatsappChannelMessage.from,
|
|
965
|
-
firstname: whatsappContact.profile.name,
|
|
966
|
-
lastname: " "
|
|
967
|
-
}
|
|
968
|
-
}
|
|
969
|
-
console.log("(testitout) message_info: ", message_info)
|
|
970
|
-
|
|
971
|
-
let CONTENT_KEY = "whatsapp-" + projectId;
|
|
972
|
-
let settings = await db.get(CONTENT_KEY);
|
|
973
|
-
if (log) {
|
|
974
|
-
console.log("[KVDB] settings: ", settings);
|
|
975
|
-
}
|
|
976
|
-
|
|
977
|
-
if (!settings) {
|
|
978
|
-
console.log("No settings found. Exit..");
|
|
979
|
-
res.sendStatus(200);
|
|
980
|
-
return;
|
|
981
|
-
}
|
|
982
|
-
|
|
983
|
-
const tlr = new TiledeskWhatsappTranslator();
|
|
984
775
|
|
|
985
|
-
whatsappChannelMessage.text.body = "/start";
|
|
986
|
-
let tiledeskJsonMessage = tlr.toTiledesk(whatsappChannelMessage, whatsappContact.profile.name);
|
|
987
|
-
console.log("(testitout) tiledeskJsonMessage: ", tiledeskJsonMessage)
|
|
988
|
-
|
|
989
|
-
const tdChannel = new TiledeskChannel({ settings: settings, API_URL: API_URL })
|
|
990
|
-
const response = await tdChannel.sendAndAddBot(tiledeskJsonMessage, message_info, test_info.bot_id)
|
|
991
|
-
console.log("testitout --> send response: ", response)
|
|
992
|
-
|
|
993
|
-
res.status(200).send("testitout")
|
|
994
776
|
})
|
|
995
777
|
|
|
996
|
-
|
|
997
778
|
// *****************************
|
|
998
779
|
// ********* FUNCTIONS *********
|
|
999
780
|
// *****************************
|
|
@@ -1033,7 +814,7 @@ async function startApp(settings, callback) {
|
|
|
1033
814
|
console.log("(whatsapp) APPS_API_URL: ", APPS_API_URL);
|
|
1034
815
|
}
|
|
1035
816
|
|
|
1036
|
-
if (settings.REDIS_HOST && settings.REDIS_PORT
|
|
817
|
+
if (settings.REDIS_HOST && settings.REDIS_PORT) {
|
|
1037
818
|
REDIS_HOST = settings.REDIS_HOST;
|
|
1038
819
|
REDIS_PORT = settings.REDIS_PORT;
|
|
1039
820
|
REDIS_PASSWORD = settings.REDIS_PASSWORD;
|
|
@@ -1090,4 +871,78 @@ function readHTMLFile(templateName, callback) {
|
|
|
1090
871
|
})
|
|
1091
872
|
}
|
|
1092
873
|
|
|
1093
|
-
module.exports = { router: router, startApp: startApp };
|
|
874
|
+
module.exports = { router: router, startApp: startApp };
|
|
875
|
+
|
|
876
|
+
/*
|
|
877
|
+
router.post("/testitout/:project_id", async (req, res) => {
|
|
878
|
+
|
|
879
|
+
console.log("\n/(testitout)");
|
|
880
|
+
let projectId = req.params.project_id;
|
|
881
|
+
console.log("(testitout) projectId: ", projectId)
|
|
882
|
+
|
|
883
|
+
let whatsappChannelMessage = req.body.entry[0].changes[0].value.messages[0];
|
|
884
|
+
console.log("(testitout) whatsappChannelMessage: ", whatsappChannelMessage);
|
|
885
|
+
|
|
886
|
+
let whatsappContact = req.body.entry[0].changes[0].value.contacts[0];
|
|
887
|
+
|
|
888
|
+
let key = "bottest:" + whatsappChannelMessage.text.body.substring(3);
|
|
889
|
+
console.log("(testitout) key: ", key);
|
|
890
|
+
|
|
891
|
+
|
|
892
|
+
let test_info;
|
|
893
|
+
redis_client.get(key, (err, value) => {
|
|
894
|
+
if (err) {
|
|
895
|
+
console.log("An error occured on redis. Exit..");
|
|
896
|
+
return res.status(500).send({ success: false, error: err})
|
|
897
|
+
} else {
|
|
898
|
+
if (!value) {
|
|
899
|
+
console.log("No test info found on redis. Exit..");
|
|
900
|
+
return res.status(200).send({ success: false, message: "No test info found on redis"})
|
|
901
|
+
}
|
|
902
|
+
test_info = JSON.parse(value)
|
|
903
|
+
console.log("test_info: ", test_info)
|
|
904
|
+
}
|
|
905
|
+
})
|
|
906
|
+
|
|
907
|
+
let message_info = {
|
|
908
|
+
channel: "whatsapp",
|
|
909
|
+
whatsapp: {
|
|
910
|
+
phone_number_id: req.body.entry[0].changes[0].value.metadata.phone_number_id,
|
|
911
|
+
from: whatsappChannelMessage.from,
|
|
912
|
+
firstname: whatsappContact.profile.name,
|
|
913
|
+
lastname: " "
|
|
914
|
+
}
|
|
915
|
+
}
|
|
916
|
+
console.log("(testitout) message_info: ", message_info)
|
|
917
|
+
|
|
918
|
+
let CONTENT_KEY = "whatsapp-" + projectId;
|
|
919
|
+
let settings = await db.get(CONTENT_KEY);
|
|
920
|
+
if (log) {
|
|
921
|
+
console.log("[KVDB] settings: ", settings);
|
|
922
|
+
}
|
|
923
|
+
|
|
924
|
+
if (!settings) {
|
|
925
|
+
console.log("No settings found. Exit..");
|
|
926
|
+
res.sendStatus(200);
|
|
927
|
+
return;
|
|
928
|
+
}
|
|
929
|
+
|
|
930
|
+
const tlr = new TiledeskWhatsappTranslator();
|
|
931
|
+
|
|
932
|
+
whatsappChannelMessage.text.body = "/start";
|
|
933
|
+
let tiledeskJsonMessage = tlr.toTiledesk(whatsappChannelMessage, whatsappContact.profile.name);
|
|
934
|
+
console.log("(testitout) tiledeskJsonMessage: ", tiledeskJsonMessage)
|
|
935
|
+
|
|
936
|
+
const tdChannel = new TiledeskChannel({ settings: settings, API_URL: API_URL })
|
|
937
|
+
|
|
938
|
+
if (test_info) {
|
|
939
|
+
const response = await tdChannel.sendAndAddBot(tiledeskJsonMessage, message_info, test_info.bot_id)
|
|
940
|
+
console.log("testitout --> send response: ", response)
|
|
941
|
+
res.status(200).send("testitout")
|
|
942
|
+
} else {
|
|
943
|
+
console.log("testitout --> no bot selected, skip test: ")
|
|
944
|
+
res.status(200).send("Test skipped")
|
|
945
|
+
}
|
|
946
|
+
|
|
947
|
+
})
|
|
948
|
+
*/
|
package/package.json
CHANGED
package/template/detail.html
CHANGED
|
@@ -54,6 +54,9 @@
|
|
|
54
54
|
<div style="display: flex; flex-direction: row; justify-content: space-between; margin-left: 40px; width: 100%;">
|
|
55
55
|
<div>
|
|
56
56
|
<p style="font-size: 26px; font-weight: 500; margin-bottom: 0px;">WhatsApp Business</p>
|
|
57
|
+
<p style="color: #676767; font-size: 16px; margin-bottom: 5px;">Connect your WhatsApp number with Tiledesk</p>
|
|
58
|
+
<a style="font-size: 18px;" href="https://tiledesk.com/" target="_blank">Tiledesk</a>
|
|
59
|
+
<p style="font-size: 15px; color: #676767; font-style: italic; margin-bottom: 0px; margin-top: 20px;">Free</p>
|
|
57
60
|
</div>
|
|
58
61
|
|
|
59
62
|
<div style="display: flex; flex-direction: column; justify-content: flex-end; align-items: flex-end;">
|
|
@@ -129,10 +132,17 @@
|
|
|
129
132
|
<div class="scrollmenu">
|
|
130
133
|
|
|
131
134
|
<img src="https://user-images.githubusercontent.com/45603238/195042381-995ff805-359d-4d97-af67-f86f937c175a.jpg" class="screenshot" />
|
|
135
|
+
|
|
136
|
+
<img src="https://user-images.githubusercontent.com/45603238/195042391-6560424b-6a20-48ef-a5b3-981f9cbda5db.jpg" class="screenshot" />
|
|
137
|
+
|
|
138
|
+
<img src=" https://user-images.githubusercontent.com/45603238/195042354-e8ce2b8b-4a82-45b6-8ac1-c1d1bcf77e40.jpg" class="screenshot"/>
|
|
139
|
+
|
|
140
|
+
<img src="https://user-images.githubusercontent.com/45603238/196982373-6b17cf76-6608-45bf-86da-551c14c46e0f.png" class="screenshot" />
|
|
132
141
|
|
|
133
142
|
</div>
|
|
134
143
|
|
|
135
144
|
<hr style="margin-top: 20px; margin-bottom: 20px">
|
|
145
|
+
|
|
136
146
|
<div>
|
|
137
147
|
<h7>Receive Whatsapp conversations in your Tiledesk dashboard. Seamlessly Chat with your targeted audience on Whatsapp right from the Tiledesk dashboard.</h7>
|
|
138
148
|
|
|
@@ -152,11 +162,14 @@
|
|
|
152
162
|
</ul>
|
|
153
163
|
|
|
154
164
|
<div style="padding: 20px; background-color: aliceblue; border-radius: 8px;">
|
|
155
|
-
|
|
165
|
+
<div style="display: flex; flex-direction: row; align-items: baseline;">
|
|
156
166
|
<div>
|
|
157
167
|
<img src="https://freeiconshop.com/wp-content/uploads/edd/arrow-flat.png" width="20px" height="auto" style="margin-right: 10px;">
|
|
158
168
|
</div>
|
|
159
|
-
|
|
169
|
+
<div>
|
|
170
|
+
<a href="https://gethelp.tiledesk.com/articles/connect-tiledesk-with-whatsapp-business/" target="_blank">Connect Tiledesk with WhatsApp Business</a>
|
|
171
|
+
<p style="color: grey; margin-bottom: 0px;">A small but very useful guide on how to connect your WhatsApp Business number with Tiledesk</p>
|
|
172
|
+
</div>
|
|
160
173
|
</div>
|
|
161
174
|
|
|
162
175
|
<hr style="margin-top: 10px; margin-bottom: 10px;">
|
|
@@ -165,8 +178,11 @@
|
|
|
165
178
|
<div>
|
|
166
179
|
<img src="https://freeiconshop.com/wp-content/uploads/edd/arrow-flat.png" width="20px" height="auto" style="margin-right: 10px;">
|
|
167
180
|
</div>
|
|
181
|
+
<div>
|
|
182
|
+
<a href="https://gethelp.tiledesk.com/articles/configure-a-whatsapp-business-account/" target="_blank">Configure a WhatsApp Business account</a>
|
|
183
|
+
<p style="color: grey; margin-bottom: 0px;">Not sure how to set up a business account? Take a look here.</p>
|
|
184
|
+
</div>
|
|
168
185
|
</div>
|
|
169
|
-
</div>
|
|
170
186
|
</div>
|
|
171
187
|
|
|
172
188
|
|
|
@@ -174,33 +190,33 @@
|
|
|
174
190
|
|
|
175
191
|
<p style="font-size: 18px;"><b>What's new?</b></p>
|
|
176
192
|
|
|
177
|
-
|
|
193
|
+
<div style="width: 100%; display: flex; flex-direction: row; justify-content: space-between; margin-bottom: 15px;">
|
|
178
194
|
<div>
|
|
179
|
-
<p style="margin-bottom: 0px">
|
|
195
|
+
<p style="margin-bottom: 0px">Bug Fixes</p>
|
|
180
196
|
<p style="margin-bottom: 0px">Minor improvements</p>
|
|
181
197
|
<!--<p>Bug fixes and performance improvements</p>-->
|
|
182
198
|
</div>
|
|
183
|
-
<p style="font-size: 12px; color: #676767; margin-bottom: 0px;">Version <b>0.1.
|
|
199
|
+
<p style="font-size: 12px; color: #676767; margin-bottom: 0px;">Version <b>0.1.31</b></p>
|
|
184
200
|
</div>
|
|
185
201
|
|
|
186
202
|
<div style="width: 100%; display: flex; flex-direction: row; justify-content: space-between; margin-bottom: 15px;">
|
|
187
203
|
<div>
|
|
188
|
-
<p style="margin-bottom: 0px">
|
|
204
|
+
<p style="margin-bottom: 0px">Added support for Test-it-out</p>
|
|
205
|
+
<p style="margin-bottom: 0px">Minor improvements</p>
|
|
189
206
|
<!--<p>Bug fixes and performance improvements</p>-->
|
|
190
207
|
</div>
|
|
191
|
-
<p style="font-size: 12px; color: #676767; margin-bottom: 0px;">Version <b>0.1.
|
|
208
|
+
<p style="font-size: 12px; color: #676767; margin-bottom: 0px;">Version <b>0.1.24</b></p>
|
|
192
209
|
</div>
|
|
193
210
|
|
|
194
211
|
<div style="width: 100%; display: flex; flex-direction: row; justify-content: space-between; margin-bottom: 15px;">
|
|
195
212
|
<div>
|
|
196
|
-
<p style="margin-bottom: 0px;">New User Interface!</p>
|
|
197
|
-
<p style="margin-bottom: 0px">Added support for tdImage</p>
|
|
198
213
|
<p style="margin-bottom: 0px">Bug Fixes</p>
|
|
199
214
|
<!--<p>Bug fixes and performance improvements</p>-->
|
|
200
215
|
</div>
|
|
201
|
-
<p style="font-size: 12px; color: #676767; margin-bottom: 0px;">Version <b>0.1.
|
|
216
|
+
<p style="font-size: 12px; color: #676767; margin-bottom: 0px;">Version <b>0.1.23</b></p>
|
|
202
217
|
</div>
|
|
203
218
|
|
|
219
|
+
|
|
204
220
|
<hr style="margin-top: 50px; margin-bottom: 20px">
|
|
205
221
|
|
|
206
222
|
</body>
|
|
@@ -36,7 +36,7 @@ class MessageHandler {
|
|
|
36
36
|
|
|
37
37
|
async generateMessageObject(command) {
|
|
38
38
|
console.log("[MessageHandler] command: ", command);
|
|
39
|
-
console.log("[MessageHandler] tiledeskChannelMessage: ", this.tiledeskChannelMessage);
|
|
39
|
+
//console.log("[MessageHandler] tiledeskChannelMessage: ", this.tiledeskChannelMessage);
|
|
40
40
|
let tiledeskCommandMessage = command.message;
|
|
41
41
|
|
|
42
42
|
tiledeskCommandMessage.recipient = this.tiledeskChannelMessage.recipient;
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
const axios = require("axios").default;
|
|
2
|
+
const jwt = require('jsonwebtoken');
|
|
3
|
+
const { v4: uuidv4 } = require('uuid');
|
|
4
|
+
|
|
5
|
+
const { TiledeskClient } = require('@tiledesk/tiledesk-client');
|
|
6
|
+
const { TiledeskWhatsappTranslator } = require('./TiledeskWhatsappTranslator');
|
|
7
|
+
const { TiledeskChannel } = require('./TiledeskChannel');
|
|
8
|
+
|
|
9
|
+
class TiledeskBotTester {
|
|
10
|
+
|
|
11
|
+
constructor(config) {
|
|
12
|
+
if (!config) {
|
|
13
|
+
throw new Error('config is mandatory');
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
if (!config.project_id) {
|
|
17
|
+
throw new Error('config.project_id is mandatory');
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (!config.redis_client) {
|
|
21
|
+
throw new Error('config.redis_client is mandatory');
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (!config.db) {
|
|
25
|
+
throw new Error('config.db is mandatory');
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (!config.tdChannel) {
|
|
29
|
+
throw new Error('config.tdChannel is mandatory');
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (!config.tlr) {
|
|
33
|
+
throw new Error('config.tlr is mandatory');
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
this.log = false;
|
|
37
|
+
if (config.log) {
|
|
38
|
+
this.log = config.log;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
this.settings = config.settings;
|
|
42
|
+
this.project_id = config.project_id;
|
|
43
|
+
this.redis_client = config.redis_client;
|
|
44
|
+
this.db = config.db;
|
|
45
|
+
this.tdChannel = config.tdChannel;
|
|
46
|
+
this.tlr = config.tlr;
|
|
47
|
+
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async startBotConversation(body) {
|
|
51
|
+
|
|
52
|
+
return new Promise( async (resolve, reject) => {
|
|
53
|
+
console.log("\n/startBotConversation()");
|
|
54
|
+
/*
|
|
55
|
+
let projectId = req.params.project_id;
|
|
56
|
+
console.log("(testitout) projectId: ", projectId)
|
|
57
|
+
*/
|
|
58
|
+
|
|
59
|
+
let whatsappChannelMessage = body.entry[0].changes[0].value.messages[0];
|
|
60
|
+
console.log("(testitout) whatsappChannelMessage: ", whatsappChannelMessage);
|
|
61
|
+
|
|
62
|
+
let whatsappContact = body.entry[0].changes[0].value.contacts[0];
|
|
63
|
+
|
|
64
|
+
let key = "bottest:" + whatsappChannelMessage.text.body.substring(3);
|
|
65
|
+
console.log("(testitout) key: ", key);
|
|
66
|
+
|
|
67
|
+
let test_info;
|
|
68
|
+
|
|
69
|
+
this.redis_client.get(key, async (err, value) => {
|
|
70
|
+
if (err) {
|
|
71
|
+
console.log("An error occured on redis. Exit..");
|
|
72
|
+
return reject(err);
|
|
73
|
+
} else {
|
|
74
|
+
|
|
75
|
+
if (!value) {
|
|
76
|
+
console.log("No test info found on redis. Exit..");
|
|
77
|
+
reject("No test info found on redis");
|
|
78
|
+
return;
|
|
79
|
+
} else {
|
|
80
|
+
|
|
81
|
+
test_info = JSON.parse(value)
|
|
82
|
+
console.log("test_info: ", test_info)
|
|
83
|
+
|
|
84
|
+
let message_info = {
|
|
85
|
+
channel: "whatsapp",
|
|
86
|
+
whatsapp: {
|
|
87
|
+
phone_number_id: body.entry[0].changes[0].value.metadata.phone_number_id,
|
|
88
|
+
from: whatsappChannelMessage.from,
|
|
89
|
+
firstname: whatsappContact.profile.name,
|
|
90
|
+
lastname: " "
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
console.log("(testitout) message_info: ", message_info)
|
|
94
|
+
|
|
95
|
+
let CONTENT_KEY = "whatsapp-" + this.project_id;
|
|
96
|
+
let settings = await this.db.get(CONTENT_KEY);
|
|
97
|
+
if (this.log) {
|
|
98
|
+
console.log("[KVDB] settings: ", settings);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (!settings) {
|
|
102
|
+
console.log("No settings found. Exit..");
|
|
103
|
+
return reject("No settings found. Exit..")
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
whatsappChannelMessage.text.body = "/start";
|
|
107
|
+
let tiledeskJsonMessage = await this.tlr.toTiledesk(whatsappChannelMessage, whatsappContact.profile.name);
|
|
108
|
+
console.log("(testitout) tiledeskJsonMessage: ", tiledeskJsonMessage)
|
|
109
|
+
|
|
110
|
+
if (test_info) {
|
|
111
|
+
const response = await this.tdChannel.sendAndAddBot(tiledeskJsonMessage, message_info, test_info.bot_id)
|
|
112
|
+
console.log("testitout --> send response: ", response)
|
|
113
|
+
return resolve()
|
|
114
|
+
} else {
|
|
115
|
+
console.log("testitout --> no bot selected, skip test: ")
|
|
116
|
+
return reject("Test skipped")
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
}
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
})
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
module.exports = { TiledeskBotTester }
|
|
137
|
+
|