@tiledesk/tiledesk-whatsapp-connector 0.1.81 → 0.1.82

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 CHANGED
@@ -212,13 +212,16 @@ router.get("/configure", async (req, res) => {
212
212
 
213
213
  let settings = await db.get(CONTENT_KEY);
214
214
  winston.debug("(wab) settings: ", settings);
215
+
216
+ settings.token = token;
217
+ await db.set(CONTENT_KEY, settings);
215
218
 
216
219
  // get departments
217
220
  const tdChannel = new TiledeskChannel({
218
221
  settings: { project_id: project_id, token: token },
219
222
  API_URL: API_URL,
220
223
  });
221
- let departments = await tdChannel.getDepartments(token);
224
+ let departments = await tdChannel.getDepartments();
222
225
  winston.debug("(wab) found " + departments.length + " departments");
223
226
 
224
227
  if (settings) {
@@ -232,6 +235,9 @@ router.get("/configure", async (req, res) => {
232
235
  wab_token: settings.wab_token,
233
236
  verify_token: settings.verify_token,
234
237
  business_account_id: settings.business_account_id,
238
+ phone_number: settings.phone_number,
239
+ phone_number_id: settings.phone_number_id,
240
+ verified_name: settings.verified_name,
235
241
  subscription_id: settings.subscriptionId,
236
242
  department_id: settings.department_id,
237
243
  departments: departments,
@@ -281,13 +287,28 @@ router.post("/update", async (req, res) => {
281
287
  settings: { project_id: project_id, token: token },
282
288
  API_URL: API_URL,
283
289
  });
284
- let departments = await tdChannel.getDepartments(token);
290
+ let departments = await tdChannel.getDepartments();
285
291
 
286
292
  if (settings) {
287
293
  settings.wab_token = wab_token;
288
294
  settings.verify_token = verify_token;
289
295
  settings.department_id = department_id;
290
296
  settings.business_account_id = business_account_id;
297
+
298
+ const twClient = new TiledeskWhatsapp({
299
+ token: settings.wab_token,
300
+ GRAPH_URL: GRAPH_URL,
301
+ API_URL: API_URL,
302
+ BASE_FILE_URL: BASE_FILE_URL,
303
+ });
304
+
305
+ await twClient.getBusinessAccountInfo(business_account_id).then((info) => {
306
+ settings.phone_number_id = info.data[0].id;
307
+ settings.phone_number = info.data[0].display_phone_number;
308
+ settings.verified_name = info.data[0].verified_name;
309
+ }).catch((err) => {
310
+ winston.error("Error getting WAB Account Info");
311
+ })
291
312
 
292
313
  await db.set(CONTENT_KEY, settings);
293
314
 
@@ -302,6 +323,9 @@ router.post("/update", async (req, res) => {
302
323
  show_success_modal: true,
303
324
  verify_token: settings.verify_token,
304
325
  business_account_id: settings.business_account_id,
326
+ phone_number: settings.phone_number,
327
+ phone_number_id: settings.phone_number_id,
328
+ verified_name: settings.verified_name,
305
329
  subscription_id: settings.subscriptionId,
306
330
  department_id: settings.department_id,
307
331
  departments: departments,
@@ -322,14 +346,7 @@ router.post("/update", async (req, res) => {
322
346
  event: "message.create.request.channel.whatsapp",
323
347
  };
324
348
 
325
- /*
326
- // callback
327
- await tdClient.subscribe(project_id, data, (err, data) => {
328
- // code here
329
- })
330
- */
331
349
 
332
- // promise
333
350
  tdClient.subscribe(subscription_info).then((data) => {
334
351
  let subscription = data;
335
352
  winston.debug("\n(wab) Subscription: ", subscription);
@@ -360,6 +377,9 @@ router.post("/update", async (req, res) => {
360
377
  wab_token: settings.wab_token,
361
378
  verify_token: settings.verify_token,
362
379
  business_account_id: settings.business_account_id,
380
+ phone_number: settings.phone_number,
381
+ phone_number_id: settings.phone_number_id,
382
+ verified_name: settings.verified_name,
363
383
  subscription_id: settings.subscriptionId,
364
384
  department_id: settings.department_id,
365
385
  departments: departments,
@@ -387,6 +407,65 @@ router.post("/update", async (req, res) => {
387
407
  }
388
408
  });
389
409
 
410
+ router.post("/getinfo", async (req, res) => {
411
+
412
+ winston.verbose("/getinfo");
413
+ let project_id = req.body.project_id;
414
+ let token = req.body.token;
415
+ let proxy_url = BASE_URL + "/webhook/" + project_id;
416
+
417
+ let CONTENT_KEY = "whatsapp-" + project_id;
418
+ let settings = await db.get(CONTENT_KEY);
419
+
420
+ const tdChannel = new TiledeskChannel({
421
+ settings: { project_id: project_id, token: token },
422
+ API_URL: API_URL,
423
+ });
424
+ let departments = await tdChannel.getDepartments();
425
+
426
+ const twClient = new TiledeskWhatsapp({
427
+ token: settings.wab_token,
428
+ GRAPH_URL: GRAPH_URL,
429
+ API_URL: API_URL,
430
+ BASE_FILE_URL: BASE_FILE_URL,
431
+ });
432
+
433
+ await twClient.getBusinessAccountInfo(settings.business_account_id).then((info) => {
434
+ settings.phone_number_id = info.data[0].id;
435
+ settings.phone_number = info.data[0].display_phone_number;
436
+ settings.verified_name = info.data[0].verified_name;
437
+ winston.debug("(wab) Business account info: ", info);
438
+ }).catch((err) => {
439
+ winston.error("Error getting WAB Account Info");
440
+ })
441
+
442
+ await db.set(CONTENT_KEY, settings);
443
+
444
+ readHTMLFile("/configure.html", (err, html) => {
445
+ var template = handlebars.compile(html);
446
+ var replacements = {
447
+ app_version: pjson.version,
448
+ project_id: project_id,
449
+ token: token,
450
+ proxy_url: proxy_url,
451
+ wab_token: settings.wab_token,
452
+ show_success_modal: true,
453
+ verify_token: settings.verify_token,
454
+ business_account_id: settings.business_account_id,
455
+ phone_number: settings.phone_number,
456
+ phone_number_id: settings.phone_number_id,
457
+ verified_name: settings.verified_name,
458
+ subscription_id: settings.subscriptionId,
459
+ department_id: settings.department_id,
460
+ departments: departments,
461
+ brand_name: BRAND_NAME
462
+ };
463
+ var html = template(replacements);
464
+ res.send(html);
465
+ });
466
+
467
+ })
468
+
390
469
  router.post("/disconnect", async (req, res) => {
391
470
  winston.verbose("(wab) /disconnect");
392
471
 
@@ -410,7 +489,7 @@ router.post("/disconnect", async (req, res) => {
410
489
  settings: { project_id: project_id, token: token },
411
490
  API_URL: API_URL,
412
491
  });
413
- let departments = await tdChannel.getDepartments(token);
492
+ let departments = await tdChannel.getDepartments();
414
493
 
415
494
  /*
416
495
  // callback
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tiledesk/tiledesk-whatsapp-connector",
3
- "version": "0.1.81",
3
+ "version": "0.1.82",
4
4
  "description": "Tiledesk Whatsapp connector",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/routes/api.js CHANGED
@@ -85,7 +85,6 @@ router.post('/customer/io', async (req, res) => {
85
85
  }
86
86
  })
87
87
 
88
-
89
88
  router.get("/:project_id", async (req, res) => {
90
89
  winston.verbose("(wab) /api/project_id");
91
90
 
@@ -137,7 +137,7 @@
137
137
  <div style="padding: 20px; background-color: #2fa2dc24; border-radius: 8px; width: 600px; margin-bottom: 5px;">
138
138
  <div style="display: flex; flex-direction: row; align-items: baseline;">
139
139
  <div>
140
- <img src="https://freeiconshop.com/wp-content/uploads/edd/arrow-flat.png" width="20px" height="auto" style="margin-right: 10px;">
140
+ <img src="https://cdn-icons-png.freepik.com/256/13077/13077927.png?semt=ais_hybrid" width="20px" height="auto" style="margin-right: 10px;">
141
141
  </div>
142
142
  <div>
143
143
  <a href="https://gethelp.tiledesk.com/articles/connect-tiledesk-with-whatsapp-business/" target="_blank">Connect Tiledesk with WhatsApp Business</a>
@@ -149,7 +149,7 @@
149
149
  <div style="padding: 20px; background-color: #2fa2dc24; border-radius: 8px; width: 600px; margin-bottom: 5px;">
150
150
  <div style="display: flex; flex-direction: row; align-items: baseline;">
151
151
  <div>
152
- <img src="https://freeiconshop.com/wp-content/uploads/edd/arrow-flat.png" width="20px" height="auto" style="margin-right: 10px;">
152
+ <img src="https://cdn-icons-png.freepik.com/256/13077/13077927.png?semt=ais_hybrid" width="20px" height="auto" style="margin-right: 10px;">
153
153
  </div>
154
154
  <div>
155
155
  <a href="https://gethelp.tiledesk.com/articles/configure-a-whatsapp-business-account/" target="_blank">Configure a WhatsApp Business account</a>
@@ -269,8 +269,57 @@
269
269
  <input type="submit" class="btn" style="width: 200px; background-color: #0ba2dc; color: white; font-weight: 500;" value="Connect"
270
270
  onclick="postMessage('connectWhatsapp')">
271
271
  </div>
272
- {{/if}}
272
+ {{/if}}
273
273
  </form>
274
+
275
+ <form action="./getinfo" method="post">
276
+ {{#if wab_token}}
277
+ {{#if phone_number_id}}
278
+ <div style="margin-top: 20px;">
279
+ <div style="display: flex;">
280
+ <p style="margin-right: 10px">Verified Name:</p>
281
+ <b>{{ verified_name }}</b>
282
+ </div>
283
+
284
+ <div style="display: flex;">
285
+ <p style="margin-right: 10px">Phone Number:</p>
286
+ <b>{{ phone_number }}</b>
287
+ </div>
288
+
289
+ <div style="display: flex;">
290
+ <p style="margin-right: 10px">Phone Number ID:</p>
291
+ <b>{{ phone_number_id }}</b>
292
+ </div>
293
+ </div>
294
+
295
+ <input type="hidden" name="project_id" value="{{ project_id }}" />
296
+ <input type="hidden" name="token" value="{{ token }}" />
297
+ <input type="submit" class="btn" style="width: 80px; background-color: #ffffff; color: #000000; font-weight: 400; border: solid 1px; border-radius: 14px;" value="Refresh">
298
+ {{else}}
299
+ <div style="margin-top: 20px">
300
+ <div style="display: flex;">
301
+ <p style="margin-right: 10px">Verified Name:</p>
302
+ <b>Unknown</b>
303
+ </div>
304
+
305
+ <div style="display: flex;">
306
+ <p style="margin-right: 10px">Phone Number:</p>
307
+ <b>Unknown</b>
308
+ </div>
309
+
310
+ <div style="display: flex;">
311
+ <p style="margin-right: 10px">Phone Number ID:</p>
312
+ <b>Unknown</b>
313
+ </div>
314
+ </div>
315
+
316
+ <input type="hidden" name="project_id" value="{{ project_id }}" />
317
+ <input type="hidden" name="token" value="{{ token }}" />
318
+ <input type="submit" class="btn" style="width: 80px; background-color: #ffffff; color: #000000; font-weight: 400; border: solid 1px; border-radius: 14px;" value="Refresh">
319
+ <p style="margin-top: 6px; margin-bottom: 0px; color: #a3a3a3; max-width: 430px; font-weight: 300;">Click on Refresh to retrieve your Whatsapp Business Account informations and automatically set your <b>Phone Number ID</b>.</p>
320
+ {{/if}}
321
+ {{/if}}
322
+ </form>
274
323
  </div>
275
324
 
276
325
 
@@ -0,0 +1,50 @@
1
+ body {
2
+ font-family: Arial, sans-serif;
3
+ background-color: #f8f9fa;
4
+ margin: 0;
5
+ padding: 0;
6
+ }
7
+
8
+ .container {
9
+ margin-top: 50px;
10
+ }
11
+
12
+ h2 {
13
+ text-align: center;
14
+ color: #343a40;
15
+ }
16
+
17
+ .btn-primary {
18
+ display: block;
19
+ width: 200px;
20
+ margin: 20px auto;
21
+ background-color: #007bff;
22
+ border-color: #007bff;
23
+ }
24
+
25
+ .btn-primary:hover {
26
+ background-color: #0056b3;
27
+ border-color: #004085;
28
+ }
29
+
30
+ .modal-content {
31
+ border-radius: 10px;
32
+ }
33
+
34
+ .modal-header {
35
+ background-color: #007bff;
36
+ color: white;
37
+ border-bottom: none;
38
+ }
39
+
40
+ .modal-footer {
41
+ border-top: none;
42
+ }
43
+
44
+ .close {
45
+ color: white;
46
+ }
47
+
48
+ .close:hover {
49
+ color: #d6d8db;
50
+ }
@@ -75,7 +75,6 @@ class TiledeskChannel {
75
75
  _id: 'wab-' + channel.from,
76
76
  first_name: channel.firstname,
77
77
  last_name: channel.lastname,
78
- email: 'na@whatsapp.com',
79
78
  sub: 'userexternal',
80
79
  aud: 'https://tiledesk.com/subscriptions/' + this.settings.subscriptionId
81
80
  }
@@ -158,7 +157,7 @@ class TiledeskChannel {
158
157
  winston.debug("(wab) [TiledeskChannel] get departments response.data: ", response.data)
159
158
  return response.data;
160
159
  }).catch((err) => {
161
- winston.error("(wab) [TiledeskChannel] get departments error");
160
+ winston.error("(wab) [TiledeskChannel] get departments error: ", err.response.data);
162
161
  })
163
162
  }
164
163
 
@@ -183,7 +182,6 @@ class TiledeskChannel {
183
182
  _id: 'wab-' + channel.from,
184
183
  first_name: channel.firstname,
185
184
  last_name: channel.lastname,
186
- email: 'na@whatsapp.com',
187
185
  sub: 'userexternal',
188
186
  aud: 'https://tiledesk.com/subscriptions/' + this.settings.subscriptionId
189
187
  }
@@ -71,6 +71,25 @@ class TiledeskWhatsapp {
71
71
  throw err;
72
72
  })
73
73
  }
74
+
75
+ async getBusinessAccountInfo(business_account_id) {
76
+
77
+ winston.debug("(wab) [TiledeskWhatsapp] Getting business account info...");
78
+
79
+ return await axios({
80
+ url: this.GRAPH_URL + business_account_id + "/phone_numbers?access_token=" + this.token,
81
+ headers: {
82
+ "Content-Type": "application/json"
83
+ },
84
+ method: "GET"
85
+ }).then((response) => {
86
+ winston.debug("(wab) [TiledeskWhatsapp] Business account info response ", response);
87
+ return response.data;
88
+ }).catch((err) => {
89
+ winston.error("(wab) [TiledeskWhatsapp] Gettings account info error ", err);
90
+ throw err;
91
+ })
92
+ }
74
93
 
75
94
  async downloadMedia(mediaId) {
76
95
 
@@ -157,7 +176,7 @@ class TiledeskWhatsapp {
157
176
 
158
177
  })
159
178
  }
160
-
179
+
161
180
 
162
181
  // HTTP REQUEST
163
182
  static async myrequest(options, callback, log) {