@tiledesk/tiledesk-whatsapp-connector 0.1.12 ā 0.1.14
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
|
-
|
|
346
|
+
let whatsappJsonMessage = await tlr.toWhatsapp(tiledeskChannelMessage, whatsapp_receiver);
|
|
347
347
|
console.log("whatsappJsonMessage: ", whatsappJsonMessage);
|
|
348
348
|
|
|
349
349
|
if (whatsappJsonMessage) {
|
package/package.json
CHANGED
|
@@ -39,6 +39,7 @@ describe('Test Translator\n', function() {
|
|
|
39
39
|
recipient: 'support-group-62c3f10152dc7400352bab0d-86a2293e-wab-104777398965560-393484506627',
|
|
40
40
|
metadata: {
|
|
41
41
|
src: 'https://fakeimageurl.com/',
|
|
42
|
+
type: 'image/png'
|
|
42
43
|
},
|
|
43
44
|
attributes: {
|
|
44
45
|
userFullname: 'John Doe'
|
|
@@ -69,7 +70,8 @@ describe('Test Translator\n', function() {
|
|
|
69
70
|
type: 'video',
|
|
70
71
|
recipient: 'support-group-62c3f10152dc7400352bab0d-86a2293e-wab-104777398965560-393484506627',
|
|
71
72
|
metadata: {
|
|
72
|
-
src: 'https://fakevideourl.com/'
|
|
73
|
+
src: 'https://fakevideourl.com/',
|
|
74
|
+
type: 'video/mp4'
|
|
73
75
|
},
|
|
74
76
|
attributes: {
|
|
75
77
|
userFullname: 'John Doe'
|
|
@@ -100,7 +102,9 @@ describe('Test Translator\n', function() {
|
|
|
100
102
|
type: 'application',
|
|
101
103
|
recipient: 'support-group-62c3f10152dc7400352bab0d-86a2293e-wab-104777398965560-393484506627',
|
|
102
104
|
metadata: {
|
|
103
|
-
src: 'https://fakedocumenturl.com/'
|
|
105
|
+
src: 'https://fakedocumenturl.com/',
|
|
106
|
+
name: "Document name",
|
|
107
|
+
type: 'application/pdf'
|
|
104
108
|
},
|
|
105
109
|
attributes: {
|
|
106
110
|
userFullname: 'John Doe'
|
|
@@ -117,7 +121,7 @@ describe('Test Translator\n', function() {
|
|
|
117
121
|
assert(!whatsappJsonMessage.text);
|
|
118
122
|
assert(whatsappJsonMessage.document);
|
|
119
123
|
assert(whatsappJsonMessage.document.link === tiledeskChannelMessage.metadata.src);
|
|
120
|
-
assert(whatsappJsonMessage.document.caption === tiledeskChannelMessage.
|
|
124
|
+
assert(whatsappJsonMessage.document.caption === tiledeskChannelMessage.metadata.name);
|
|
121
125
|
if (log) {
|
|
122
126
|
console.log("(test) whatsappJsonMessage: ", whatsappJsonMessage);
|
|
123
127
|
}
|
|
@@ -33,7 +33,7 @@ class TiledeskWhatsapp {
|
|
|
33
33
|
// this.media = config.channelMedia;
|
|
34
34
|
this.token = config.token;
|
|
35
35
|
this.GRAPH_URL = config.GRAPH_URL
|
|
36
|
-
|
|
36
|
+
|
|
37
37
|
this.log = false;
|
|
38
38
|
if (config.log) {
|
|
39
39
|
this.log = config.log;
|
|
@@ -42,11 +42,11 @@ class TiledeskWhatsapp {
|
|
|
42
42
|
|
|
43
43
|
async sendMessage(phone_number_id, message) {
|
|
44
44
|
if (this.log) {
|
|
45
|
-
console.log("[Tiledesk Whatsapp] Sending message...", message);
|
|
45
|
+
console.log("[Tiledesk Whatsapp] Sending message...", message);
|
|
46
46
|
} else {
|
|
47
|
-
console.log("[Tiledesk Whatsapp] Sending message...");
|
|
47
|
+
console.log("[Tiledesk Whatsapp] Sending message...");
|
|
48
48
|
}
|
|
49
|
-
|
|
49
|
+
|
|
50
50
|
return await axios({
|
|
51
51
|
url: this.GRAPH_URL + phone_number_id + "/messages?access_token=" + this.token,
|
|
52
52
|
headers: {
|
|
@@ -65,7 +65,7 @@ class TiledeskWhatsapp {
|
|
|
65
65
|
|
|
66
66
|
async downloadMedia(mediaId) {
|
|
67
67
|
if (this.log) {
|
|
68
|
-
console.log("[Tiledesk Whatsapp] Download media with id: ", mediaId);
|
|
68
|
+
console.log("[Tiledesk Whatsapp] Download media with id: ", mediaId);
|
|
69
69
|
}
|
|
70
70
|
|
|
71
71
|
return await axios({
|
|
@@ -75,7 +75,7 @@ class TiledeskWhatsapp {
|
|
|
75
75
|
'Authorization': "Bearer " + this.token
|
|
76
76
|
}
|
|
77
77
|
}).then(async (response) => {
|
|
78
|
-
console.log("response
|
|
78
|
+
console.log("response: ", response.data)
|
|
79
79
|
|
|
80
80
|
let download_url = response.data.url;
|
|
81
81
|
let mime_type = response.data.mime_type;
|
|
@@ -85,7 +85,7 @@ class TiledeskWhatsapp {
|
|
|
85
85
|
|
|
86
86
|
let example_path = path.join(__dirname, '..', 'tmp', type);
|
|
87
87
|
console.log("read file: ", example_path)
|
|
88
|
-
|
|
88
|
+
|
|
89
89
|
const writeStream = fs.createWriteStream(example_path);
|
|
90
90
|
console.log("[Tiledesk Whatsapp] Downloading file...");
|
|
91
91
|
|
|
@@ -98,7 +98,6 @@ class TiledeskWhatsapp {
|
|
|
98
98
|
responseType: 'stream'
|
|
99
99
|
}).then((response) => {
|
|
100
100
|
|
|
101
|
-
console.log("response 2: ", response.data)
|
|
102
101
|
return new Promise((resolve, reject) => {
|
|
103
102
|
response.data.pipe(writeStream);
|
|
104
103
|
let error = null;
|
|
@@ -116,7 +115,7 @@ class TiledeskWhatsapp {
|
|
|
116
115
|
})
|
|
117
116
|
})
|
|
118
117
|
}).catch((err) => {
|
|
119
|
-
console.log("axios err: ", err);
|
|
118
|
+
console.log("axios err: ", err);
|
|
120
119
|
})
|
|
121
120
|
}).catch((err) => {
|
|
122
121
|
console.log("axios err: ", err);
|
|
@@ -153,7 +152,7 @@ class TiledeskWhatsapp {
|
|
|
153
152
|
})
|
|
154
153
|
}
|
|
155
154
|
|
|
156
|
-
|
|
155
|
+
|
|
157
156
|
// HTTP REQUEST
|
|
158
157
|
static async myrequest(options, callback, log) {
|
|
159
158
|
if (this.log) {
|
|
@@ -178,7 +177,7 @@ class TiledeskWhatsapp {
|
|
|
178
177
|
}
|
|
179
178
|
else {
|
|
180
179
|
if (callback) {
|
|
181
|
-
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);
|
|
182
181
|
}
|
|
183
182
|
}
|
|
184
183
|
}).catch((err) => {
|
|
@@ -63,7 +63,7 @@ class TiledeskWhatsappTranslator {
|
|
|
63
63
|
|
|
64
64
|
if (tiledeskChannelMessage.metadata) {
|
|
65
65
|
|
|
66
|
-
if (tiledeskChannelMessage.type.startsWith('image')) {
|
|
66
|
+
if (tiledeskChannelMessage.metadata.type.startsWith('image')) {
|
|
67
67
|
//if (tiledeskChannelMessage.metadata.type.startsWith('image/')) {
|
|
68
68
|
var imgUrl = tiledeskChannelMessage.metadata.src;
|
|
69
69
|
whatsapp_message.type = 'image'
|
|
@@ -71,9 +71,9 @@ class TiledeskWhatsappTranslator {
|
|
|
71
71
|
link: imgUrl,
|
|
72
72
|
caption: text
|
|
73
73
|
}
|
|
74
|
-
}
|
|
74
|
+
}
|
|
75
75
|
|
|
76
|
-
if (tiledeskChannelMessage.type.startsWith('video')) {
|
|
76
|
+
else if (tiledeskChannelMessage.metadata.type.startsWith('video')) {
|
|
77
77
|
//if (tiledeskChannelMessage.metadata.type.startsWith('video/')) {
|
|
78
78
|
var videoUrl = tiledeskChannelMessage.metadata.src;
|
|
79
79
|
whatsapp_message.type = 'document'
|
|
@@ -90,16 +90,21 @@ class TiledeskWhatsappTranslator {
|
|
|
90
90
|
*/
|
|
91
91
|
}
|
|
92
92
|
|
|
93
|
-
if (tiledeskChannelMessage.type.startsWith('application')) {
|
|
93
|
+
else if (tiledeskChannelMessage.metadata.type.startsWith('application')) {
|
|
94
94
|
//if (tiledeskChannelMessage.metadata.type.startsWith('application/')) {
|
|
95
95
|
var doc = tiledeskChannelMessage.metadata.src;
|
|
96
96
|
whatsapp_message.type = 'document'
|
|
97
97
|
whatsapp_message.document = {
|
|
98
98
|
link: doc,
|
|
99
|
-
caption:
|
|
99
|
+
caption: tiledeskChannelMessage.metadata.name
|
|
100
100
|
}
|
|
101
101
|
}
|
|
102
102
|
|
|
103
|
+
else {
|
|
104
|
+
console.log("file type not supported")
|
|
105
|
+
return null
|
|
106
|
+
}
|
|
107
|
+
|
|
103
108
|
if (this.log) {
|
|
104
109
|
console.log("[Translator] whatsapp message: ", whatsapp_message)
|
|
105
110
|
}
|
|
@@ -122,7 +127,24 @@ class TiledeskWhatsappTranslator {
|
|
|
122
127
|
}
|
|
123
128
|
}
|
|
124
129
|
|
|
125
|
-
if (buttons_count
|
|
130
|
+
if (buttons_count == 0) {
|
|
131
|
+
|
|
132
|
+
for (let btn of buttons) {
|
|
133
|
+
if (btn.type == 'url') {
|
|
134
|
+
text = text + "\n\nš " + btn.value + " (" + btn.link + ")"
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
whatsapp_message.text = { body: text };
|
|
139
|
+
|
|
140
|
+
if (this.log) {
|
|
141
|
+
console.log("[Translator] whatsapp message: ", whatsapp_message)
|
|
142
|
+
}
|
|
143
|
+
return whatsapp_message;
|
|
144
|
+
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (buttons_count > 0 && buttons_count < 4) {
|
|
126
148
|
|
|
127
149
|
for (let btn of buttons) {
|
|
128
150
|
let title = (btn.value.length > 20) ? btn.value.substr(0, 18) + '..' : btn.value;
|
|
@@ -282,7 +304,15 @@ class TiledeskWhatsappTranslator {
|
|
|
282
304
|
|
|
283
305
|
} else {
|
|
284
306
|
// Skip message - non si deve fare qui!
|
|
285
|
-
|
|
307
|
+
whatsapp_message.text = { body: text };
|
|
308
|
+
|
|
309
|
+
if (this.log) {
|
|
310
|
+
console.log("[Translator] whatsapp message: ", whatsapp_message)
|
|
311
|
+
}
|
|
312
|
+
return whatsapp_message;
|
|
313
|
+
|
|
314
|
+
|
|
315
|
+
//return null
|
|
286
316
|
}
|
|
287
317
|
}
|
|
288
318
|
|
|
Binary file
|