@tiledesk/tiledesk-whatsapp-connector 0.1.46 → 0.1.48
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 +38 -28
- package/package.json +1 -1
- package/template/configure.html +1 -1
- package/template/detail.html +1 -1
- package/tiledesk/KVBaseMongo.js +1 -1
- package/tiledesk/MessageHandler.js +1 -1
- package/tiledesk/TiledeskAppsClient.js +1 -1
- package/tiledesk/TiledeskChannel.js +11 -11
- package/tiledesk/TiledeskWhatsapp.js +4 -4
- package/tiledesk/TiledeskWhatsappTranslator.js +2 -2
package/index.js
CHANGED
|
@@ -122,7 +122,7 @@ router.post('/install', async (req, res) => {
|
|
|
122
122
|
|
|
123
123
|
const appClient = new TiledeskAppsClient({ APPS_API_URL: APPS_API_URL });
|
|
124
124
|
appClient.install(installation_info).then((installation) => {
|
|
125
|
-
winston.debug("(wab) installation response: "
|
|
125
|
+
winston.debug("(wab) installation response: ", installation);
|
|
126
126
|
|
|
127
127
|
res.redirect(url.format({
|
|
128
128
|
pathname: "/detail",
|
|
@@ -150,7 +150,7 @@ router.post('/uninstall', async (req, res) => {
|
|
|
150
150
|
const appClient = new TiledeskAppsClient({ APPS_API_URL: APPS_API_URL });
|
|
151
151
|
appClient.uninstall(project_id, app_id).then((response) => {
|
|
152
152
|
|
|
153
|
-
winston.debug("(wab) uninstallation response: "
|
|
153
|
+
winston.debug("(wab) uninstallation response: ", response);
|
|
154
154
|
|
|
155
155
|
res.redirect(url.format({
|
|
156
156
|
pathname: "/detail",
|
|
@@ -194,7 +194,7 @@ router.get('/configure', async (req, res) => {
|
|
|
194
194
|
let CONTENT_KEY = "whatsapp-" + project_id;
|
|
195
195
|
|
|
196
196
|
let settings = await db.get(CONTENT_KEY);
|
|
197
|
-
winston.debug("(wab) settings: "
|
|
197
|
+
winston.debug("(wab) settings: ", settings);
|
|
198
198
|
|
|
199
199
|
// get departments
|
|
200
200
|
const tdChannel = new TiledeskChannel({ settings: { project_id: project_id, token: token }, API_URL: API_URL })
|
|
@@ -314,7 +314,7 @@ router.post('/update', async (req, res) => {
|
|
|
314
314
|
// promise
|
|
315
315
|
tdClient.subscribe(subscription_info).then((data) => {
|
|
316
316
|
let subscription = data;
|
|
317
|
-
winston.debug("\n(wab) Subscription: "
|
|
317
|
+
winston.debug("\n(wab) Subscription: ", subscription)
|
|
318
318
|
|
|
319
319
|
let settings = {
|
|
320
320
|
project_id: project_id,
|
|
@@ -434,7 +434,7 @@ router.get('/direct/tiledesk', async (req, res) => {
|
|
|
434
434
|
text: "Ciao, benvenuto sull'ambiente di sviluppo di Tiledesk! Questo messaggio serve ad aprire una nuova conversazione. Ha funzionato?"
|
|
435
435
|
}
|
|
436
436
|
*/
|
|
437
|
-
|
|
437
|
+
|
|
438
438
|
let tiledeskChannelMessage = {
|
|
439
439
|
text: "Sample text",
|
|
440
440
|
attributes: {
|
|
@@ -456,16 +456,16 @@ router.get('/direct/tiledesk', async (req, res) => {
|
|
|
456
456
|
twClient.sendMessage(phone_number_id, whatsappJsonMessage).then((response) => {
|
|
457
457
|
winston.verbose("(wab) Message sent to WhatsApp! " + response.status + " " + response.statusText);
|
|
458
458
|
}).catch((err) => {
|
|
459
|
-
|
|
460
|
-
|
|
459
|
+
winston.error("(wab) error send message: ", err);
|
|
460
|
+
return res.status(400).send({ success: false, error: err });
|
|
461
461
|
})
|
|
462
462
|
|
|
463
463
|
res.status(200).send("Message sent");
|
|
464
|
-
|
|
464
|
+
|
|
465
465
|
})
|
|
466
466
|
|
|
467
467
|
router.post('/tiledesk', async (req, res) => {
|
|
468
|
-
|
|
468
|
+
|
|
469
469
|
winston.verbose("(wab) Message received from Tiledesk")
|
|
470
470
|
|
|
471
471
|
var tiledeskChannelMessage = req.body.payload;
|
|
@@ -552,14 +552,14 @@ router.post('/tiledesk', async (req, res) => {
|
|
|
552
552
|
const messageHandler = new MessageHandler({ tiledeskChannelMessage: tiledeskChannelMessage });
|
|
553
553
|
const tlr = new TiledeskWhatsappTranslator();
|
|
554
554
|
|
|
555
|
-
|
|
555
|
+
|
|
556
556
|
if (commands) {
|
|
557
557
|
let i = 0;
|
|
558
558
|
async function execute(command) {
|
|
559
559
|
// message
|
|
560
560
|
if (command.type === "message") {
|
|
561
561
|
let tiledeskCommandMessage = await messageHandler.generateMessageObject(command);
|
|
562
|
-
winston.debug("(wab) message generated from command: "
|
|
562
|
+
winston.debug("(wab) message generated from command: ", tiledeskCommandMessage)
|
|
563
563
|
|
|
564
564
|
let whatsappJsonMessage = await tlr.toWhatsapp(tiledeskCommandMessage, whatsapp_receiver);
|
|
565
565
|
winston.verbose("(wab) whatsappJsonMessage", whatsappJsonMessage)
|
|
@@ -575,7 +575,7 @@ router.post('/tiledesk', async (req, res) => {
|
|
|
575
575
|
winston.debug("(wab) End of commands")
|
|
576
576
|
}
|
|
577
577
|
}).catch((err) => {
|
|
578
|
-
winston.error("(wab) send message error: "
|
|
578
|
+
winston.error("(wab) send message error: ", err);
|
|
579
579
|
})
|
|
580
580
|
} else {
|
|
581
581
|
winston.error("(wab) WhatsappJsonMessage is undefined!")
|
|
@@ -601,7 +601,7 @@ router.post('/tiledesk', async (req, res) => {
|
|
|
601
601
|
else if (tiledeskChannelMessage.text || tiledeskChannelMessage.metadata) {
|
|
602
602
|
|
|
603
603
|
let whatsappJsonMessage = await tlr.toWhatsapp(tiledeskChannelMessage, whatsapp_receiver);
|
|
604
|
-
winston.verbose("(wab)
|
|
604
|
+
winston.verbose("(wab) whatsappJsonMessage", whatsappJsonMessage)
|
|
605
605
|
|
|
606
606
|
if (whatsappJsonMessage) {
|
|
607
607
|
const twClient = new TiledeskWhatsapp({ token: settings.wab_token, GRAPH_URL: GRAPH_URL });
|
|
@@ -609,12 +609,12 @@ router.post('/tiledesk', async (req, res) => {
|
|
|
609
609
|
twClient.sendMessage(phone_number_id, whatsappJsonMessage).then((response) => {
|
|
610
610
|
winston.verbose("(wab) Message sent to WhatsApp! " + response.status + " " + response.statusText);
|
|
611
611
|
}).catch((err) => {
|
|
612
|
-
res.status(400).send({success: false, error: "il template non esiste"});
|
|
613
|
-
winston.error("(wab) error send message: "
|
|
612
|
+
res.status(400).send({ success: false, error: "il template non esiste" });
|
|
613
|
+
winston.error("(wab) error send message: ", err);
|
|
614
614
|
})
|
|
615
615
|
|
|
616
616
|
} else {
|
|
617
|
-
res.status(400).send({success: false, error: "il template non esiste"});
|
|
617
|
+
res.status(400).send({ success: false, error: "il template non esiste" });
|
|
618
618
|
winston.error("(wab) Whatsapp Json Message is undefined!")
|
|
619
619
|
}
|
|
620
620
|
|
|
@@ -622,7 +622,7 @@ router.post('/tiledesk', async (req, res) => {
|
|
|
622
622
|
winston.debug("(wab) no command, no text --> skip")
|
|
623
623
|
}
|
|
624
624
|
|
|
625
|
-
return res.
|
|
625
|
+
return res.sendStatus(200);
|
|
626
626
|
})
|
|
627
627
|
|
|
628
628
|
// Endpoint for Whatsapp Business
|
|
@@ -653,7 +653,7 @@ router.post("/webhook/:project_id", async (req, res) => {
|
|
|
653
653
|
|
|
654
654
|
let CONTENT_KEY = "whatsapp-" + project_id;
|
|
655
655
|
let settings = await db.get(CONTENT_KEY);
|
|
656
|
-
winston.debug("(wab) settings: "
|
|
656
|
+
winston.debug("(wab) settings: ", settings);
|
|
657
657
|
|
|
658
658
|
if (!settings) {
|
|
659
659
|
winston.verbose("(wab) No settings found. Exit..");
|
|
@@ -671,9 +671,9 @@ router.post("/webhook/:project_id", async (req, res) => {
|
|
|
671
671
|
const bottester = new TiledeskBotTester({ project_id: project_id, redis_client: redis_client, db: db, tdChannel: tdChannel, tlr: tlr });
|
|
672
672
|
bottester.startBotConversation(req.body, code).then((result) => {
|
|
673
673
|
winston.verbose("(wab) test conversation started");
|
|
674
|
-
winston.debug("(wab) startBotConversation result: "
|
|
674
|
+
winston.debug("(wab) startBotConversation result: ", result);
|
|
675
675
|
}).catch((err) => {
|
|
676
|
-
winston.error("(wab) start test onversation error: "
|
|
676
|
+
winston.error("(wab) start test onversation error: ", err);
|
|
677
677
|
})
|
|
678
678
|
|
|
679
679
|
// Standard message
|
|
@@ -779,7 +779,7 @@ router.post("/webhook/:project_id", async (req, res) => {
|
|
|
779
779
|
winston.verbose("(wab) tiledeskJsonMessage: ", tiledeskJsonMessage);
|
|
780
780
|
const response = await tdChannel.send(tiledeskJsonMessage, message_info, settings.department_id);
|
|
781
781
|
winston.verbose("(wab) Message sent to Tiledesk!")
|
|
782
|
-
winston.debug("(wab) response: "
|
|
782
|
+
winston.debug("(wab) response: ", response)
|
|
783
783
|
} else {
|
|
784
784
|
winston.verbose("(wab) tiledeskJsonMessage is undefined")
|
|
785
785
|
}
|
|
@@ -864,7 +864,7 @@ router.post("/newtest", async (req, res) => {
|
|
|
864
864
|
await redis_client.set(key, JSON.stringify(info), 'EX', 604800);
|
|
865
865
|
redis_client.get(key, (err, value) => {
|
|
866
866
|
if (err) {
|
|
867
|
-
winston.error("(wab) redis get err: "
|
|
867
|
+
winston.error("(wab) redis get err: ", err)
|
|
868
868
|
return res.status(500).send({ success: "false", message: "Testing info could not be saved" });
|
|
869
869
|
} else {
|
|
870
870
|
winston.debug("(wab) new test initialized with id: " + short_uid)
|
|
@@ -942,6 +942,8 @@ router.get("/templates/:project_id", async (req, res) => {
|
|
|
942
942
|
let tm = new TemplateManager({ token: settings.wab_token, business_account_id: settings.business_account_id, GRAPH_URL: GRAPH_URL })
|
|
943
943
|
let templates = await tm.getTemplates();
|
|
944
944
|
|
|
945
|
+
|
|
946
|
+
|
|
945
947
|
readHTMLFile('/templates.html', (err, html) => {
|
|
946
948
|
var template = handlebars.compile(html);
|
|
947
949
|
var replacements = {
|
|
@@ -970,17 +972,25 @@ router.get("/ext/templates/:project_id", async (req, res) => {
|
|
|
970
972
|
let settings = await db.get(CONTENT_KEY);
|
|
971
973
|
|
|
972
974
|
if (settings) {
|
|
973
|
-
let tm = new TemplateManager({ token: settings.wab_token, business_account_id: settings.business_account_id, GRAPH_URL: GRAPH_URL })
|
|
974
|
-
let templates = await tm.getTemplates();
|
|
975
975
|
|
|
976
|
-
if (
|
|
977
|
-
|
|
976
|
+
if (settings.business_account_id) {
|
|
977
|
+
let tm = new TemplateManager({ token: settings.wab_token, business_account_id: settings.business_account_id, GRAPH_URL: GRAPH_URL })
|
|
978
|
+
let templates = await tm.getTemplates();
|
|
979
|
+
|
|
980
|
+
winston.info("(wab) ext templates: ", templates);
|
|
981
|
+
|
|
982
|
+
if (templates) {
|
|
983
|
+
res.status(200).send(templates.data);
|
|
984
|
+
} else {
|
|
985
|
+
res.status(500).send({ success: false, code: '02', message: "A problem occurred while getting templates from WhatsApp" })
|
|
986
|
+
}
|
|
987
|
+
|
|
978
988
|
} else {
|
|
979
|
-
res.status(500).send({ success: false, code: '
|
|
989
|
+
res.status(500).send({ success: false, code: '03', message: "Missing parameter 'WhatsApp Business Account ID'. Please update your app."})
|
|
980
990
|
}
|
|
981
991
|
|
|
982
992
|
} else {
|
|
983
|
-
res.status(400).send({ success: false, code: '01', message: "
|
|
993
|
+
res.status(400).send({ success: false, code: '01', message: "WhatsApp not installed for the project_id " + project_id })
|
|
984
994
|
}
|
|
985
995
|
|
|
986
996
|
})
|
package/package.json
CHANGED
package/template/configure.html
CHANGED
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
</script>
|
|
26
26
|
|
|
27
27
|
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
|
|
28
|
-
<link rel="stylesheet" href="
|
|
28
|
+
<link rel="stylesheet" href="./css/configure.css">
|
|
29
29
|
|
|
30
30
|
<style>
|
|
31
31
|
body {
|
package/template/detail.html
CHANGED
|
@@ -218,7 +218,7 @@
|
|
|
218
218
|
<p style="margin-bottom: 0px">Added support for WhatsApp templates</p>
|
|
219
219
|
<!--<p>Bug fixes and performance improvements</p>-->
|
|
220
220
|
</div>
|
|
221
|
-
<p style="font-size: 12px; color: #676767; margin-bottom: 0px;">Version <b>0.1.
|
|
221
|
+
<p style="font-size: 12px; color: #676767; margin-bottom: 0px;">Version <b>0.1.48</b></p>
|
|
222
222
|
</div>
|
|
223
223
|
|
|
224
224
|
<div style="width: 100%; display: flex; flex-direction: row; justify-content: space-between; margin-bottom: 15px;">
|
package/tiledesk/KVBaseMongo.js
CHANGED
|
@@ -40,7 +40,7 @@ class MessageHandler {
|
|
|
40
40
|
|
|
41
41
|
async generateMessageObject(command) {
|
|
42
42
|
|
|
43
|
-
winston.debug("(wab) [MessageHandler] command: "
|
|
43
|
+
winston.debug("(wab) [MessageHandler] command: ", command);
|
|
44
44
|
let tiledeskCommandMessage = command.message;
|
|
45
45
|
tiledeskCommandMessage.recipient = this.tiledeskChannelMessage.recipient;
|
|
46
46
|
|
|
@@ -103,7 +103,7 @@ class TiledeskChannel {
|
|
|
103
103
|
method: 'GET'
|
|
104
104
|
}).then((response) => {
|
|
105
105
|
|
|
106
|
-
winston.debug("(wab) [TiledeskChannel] get request response: "
|
|
106
|
+
winston.debug("(wab) [TiledeskChannel] get request response: ", response.data);
|
|
107
107
|
|
|
108
108
|
let request_id;
|
|
109
109
|
if (response.data.requests[0]) {
|
|
@@ -114,7 +114,7 @@ class TiledeskChannel {
|
|
|
114
114
|
winston.debug("(wab) [TiledeskChannel] New request_id: " + request_id);
|
|
115
115
|
}
|
|
116
116
|
|
|
117
|
-
winston.debug("(wab) [TiledeskChannel] tiledeskMessage:"
|
|
117
|
+
winston.debug("(wab) [TiledeskChannel] tiledeskMessage:", tiledeskMessage);
|
|
118
118
|
|
|
119
119
|
return axios({
|
|
120
120
|
url: this.API_URL + `/${this.settings.project_id}/requests/${request_id}/messages`,
|
|
@@ -126,20 +126,20 @@ class TiledeskChannel {
|
|
|
126
126
|
method: 'POST'
|
|
127
127
|
}).then((response) => {
|
|
128
128
|
|
|
129
|
-
winston.debug("(wab) [TiledeskChannel] send message response: "
|
|
129
|
+
winston.debug("(wab) [TiledeskChannel] send message response: ", response.data);
|
|
130
130
|
|
|
131
131
|
return response.data;
|
|
132
132
|
|
|
133
133
|
}).catch((err) => {
|
|
134
|
-
winston.error("(wab) [TiledeskChannel] send message: "
|
|
134
|
+
winston.error("(wab) [TiledeskChannel] send message: ", err);
|
|
135
135
|
})
|
|
136
136
|
}).catch((err) => {
|
|
137
|
-
winston.error("(wab) [TiledeskChannel] get requests: "
|
|
137
|
+
winston.error("(wab) [TiledeskChannel] get requests: ", err);
|
|
138
138
|
})
|
|
139
139
|
|
|
140
140
|
|
|
141
141
|
}).catch((err) => {
|
|
142
|
-
winston.error("(wab) [TiledeskChannel] sign in error: "
|
|
142
|
+
winston.error("(wab) [TiledeskChannel] sign in error: ", err);
|
|
143
143
|
})
|
|
144
144
|
}
|
|
145
145
|
|
|
@@ -154,10 +154,10 @@ class TiledeskChannel {
|
|
|
154
154
|
},
|
|
155
155
|
method: 'GET'
|
|
156
156
|
}).then((response) => {
|
|
157
|
-
winston.debug("(wab) [TiledeskChannel] get departments response.data: "
|
|
157
|
+
winston.debug("(wab) [TiledeskChannel] get departments response.data: ", response.data)
|
|
158
158
|
return response.data;
|
|
159
159
|
}).catch((err) => {
|
|
160
|
-
winston.error("(wab) [TiledeskChannel] get departments error: "
|
|
160
|
+
winston.error("(wab) [TiledeskChannel] get departments error: ", err);
|
|
161
161
|
})
|
|
162
162
|
}
|
|
163
163
|
|
|
@@ -211,10 +211,10 @@ class TiledeskChannel {
|
|
|
211
211
|
}).then((response) => {
|
|
212
212
|
return response.data
|
|
213
213
|
}).catch((err) => {
|
|
214
|
-
winston.error("(wab) [TiledeskChannel] send message (open conversation) error: "
|
|
214
|
+
winston.error("(wab) [TiledeskChannel] send message (open conversation) error: ", err);
|
|
215
215
|
})
|
|
216
216
|
}).catch((err) => {
|
|
217
|
-
winston.error("(wab) [TiledeskChannel] sign in error: "
|
|
217
|
+
winston.error("(wab) [TiledeskChannel] sign in error: ", err);
|
|
218
218
|
})
|
|
219
219
|
}
|
|
220
220
|
|
|
@@ -230,7 +230,7 @@ class TiledeskChannel {
|
|
|
230
230
|
}).then((response) => {
|
|
231
231
|
return this.checkPlan(response.data);
|
|
232
232
|
}).catch((err) => {
|
|
233
|
-
winston.error("(wab) [TiledeskChannel] get project detail error: "
|
|
233
|
+
winston.error("(wab) [TiledeskChannel] get project detail error: ", err);
|
|
234
234
|
return null;
|
|
235
235
|
})
|
|
236
236
|
}
|
|
@@ -107,10 +107,10 @@ class TiledeskWhatsapp {
|
|
|
107
107
|
})
|
|
108
108
|
})
|
|
109
109
|
}).catch((err) => {
|
|
110
|
-
winston.error("(wab) [TiledeskWhatsapp] download file error: "
|
|
110
|
+
winston.error("(wab) [TiledeskWhatsapp] download file error: ", err.data);
|
|
111
111
|
})
|
|
112
112
|
}).catch((err) => {
|
|
113
|
-
winston.error("(wab) [TiledeskWhatsapp] get file error: "
|
|
113
|
+
winston.error("(wab) [TiledeskWhatsapp] get file error: ", err.data);
|
|
114
114
|
})
|
|
115
115
|
}
|
|
116
116
|
|
|
@@ -130,7 +130,7 @@ class TiledeskWhatsapp {
|
|
|
130
130
|
|
|
131
131
|
return await axios.post(url, form, request_config).then((response) => {
|
|
132
132
|
|
|
133
|
-
winston.debug("(wab) [TiledeskWhatsapp] upload response: "
|
|
133
|
+
winston.debug("(wab) [TiledeskWhatsapp] upload response: ", response.data);
|
|
134
134
|
if (type == "images") {
|
|
135
135
|
let image_url = "https://tiledesk-server-pre.herokuapp.com/images/?path=" + response.data.filename;
|
|
136
136
|
return image_url;
|
|
@@ -164,7 +164,7 @@ class TiledeskWhatsapp {
|
|
|
164
164
|
}
|
|
165
165
|
}
|
|
166
166
|
}).catch((err) => {
|
|
167
|
-
winston.error("(wab) [TiledeskWhatsapp] An error occured: "
|
|
167
|
+
winston.error("(wab) [TiledeskWhatsapp] An error occured: ", err);
|
|
168
168
|
if (callback) {
|
|
169
169
|
callback(err, null, null);
|
|
170
170
|
}
|
|
@@ -50,7 +50,7 @@ const path = require('path');
|
|
|
50
50
|
*/
|
|
51
51
|
toWhatsapp(tiledeskChannelMessage, whatsapp_receiver) {
|
|
52
52
|
|
|
53
|
-
winston.debug("(wab) [Translator] tiledesk message: "
|
|
53
|
+
winston.debug("(wab) [Translator] tiledesk message: ", tiledeskChannelMessage);
|
|
54
54
|
|
|
55
55
|
let text = '';
|
|
56
56
|
if (tiledeskChannelMessage.text) {
|
|
@@ -318,7 +318,7 @@ const path = require('path');
|
|
|
318
318
|
|
|
319
319
|
async toTiledesk(whatsappChannelMessage, from, media_url) {
|
|
320
320
|
|
|
321
|
-
winston.debug("(wab) [Translator] whatsapp message: "
|
|
321
|
+
winston.debug("(wab) [Translator] whatsapp message: ", whatsappChannelMessage);
|
|
322
322
|
|
|
323
323
|
// text message
|
|
324
324
|
if (whatsappChannelMessage.type == 'text') {
|