@tiledesk/tiledesk-whatsapp-connector 0.1.11 → 0.1.13

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
@@ -343,7 +343,7 @@ router.post('/tiledesk', async (req, res) => {
343
343
 
344
344
  const tlr = new TiledeskWhatsappTranslator();
345
345
 
346
- const whatsappJsonMessage = tlr.toWhatsapp(tiledeskChannelMessage, whatsapp_receiver);
346
+ let whatsappJsonMessage = await tlr.toWhatsapp(tiledeskChannelMessage, whatsapp_receiver);
347
347
  console.log("whatsappJsonMessage: ", whatsappJsonMessage);
348
348
 
349
349
  if (whatsappJsonMessage) {
@@ -452,8 +452,9 @@ router.post("/webhook/:project_id", async (req, res) => {
452
452
 
453
453
  const filename = await util.downloadMedia(media.id);
454
454
  console.log("File position: ", filename);
455
+ let file_path = path.join(__dirname, 'tmp', filename);
455
456
 
456
- const image_url = await util.uploadMedia(filename, "images");
457
+ const image_url = await util.uploadMedia(file_path, "images");
457
458
  console.log("image_url: ", image_url)
458
459
 
459
460
  tiledeskJsonMessage = tlr.toTiledesk(whatsappChannelMessage, firstname, image_url);
@@ -465,8 +466,10 @@ router.post("/webhook/:project_id", async (req, res) => {
465
466
 
466
467
  const filename = await util.downloadMedia(media.id);
467
468
  console.log("File position: ", filename);
469
+ let file_path = path.join(__dirname, 'tmp', filename);
468
470
 
469
- const media_url = await util.uploadMedia(filename, "files");
471
+
472
+ const media_url = await util.uploadMedia(file_path, "files");
470
473
  console.log("image_url: ", media_url)
471
474
 
472
475
  tiledeskJsonMessage = tlr.toTiledesk(whatsappChannelMessage, firstname, media_url);
@@ -477,8 +480,9 @@ router.post("/webhook/:project_id", async (req, res) => {
477
480
 
478
481
  const filename = await util.downloadMedia(media.id);
479
482
  console.log("File position: ", filename);
483
+ let file_path = path.join(__dirname, 'tmp', filename);
480
484
 
481
- const media_url = await util.uploadMedia(filename, "files");
485
+ const media_url = await util.uploadMedia(file_path, "files");
482
486
  console.log("image_url: ", media_url)
483
487
 
484
488
  tiledeskJsonMessage = tlr.toTiledesk(whatsappChannelMessage, firstname, media_url);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tiledesk/tiledesk-whatsapp-connector",
3
- "version": "0.1.11",
3
+ "version": "0.1.13",
4
4
  "description": "Tiledesk Whatsapp connector",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -1,6 +1,7 @@
1
1
  const axios = require("axios").default;
2
2
  const fs = require('fs');
3
3
  const FormData = require('form-data');
4
+ const path = require('path');
4
5
 
5
6
  class TiledeskWhatsapp {
6
7
 
@@ -32,7 +33,7 @@ class TiledeskWhatsapp {
32
33
  // this.media = config.channelMedia;
33
34
  this.token = config.token;
34
35
  this.GRAPH_URL = config.GRAPH_URL
35
-
36
+
36
37
  this.log = false;
37
38
  if (config.log) {
38
39
  this.log = config.log;
@@ -41,11 +42,11 @@ class TiledeskWhatsapp {
41
42
 
42
43
  async sendMessage(phone_number_id, message) {
43
44
  if (this.log) {
44
- console.log("[Tiledesk Whatsapp] Sending message...", message);
45
+ console.log("[Tiledesk Whatsapp] Sending message...", message);
45
46
  } else {
46
- console.log("[Tiledesk Whatsapp] Sending message...");
47
+ console.log("[Tiledesk Whatsapp] Sending message...");
47
48
  }
48
-
49
+
49
50
  return await axios({
50
51
  url: this.GRAPH_URL + phone_number_id + "/messages?access_token=" + this.token,
51
52
  headers: {
@@ -64,7 +65,7 @@ class TiledeskWhatsapp {
64
65
 
65
66
  async downloadMedia(mediaId) {
66
67
  if (this.log) {
67
- console.log("[Tiledesk Whatsapp] Download media with id: ", mediaId);
68
+ console.log("[Tiledesk Whatsapp] Download media with id: ", mediaId);
68
69
  }
69
70
 
70
71
  return await axios({
@@ -74,7 +75,7 @@ class TiledeskWhatsapp {
74
75
  'Authorization': "Bearer " + this.token
75
76
  }
76
77
  }).then(async (response) => {
77
- console.log("response: ", response.data);
78
+ console.log("response: ", response.data)
78
79
 
79
80
  let download_url = response.data.url;
80
81
  let mime_type = response.data.mime_type;
@@ -82,7 +83,10 @@ class TiledeskWhatsapp {
82
83
  let tid = this.getId();
83
84
  let type = "media-" + tid + "." + extension;
84
85
 
85
- const writeStream = fs.createWriteStream('tmp/' + type);
86
+ let example_path = path.join(__dirname, '..', 'tmp', type);
87
+ console.log("read file: ", example_path)
88
+
89
+ const writeStream = fs.createWriteStream(example_path);
86
90
  console.log("[Tiledesk Whatsapp] Downloading file...");
87
91
 
88
92
  return await axios({
@@ -106,12 +110,12 @@ class TiledeskWhatsapp {
106
110
  writeStream.on('close', () => {
107
111
  if (!error) {
108
112
  console.log("[Tiledesk Whatsapp] Download completed")
109
- resolve('tmp/' + type);
113
+ resolve(type);
110
114
  }
111
115
  })
112
116
  })
113
117
  }).catch((err) => {
114
- console.log("axios err: ", err);
118
+ console.log("axios err: ", err);
115
119
  })
116
120
  }).catch((err) => {
117
121
  console.log("axios err: ", err);
@@ -148,7 +152,7 @@ class TiledeskWhatsapp {
148
152
  })
149
153
  }
150
154
 
151
-
155
+
152
156
  // HTTP REQUEST
153
157
  static async myrequest(options, callback, log) {
154
158
  if (this.log) {
@@ -173,7 +177,7 @@ class TiledeskWhatsapp {
173
177
  }
174
178
  else {
175
179
  if (callback) {
176
- callback(TiledeskClient.getErr({message: "Response status not 200"}, options, res), null, null);
180
+ callback(TiledeskClient.getErr({ message: "Response status not 200" }, options, res), null, null);
177
181
  }
178
182
  }
179
183
  }).catch((err) => {
@@ -122,7 +122,24 @@ class TiledeskWhatsappTranslator {
122
122
  }
123
123
  }
124
124
 
125
- if (buttons_count < 4) {
125
+ if (buttons_count == 0) {
126
+
127
+ for (let btn of buttons) {
128
+ if (btn.type == 'url') {
129
+ text = text + "\n\nšŸ‘‰ " + btn.value + " (" + btn.link + ")"
130
+ }
131
+ }
132
+
133
+ whatsapp_message.text = { body: text };
134
+
135
+ if (this.log) {
136
+ console.log("[Translator] whatsapp message: ", whatsapp_message)
137
+ }
138
+ return whatsapp_message;
139
+
140
+ }
141
+
142
+ if (buttons_count > 0 && buttons_count < 4) {
126
143
 
127
144
  for (let btn of buttons) {
128
145
  let title = (btn.value.length > 20) ? btn.value.substr(0, 18) + '..' : btn.value;
@@ -0,0 +1 @@
1
+ Hello world!
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file