@tiledesk/tiledesk-whatsapp-connector 0.1.42 → 0.1.43

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
@@ -10,6 +10,7 @@ const pjson = require('./package.json');
10
10
  const { v4: uuidv4 } = require('uuid');
11
11
  const cors = require('cors');
12
12
  var winston = require('./winston');
13
+ const url = require('url');
13
14
 
14
15
  // tiledesk clients
15
16
  //const { TiledeskClient } = require('@tiledesk/tiledesk-client');
@@ -65,12 +66,16 @@ router.get('/detail', async (req, res) => {
65
66
 
66
67
  winston.verbose("(wab) /detail")
67
68
 
68
- let projectId = req.query.project_id;
69
+ let project_id = req.query.project_id;
69
70
  let token = req.query.token;
70
71
  let app_id = req.query.app_id;
71
72
 
73
+ const tdChannel = new TiledeskChannel({ settings: { project_id: project_id, token: token }, API_URL: API_URL })
74
+ let isAvailable = await tdChannel.getProjectDetail();
75
+ winston.debug("(wab) app is available: ", isAvailable);
76
+
72
77
  const appClient = new TiledeskAppsClient({ APPS_API_URL: APPS_API_URL });
73
- let installation = await appClient.getInstallations(projectId, app_id);
78
+ let installation = await appClient.getInstallations(project_id, app_id);
74
79
 
75
80
  let installed = false;
76
81
  if (installation) {
@@ -81,10 +86,11 @@ router.get('/detail', async (req, res) => {
81
86
  var template = handlebars.compile(html);
82
87
  var replacements = {
83
88
  app_version: pjson.version,
84
- project_id: projectId,
89
+ project_id: project_id,
85
90
  token: token,
86
91
  app_id: app_id,
87
- installed: installed
92
+ installed: installed,
93
+ isAvailable: isAvailable
88
94
  }
89
95
  var html = template(replacements);
90
96
  res.send(html);
@@ -110,21 +116,15 @@ router.post('/install', async (req, res) => {
110
116
  appClient.install(installation_info).then((installation) => {
111
117
  winston.debug("(wab) installation response: " + installation);
112
118
 
113
- let installed = true;
114
-
115
- readHTMLFile('/detail.html', (err, html) => {
116
- var template = handlebars.compile(html);
117
- var replacements = {
118
- app_version: pjson.version,
119
- project_id: project_id,
120
- token: token,
121
- app_id: app_id,
122
- installed: installed
119
+ res.redirect(url.format({
120
+ pathname:"/detail",
121
+ query: {
122
+ "project_id": project_id,
123
+ "app_id": app_id,
124
+ "token": token
123
125
  }
124
- var html = template(replacements);
125
- res.send(html);
126
- })
127
-
126
+ }));
127
+
128
128
  }).catch((err) => {
129
129
  winston.error("(wab) installation error: " + err.data)
130
130
  res.send("An error occurred during the installation");
@@ -144,20 +144,14 @@ router.post('/uninstall', async (req, res) => {
144
144
 
145
145
  winston.debug("(wab) uninstallation response: " + response);
146
146
 
147
- let installed = false;
148
-
149
- readHTMLFile('/detail.html', (err, html) => {
150
- var template = handlebars.compile(html);
151
- var replacements = {
152
- app_version: pjson.version,
153
- project_id: project_id,
154
- token: token,
155
- app_id: app_id,
156
- installed: installed
147
+ res.redirect(url.format({
148
+ pathname:"/detail",
149
+ query: {
150
+ "project_id": project_id,
151
+ "app_id": app_id,
152
+ "token": token
157
153
  }
158
- var html = template(replacements);
159
- res.send(html);
160
- })
154
+ }));
161
155
 
162
156
  }).catch((err) => {
163
157
  winston.error("(wab) uninsallation error: " + err.data)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tiledesk/tiledesk-whatsapp-connector",
3
- "version": "0.1.42",
3
+ "version": "0.1.43",
4
4
  "description": "Tiledesk Whatsapp connector",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -211,3 +211,25 @@ div.scrollmenu a:hover {
211
211
  .btn:focus {
212
212
  outline: none !important;
213
213
  }
214
+
215
+ .plans-div {
216
+ display: flex;
217
+ flex-direction: row;
218
+ }
219
+
220
+ .plan {
221
+ margin-right: 6px;
222
+ padding: 2px 6px;
223
+ border-radius: 4px;
224
+ font-size: 11px;
225
+ }
226
+
227
+ .scale-plan {
228
+ background-color: #eb459e;
229
+ color: white;
230
+ }
231
+
232
+ .plus-plan {
233
+ background-color: #5865f2;
234
+ color: white;
235
+ }
@@ -42,7 +42,11 @@
42
42
  <hr style="margin-top: 10px; margin-bottom: 10px">
43
43
 
44
44
  <div class="installation-info-box">
45
+ {{#if isAvailable}}
45
46
  <p style="margin: 0px; color: #676767; font-size: 13px;">You can install this application for free.</p>
47
+ {{else}}
48
+ <p style="margin: 0px; color: #676767; font-size: 13px;">This app is available for Scale and Plus plans</p>
49
+ {{/if}}
46
50
  </div>
47
51
 
48
52
 
@@ -56,19 +60,32 @@
56
60
  <p style="font-size: 26px; font-weight: 500; margin-bottom: 0px;">WhatsApp Business</p>
57
61
  <p style="color: #676767; font-size: 16px; margin-bottom: 5px;">Connect your WhatsApp number with Tiledesk</p>
58
62
  <a style="font-size: 18px;" href="https://tiledesk.com/" target="_blank">Tiledesk</a>
59
- <p style="font-size: 15px; color: #676767; font-style: italic; margin-bottom: 0px; margin-top: 20px;">Free</p>
63
+ {{#if isAvailable}}
64
+ <p style="font-size: 15px; color: #676767; font-style: italic; margin-bottom: 0px; margin-top: 20px;">Free</p>
65
+ {{else}}
66
+ <p style="font-size: 15px; color: #676767; font-style: italic; margin-bottom: 0px; margin-top: 20px;">Free for</p>
67
+ <div class="plans-div">
68
+ <div class="plan scale-plan">Scale Plan</div>
69
+ <div class="plan plus-plan">Plus Plan</div>
70
+ </div>
71
+ {{/if}}
60
72
  </div>
61
73
 
62
74
  <div style="display: flex; flex-direction: column; justify-content: flex-end; align-items: flex-end;">
63
75
 
64
76
  {{#if installed}}
77
+
65
78
  <!-- Open button -->
79
+ {{#if isAvailable}}
66
80
  <form action="./configure" method="get">
67
81
  <input type="hidden" name="project_id" value="{{ project_id }}" />
68
82
  <input type="hidden" name="app_id" value="{{ app_id }}" />
69
83
  <input type="hidden" name="token" value="{{ token }}" />
70
84
  <input type="submit" class="btn install-button" value="Open">
71
85
  </form>
86
+ {{else}}
87
+ <button class="btn install-button" disabled>Open</button>
88
+ {{/if}}
72
89
 
73
90
  <!-- Uninstall button -->
74
91
  <button class="btn uninstall-button" onclick="document.getElementById('uninstall-modal').style.display='flex'">Uninstall</button>
@@ -97,7 +114,13 @@
97
114
  </div>
98
115
 
99
116
  {{else}}
100
- <button class="btn install-button" onclick="document.getElementById('install-modal').style.display='flex'">Install</button>
117
+
118
+ {{#if isAvailable}}
119
+ <button class="btn install-button" onclick="document.getElementById('install-modal').style.display='flex'">Install</button>
120
+ {{else}}
121
+ <button class="btn install-button" disabled>Install</button>
122
+ {{/if}}
123
+
101
124
  <!-- Install modal -->
102
125
  <div id="install-modal" class="modal">
103
126
  <div class="modal-content">
@@ -2,6 +2,7 @@ const axios = require("axios").default;
2
2
  const fs = require('fs');
3
3
  const FormData = require('form-data');
4
4
  const path = require('path');
5
+ const winston = require('../winston');
5
6
 
6
7
  class TiledeskAppsClient {
7
8
  /**
@@ -217,7 +217,58 @@ class TiledeskChannel {
217
217
  winston.error("(wab) [TiledeskChannel] sign in error: " + err);
218
218
  })
219
219
  }
220
-
220
+
221
+ async getProjectDetail() {
222
+
223
+ return await axios({
224
+ url: this.API_URL + '/projects/' + this.settings.project_id,
225
+ headers: {
226
+ 'Content-Type': 'application/json',
227
+ 'Authorization': this.settings.token
228
+ },
229
+ method: 'GET'
230
+ }).then((response) => {
231
+ return this.checkPlan(response.data);
232
+ }).catch((err) => {
233
+ winston.error("(wab) [TiledeskChannel] get project detail error: " + err);
234
+ return null;
235
+ })
236
+ }
237
+
238
+ checkPlan(project) {
239
+ let profile_name = project.profile.name;
240
+ let profile_type = project.profile.type;
241
+ let isActiveSubscription = project.isActiveSubscription;
242
+ let trialExpired = project.trialExpired;
243
+
244
+ winston.debug("profile_name: " + profile_name)
245
+ winston.debug("profile_type: " + profile_type)
246
+ winston.debug("isActiveSubscription: " + isActiveSubscription)
247
+
248
+ return new Promise((resolve, reject) => {
249
+ if (
250
+ (profile_name === 'Growth') ||
251
+ (profile_name === 'Scale' && isActiveSubscription === false) ||
252
+ (profile_name === 'Plus' && isActiveSubscription === false) ||
253
+ (profile_type === 'free' && trialExpired === true)
254
+ ) {
255
+ winston.verbose('Feature not available')
256
+ resolve(false);
257
+
258
+ } else if (
259
+ (profile_name === 'Scale' && isActiveSubscription === true) ||
260
+ (profile_name === 'Plus' && isActiveSubscription === true) ||
261
+ (profile_type === 'free' && trialExpired === false)
262
+ ) {
263
+ winston.verbose('Feature available')
264
+ resolve(true);
265
+
266
+ } else {
267
+ winston.verbose('Other case: feature not available');
268
+ resolve(false);
269
+ }
270
+ })
271
+ }
221
272
 
222
273
  }
223
274
 
@@ -271,8 +271,26 @@ const path = require('path');
271
271
  }
272
272
 
273
273
  } else {
274
- whatsapp_message.text = { body: text };
275
- return whatsapp_message;
274
+
275
+ // template
276
+ console.log("tiledeskChannelMessage.text: ", tiledeskChannelMessage.text);
277
+ if (tiledeskChannelMessage.text.startsWith("/template:")) {
278
+ let template_name = tiledeskChannelMessage.text.substring(tiledeskChannelMessage.text.lastIndexOf(':') + 1);
279
+ console.log("template name to send: ", template_name);
280
+ whatsapp_message.type = "template";
281
+ whatsapp_message.template = {
282
+ name: template_name,
283
+ language: {
284
+ code: "en_US"
285
+ }
286
+ }
287
+ return whatsapp_message;
288
+ }
289
+ // standard message
290
+ else {
291
+ whatsapp_message.text = { body: text };
292
+ return whatsapp_message;
293
+ }
276
294
  }
277
295
  }
278
296