@tiledesk/tiledesk-whatsapp-connector 0.1.57 → 0.1.59
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 +741 -497
- package/package.json +1 -1
- package/template/detail.html +8 -8
- package/tiledesk/WhatsappLogger.js +40 -32
package/index.js
CHANGED
|
@@ -2,37 +2,41 @@
|
|
|
2
2
|
const express = require("express");
|
|
3
3
|
const router = express.Router();
|
|
4
4
|
const bodyParser = require("body-parser");
|
|
5
|
-
const appRoot = require(
|
|
6
|
-
const handlebars = require(
|
|
7
|
-
const fs = require(
|
|
8
|
-
const path = require(
|
|
9
|
-
const pjson = require(
|
|
10
|
-
const { v4: uuidv4 } = require(
|
|
11
|
-
const cors = require(
|
|
12
|
-
var winston = require(
|
|
13
|
-
const url = require(
|
|
14
|
-
const mongoose = require(
|
|
5
|
+
const appRoot = require("app-root-path");
|
|
6
|
+
const handlebars = require("handlebars");
|
|
7
|
+
const fs = require("fs");
|
|
8
|
+
const path = require("path");
|
|
9
|
+
const pjson = require("./package.json");
|
|
10
|
+
const { v4: uuidv4 } = require("uuid");
|
|
11
|
+
const cors = require("cors");
|
|
12
|
+
var winston = require("./winston");
|
|
13
|
+
const url = require("url");
|
|
14
|
+
const mongoose = require("mongoose");
|
|
15
15
|
|
|
16
16
|
//const ext = require('./routes/ext')
|
|
17
17
|
//const extRoute = ext.router;
|
|
18
|
-
const api = require(
|
|
18
|
+
const api = require("./routes/api");
|
|
19
19
|
const apiRoute = api.router;
|
|
20
20
|
|
|
21
21
|
// tiledesk clients
|
|
22
|
-
const { TiledeskClient } = require(
|
|
23
|
-
const {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
const {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
const {
|
|
30
|
-
const {
|
|
31
|
-
const {
|
|
22
|
+
const { TiledeskClient } = require("@tiledesk/tiledesk-client");
|
|
23
|
+
const {
|
|
24
|
+
TiledeskWhatsappTranslator,
|
|
25
|
+
} = require("./tiledesk/TiledeskWhatsappTranslator");
|
|
26
|
+
const {
|
|
27
|
+
TiledeskSubscriptionClient,
|
|
28
|
+
} = require("./tiledesk/TiledeskSubscriptionClient");
|
|
29
|
+
const { TiledeskWhatsapp } = require("./tiledesk/TiledeskWhatsapp");
|
|
30
|
+
const { TiledeskChannel } = require("./tiledesk/TiledeskChannel");
|
|
31
|
+
const { TiledeskAppsClient } = require("./tiledesk/TiledeskAppsClient");
|
|
32
|
+
const { MessageHandler } = require("./tiledesk/MessageHandler");
|
|
33
|
+
const { TiledeskBotTester } = require("./tiledesk/TiledeskBotTester");
|
|
34
|
+
const { TemplateManager } = require("./tiledesk/TemplateManager");
|
|
35
|
+
const { WhatsappLogger } = require("./tiledesk/WhatsappLogger");
|
|
32
36
|
|
|
33
37
|
// mongo
|
|
34
|
-
const { KVBaseMongo } = require(
|
|
35
|
-
const kvbase_collection =
|
|
38
|
+
const { KVBaseMongo } = require("./tiledesk/KVBaseMongo");
|
|
39
|
+
const kvbase_collection = "kvstore";
|
|
36
40
|
const db = new KVBaseMongo(kvbase_collection);
|
|
37
41
|
|
|
38
42
|
// mongo old
|
|
@@ -41,16 +45,16 @@ const db = new KVBaseMongo(kvbase_collection);
|
|
|
41
45
|
//const db = new KVBaseMongo(kvbase_collection);
|
|
42
46
|
|
|
43
47
|
// redis
|
|
44
|
-
var redis = require(
|
|
48
|
+
var redis = require("redis");
|
|
45
49
|
var redis_client;
|
|
46
50
|
|
|
47
51
|
router.use(bodyParser.json());
|
|
48
52
|
router.use(bodyParser.urlencoded({ extended: true }));
|
|
49
|
-
router.use(express.static(path.join(__dirname,
|
|
53
|
+
router.use(express.static(path.join(__dirname, "template")));
|
|
50
54
|
router.use(cors());
|
|
51
55
|
|
|
52
|
-
router.use(
|
|
53
|
-
router.use(
|
|
56
|
+
router.use("/ext", apiRoute);
|
|
57
|
+
router.use("/api", apiRoute);
|
|
54
58
|
|
|
55
59
|
let API_URL = null;
|
|
56
60
|
let GRAPH_URL = null;
|
|
@@ -63,37 +67,42 @@ let BASE_FILE_URL = null;
|
|
|
63
67
|
let AMQP_MANAGER_URL = null;
|
|
64
68
|
let JOB_TOPIC_EXCHANGE = null;
|
|
65
69
|
|
|
66
|
-
|
|
67
70
|
// Handlebars register helpers
|
|
68
|
-
handlebars.registerHelper(
|
|
71
|
+
handlebars.registerHelper("isEqual", (a, b) => {
|
|
69
72
|
if (a == b) {
|
|
70
|
-
return true
|
|
73
|
+
return true;
|
|
71
74
|
} else {
|
|
72
|
-
return false
|
|
75
|
+
return false;
|
|
73
76
|
}
|
|
74
|
-
})
|
|
77
|
+
});
|
|
75
78
|
|
|
76
|
-
handlebars.registerHelper(
|
|
79
|
+
handlebars.registerHelper("json", (a) => {
|
|
77
80
|
return JSON.stringify(a);
|
|
78
|
-
})
|
|
79
|
-
|
|
80
|
-
router.get('/', async (req, res) => {
|
|
81
|
-
res.send('Welcome to Tiledesk-WhatsApp Business connector!')
|
|
82
|
-
})
|
|
81
|
+
});
|
|
83
82
|
|
|
84
|
-
router.get(
|
|
83
|
+
router.get("/", async (req, res) => {
|
|
84
|
+
res.send("Welcome to Tiledesk-WhatsApp Business connector!");
|
|
85
|
+
});
|
|
85
86
|
|
|
86
|
-
|
|
87
|
+
router.get("/detail", async (req, res) => {
|
|
88
|
+
winston.verbose("(wab) /detail");
|
|
87
89
|
|
|
88
90
|
let project_id = req.query.project_id;
|
|
89
91
|
let token = req.query.token;
|
|
90
92
|
let app_id = req.query.app_id;
|
|
91
93
|
|
|
92
|
-
const tdChannel = new TiledeskChannel({
|
|
94
|
+
const tdChannel = new TiledeskChannel({
|
|
95
|
+
settings: { project_id: project_id, token: token },
|
|
96
|
+
API_URL: API_URL,
|
|
97
|
+
});
|
|
93
98
|
let isAvailable = await tdChannel.getProjectDetail();
|
|
94
99
|
winston.debug("(wab) app is available: ", isAvailable);
|
|
95
100
|
if (!project_id || !token || !app_id) {
|
|
96
|
-
return res
|
|
101
|
+
return res
|
|
102
|
+
.status(500)
|
|
103
|
+
.send(
|
|
104
|
+
"<p>Ops! An error occured.</p><p>Missing query params! project_id, token and app_id are required.</p>"
|
|
105
|
+
);
|
|
97
106
|
}
|
|
98
107
|
|
|
99
108
|
const appClient = new TiledeskAppsClient({ APPS_API_URL: APPS_API_URL });
|
|
@@ -104,7 +113,7 @@ router.get('/detail', async (req, res) => {
|
|
|
104
113
|
installed = true;
|
|
105
114
|
}
|
|
106
115
|
|
|
107
|
-
readHTMLFile(
|
|
116
|
+
readHTMLFile("/detail.html", (err, html) => {
|
|
108
117
|
var template = handlebars.compile(html);
|
|
109
118
|
var replacements = {
|
|
110
119
|
app_version: pjson.version,
|
|
@@ -112,102 +121,105 @@ router.get('/detail', async (req, res) => {
|
|
|
112
121
|
token: token,
|
|
113
122
|
app_id: app_id,
|
|
114
123
|
installed: installed,
|
|
115
|
-
isAvailable: isAvailable
|
|
116
|
-
}
|
|
124
|
+
isAvailable: isAvailable,
|
|
125
|
+
};
|
|
117
126
|
var html = template(replacements);
|
|
118
127
|
res.send(html);
|
|
119
|
-
})
|
|
120
|
-
})
|
|
121
|
-
|
|
122
|
-
router.post('/install', async (req, res) => {
|
|
128
|
+
});
|
|
129
|
+
});
|
|
123
130
|
|
|
124
|
-
|
|
131
|
+
router.post("/install", async (req, res) => {
|
|
132
|
+
winston.verbose("(wab) /install");
|
|
125
133
|
|
|
126
134
|
let project_id = req.body.project_id;
|
|
127
135
|
let app_id = req.body.app_id;
|
|
128
136
|
let token = req.body.token;
|
|
129
137
|
|
|
130
|
-
winston.debug(
|
|
138
|
+
winston.debug(
|
|
139
|
+
"(wab) Install app " + app_id + " for project id " + project_id
|
|
140
|
+
);
|
|
131
141
|
let installation_info = {
|
|
132
142
|
project_id: project_id,
|
|
133
143
|
app_id: app_id,
|
|
134
|
-
createdAt: Date.now()
|
|
144
|
+
createdAt: Date.now(),
|
|
135
145
|
};
|
|
136
146
|
|
|
137
147
|
const appClient = new TiledeskAppsClient({ APPS_API_URL: APPS_API_URL });
|
|
138
|
-
appClient
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
148
|
+
appClient
|
|
149
|
+
.install(installation_info)
|
|
150
|
+
.then((installation) => {
|
|
151
|
+
winston.debug("(wab) installation response: ", installation);
|
|
152
|
+
|
|
153
|
+
res.redirect(
|
|
154
|
+
url.format({
|
|
155
|
+
pathname: BASE_URL + "/detail",
|
|
156
|
+
query: {
|
|
157
|
+
project_id: project_id,
|
|
158
|
+
app_id: app_id,
|
|
159
|
+
token: token,
|
|
160
|
+
},
|
|
161
|
+
})
|
|
162
|
+
);
|
|
163
|
+
})
|
|
164
|
+
.catch((err) => {
|
|
165
|
+
winston.error("(wab) installation error: " + err.data);
|
|
166
|
+
res.send("An error occurred during the installation");
|
|
167
|
+
});
|
|
168
|
+
});
|
|
158
169
|
|
|
170
|
+
router.post("/uninstall", async (req, res) => {
|
|
159
171
|
winston.verbose("(wab) /uninstall");
|
|
160
172
|
let project_id = req.body.project_id;
|
|
161
173
|
let app_id = req.body.app_id;
|
|
162
174
|
let token = req.body.token;
|
|
163
175
|
|
|
164
176
|
const appClient = new TiledeskAppsClient({ APPS_API_URL: APPS_API_URL });
|
|
165
|
-
appClient
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
177
|
+
appClient
|
|
178
|
+
.uninstall(project_id, app_id)
|
|
179
|
+
.then((response) => {
|
|
180
|
+
winston.debug("(wab) uninstallation response: ", response);
|
|
181
|
+
|
|
182
|
+
res.redirect(
|
|
183
|
+
url.format({
|
|
184
|
+
pathname: BASE_URL + "/detail",
|
|
185
|
+
query: {
|
|
186
|
+
project_id: project_id,
|
|
187
|
+
app_id: app_id,
|
|
188
|
+
token: token,
|
|
189
|
+
},
|
|
190
|
+
})
|
|
191
|
+
);
|
|
192
|
+
})
|
|
193
|
+
.catch((err) => {
|
|
194
|
+
winston.error("(wab) uninstallation error: " + err.data);
|
|
195
|
+
res.send("An error occurred during the uninstallation");
|
|
196
|
+
});
|
|
197
|
+
});
|
|
185
198
|
|
|
199
|
+
router.get("/configure", async (req, res) => {
|
|
186
200
|
winston.verbose("(wab) /configure");
|
|
187
201
|
|
|
188
202
|
let project_id = req.query.project_id;
|
|
189
203
|
let token = req.query.token;
|
|
190
204
|
let popup_view = false;
|
|
191
205
|
|
|
192
|
-
if (req.query.view && req.query.view ===
|
|
193
|
-
popup_view = true
|
|
206
|
+
if (req.query.view && req.query.view === "popup") {
|
|
207
|
+
popup_view = true;
|
|
194
208
|
}
|
|
195
209
|
|
|
196
210
|
if (!project_id || !token) {
|
|
197
|
-
let error_message = "Query params project_id and token are required."
|
|
198
|
-
readHTMLFile(
|
|
211
|
+
let error_message = "Query params project_id and token are required.";
|
|
212
|
+
readHTMLFile("/error.html", (err, html) => {
|
|
199
213
|
var template = handlebars.compile(html);
|
|
200
214
|
|
|
201
215
|
var replacements = {
|
|
202
216
|
app_version: pjson.version,
|
|
203
|
-
error_message: error_message
|
|
204
|
-
}
|
|
217
|
+
error_message: error_message,
|
|
218
|
+
};
|
|
205
219
|
var html = template(replacements);
|
|
206
220
|
return res.send(html);
|
|
207
|
-
})
|
|
208
|
-
|
|
221
|
+
});
|
|
209
222
|
} else {
|
|
210
|
-
|
|
211
223
|
let proxy_url = BASE_URL + "/webhook/" + project_id;
|
|
212
224
|
|
|
213
225
|
let CONTENT_KEY = "whatsapp-" + project_id;
|
|
@@ -216,13 +228,15 @@ router.get('/configure', async (req, res) => {
|
|
|
216
228
|
winston.debug("(wab) settings: ", settings);
|
|
217
229
|
|
|
218
230
|
// get departments
|
|
219
|
-
const tdChannel = new TiledeskChannel({
|
|
231
|
+
const tdChannel = new TiledeskChannel({
|
|
232
|
+
settings: { project_id: project_id, token: token },
|
|
233
|
+
API_URL: API_URL,
|
|
234
|
+
});
|
|
220
235
|
let departments = await tdChannel.getDepartments(token);
|
|
221
|
-
winston.debug("(wab) found " + departments.length + " departments")
|
|
236
|
+
winston.debug("(wab) found " + departments.length + " departments");
|
|
222
237
|
|
|
223
238
|
if (settings) {
|
|
224
|
-
|
|
225
|
-
readHTMLFile('/configure.html', (err, html) => {
|
|
239
|
+
readHTMLFile("/configure.html", (err, html) => {
|
|
226
240
|
var template = handlebars.compile(html);
|
|
227
241
|
var replacements = {
|
|
228
242
|
app_version: pjson.version,
|
|
@@ -234,17 +248,13 @@ router.get('/configure', async (req, res) => {
|
|
|
234
248
|
business_account_id: settings.business_account_id,
|
|
235
249
|
subscription_id: settings.subscriptionId,
|
|
236
250
|
department_id: settings.department_id,
|
|
237
|
-
departments: departments
|
|
238
|
-
}
|
|
251
|
+
departments: departments,
|
|
252
|
+
};
|
|
239
253
|
var html = template(replacements);
|
|
240
254
|
res.send(html);
|
|
241
|
-
|
|
242
|
-
})
|
|
243
|
-
|
|
255
|
+
});
|
|
244
256
|
} else {
|
|
245
|
-
|
|
246
|
-
readHTMLFile('/configure.html', (err, html) => {
|
|
247
|
-
|
|
257
|
+
readHTMLFile("/configure.html", (err, html) => {
|
|
248
258
|
if (err) {
|
|
249
259
|
winston.error("(wab) error read html file: " + err);
|
|
250
260
|
}
|
|
@@ -256,19 +266,16 @@ router.get('/configure', async (req, res) => {
|
|
|
256
266
|
token: token,
|
|
257
267
|
proxy_url: proxy_url,
|
|
258
268
|
departments: departments,
|
|
259
|
-
popup_view: popup_view
|
|
260
|
-
}
|
|
269
|
+
popup_view: popup_view,
|
|
270
|
+
};
|
|
261
271
|
var html = template(replacements);
|
|
262
272
|
res.send(html);
|
|
263
|
-
|
|
264
|
-
})
|
|
273
|
+
});
|
|
265
274
|
}
|
|
266
275
|
}
|
|
276
|
+
});
|
|
267
277
|
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
router.post('/update', async (req, res) => {
|
|
271
|
-
|
|
278
|
+
router.post("/update", async (req, res) => {
|
|
272
279
|
winston.verbose("(wab) /update");
|
|
273
280
|
|
|
274
281
|
let project_id = req.body.project_id;
|
|
@@ -284,11 +291,13 @@ router.post('/update', async (req, res) => {
|
|
|
284
291
|
let proxy_url = BASE_URL + "/webhook/" + project_id;
|
|
285
292
|
|
|
286
293
|
// get departments
|
|
287
|
-
const tdChannel = new TiledeskChannel({
|
|
294
|
+
const tdChannel = new TiledeskChannel({
|
|
295
|
+
settings: { project_id: project_id, token: token },
|
|
296
|
+
API_URL: API_URL,
|
|
297
|
+
});
|
|
288
298
|
let departments = await tdChannel.getDepartments(token);
|
|
289
299
|
|
|
290
300
|
if (settings) {
|
|
291
|
-
|
|
292
301
|
settings.wab_token = wab_token;
|
|
293
302
|
settings.verify_token = verify_token;
|
|
294
303
|
settings.department_id = department_id;
|
|
@@ -296,7 +305,7 @@ router.post('/update', async (req, res) => {
|
|
|
296
305
|
|
|
297
306
|
await db.set(CONTENT_KEY, settings);
|
|
298
307
|
|
|
299
|
-
readHTMLFile(
|
|
308
|
+
readHTMLFile("/configure.html", (err, html) => {
|
|
300
309
|
var template = handlebars.compile(html);
|
|
301
310
|
var replacements = {
|
|
302
311
|
app_version: pjson.version,
|
|
@@ -309,20 +318,22 @@ router.post('/update', async (req, res) => {
|
|
|
309
318
|
business_account_id: settings.business_account_id,
|
|
310
319
|
subscription_id: settings.subscriptionId,
|
|
311
320
|
department_id: settings.department_id,
|
|
312
|
-
departments: departments
|
|
313
|
-
}
|
|
321
|
+
departments: departments,
|
|
322
|
+
};
|
|
314
323
|
var html = template(replacements);
|
|
315
324
|
res.send(html);
|
|
316
|
-
})
|
|
317
|
-
|
|
325
|
+
});
|
|
318
326
|
} else {
|
|
319
|
-
|
|
320
|
-
|
|
327
|
+
const tdClient = new TiledeskSubscriptionClient({
|
|
328
|
+
API_URL: API_URL,
|
|
329
|
+
project_id: project_id,
|
|
330
|
+
token: token,
|
|
331
|
+
});
|
|
321
332
|
|
|
322
333
|
const subscription_info = {
|
|
323
334
|
target: BASE_URL + "/tiledesk",
|
|
324
|
-
event:
|
|
325
|
-
}
|
|
335
|
+
event: "message.create.request.channel.whatsapp",
|
|
336
|
+
};
|
|
326
337
|
|
|
327
338
|
/*
|
|
328
339
|
// callback
|
|
@@ -332,82 +343,87 @@ router.post('/update', async (req, res) => {
|
|
|
332
343
|
*/
|
|
333
344
|
|
|
334
345
|
// promise
|
|
335
|
-
tdClient
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
project_id: project_id,
|
|
341
|
-
token: token,
|
|
342
|
-
proxy_url: proxy_url,
|
|
343
|
-
subscriptionId: subscription._id,
|
|
344
|
-
secret: subscription.secret,
|
|
345
|
-
wab_token: wab_token,
|
|
346
|
-
verify_token: verify_token,
|
|
347
|
-
business_account_id: business_account_id,
|
|
348
|
-
department_id: department_id
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
db.set(CONTENT_KEY, settings)
|
|
352
|
-
//let cnt = db.get(CONTENT_KEY);
|
|
353
|
-
|
|
354
|
-
readHTMLFile('/configure.html', (err, html) => {
|
|
355
|
-
var template = handlebars.compile(html);
|
|
356
|
-
var replacements = {
|
|
357
|
-
app_version: pjson.version,
|
|
358
|
-
project_id: project_id,
|
|
359
|
-
token: token,
|
|
360
|
-
proxy_url: proxy_url,
|
|
361
|
-
show_success_modal: true,
|
|
362
|
-
wab_token: settings.wab_token,
|
|
363
|
-
verify_token: settings.verify_token,
|
|
364
|
-
business_account_id: settings.business_account_id,
|
|
365
|
-
subscription_id: settings.subscriptionId,
|
|
366
|
-
department_id: settings.department_id,
|
|
367
|
-
departments: departments
|
|
368
|
-
}
|
|
369
|
-
var html = template(replacements);
|
|
370
|
-
res.send(html);
|
|
371
|
-
})
|
|
346
|
+
tdClient
|
|
347
|
+
.subscribe(subscription_info)
|
|
348
|
+
.then((data) => {
|
|
349
|
+
let subscription = data;
|
|
350
|
+
winston.debug("\n(wab) Subscription: ", subscription);
|
|
372
351
|
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
readHTMLFile('/configure.html', (err, html) => {
|
|
376
|
-
var template = handlebars.compile(html);
|
|
377
|
-
var replacements = {
|
|
378
|
-
app_version: pjson.version,
|
|
352
|
+
let settings = {
|
|
379
353
|
project_id: project_id,
|
|
380
354
|
token: token,
|
|
381
355
|
proxy_url: proxy_url,
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
356
|
+
subscriptionId: subscription._id,
|
|
357
|
+
secret: subscription.secret,
|
|
358
|
+
wab_token: wab_token,
|
|
359
|
+
verify_token: verify_token,
|
|
360
|
+
business_account_id: business_account_id,
|
|
361
|
+
department_id: department_id,
|
|
362
|
+
};
|
|
363
|
+
|
|
364
|
+
db.set(CONTENT_KEY, settings);
|
|
365
|
+
//let cnt = db.get(CONTENT_KEY);
|
|
366
|
+
|
|
367
|
+
readHTMLFile("/configure.html", (err, html) => {
|
|
368
|
+
var template = handlebars.compile(html);
|
|
369
|
+
var replacements = {
|
|
370
|
+
app_version: pjson.version,
|
|
371
|
+
project_id: project_id,
|
|
372
|
+
token: token,
|
|
373
|
+
proxy_url: proxy_url,
|
|
374
|
+
show_success_modal: true,
|
|
375
|
+
wab_token: settings.wab_token,
|
|
376
|
+
verify_token: settings.verify_token,
|
|
377
|
+
business_account_id: settings.business_account_id,
|
|
378
|
+
subscription_id: settings.subscriptionId,
|
|
379
|
+
department_id: settings.department_id,
|
|
380
|
+
departments: departments,
|
|
381
|
+
};
|
|
382
|
+
var html = template(replacements);
|
|
383
|
+
res.send(html);
|
|
384
|
+
});
|
|
387
385
|
})
|
|
388
|
-
|
|
389
|
-
|
|
386
|
+
.catch((err) => {
|
|
387
|
+
readHTMLFile("/configure.html", (err, html) => {
|
|
388
|
+
var template = handlebars.compile(html);
|
|
389
|
+
var replacements = {
|
|
390
|
+
app_version: pjson.version,
|
|
391
|
+
project_id: project_id,
|
|
392
|
+
token: token,
|
|
393
|
+
proxy_url: proxy_url,
|
|
394
|
+
departments: departments,
|
|
395
|
+
show_error_modal: true,
|
|
396
|
+
};
|
|
397
|
+
var html = template(replacements);
|
|
398
|
+
res.send(html);
|
|
399
|
+
});
|
|
400
|
+
});
|
|
390
401
|
}
|
|
391
|
-
})
|
|
392
|
-
|
|
393
|
-
router.post('/disconnect', async (req, res) => {
|
|
402
|
+
});
|
|
394
403
|
|
|
395
|
-
|
|
404
|
+
router.post("/disconnect", async (req, res) => {
|
|
405
|
+
winston.verbose("(wab) /disconnect");
|
|
396
406
|
|
|
397
407
|
let project_id = req.body.project_id;
|
|
398
408
|
let token = req.body.token;
|
|
399
409
|
let subscriptionId = req.body.subscription_id;
|
|
400
410
|
|
|
401
|
-
|
|
402
411
|
let CONTENT_KEY = "whatsapp-" + project_id;
|
|
403
412
|
await db.remove(CONTENT_KEY);
|
|
404
413
|
winston.verbose("(wab) Content deleted.");
|
|
405
414
|
|
|
406
415
|
let proxy_url = BASE_URL + "/webhook/" + project_id;
|
|
407
416
|
|
|
408
|
-
const tdClient = new TiledeskSubscriptionClient({
|
|
417
|
+
const tdClient = new TiledeskSubscriptionClient({
|
|
418
|
+
API_URL: API_URL,
|
|
419
|
+
project_id: project_id,
|
|
420
|
+
token: token,
|
|
421
|
+
});
|
|
409
422
|
// get departments
|
|
410
|
-
const tdChannel = new TiledeskChannel({
|
|
423
|
+
const tdChannel = new TiledeskChannel({
|
|
424
|
+
settings: { project_id: project_id, token: token },
|
|
425
|
+
API_URL: API_URL,
|
|
426
|
+
});
|
|
411
427
|
let departments = await tdChannel.getDepartments(token);
|
|
412
428
|
|
|
413
429
|
/*
|
|
@@ -417,30 +433,29 @@ router.post('/disconnect', async (req, res) => {
|
|
|
417
433
|
})
|
|
418
434
|
*/
|
|
419
435
|
|
|
420
|
-
tdClient
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
436
|
+
tdClient
|
|
437
|
+
.unsubscribe(subscriptionId)
|
|
438
|
+
.then((data) => {
|
|
439
|
+
readHTMLFile("/configure.html", (err, html) => {
|
|
440
|
+
var template = handlebars.compile(html);
|
|
441
|
+
var replacements = {
|
|
442
|
+
app_version: pjson.version,
|
|
443
|
+
project_id: project_id,
|
|
444
|
+
token: token,
|
|
445
|
+
proxy_url: proxy_url,
|
|
446
|
+
departments: departments,
|
|
447
|
+
};
|
|
448
|
+
var html = template(replacements);
|
|
449
|
+
res.send(html);
|
|
450
|
+
});
|
|
433
451
|
})
|
|
452
|
+
.catch((err) => {
|
|
453
|
+
winston.error("(wab) unsubscribe error: " + err);
|
|
454
|
+
});
|
|
455
|
+
});
|
|
434
456
|
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
})
|
|
438
|
-
|
|
439
|
-
})
|
|
440
|
-
|
|
441
|
-
router.get('/direct/tiledesk', async (req, res) => {
|
|
442
|
-
|
|
443
|
-
winston.verbose("/direct/tiledesk")
|
|
457
|
+
router.get("/direct/tiledesk", async (req, res) => {
|
|
458
|
+
winston.verbose("/direct/tiledesk");
|
|
444
459
|
|
|
445
460
|
let project_id = req.query.project_id;
|
|
446
461
|
let whatsapp_receiver = req.query.whatsapp_receiver;
|
|
@@ -456,62 +471,99 @@ router.get('/direct/tiledesk', async (req, res) => {
|
|
|
456
471
|
type: "wa_template",
|
|
457
472
|
template: {
|
|
458
473
|
language: "en_US",
|
|
459
|
-
name: "hello_world"
|
|
460
|
-
}
|
|
461
|
-
}
|
|
462
|
-
}
|
|
463
|
-
}
|
|
474
|
+
name: "hello_world",
|
|
475
|
+
},
|
|
476
|
+
},
|
|
477
|
+
},
|
|
478
|
+
};
|
|
464
479
|
const tlr = new TiledeskWhatsappTranslator();
|
|
465
|
-
const twClient = new TiledeskWhatsapp({
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
480
|
+
const twClient = new TiledeskWhatsapp({
|
|
481
|
+
token: settings.wab_token,
|
|
482
|
+
GRAPH_URL: GRAPH_URL,
|
|
483
|
+
API_URL: API_URL,
|
|
484
|
+
BASE_FILE_URL: BASE_FILE_URL,
|
|
485
|
+
});
|
|
469
486
|
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
487
|
+
let whatsappJsonMessage = await tlr.toWhatsapp(
|
|
488
|
+
tiledeskChannelMessage,
|
|
489
|
+
whatsapp_receiver
|
|
490
|
+
);
|
|
491
|
+
winston.verbose("(wab) whatsappJsonMessage", whatsappJsonMessage);
|
|
492
|
+
|
|
493
|
+
twClient
|
|
494
|
+
.sendMessage(phone_number_id, whatsappJsonMessage)
|
|
495
|
+
.then((response) => {
|
|
496
|
+
winston.verbose(
|
|
497
|
+
"(wab) Message sent to WhatsApp! " +
|
|
498
|
+
response.status +
|
|
499
|
+
" " +
|
|
500
|
+
response.statusText
|
|
501
|
+
);
|
|
502
|
+
})
|
|
503
|
+
.catch((err) => {
|
|
504
|
+
winston.error("(wab) error send message: ", err);
|
|
505
|
+
return res.status(400).send({ success: false, error: err });
|
|
506
|
+
});
|
|
476
507
|
|
|
477
508
|
res.status(200).send("Message sent");
|
|
509
|
+
});
|
|
478
510
|
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
router.post('/tiledesk/broadcast', async (req, res) => {
|
|
511
|
+
router.post("/tiledesk/broadcast", async (req, res) => {
|
|
482
512
|
winston.verbose("(wab) Message received from Tiledesk (Broadcast)");
|
|
483
513
|
|
|
484
514
|
let tiledeskChannelMessage = req.body.payload;
|
|
485
|
-
winston.verbose("(wab) tiledeskChannelMessage: ", tiledeskChannelMessage)
|
|
515
|
+
winston.verbose("(wab) tiledeskChannelMessage: ", tiledeskChannelMessage);
|
|
486
516
|
let project_id = req.body.payload.id_project;
|
|
487
517
|
|
|
488
518
|
let CONTENT_KEY = "whatsapp-" + project_id;
|
|
489
519
|
let settings = await db.get(CONTENT_KEY);
|
|
490
520
|
|
|
491
521
|
if (!settings) {
|
|
492
|
-
return res
|
|
522
|
+
return res
|
|
523
|
+
.status(400)
|
|
524
|
+
.send({
|
|
525
|
+
success: false,
|
|
526
|
+
error: "WhatsApp is not installed for the project_id: " + project_id,
|
|
527
|
+
});
|
|
493
528
|
}
|
|
494
529
|
|
|
495
530
|
if (!settings.business_account_id) {
|
|
496
|
-
return res
|
|
531
|
+
return res
|
|
532
|
+
.status(400)
|
|
533
|
+
.send({
|
|
534
|
+
success: false,
|
|
535
|
+
error:
|
|
536
|
+
"Missing parameter 'WhatsApp Business Account ID'. Please update your app.",
|
|
537
|
+
});
|
|
497
538
|
}
|
|
498
539
|
|
|
499
540
|
let receiver_list = req.body.receiver_list;
|
|
500
541
|
let phone_number_id = req.body.phone_number_id;
|
|
501
542
|
|
|
502
|
-
let tm = new TemplateManager({
|
|
543
|
+
let tm = new TemplateManager({
|
|
544
|
+
token: settings.wab_token,
|
|
545
|
+
business_account_id: settings.business_account_id,
|
|
546
|
+
GRAPH_URL: GRAPH_URL,
|
|
547
|
+
});
|
|
503
548
|
const tlr = new TiledeskWhatsappTranslator();
|
|
504
|
-
const twClient = new TiledeskWhatsapp({
|
|
549
|
+
const twClient = new TiledeskWhatsapp({
|
|
550
|
+
token: settings.wab_token,
|
|
551
|
+
GRAPH_URL: GRAPH_URL,
|
|
552
|
+
API_URL: API_URL,
|
|
553
|
+
BASE_FILE_URL: BASE_FILE_URL,
|
|
554
|
+
});
|
|
505
555
|
|
|
506
556
|
let response = await tm.getTemplates();
|
|
507
557
|
let templates = response.data;
|
|
508
558
|
|
|
509
|
-
let selected_template = templates.find(
|
|
559
|
+
let selected_template = templates.find(
|
|
560
|
+
(t) => t.name === tiledeskChannelMessage.attributes.attachment.template.name
|
|
561
|
+
);
|
|
510
562
|
|
|
511
563
|
let params_object = await tm.generateParamsObject(selected_template);
|
|
512
564
|
|
|
513
565
|
tiledeskChannelMessage.attributes.attachment.template.params = params_object;
|
|
514
|
-
winston.verbose("(wab) --> tiledeskChannelMessage: ", tiledeskChannelMessage)
|
|
566
|
+
winston.verbose("(wab) --> tiledeskChannelMessage: ", tiledeskChannelMessage);
|
|
515
567
|
|
|
516
568
|
let messages_sent = 0;
|
|
517
569
|
let errors = [];
|
|
@@ -520,41 +572,73 @@ router.post('/tiledesk/broadcast', async (req, res) => {
|
|
|
520
572
|
if (receiver_list) {
|
|
521
573
|
let i = 0;
|
|
522
574
|
async function execute(whatsapp_receiver) {
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
575
|
+
let customTiledeskJsonMessage = await tlr.sanitizeTiledeskMessage(
|
|
576
|
+
tiledeskChannelMessage,
|
|
577
|
+
whatsapp_receiver
|
|
578
|
+
);
|
|
579
|
+
winston.info("customTiledeskJsonMessage: ", customTiledeskJsonMessage);
|
|
580
|
+
|
|
581
|
+
let whatsappJsonMessage = await tlr.toWhatsapp(
|
|
582
|
+
customTiledeskJsonMessage,
|
|
583
|
+
whatsapp_receiver.phone_number
|
|
584
|
+
);
|
|
585
|
+
winston.verbose(
|
|
586
|
+
"(wab) message created for receiver: " + whatsapp_receiver.phone_number
|
|
587
|
+
);
|
|
529
588
|
winston.verbose("(wab) whatsappJsonMessage", whatsappJsonMessage);
|
|
530
589
|
|
|
531
|
-
await twClient
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
590
|
+
await twClient
|
|
591
|
+
.sendMessage(phone_number_id, whatsappJsonMessage)
|
|
592
|
+
.then((response) => {
|
|
593
|
+
winston.verbose(
|
|
594
|
+
"(wab) Message sent to WhatsApp! " +
|
|
595
|
+
response.status +
|
|
596
|
+
" " +
|
|
597
|
+
response.statusText
|
|
598
|
+
);
|
|
599
|
+
messages_sent += 1;
|
|
600
|
+
i += 1;
|
|
601
|
+
if (i < receiver_list.length) {
|
|
602
|
+
execute(receiver_list[i]);
|
|
603
|
+
} else {
|
|
604
|
+
winston.debug("(wab) End of list");
|
|
605
|
+
return res
|
|
606
|
+
.status(200)
|
|
607
|
+
.send({
|
|
608
|
+
success: true,
|
|
609
|
+
message: "Broadcast terminated",
|
|
610
|
+
messages_sent: messages_sent,
|
|
611
|
+
errors: errors,
|
|
612
|
+
});
|
|
613
|
+
}
|
|
614
|
+
})
|
|
615
|
+
.catch((err) => {
|
|
616
|
+
winston.error(
|
|
617
|
+
"(wab) error send message: " + err.response.data.error.message
|
|
618
|
+
);
|
|
619
|
+
errors.push({
|
|
620
|
+
receiver: whatsapp_receiver.phone_number,
|
|
621
|
+
error: err.response.data.error.message,
|
|
622
|
+
});
|
|
623
|
+
i += 1;
|
|
624
|
+
if (i < receiver_list.length) {
|
|
625
|
+
execute(receiver_list[i]);
|
|
626
|
+
} else {
|
|
627
|
+
winston.debug("(wab) End of list");
|
|
628
|
+
return res
|
|
629
|
+
.status(200)
|
|
630
|
+
.send({
|
|
631
|
+
success: true,
|
|
632
|
+
message: "Broadcast terminated",
|
|
633
|
+
messages_sent: messages_sent,
|
|
634
|
+
errors: errors,
|
|
635
|
+
});
|
|
636
|
+
}
|
|
637
|
+
});
|
|
552
638
|
}
|
|
553
639
|
execute(receiver_list[0]);
|
|
554
640
|
}
|
|
555
|
-
|
|
556
|
-
})
|
|
557
|
-
|
|
641
|
+
});
|
|
558
642
|
|
|
559
643
|
/*
|
|
560
644
|
router.post('/tiledesk/broadcast2', async (req, res) => {
|
|
@@ -627,12 +711,11 @@ router.post('/tiledesk/broadcast2', async (req, res) => {
|
|
|
627
711
|
})
|
|
628
712
|
*/
|
|
629
713
|
|
|
630
|
-
router.post(
|
|
631
|
-
|
|
632
|
-
winston.verbose("(wab) Message received from Tiledesk")
|
|
714
|
+
router.post("/tiledesk", async (req, res) => {
|
|
715
|
+
winston.verbose("(wab) Message received from Tiledesk");
|
|
633
716
|
|
|
634
717
|
var tiledeskChannelMessage = req.body.payload;
|
|
635
|
-
winston.debug("(wab) tiledeskChannelMessage: ", tiledeskChannelMessage)
|
|
718
|
+
winston.debug("(wab) tiledeskChannelMessage: ", tiledeskChannelMessage);
|
|
636
719
|
var project_id = req.body.payload.id_project;
|
|
637
720
|
|
|
638
721
|
// get settings from mongo
|
|
@@ -659,28 +742,33 @@ router.post('/tiledesk', async (req, res) => {
|
|
|
659
742
|
return res.sendStatus(200);
|
|
660
743
|
}
|
|
661
744
|
|
|
662
|
-
if (attributes && attributes.subtype ===
|
|
745
|
+
if (attributes && attributes.subtype === "info/support") {
|
|
663
746
|
winston.verbose("(wab) Skip subtype: " + attributes.subtype);
|
|
664
747
|
return res.sendStatus(200);
|
|
665
748
|
}
|
|
666
749
|
|
|
667
750
|
let recipient_id = tiledeskChannelMessage.recipient;
|
|
668
751
|
let sender = tiledeskChannelMessage.sender;
|
|
669
|
-
let whatsapp_receiver = recipient_id.substring(
|
|
752
|
+
let whatsapp_receiver = recipient_id.substring(
|
|
753
|
+
recipient_id.lastIndexOf("-") + 1
|
|
754
|
+
);
|
|
670
755
|
|
|
671
756
|
let phone_number_id;
|
|
672
757
|
if (attributes && attributes.whatsapp_phone_number_id) {
|
|
673
758
|
phone_number_id = attributes.whatsapp_phone_number_id;
|
|
674
759
|
} else {
|
|
675
|
-
phone_number_id = recipient_id.substring(
|
|
760
|
+
phone_number_id = recipient_id.substring(
|
|
761
|
+
recipient_id.lastIndexOf("wab-") + 4,
|
|
762
|
+
recipient_id.lastIndexOf("-")
|
|
763
|
+
);
|
|
676
764
|
}
|
|
677
765
|
|
|
678
766
|
if (!phone_number_id) {
|
|
679
|
-
return res
|
|
767
|
+
return res
|
|
768
|
+
.status(400)
|
|
769
|
+
.send({ success: false, message: "Phone number id undefined" });
|
|
680
770
|
}
|
|
681
771
|
|
|
682
|
-
console.log("phone_number_id: ", phone_number_id)
|
|
683
|
-
|
|
684
772
|
/*
|
|
685
773
|
if (settings.expired &&
|
|
686
774
|
settings.expired === true) {
|
|
@@ -690,72 +778,98 @@ router.post('/tiledesk', async (req, res) => {
|
|
|
690
778
|
*/
|
|
691
779
|
|
|
692
780
|
// Return an info message option
|
|
693
|
-
if (settings.expired &&
|
|
694
|
-
settings.expired === true) {
|
|
695
|
-
|
|
781
|
+
if (settings.expired && settings.expired === true) {
|
|
696
782
|
winston.verbose("settings expired: " + settings.expired);
|
|
697
783
|
let tiledeskJsonMessage = {
|
|
698
|
-
text:
|
|
784
|
+
text: "Expired. Upgrade Plan.",
|
|
699
785
|
sender: "system",
|
|
700
786
|
senderFullname: "System",
|
|
701
787
|
attributes: {
|
|
702
|
-
subtype:
|
|
788
|
+
subtype: "info",
|
|
703
789
|
},
|
|
704
|
-
channel: { name:
|
|
705
|
-
}
|
|
790
|
+
channel: { name: "whatsapp" },
|
|
791
|
+
};
|
|
706
792
|
let message_info = {
|
|
707
793
|
channel: "whatsapp",
|
|
708
794
|
whatsapp: {
|
|
709
795
|
from: whatsapp_receiver,
|
|
710
|
-
phone_number_id: phone_number_id
|
|
711
|
-
}
|
|
712
|
-
}
|
|
713
|
-
|
|
714
|
-
const tdChannel = new TiledeskChannel({
|
|
715
|
-
|
|
716
|
-
|
|
796
|
+
phone_number_id: phone_number_id,
|
|
797
|
+
},
|
|
798
|
+
};
|
|
799
|
+
|
|
800
|
+
const tdChannel = new TiledeskChannel({
|
|
801
|
+
settings: settings,
|
|
802
|
+
API_URL: API_URL,
|
|
803
|
+
});
|
|
804
|
+
const response = await tdChannel.send(
|
|
805
|
+
tiledeskJsonMessage,
|
|
806
|
+
message_info,
|
|
807
|
+
settings.department_id
|
|
808
|
+
);
|
|
809
|
+
winston.verbose("(wab) Expiration message sent to Tiledesk");
|
|
717
810
|
return res.sendStatus(200);
|
|
718
811
|
}
|
|
719
812
|
|
|
720
813
|
winston.debug("(wab) text: " + text);
|
|
721
|
-
winston.debug("(wab) attributes: " + attributes)
|
|
814
|
+
winston.debug("(wab) attributes: " + attributes);
|
|
722
815
|
winston.debug("(wab) tiledesk sender_id: " + sender_id);
|
|
723
816
|
winston.debug("(wab) recipient_id: " + recipient_id);
|
|
724
817
|
winston.debug("(wab) whatsapp_receiver: " + whatsapp_receiver);
|
|
725
818
|
winston.debug("(wab) phone_number_id: " + phone_number_id);
|
|
726
819
|
|
|
727
|
-
const messageHandler = new MessageHandler({
|
|
820
|
+
const messageHandler = new MessageHandler({
|
|
821
|
+
tiledeskChannelMessage: tiledeskChannelMessage,
|
|
822
|
+
});
|
|
728
823
|
const tlr = new TiledeskWhatsappTranslator();
|
|
729
824
|
|
|
730
|
-
|
|
731
825
|
if (commands) {
|
|
732
826
|
let i = 0;
|
|
733
827
|
async function execute(command) {
|
|
734
828
|
// message
|
|
735
829
|
if (command.type === "message") {
|
|
736
|
-
let tiledeskCommandMessage = await messageHandler.generateMessageObject(
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
830
|
+
let tiledeskCommandMessage = await messageHandler.generateMessageObject(
|
|
831
|
+
command
|
|
832
|
+
);
|
|
833
|
+
winston.debug(
|
|
834
|
+
"(wab) message generated from command: ",
|
|
835
|
+
tiledeskCommandMessage
|
|
836
|
+
);
|
|
837
|
+
|
|
838
|
+
let whatsappJsonMessage = await tlr.toWhatsapp(
|
|
839
|
+
tiledeskCommandMessage,
|
|
840
|
+
whatsapp_receiver
|
|
841
|
+
);
|
|
842
|
+
winston.verbose("(wab) whatsappJsonMessage", whatsappJsonMessage);
|
|
741
843
|
|
|
742
844
|
if (whatsappJsonMessage) {
|
|
743
|
-
const twClient = new TiledeskWhatsapp({
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
845
|
+
const twClient = new TiledeskWhatsapp({
|
|
846
|
+
token: settings.wab_token,
|
|
847
|
+
GRAPH_URL: GRAPH_URL,
|
|
848
|
+
API_URL: API_URL,
|
|
849
|
+
BASE_FILE_URL: BASE_FILE_URL,
|
|
850
|
+
});
|
|
851
|
+
twClient
|
|
852
|
+
.sendMessage(phone_number_id, whatsappJsonMessage)
|
|
853
|
+
.then((response) => {
|
|
854
|
+
winston.verbose(
|
|
855
|
+
"(wab) Message sent to WhatsApp! " +
|
|
856
|
+
response.status +
|
|
857
|
+
" " +
|
|
858
|
+
response.statusText
|
|
859
|
+
);
|
|
860
|
+
i += 1;
|
|
861
|
+
if (i < commands.length) {
|
|
862
|
+
execute(commands[i]);
|
|
863
|
+
} else {
|
|
864
|
+
winston.debug("(wab) End of commands");
|
|
865
|
+
}
|
|
866
|
+
})
|
|
867
|
+
.catch((err) => {
|
|
868
|
+
winston.error("(wab) send message error: ", err);
|
|
869
|
+
});
|
|
755
870
|
} else {
|
|
756
|
-
winston.error("(wab) WhatsappJsonMessage is undefined!")
|
|
871
|
+
winston.error("(wab) WhatsappJsonMessage is undefined!");
|
|
757
872
|
}
|
|
758
|
-
|
|
759
873
|
}
|
|
760
874
|
|
|
761
875
|
//wait
|
|
@@ -765,45 +879,62 @@ router.post('/tiledesk', async (req, res) => {
|
|
|
765
879
|
if (i < commands.length) {
|
|
766
880
|
execute(commands[i]);
|
|
767
881
|
} else {
|
|
768
|
-
winston.debug("(wab) End of commands")
|
|
882
|
+
winston.debug("(wab) End of commands");
|
|
769
883
|
}
|
|
770
|
-
}, command.time)
|
|
884
|
+
}, command.time);
|
|
771
885
|
}
|
|
772
886
|
}
|
|
773
887
|
execute(commands[0]);
|
|
774
|
-
}
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
winston.verbose("(wab) whatsappJsonMessage", whatsappJsonMessage)
|
|
888
|
+
} else if (tiledeskChannelMessage.text || tiledeskChannelMessage.metadata) {
|
|
889
|
+
let whatsappJsonMessage = await tlr.toWhatsapp(
|
|
890
|
+
tiledeskChannelMessage,
|
|
891
|
+
whatsapp_receiver
|
|
892
|
+
);
|
|
893
|
+
winston.verbose("(wab) whatsappJsonMessage", whatsappJsonMessage);
|
|
780
894
|
|
|
781
895
|
if (whatsappJsonMessage) {
|
|
782
|
-
const twClient = new TiledeskWhatsapp({
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
res.status(400).send({ success: false, error: "Template not existing" });
|
|
789
|
-
})
|
|
896
|
+
const twClient = new TiledeskWhatsapp({
|
|
897
|
+
token: settings.wab_token,
|
|
898
|
+
GRAPH_URL: GRAPH_URL,
|
|
899
|
+
API_URL: API_URL,
|
|
900
|
+
BASE_FILE_URL: BASE_FILE_URL,
|
|
901
|
+
});
|
|
790
902
|
|
|
903
|
+
twClient
|
|
904
|
+
.sendMessage(phone_number_id, whatsappJsonMessage)
|
|
905
|
+
.then((response) => {
|
|
906
|
+
winston.verbose(
|
|
907
|
+
"(wab) Message sent to WhatsApp! " +
|
|
908
|
+
response.status +
|
|
909
|
+
" " +
|
|
910
|
+
response.statusText
|
|
911
|
+
);
|
|
912
|
+
})
|
|
913
|
+
.catch((err) => {
|
|
914
|
+
winston.error("(wab) error send message: ", err);
|
|
915
|
+
res
|
|
916
|
+
.status(400)
|
|
917
|
+
.send({ success: false, error: "Template not existing" });
|
|
918
|
+
});
|
|
791
919
|
} else {
|
|
792
|
-
winston.error("(wab) Whatsapp Json Message is undefined!")
|
|
793
|
-
res
|
|
920
|
+
winston.error("(wab) Whatsapp Json Message is undefined!");
|
|
921
|
+
res
|
|
922
|
+
.status(400)
|
|
923
|
+
.send({
|
|
924
|
+
success: false,
|
|
925
|
+
error: "An error occurred during message translation",
|
|
926
|
+
});
|
|
794
927
|
}
|
|
795
|
-
|
|
796
928
|
} else {
|
|
797
|
-
winston.debug("(wab) no command, no text --> skip")
|
|
929
|
+
winston.debug("(wab) no command, no text --> skip");
|
|
798
930
|
}
|
|
799
931
|
|
|
800
932
|
return res.sendStatus(200);
|
|
801
|
-
})
|
|
933
|
+
});
|
|
802
934
|
|
|
803
935
|
// Endpoint for Whatsapp Business
|
|
804
936
|
// Accepts POST requests at /webhook endpoint
|
|
805
937
|
router.post("/webhook/:project_id", async (req, res) => {
|
|
806
|
-
|
|
807
938
|
// Parse the request body from the POST
|
|
808
939
|
let project_id = req.params.project_id;
|
|
809
940
|
winston.verbose("(wab) Message received from WhatsApp");
|
|
@@ -811,6 +942,22 @@ router.post("/webhook/:project_id", async (req, res) => {
|
|
|
811
942
|
// Check the Incoming webhook message
|
|
812
943
|
// info on WhatsApp text message payload: https://developers.facebook.com/docs/whatsapp/cloud-api/webhooks/payload-examples#text-messages
|
|
813
944
|
if (req.body.object) {
|
|
945
|
+
|
|
946
|
+
let CONTENT_KEY = "whatsapp-" + project_id;
|
|
947
|
+
let settings = await db.get(CONTENT_KEY);
|
|
948
|
+
winston.debug("(wab) settings: ", settings);
|
|
949
|
+
|
|
950
|
+
const tdClient = new TiledeskClient({
|
|
951
|
+
projectId: project_id,
|
|
952
|
+
token: settings.token,
|
|
953
|
+
APIURL: API_URL,
|
|
954
|
+
APIKEY: "___",
|
|
955
|
+
log: false,
|
|
956
|
+
});
|
|
957
|
+
|
|
958
|
+
const wl = new WhatsappLogger({ tdClient: tdClient });
|
|
959
|
+
wl.forwardMessage(req.body);
|
|
960
|
+
|
|
814
961
|
if (
|
|
815
962
|
req.body.entry &&
|
|
816
963
|
req.body.entry[0].changes &&
|
|
@@ -818,54 +965,71 @@ router.post("/webhook/:project_id", async (req, res) => {
|
|
|
818
965
|
req.body.entry[0].changes[0].value.messages &&
|
|
819
966
|
req.body.entry[0].changes[0].value.messages[0]
|
|
820
967
|
) {
|
|
821
|
-
|
|
822
968
|
if (req.body.entry[0].changes[0].value.messages[0].type == "system") {
|
|
823
|
-
winston.verbose("(wab) Skip system message")
|
|
969
|
+
winston.verbose("(wab) Skip system message");
|
|
824
970
|
return res.sendStatus(200);
|
|
825
971
|
}
|
|
826
972
|
|
|
827
|
-
let whatsappChannelMessage =
|
|
973
|
+
let whatsappChannelMessage =
|
|
974
|
+
req.body.entry[0].changes[0].value.messages[0];
|
|
828
975
|
|
|
976
|
+
/*
|
|
829
977
|
let CONTENT_KEY = "whatsapp-" + project_id;
|
|
830
978
|
let settings = await db.get(CONTENT_KEY);
|
|
831
979
|
winston.debug("(wab) settings: ", settings);
|
|
832
|
-
|
|
980
|
+
*/
|
|
981
|
+
|
|
833
982
|
if (!settings) {
|
|
834
983
|
winston.verbose("(wab) No settings found. Exit..");
|
|
835
984
|
return res.sendStatus(200);
|
|
836
985
|
}
|
|
837
986
|
|
|
838
987
|
const tlr = new TiledeskWhatsappTranslator();
|
|
839
|
-
const tdChannel = new TiledeskChannel({
|
|
988
|
+
const tdChannel = new TiledeskChannel({
|
|
989
|
+
settings: settings,
|
|
990
|
+
API_URL: API_URL,
|
|
991
|
+
});
|
|
840
992
|
|
|
841
993
|
// Initialize conversation with chatbot
|
|
842
|
-
if (
|
|
843
|
-
|
|
844
|
-
|
|
994
|
+
if (
|
|
995
|
+
whatsappChannelMessage.text &&
|
|
996
|
+
whatsappChannelMessage.text.body.startsWith("#td")
|
|
997
|
+
) {
|
|
998
|
+
let code = whatsappChannelMessage.text.body.split(" ")[0];
|
|
845
999
|
|
|
846
|
-
const bottester = new TiledeskBotTester({
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
})
|
|
1000
|
+
const bottester = new TiledeskBotTester({
|
|
1001
|
+
project_id: project_id,
|
|
1002
|
+
redis_client: redis_client,
|
|
1003
|
+
db: db,
|
|
1004
|
+
tdChannel: tdChannel,
|
|
1005
|
+
tlr: tlr,
|
|
1006
|
+
});
|
|
1007
|
+
bottester
|
|
1008
|
+
.startBotConversation(req.body, code)
|
|
1009
|
+
.then((result) => {
|
|
1010
|
+
winston.verbose("(wab) test conversation started");
|
|
1011
|
+
winston.debug("(wab) startBotConversation result: ", result);
|
|
1012
|
+
})
|
|
1013
|
+
.catch((err) => {
|
|
1014
|
+
winston.error("(wab) start test onversation error: ", err);
|
|
1015
|
+
});
|
|
853
1016
|
|
|
854
1017
|
// Standard message
|
|
855
|
-
}
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
let firstname = req.body.entry[0].changes[0].value.contacts[0].profile.name;
|
|
1018
|
+
} else {
|
|
1019
|
+
let firstname =
|
|
1020
|
+
req.body.entry[0].changes[0].value.contacts[0].profile.name;
|
|
859
1021
|
|
|
860
1022
|
let message_info = {
|
|
861
1023
|
channel: "whatsapp",
|
|
862
1024
|
whatsapp: {
|
|
863
|
-
phone_number_id:
|
|
1025
|
+
phone_number_id:
|
|
1026
|
+
req.body.entry[0].changes[0].value.metadata.phone_number_id,
|
|
864
1027
|
from: req.body.entry[0].changes[0].value.messages[0].from,
|
|
865
|
-
firstname:
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
1028
|
+
firstname:
|
|
1029
|
+
req.body.entry[0].changes[0].value.contacts[0].profile.name,
|
|
1030
|
+
lastname: " ",
|
|
1031
|
+
},
|
|
1032
|
+
};
|
|
869
1033
|
|
|
870
1034
|
let tiledeskJsonMessage;
|
|
871
1035
|
|
|
@@ -953,104 +1117,156 @@ router.post("/webhook/:project_id", async (req, res) => {
|
|
|
953
1117
|
}
|
|
954
1118
|
*/
|
|
955
1119
|
|
|
956
|
-
|
|
957
|
-
|
|
958
1120
|
////////////////////////////////////////////////////
|
|
959
1121
|
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
1122
|
+
if (
|
|
1123
|
+
whatsappChannelMessage.type == "image" ||
|
|
1124
|
+
whatsappChannelMessage.type == "video" ||
|
|
1125
|
+
whatsappChannelMessage.type == "document" ||
|
|
1126
|
+
whatsappChannelMessage.type == "audio"
|
|
1127
|
+
) {
|
|
963
1128
|
let media;
|
|
964
|
-
const util = new TiledeskWhatsapp({
|
|
965
|
-
|
|
966
|
-
|
|
1129
|
+
const util = new TiledeskWhatsapp({
|
|
1130
|
+
token: settings.wab_token,
|
|
1131
|
+
GRAPH_URL: GRAPH_URL,
|
|
1132
|
+
API_URL: API_URL,
|
|
1133
|
+
BASE_FILE_URL: BASE_FILE_URL,
|
|
1134
|
+
});
|
|
1135
|
+
|
|
1136
|
+
if (whatsappChannelMessage.type == "image") {
|
|
967
1137
|
media = whatsappChannelMessage.image;
|
|
968
1138
|
const filename = await util.downloadMedia(media.id);
|
|
969
1139
|
if (!filename) {
|
|
970
|
-
winston.debug(
|
|
971
|
-
|
|
1140
|
+
winston.debug(
|
|
1141
|
+
"(wab) Unable to download media with id " +
|
|
1142
|
+
media.id +
|
|
1143
|
+
". Message not sent."
|
|
1144
|
+
);
|
|
1145
|
+
return res
|
|
1146
|
+
.status(500)
|
|
1147
|
+
.send({ success: false, error: "unable to download media" });
|
|
972
1148
|
}
|
|
973
|
-
let file_path = path.join(__dirname,
|
|
1149
|
+
let file_path = path.join(__dirname, "tmp", filename);
|
|
974
1150
|
|
|
975
1151
|
const image_url = await util.uploadMedia(file_path, "images");
|
|
976
|
-
winston.debug("(wab) image_url: " + image_url)
|
|
1152
|
+
winston.debug("(wab) image_url: " + image_url);
|
|
977
1153
|
|
|
978
|
-
tiledeskJsonMessage = await tlr.toTiledesk(
|
|
1154
|
+
tiledeskJsonMessage = await tlr.toTiledesk(
|
|
1155
|
+
whatsappChannelMessage,
|
|
1156
|
+
firstname,
|
|
1157
|
+
image_url
|
|
1158
|
+
);
|
|
979
1159
|
}
|
|
980
1160
|
|
|
981
|
-
if (whatsappChannelMessage.type ==
|
|
1161
|
+
if (whatsappChannelMessage.type == "video") {
|
|
982
1162
|
media = whatsappChannelMessage.video;
|
|
983
1163
|
|
|
984
1164
|
const filename = await util.downloadMedia(media.id);
|
|
985
1165
|
if (!filename) {
|
|
986
|
-
winston.debug(
|
|
987
|
-
|
|
1166
|
+
winston.debug(
|
|
1167
|
+
"(wab) Unable to download media with id " +
|
|
1168
|
+
media.id +
|
|
1169
|
+
". Message not sent."
|
|
1170
|
+
);
|
|
1171
|
+
return res
|
|
1172
|
+
.status(500)
|
|
1173
|
+
.send({ success: false, error: "unable to download media" });
|
|
988
1174
|
}
|
|
989
|
-
let file_path = path.join(__dirname,
|
|
1175
|
+
let file_path = path.join(__dirname, "tmp", filename);
|
|
990
1176
|
|
|
991
1177
|
const media_url = await util.uploadMedia(file_path, "files");
|
|
992
|
-
winston.debug("(wab) media_url: " + media_url)
|
|
1178
|
+
winston.debug("(wab) media_url: " + media_url);
|
|
993
1179
|
|
|
994
|
-
tiledeskJsonMessage = await tlr.toTiledesk(
|
|
1180
|
+
tiledeskJsonMessage = await tlr.toTiledesk(
|
|
1181
|
+
whatsappChannelMessage,
|
|
1182
|
+
firstname,
|
|
1183
|
+
media_url
|
|
1184
|
+
);
|
|
995
1185
|
}
|
|
996
1186
|
|
|
997
|
-
if (whatsappChannelMessage.type ==
|
|
1187
|
+
if (whatsappChannelMessage.type == "document") {
|
|
998
1188
|
media = whatsappChannelMessage.document;
|
|
999
1189
|
|
|
1000
1190
|
const filename = await util.downloadMedia(media.id);
|
|
1001
1191
|
if (!filename) {
|
|
1002
|
-
winston.debug(
|
|
1003
|
-
|
|
1192
|
+
winston.debug(
|
|
1193
|
+
"(wab) Unable to download media with id " +
|
|
1194
|
+
media.id +
|
|
1195
|
+
". Message not sent."
|
|
1196
|
+
);
|
|
1197
|
+
return res
|
|
1198
|
+
.status(500)
|
|
1199
|
+
.send({ success: false, error: "unable to download media" });
|
|
1004
1200
|
}
|
|
1005
|
-
let file_path = path.join(__dirname,
|
|
1201
|
+
let file_path = path.join(__dirname, "tmp", filename);
|
|
1006
1202
|
|
|
1007
1203
|
const media_url = await util.uploadMedia(file_path, "files");
|
|
1008
|
-
winston.debug("(wab) media_url: " + media_url)
|
|
1204
|
+
winston.debug("(wab) media_url: " + media_url);
|
|
1009
1205
|
|
|
1010
|
-
tiledeskJsonMessage = await tlr.toTiledesk(
|
|
1206
|
+
tiledeskJsonMessage = await tlr.toTiledesk(
|
|
1207
|
+
whatsappChannelMessage,
|
|
1208
|
+
firstname,
|
|
1209
|
+
media_url
|
|
1210
|
+
);
|
|
1011
1211
|
}
|
|
1012
1212
|
|
|
1013
|
-
if (whatsappChannelMessage.type ==
|
|
1213
|
+
if (whatsappChannelMessage.type == "audio") {
|
|
1014
1214
|
media = whatsappChannelMessage.audio;
|
|
1015
1215
|
|
|
1016
1216
|
const filename = await util.downloadMedia(media.id);
|
|
1017
1217
|
if (!filename) {
|
|
1018
|
-
winston.debug(
|
|
1019
|
-
|
|
1218
|
+
winston.debug(
|
|
1219
|
+
"(wab) Unable to download media with id " +
|
|
1220
|
+
media.id +
|
|
1221
|
+
". Message not sent."
|
|
1222
|
+
);
|
|
1223
|
+
return res
|
|
1224
|
+
.status(500)
|
|
1225
|
+
.send({ success: false, error: "unable to download media" });
|
|
1020
1226
|
}
|
|
1021
|
-
let file_path = path.join(__dirname,
|
|
1227
|
+
let file_path = path.join(__dirname, "tmp", filename);
|
|
1022
1228
|
|
|
1023
1229
|
const media_url = await util.uploadMedia(file_path, "files");
|
|
1024
|
-
winston.debug("(wab) media_url: " + media_url)
|
|
1230
|
+
winston.debug("(wab) media_url: " + media_url);
|
|
1025
1231
|
|
|
1026
|
-
tiledeskJsonMessage = await tlr.toTiledesk(
|
|
1232
|
+
tiledeskJsonMessage = await tlr.toTiledesk(
|
|
1233
|
+
whatsappChannelMessage,
|
|
1234
|
+
firstname,
|
|
1235
|
+
media_url
|
|
1236
|
+
);
|
|
1027
1237
|
}
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1238
|
+
} else {
|
|
1239
|
+
winston.debug("(wab) message type: ", whatsappChannelMessage.type);
|
|
1240
|
+
tiledeskJsonMessage = await tlr.toTiledesk(
|
|
1241
|
+
whatsappChannelMessage,
|
|
1242
|
+
firstname
|
|
1243
|
+
);
|
|
1033
1244
|
}
|
|
1034
1245
|
|
|
1035
1246
|
if (tiledeskJsonMessage) {
|
|
1036
1247
|
winston.verbose("(wab) tiledeskJsonMessage: ", tiledeskJsonMessage);
|
|
1037
|
-
const response = await tdChannel.send(
|
|
1038
|
-
|
|
1039
|
-
|
|
1248
|
+
const response = await tdChannel.send(
|
|
1249
|
+
tiledeskJsonMessage,
|
|
1250
|
+
message_info,
|
|
1251
|
+
settings.department_id
|
|
1252
|
+
);
|
|
1253
|
+
winston.verbose("(wab) Message sent to Tiledesk!");
|
|
1254
|
+
winston.debug("(wab) response: ", response);
|
|
1040
1255
|
} else {
|
|
1041
|
-
winston.verbose("(wab) tiledeskJsonMessage is undefined")
|
|
1256
|
+
winston.verbose("(wab) tiledeskJsonMessage is undefined");
|
|
1042
1257
|
}
|
|
1043
|
-
|
|
1044
1258
|
}
|
|
1045
1259
|
}
|
|
1046
1260
|
|
|
1047
|
-
if (
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1261
|
+
if (
|
|
1262
|
+
req.body.entry &&
|
|
1263
|
+
req.body.entry[0].changes &&
|
|
1264
|
+
req.body.entry[0].changes[0] &&
|
|
1265
|
+
req.body.entry[0].changes[0].value.statuses &&
|
|
1266
|
+
req.body.entry[0].changes[0].value.statuses[0]
|
|
1052
1267
|
) {
|
|
1053
|
-
let whatsappStatusMessage =
|
|
1268
|
+
let whatsappStatusMessage =
|
|
1269
|
+
req.body.entry[0].changes[0].value.statuses[0];
|
|
1054
1270
|
winston.verbose("(wab) whatsappStatusMessage: ", whatsappStatusMessage);
|
|
1055
1271
|
|
|
1056
1272
|
let message_id = whatsappStatusMessage.id;
|
|
@@ -1059,41 +1275,43 @@ router.post("/webhook/:project_id", async (req, res) => {
|
|
|
1059
1275
|
if (whatsappStatusMessage.errors) {
|
|
1060
1276
|
error = whatsappStatusMessage.errors[0].title;
|
|
1061
1277
|
}
|
|
1062
|
-
winston.verbose(
|
|
1278
|
+
winston.verbose(
|
|
1279
|
+
"(wab) update status in " + status + " for message_id " + message_id
|
|
1280
|
+
);
|
|
1063
1281
|
|
|
1282
|
+
/*
|
|
1064
1283
|
let CONTENT_KEY = "whatsapp-" + project_id;
|
|
1065
1284
|
let settings = await db.get(CONTENT_KEY);
|
|
1066
1285
|
winston.debug("(wab) settings: ", settings);
|
|
1067
|
-
|
|
1286
|
+
*/
|
|
1287
|
+
|
|
1068
1288
|
const tdClient = new TiledeskClient({
|
|
1069
1289
|
projectId: project_id,
|
|
1070
1290
|
token: settings.token,
|
|
1071
1291
|
APIURL: API_URL,
|
|
1072
1292
|
APIKEY: "___",
|
|
1073
|
-
log: false
|
|
1293
|
+
log: false,
|
|
1074
1294
|
});
|
|
1075
1295
|
|
|
1076
|
-
const wl = new WhatsappLogger({ tdClient: tdClient});
|
|
1296
|
+
const wl = new WhatsappLogger({ tdClient: tdClient });
|
|
1077
1297
|
wl.updateMessageStatus(message_id, status, error);
|
|
1078
|
-
|
|
1079
1298
|
}
|
|
1080
|
-
|
|
1081
|
-
res.sendStatus(200);
|
|
1082
1299
|
|
|
1300
|
+
res.sendStatus(200);
|
|
1083
1301
|
} else {
|
|
1084
1302
|
// Return a '404 Not Found' if event is not from a WhatsApp API
|
|
1085
|
-
winston.verbose("(wab) event not from whatsapp")
|
|
1303
|
+
winston.verbose("(wab) event not from whatsapp");
|
|
1086
1304
|
res.sendStatus(404);
|
|
1087
1305
|
}
|
|
1088
1306
|
});
|
|
1089
1307
|
|
|
1090
1308
|
// Accepts GET requests at the /webhook endpoint. You need this URL to setup webhook initially.
|
|
1091
|
-
// info on verification request payload: https://developers.facebook.com/docs/graph-api/webhooks/getting-started#verification-requests
|
|
1309
|
+
// info on verification request payload: https://developers.facebook.com/docs/graph-api/webhooks/getting-started#verification-requests
|
|
1092
1310
|
router.get("/webhook/:project_id", async (req, res) => {
|
|
1093
1311
|
/**
|
|
1094
1312
|
* UPDATE YOUR VERIFY TOKEN
|
|
1095
1313
|
*This will be the Verify Token value when you set up webhook
|
|
1096
|
-
|
|
1314
|
+
*/
|
|
1097
1315
|
winston.verbose("(wab) Verify the webhook... ");
|
|
1098
1316
|
winston.debug("(wab) req.query: " + req.query);
|
|
1099
1317
|
|
|
@@ -1107,7 +1325,7 @@ router.get("/webhook/:project_id", async (req, res) => {
|
|
|
1107
1325
|
let settings = await db.get(CONTENT_KEY);
|
|
1108
1326
|
|
|
1109
1327
|
if (!settings || !settings.verify_token) {
|
|
1110
|
-
winston.error("(wab) No settings found! Unable to verify token.")
|
|
1328
|
+
winston.error("(wab) No settings found! Unable to verify token.");
|
|
1111
1329
|
res.sendStatus(403);
|
|
1112
1330
|
} else {
|
|
1113
1331
|
let VERIFY_TOKEN = settings.verify_token;
|
|
@@ -1126,16 +1344,14 @@ router.get("/webhook/:project_id", async (req, res) => {
|
|
|
1126
1344
|
}
|
|
1127
1345
|
} else {
|
|
1128
1346
|
winston.error("(wab) mode or token undefined");
|
|
1129
|
-
res
|
|
1347
|
+
res
|
|
1348
|
+
.status(400)
|
|
1349
|
+
.send("impossible to verify the webhook: mode or token undefined.");
|
|
1130
1350
|
}
|
|
1131
|
-
|
|
1132
1351
|
}
|
|
1133
|
-
|
|
1134
1352
|
});
|
|
1135
1353
|
|
|
1136
|
-
|
|
1137
1354
|
router.post("/newtest", async (req, res) => {
|
|
1138
|
-
|
|
1139
1355
|
winston.verbose("(wab) initializing new test..");
|
|
1140
1356
|
|
|
1141
1357
|
let project_id = req.body.project_id;
|
|
@@ -1143,31 +1359,35 @@ router.post("/newtest", async (req, res) => {
|
|
|
1143
1359
|
|
|
1144
1360
|
let info = {
|
|
1145
1361
|
project_id: project_id,
|
|
1146
|
-
bot_id: bot_id
|
|
1147
|
-
}
|
|
1362
|
+
bot_id: bot_id,
|
|
1363
|
+
};
|
|
1148
1364
|
|
|
1149
|
-
let short_uid = uuidv4().substring(0, 8)
|
|
1365
|
+
let short_uid = uuidv4().substring(0, 8);
|
|
1150
1366
|
let key = "bottest:" + short_uid;
|
|
1151
1367
|
|
|
1152
1368
|
if (!redis_client) {
|
|
1153
|
-
return res
|
|
1369
|
+
return res
|
|
1370
|
+
.status(500)
|
|
1371
|
+
.send({
|
|
1372
|
+
message: "Test it out on Whatsapp not available. Redis not ready.",
|
|
1373
|
+
});
|
|
1154
1374
|
}
|
|
1155
1375
|
|
|
1156
|
-
await redis_client.set(key, JSON.stringify(info),
|
|
1376
|
+
await redis_client.set(key, JSON.stringify(info), "EX", 604800);
|
|
1157
1377
|
redis_client.get(key, (err, value) => {
|
|
1158
1378
|
if (err) {
|
|
1159
|
-
winston.error("(wab) redis get err: ", err)
|
|
1160
|
-
return res
|
|
1379
|
+
winston.error("(wab) redis get err: ", err);
|
|
1380
|
+
return res
|
|
1381
|
+
.status(500)
|
|
1382
|
+
.send({ success: "false", message: "Testing info could not be saved" });
|
|
1161
1383
|
} else {
|
|
1162
|
-
winston.debug("(wab) new test initialized with id: " + short_uid)
|
|
1384
|
+
winston.debug("(wab) new test initialized with id: " + short_uid);
|
|
1163
1385
|
return res.status(200).send({ short_uid: short_uid });
|
|
1164
1386
|
}
|
|
1165
|
-
})
|
|
1166
|
-
|
|
1167
|
-
})
|
|
1168
|
-
|
|
1169
|
-
router.get('/templates/detail', async (req, res) => {
|
|
1387
|
+
});
|
|
1388
|
+
});
|
|
1170
1389
|
|
|
1390
|
+
router.get("/templates/detail", async (req, res) => {
|
|
1171
1391
|
let project_id = req.query.project_id;
|
|
1172
1392
|
let token = req.query.token;
|
|
1173
1393
|
let app_id = req.query.app_id;
|
|
@@ -1186,9 +1406,15 @@ router.get('/templates/detail', async (req, res) => {
|
|
|
1186
1406
|
let namespace = templates_info.message_template_namespace;
|
|
1187
1407
|
let template = await tm.getTemplateById(namespace);
|
|
1188
1408
|
*/
|
|
1189
|
-
let tm = new TemplateManager({
|
|
1409
|
+
let tm = new TemplateManager({
|
|
1410
|
+
token: settings.wab_token,
|
|
1411
|
+
business_account_id: settings.business_account_id,
|
|
1412
|
+
GRAPH_URL: GRAPH_URL,
|
|
1413
|
+
});
|
|
1190
1414
|
let templates = await tm.getTemplates();
|
|
1191
|
-
let template = JSON.parse(
|
|
1415
|
+
let template = JSON.parse(
|
|
1416
|
+
JSON.stringify(templates.data.find((t) => t.id === template_id))
|
|
1417
|
+
);
|
|
1192
1418
|
let template_name = template.name;
|
|
1193
1419
|
|
|
1194
1420
|
let template_copy = {
|
|
@@ -1197,10 +1423,10 @@ router.get('/templates/detail', async (req, res) => {
|
|
|
1197
1423
|
language: template.language,
|
|
1198
1424
|
status: template.status,
|
|
1199
1425
|
id: template.id,
|
|
1200
|
-
category: template.category
|
|
1201
|
-
}
|
|
1426
|
+
category: template.category,
|
|
1427
|
+
};
|
|
1202
1428
|
|
|
1203
|
-
readHTMLFile(
|
|
1429
|
+
readHTMLFile("/template_detail.html", (err, html) => {
|
|
1204
1430
|
var template = handlebars.compile(html);
|
|
1205
1431
|
var replacements = {
|
|
1206
1432
|
app_version: pjson.version,
|
|
@@ -1208,16 +1434,15 @@ router.get('/templates/detail', async (req, res) => {
|
|
|
1208
1434
|
token: token,
|
|
1209
1435
|
app_id: app_id,
|
|
1210
1436
|
name: template_name,
|
|
1211
|
-
template: template_copy
|
|
1212
|
-
}
|
|
1437
|
+
template: template_copy,
|
|
1438
|
+
};
|
|
1213
1439
|
var html = template(replacements);
|
|
1214
1440
|
res.send(html);
|
|
1215
|
-
})
|
|
1216
|
-
|
|
1441
|
+
});
|
|
1217
1442
|
} else {
|
|
1218
|
-
return res.send("whatsapp not installed on this project")
|
|
1443
|
+
return res.send("whatsapp not installed on this project");
|
|
1219
1444
|
}
|
|
1220
|
-
})
|
|
1445
|
+
});
|
|
1221
1446
|
|
|
1222
1447
|
router.get("/templates/:project_id", async (req, res) => {
|
|
1223
1448
|
winston.verbose("(wab) /templates");
|
|
@@ -1231,29 +1456,35 @@ router.get("/templates/:project_id", async (req, res) => {
|
|
|
1231
1456
|
winston.debug("(wab) settings: ", settings);
|
|
1232
1457
|
|
|
1233
1458
|
if (settings) {
|
|
1234
|
-
let tm = new TemplateManager({
|
|
1459
|
+
let tm = new TemplateManager({
|
|
1460
|
+
token: settings.wab_token,
|
|
1461
|
+
business_account_id: settings.business_account_id,
|
|
1462
|
+
GRAPH_URL: GRAPH_URL,
|
|
1463
|
+
});
|
|
1235
1464
|
let templates = await tm.getTemplates();
|
|
1236
1465
|
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
readHTMLFile('/templates.html', (err, html) => {
|
|
1466
|
+
readHTMLFile("/templates.html", (err, html) => {
|
|
1240
1467
|
var template = handlebars.compile(html);
|
|
1241
1468
|
var replacements = {
|
|
1242
1469
|
app_version: pjson.version,
|
|
1243
1470
|
project_id: project_id,
|
|
1244
1471
|
token: token,
|
|
1245
1472
|
app_id: app_id,
|
|
1246
|
-
templates: templates.data
|
|
1247
|
-
}
|
|
1473
|
+
templates: templates.data,
|
|
1474
|
+
};
|
|
1248
1475
|
var html = template(replacements);
|
|
1249
1476
|
res.send(html);
|
|
1250
|
-
})
|
|
1477
|
+
});
|
|
1251
1478
|
} else {
|
|
1252
|
-
winston.verbose("No settings found.")
|
|
1253
|
-
return res
|
|
1479
|
+
winston.verbose("No settings found.");
|
|
1480
|
+
return res
|
|
1481
|
+
.status(404)
|
|
1482
|
+
.send({
|
|
1483
|
+
success: false,
|
|
1484
|
+
error: "whatsapp not installed for the project id " + project_id,
|
|
1485
|
+
});
|
|
1254
1486
|
}
|
|
1255
|
-
|
|
1256
|
-
})
|
|
1487
|
+
});
|
|
1257
1488
|
|
|
1258
1489
|
/*
|
|
1259
1490
|
router.get("/ext/templates/:project_id", async (req, res) => {
|
|
@@ -1308,16 +1539,16 @@ router.get("/ext/:project_id", async (req, res) => {
|
|
|
1308
1539
|
// *****************************
|
|
1309
1540
|
|
|
1310
1541
|
async function startApp(settings, callback) {
|
|
1311
|
-
winston.info("(wab) Starting Whatsapp App")
|
|
1542
|
+
winston.info("(wab) Starting Whatsapp App");
|
|
1312
1543
|
|
|
1313
1544
|
if (!settings.MONGODB_URL) {
|
|
1314
1545
|
winston.error("(wab) MONGODB_URL is mandatory. Exit...");
|
|
1315
|
-
return callback(
|
|
1546
|
+
return callback("Missing parameter: MONGODB_URL");
|
|
1316
1547
|
}
|
|
1317
1548
|
|
|
1318
1549
|
if (!settings.API_URL) {
|
|
1319
1550
|
winston.error("(wab) API_URL is mandatory. Exit...");
|
|
1320
|
-
return callback(
|
|
1551
|
+
return callback("Missing parameter: API_URL");
|
|
1321
1552
|
} else {
|
|
1322
1553
|
API_URL = settings.API_URL;
|
|
1323
1554
|
winston.info("(wab) API_URL: " + API_URL);
|
|
@@ -1325,7 +1556,7 @@ async function startApp(settings, callback) {
|
|
|
1325
1556
|
|
|
1326
1557
|
if (!settings.BASE_FILE_URL) {
|
|
1327
1558
|
winston.error("(wab) BASE_FILE_URL is mandatory. Exit...");
|
|
1328
|
-
return callback(
|
|
1559
|
+
return callback("Missing parameter: BASE_FILE_URL");
|
|
1329
1560
|
} else {
|
|
1330
1561
|
BASE_FILE_URL = settings.BASE_FILE_URL;
|
|
1331
1562
|
winston.info("(wab) BASE_FILE_URL: " + BASE_FILE_URL);
|
|
@@ -1333,7 +1564,7 @@ async function startApp(settings, callback) {
|
|
|
1333
1564
|
|
|
1334
1565
|
if (!settings.BASE_URL) {
|
|
1335
1566
|
winston.error("(wab) BASE_URL is mandatory. Exit...");
|
|
1336
|
-
return callback(
|
|
1567
|
+
return callback("Missing parameter: BASE_URL");
|
|
1337
1568
|
} else {
|
|
1338
1569
|
BASE_URL = settings.BASE_URL;
|
|
1339
1570
|
winston.info("(wab) BASE_URL: " + BASE_URL);
|
|
@@ -1341,7 +1572,7 @@ async function startApp(settings, callback) {
|
|
|
1341
1572
|
|
|
1342
1573
|
if (!settings.GRAPH_URL) {
|
|
1343
1574
|
winston.error("(wab) GRAPH_URL is mandatory. Exit...");
|
|
1344
|
-
return callback(
|
|
1575
|
+
return callback("Missing parameter: GRAPH_URL");
|
|
1345
1576
|
} else {
|
|
1346
1577
|
GRAPH_URL = settings.GRAPH_URL;
|
|
1347
1578
|
winston.info("(wab) GRAPH_URL: " + GRAPH_URL);
|
|
@@ -1349,7 +1580,7 @@ async function startApp(settings, callback) {
|
|
|
1349
1580
|
|
|
1350
1581
|
if (!settings.APPS_API_URL) {
|
|
1351
1582
|
winston.error("(wab) APPS_API_URL is mandatory. Exit...");
|
|
1352
|
-
return callback(
|
|
1583
|
+
return callback("Missing parameter: APPS_API_URL");
|
|
1353
1584
|
} else {
|
|
1354
1585
|
APPS_API_URL = settings.APPS_API_URL;
|
|
1355
1586
|
winston.info("(wab) APPS_API_URL: " + APPS_API_URL);
|
|
@@ -1361,7 +1592,9 @@ async function startApp(settings, callback) {
|
|
|
1361
1592
|
REDIS_PASSWORD = settings.REDIS_PASSWORD;
|
|
1362
1593
|
connectRedis();
|
|
1363
1594
|
} else {
|
|
1364
|
-
winston.info(
|
|
1595
|
+
winston.info(
|
|
1596
|
+
"(wab) Missing redis parameters --> Test it out on WhatsApp disabled"
|
|
1597
|
+
);
|
|
1365
1598
|
}
|
|
1366
1599
|
|
|
1367
1600
|
if (!settings.AMQP_MANAGER_URL) {
|
|
@@ -1372,7 +1605,9 @@ async function startApp(settings, callback) {
|
|
|
1372
1605
|
}
|
|
1373
1606
|
|
|
1374
1607
|
if (!settings.JOB_TOPIC_EXCHANGE) {
|
|
1375
|
-
winston.warning(
|
|
1608
|
+
winston.warning(
|
|
1609
|
+
"(wab) JOB_TOPIC_EXCHANGE should be present. Using default value"
|
|
1610
|
+
);
|
|
1376
1611
|
JOB_TOPIC_EXCHANGE = "tiledesk-whatsapp";
|
|
1377
1612
|
} else {
|
|
1378
1613
|
JOB_TOPIC_EXCHANGE = settings.JOB_TOPIC_EXCHANGE;
|
|
@@ -1385,11 +1620,15 @@ async function startApp(settings, callback) {
|
|
|
1385
1620
|
} else {
|
|
1386
1621
|
LOG_MONGODB_URL = settings.LOG_MONGODB_URL;
|
|
1387
1622
|
}
|
|
1388
|
-
|
|
1389
|
-
mongoose
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1623
|
+
|
|
1624
|
+
mongoose
|
|
1625
|
+
.connect(LOG_MONGODB_URL)
|
|
1626
|
+
.then(() => {
|
|
1627
|
+
winston.info("Mongoose DB Connected");
|
|
1628
|
+
})
|
|
1629
|
+
.catch((err) => {
|
|
1630
|
+
winston.error("(Mongoose) Unable to connect with MongoDB ", err);
|
|
1631
|
+
});
|
|
1393
1632
|
|
|
1394
1633
|
db.connect(settings.MONGODB_URL, () => {
|
|
1395
1634
|
winston.info("(wab) KVBaseMongo successfully connected.");
|
|
@@ -1397,56 +1636,61 @@ async function startApp(settings, callback) {
|
|
|
1397
1636
|
if (callback) {
|
|
1398
1637
|
callback(null);
|
|
1399
1638
|
}
|
|
1400
|
-
})
|
|
1639
|
+
});
|
|
1401
1640
|
|
|
1402
|
-
api.startRoute(
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1641
|
+
api.startRoute(
|
|
1642
|
+
{
|
|
1643
|
+
DB: db,
|
|
1644
|
+
API_URL: API_URL,
|
|
1645
|
+
GRAPH_URL: GRAPH_URL,
|
|
1646
|
+
BASE_FILE_URL: BASE_FILE_URL,
|
|
1647
|
+
AMQP_MANAGER_URL: AMQP_MANAGER_URL,
|
|
1648
|
+
JOB_TOPIC_EXCHANGE: JOB_TOPIC_EXCHANGE,
|
|
1649
|
+
},
|
|
1650
|
+
(err) => {
|
|
1651
|
+
if (!err) {
|
|
1652
|
+
winston.info("(wab) API route successfully started.");
|
|
1653
|
+
} else {
|
|
1654
|
+
winston.info("(wab) Unable to start API route.");
|
|
1655
|
+
}
|
|
1414
1656
|
}
|
|
1415
|
-
|
|
1657
|
+
);
|
|
1416
1658
|
}
|
|
1417
1659
|
|
|
1418
1660
|
function connectRedis() {
|
|
1419
1661
|
redis_client = redis.createClient({
|
|
1420
1662
|
host: REDIS_HOST,
|
|
1421
1663
|
port: REDIS_PORT,
|
|
1422
|
-
password: REDIS_PASSWORD
|
|
1664
|
+
password: REDIS_PASSWORD,
|
|
1423
1665
|
});
|
|
1424
1666
|
|
|
1425
|
-
redis_client.on(
|
|
1426
|
-
winston.info(
|
|
1427
|
-
})
|
|
1667
|
+
redis_client.on("error", (err) => {
|
|
1668
|
+
winston.info("(wab) Connect Redis Error " + err);
|
|
1669
|
+
});
|
|
1428
1670
|
/*
|
|
1429
1671
|
redis_client.on('connect', () => {
|
|
1430
1672
|
winston.info('Redis Connected!'); // Connected!
|
|
1431
1673
|
});
|
|
1432
1674
|
*/
|
|
1433
|
-
redis_client.on(
|
|
1434
|
-
winston.info("(wab) Redis ready!")
|
|
1435
|
-
})
|
|
1675
|
+
redis_client.on("ready", () => {
|
|
1676
|
+
winston.info("(wab) Redis ready!");
|
|
1677
|
+
});
|
|
1436
1678
|
//await redis_client.connect(); // only for v4
|
|
1437
|
-
|
|
1438
1679
|
}
|
|
1439
1680
|
|
|
1440
1681
|
function readHTMLFile(templateName, callback) {
|
|
1441
|
-
fs.readFile(
|
|
1442
|
-
|
|
1682
|
+
fs.readFile(
|
|
1683
|
+
__dirname + "/template" + templateName,
|
|
1684
|
+
{ encoding: "utf-8" },
|
|
1685
|
+
function (err, html) {
|
|
1443
1686
|
if (err) {
|
|
1444
1687
|
throw err;
|
|
1445
1688
|
//callback(err);
|
|
1446
1689
|
} else {
|
|
1447
|
-
callback(null, html)
|
|
1690
|
+
callback(null, html);
|
|
1448
1691
|
}
|
|
1449
|
-
}
|
|
1692
|
+
}
|
|
1693
|
+
);
|
|
1450
1694
|
}
|
|
1451
1695
|
|
|
1452
|
-
module.exports = { router: router, startApp: startApp };
|
|
1696
|
+
module.exports = { router: router, startApp: startApp };
|