@resultcrafter/aimanager-instagram-connector 0.1.0
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/LICENSE +26 -0
- package/README.md +50 -0
- package/aimanager/FacebookClient.js +223 -0
- package/aimanager/KVBaseMongo.js +99 -0
- package/aimanager/MessageHandler.js +138 -0
- package/aimanager/TiledeskAppsClient.js +158 -0
- package/aimanager/TiledeskChannel.js +257 -0
- package/aimanager/TiledeskInstagramTranslator.js +452 -0
- package/aimanager/TiledeskSubscriptionClient.js +131 -0
- package/docs/adding-new-channel-guide.md +848 -0
- package/docs/connector-research-design.md +64 -0
- package/docs/connector-research-proposal.md +23 -0
- package/docs/design.md +53 -0
- package/docs/proposal/design.md +44 -0
- package/docs/proposal/proposal.md +38 -0
- package/docs/proposal/tasks.md +38 -0
- package/docs/proposal.md +37 -0
- package/docs/specs/instagram-connector/spec.md +44 -0
- package/docs/tasks.md +46 -0
- package/index.js +963 -0
- package/package.json +32 -0
- package/publish.sh +36 -0
- package/template/configure.html +378 -0
- package/template/css/configure.css +500 -0
- package/template/css/detail.css +236 -0
- package/template/detail.html +296 -0
- package/template/error.html +64 -0
- package/test/test_translate_instagram.js +486 -0
- package/winston.js +41 -0
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
const axios = require("axios").default;
|
|
2
|
+
const jwt = require('jsonwebtoken');
|
|
3
|
+
const { v4: uuidv4 } = require('uuid');
|
|
4
|
+
const winston = require('../winston');
|
|
5
|
+
|
|
6
|
+
class TiledeskChannel {
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Constructor for TiledeskChannel
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* const { TiledeskChannel } = require('tiledesk-channel');
|
|
13
|
+
* const tdChannel = new TiledeskChannel({tiledeskJsonMessage: replyFromWhatsapp, settings: appSettings, whatsappJsonMessage: originalWhatsappMessage, API_URL: tiledeskApiUrl });
|
|
14
|
+
*
|
|
15
|
+
* @param {Object} config JSON configuration.
|
|
16
|
+
* @param {string} config.tiledeskJsonMessage Mandatory. Message translated from Whatsapp to Tiledesk
|
|
17
|
+
* @param {string} config.whatsappJsonMessage Mandatory. Original whatsapp message.
|
|
18
|
+
* @param {string} config.settings Mandatory. Installation settings.
|
|
19
|
+
* @param {string} config.API_URL Mandatory. Tiledesk api url.
|
|
20
|
+
* @param {boolean} options.log Optional. If true HTTP requests are logged.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
constructor(config) {
|
|
24
|
+
if (!config) {
|
|
25
|
+
throw new Error('config is mandatory');
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (!config.settings) {
|
|
29
|
+
throw new Error('config.settings is mandatory');
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (!config.API_URL) {
|
|
33
|
+
throw new Error('config.API_URL is mandatory');
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
this.settings = config.settings;
|
|
37
|
+
this.API_URL = config.API_URL;
|
|
38
|
+
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async send(tiledeskMessage, messageInfo, department_id) {
|
|
42
|
+
|
|
43
|
+
let channel;
|
|
44
|
+
let new_request_id;
|
|
45
|
+
|
|
46
|
+
if (department_id) {
|
|
47
|
+
tiledeskMessage.departmentid = department_id;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (messageInfo.channel == "whatsapp") {
|
|
51
|
+
channel = messageInfo.whatsapp;
|
|
52
|
+
new_request_id = "support-group-" + this.settings.project_id + "-" + uuidv4().substring(0, 8) + "-wab-" + channel.phone_number_id + "-" + channel.from;
|
|
53
|
+
|
|
54
|
+
} else if (messageInfo.channel == "telegram") {
|
|
55
|
+
channel = messageInfo.telegram;
|
|
56
|
+
// Check it
|
|
57
|
+
//new_request_id = "support-group-" + projectId + "-" + uuidv4() + "-telegram-" + from;
|
|
58
|
+
|
|
59
|
+
} else if (messageInfo.channel == "instagram") {
|
|
60
|
+
channel = messageInfo.instagram;
|
|
61
|
+
new_request_id = "support-group-" + this.settings.project_id + "-" + uuidv4().substring(0, 8) + "-fbm-" + channel.page_id + "-" + channel.sender_id;
|
|
62
|
+
|
|
63
|
+
} else {
|
|
64
|
+
winston.verbose("(fbm) [TiledeskChannel] Channel not supported");
|
|
65
|
+
throw new Error(`(fbm) Channel not supported: ${messageInfo.channel}`);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
var payload = {
|
|
69
|
+
_id: 'fbm-' + channel.sender_id,
|
|
70
|
+
first_name: channel.firstname,
|
|
71
|
+
last_name: channel.lastname,
|
|
72
|
+
email: 'na@instagram.com',
|
|
73
|
+
sub: 'userexternal',
|
|
74
|
+
aud: 'https://tiledesk.com/subscriptions/' + this.settings.subscription_id
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
var customToken = jwt.sign(payload, this.settings.secret);
|
|
78
|
+
|
|
79
|
+
try {
|
|
80
|
+
const signinRes = await axios.post(`${this.API_URL}/auth/signinWithCustomToken`, {}, {
|
|
81
|
+
headers: {
|
|
82
|
+
'Content-Type': 'application/json',
|
|
83
|
+
'Authorization': "JWT " + customToken
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
let token = this.fixToken(signinRes.data.token);
|
|
88
|
+
|
|
89
|
+
const requestsRes = await axios.get(`${this.API_URL}/${this.settings.project_id}/requests/me`, {
|
|
90
|
+
headers: {
|
|
91
|
+
'Content-Type': 'application/json',
|
|
92
|
+
'Authorization': token
|
|
93
|
+
},
|
|
94
|
+
params: { channel: messageInfo.channel }
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
let request_id = requestsRes.data.requests?.[0]?.request_id || new_request_id;
|
|
98
|
+
winston.debug("(fbm) [TiledeskChannel] request id: " + request_id);
|
|
99
|
+
|
|
100
|
+
const sendRes = await axios.post(`${this.API_URL}/${this.settings.project_id}/requests/${request_id}/messages`, tiledeskMessage, {
|
|
101
|
+
headers: {
|
|
102
|
+
'Content-Type': 'application/json',
|
|
103
|
+
'Authorization': token
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
winston.debug("(fbm) [TiledeskChannel] send message response: ", sendRes.data);
|
|
108
|
+
return sendRes.data;
|
|
109
|
+
|
|
110
|
+
} catch (err) {
|
|
111
|
+
winston.error("(fbm) Send message error: ", err?.response?.data || err.message || err);
|
|
112
|
+
throw err;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
async getDepartments() {
|
|
118
|
+
|
|
119
|
+
return await axios({
|
|
120
|
+
url: this.API_URL + "/" + this.settings.project_id + "/departments/allstatus",
|
|
121
|
+
headers: {
|
|
122
|
+
'Content-Type': 'application/json',
|
|
123
|
+
'Authorization': this.settings.token
|
|
124
|
+
},
|
|
125
|
+
method: 'GET'
|
|
126
|
+
}).then((response) => {
|
|
127
|
+
winston.debug("(fbm) [TiledeskChannel] get departments: ", response.data)
|
|
128
|
+
return response.data;
|
|
129
|
+
}).catch((err) => {
|
|
130
|
+
winston.error("(fbm) [TiledeskChannel] get departments error: ", err);
|
|
131
|
+
})
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
async sendAndAddBot(tiledeskMessage, messageInfo, bot_id) {
|
|
135
|
+
|
|
136
|
+
let channel;
|
|
137
|
+
let new_request_id;
|
|
138
|
+
tiledeskMessage.participants = ["bot_" + bot_id]
|
|
139
|
+
|
|
140
|
+
if (messageInfo.channel == "whatsapp") {
|
|
141
|
+
channel = messageInfo.whatsapp;
|
|
142
|
+
new_request_id = "support-group-" + this.settings.project_id + "-" + uuidv4().substring(0, 8) + "-wab-" + channel.phone_number_id + "-" + channel.from;
|
|
143
|
+
} else {
|
|
144
|
+
winston.verbose("(fbm) [TiledeskChannel] Channel not supported")
|
|
145
|
+
return null;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
var payload = {
|
|
149
|
+
_id: 'wab-' + channel.from,
|
|
150
|
+
first_name: channel.firstname,
|
|
151
|
+
last_name: channel.lastname,
|
|
152
|
+
email: 'na@whatsapp.com',
|
|
153
|
+
sub: 'userexternal',
|
|
154
|
+
aud: 'https://tiledesk.com/subscriptions/' + this.settings.subscriptionId
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
var customToken = jwt.sign(payload, this.settings.secret);
|
|
158
|
+
|
|
159
|
+
return await axios({
|
|
160
|
+
url: this.API_URL + "/auth/signinWithCustomToken",
|
|
161
|
+
headers: {
|
|
162
|
+
'Content-Type': 'application/json',
|
|
163
|
+
'Authorization': 'JWT ' + customToken
|
|
164
|
+
},
|
|
165
|
+
data: {},
|
|
166
|
+
method: 'POST'
|
|
167
|
+
}).then((response) => {
|
|
168
|
+
|
|
169
|
+
let token = response.data.token;
|
|
170
|
+
token = this.fixToken(token);
|
|
171
|
+
|
|
172
|
+
return axios({
|
|
173
|
+
url: this.API_URL + `/${this.settings.project_id}/requests/${new_request_id}/messages`,
|
|
174
|
+
headers: {
|
|
175
|
+
'Content-Type': 'application/json',
|
|
176
|
+
'Authorization': token
|
|
177
|
+
},
|
|
178
|
+
data: tiledeskMessage,
|
|
179
|
+
method: 'POST'
|
|
180
|
+
}).then((response) => {
|
|
181
|
+
return response.data
|
|
182
|
+
}).catch((err) => {
|
|
183
|
+
winston.error("(fbm) [TiledeskChannel] send message error (open conversation): ", err);
|
|
184
|
+
})
|
|
185
|
+
}).catch((err) => {
|
|
186
|
+
winston.error("(fbm) [TiledeskChannel] sign in error: ", err);
|
|
187
|
+
})
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
async getProjectDetail() {
|
|
191
|
+
|
|
192
|
+
return await axios({
|
|
193
|
+
url: this.API_URL + '/projects/' + this.settings.project_id,
|
|
194
|
+
headers: {
|
|
195
|
+
'Content-Type': 'application/json',
|
|
196
|
+
'Authorization': this.settings.token
|
|
197
|
+
},
|
|
198
|
+
method: 'GET'
|
|
199
|
+
}).then((response) => {
|
|
200
|
+
return this.checkPlan(response.data);
|
|
201
|
+
}).catch((err) => {
|
|
202
|
+
winston.error("(wab) [TiledeskChannel] get project detail error: ", err);
|
|
203
|
+
return null;
|
|
204
|
+
})
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
checkPlan(project) {
|
|
208
|
+
let profile_name = project.profile.name;
|
|
209
|
+
let profile_type = project.profile.type;
|
|
210
|
+
let isActiveSubscription = project.isActiveSubscription;
|
|
211
|
+
let trialExpired = project.trialExpired;
|
|
212
|
+
|
|
213
|
+
winston.debug("profile_name: " + profile_name)
|
|
214
|
+
winston.debug("profile_type: " + profile_type)
|
|
215
|
+
winston.debug("isActiveSubscription: " + isActiveSubscription)
|
|
216
|
+
|
|
217
|
+
return new Promise((resolve, reject) => {
|
|
218
|
+
if (
|
|
219
|
+
((profile_name === 'Growth' || profile_name === 'Basic')) ||
|
|
220
|
+
((profile_name === 'Scale' || profile_name === 'Premium') && isActiveSubscription === false) ||
|
|
221
|
+
((profile_name === 'Plus' || profile_name === 'Custom') && isActiveSubscription === false) ||
|
|
222
|
+
(profile_type === 'free' && trialExpired === true)
|
|
223
|
+
) {
|
|
224
|
+
winston.verbose('Feature not available')
|
|
225
|
+
resolve(false);
|
|
226
|
+
|
|
227
|
+
} else if (
|
|
228
|
+
((profile_name === 'Scale' || profile_name === 'Premium') && isActiveSubscription === true) ||
|
|
229
|
+
((profile_name === 'Plus' || profile_name === 'Custom') && isActiveSubscription === true) ||
|
|
230
|
+
(profile_type === 'free' && trialExpired === false)
|
|
231
|
+
) {
|
|
232
|
+
winston.verbose('Feature available')
|
|
233
|
+
resolve(true);
|
|
234
|
+
|
|
235
|
+
} else {
|
|
236
|
+
winston.verbose('Other case: feature not available');
|
|
237
|
+
resolve(false);
|
|
238
|
+
}
|
|
239
|
+
})
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
fixToken(token) {
|
|
243
|
+
|
|
244
|
+
let index = token.lastIndexOf("JWT ");
|
|
245
|
+
if (index != -1) {
|
|
246
|
+
let new_token = token.substring(index + 4);
|
|
247
|
+
return 'JWT ' + new_token;
|
|
248
|
+
} else {
|
|
249
|
+
return 'JWT ' + token;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
module.exports = { TiledeskChannel }
|
|
@@ -0,0 +1,452 @@
|
|
|
1
|
+
const { v4: uuidv4 } = require('uuid');
|
|
2
|
+
const { TiledeskChannel } = require('./TiledeskChannel');
|
|
3
|
+
const winston = require('../winston');
|
|
4
|
+
|
|
5
|
+
class TiledeskInstagramTranslator {
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Constructor for TiledeskWhatsappTranslator
|
|
9
|
+
*const axios = require("axios").default;
|
|
10
|
+
const fs = require('fs');
|
|
11
|
+
const FormData = require('form-data');
|
|
12
|
+
const path = require('path');
|
|
13
|
+
* @example
|
|
14
|
+
* const { TiledeskWhatsappTranslator } = require('tiledesk-whatsapp-translator');
|
|
15
|
+
* const tlr = new TiledeskWhatsappTranslator();
|
|
16
|
+
*
|
|
17
|
+
* @param {Object} config JSON configuration.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
static INSTAGRAM_MESSAGING_PRODUCT = "instagram";
|
|
21
|
+
static CHANNEL_NAME = "instagram";
|
|
22
|
+
|
|
23
|
+
constructor() {
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
/*
|
|
27
|
+
if (!config.tiledeskChannelMessage) {
|
|
28
|
+
throw new Error('config.tiledeskChannelMessage is mandatory');
|
|
29
|
+
}
|
|
30
|
+
this.tiledeskChannelMessage = config.tiledeskChannelMessage;
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
/*
|
|
37
|
+
*************** START ***************
|
|
38
|
+
******** FACEBOOK INSTAGRAM *********
|
|
39
|
+
*************************************
|
|
40
|
+
*/
|
|
41
|
+
toInstagram(tiledeskChannelMessage, instagram_receiver) {
|
|
42
|
+
|
|
43
|
+
winston.debug("(fbm) [Translator] tiledeskChannelMessage: ", tiledeskChannelMessage)
|
|
44
|
+
|
|
45
|
+
let text = '';
|
|
46
|
+
if (tiledeskChannelMessage.text) {
|
|
47
|
+
text = tiledeskChannelMessage.text;
|
|
48
|
+
//text = tiledeskChannelMessage.text.replace(/-{1,}/g, '');
|
|
49
|
+
//text = text.replace(/\*{2,}/g, '*')
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
let instagram_message = {
|
|
53
|
+
//messaging_product: TiledeskInstagramTranslator.INSTAGRAM_MESSAGING_PRODUCT,
|
|
54
|
+
recipient: { id: instagram_receiver }
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (tiledeskChannelMessage.type === 'frame') {
|
|
58
|
+
text = text + "\n\n👉 " + tiledeskChannelMessage.metadata.src
|
|
59
|
+
instagram_message.message = {
|
|
60
|
+
text: text
|
|
61
|
+
};
|
|
62
|
+
return instagram_message
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
else if (tiledeskChannelMessage.metadata) {
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
if ((tiledeskChannelMessage.metadata.type && tiledeskChannelMessage.metadata.type.startsWith('image')) || tiledeskChannelMessage.type.startsWith('image')) {
|
|
69
|
+
|
|
70
|
+
instagram_message.message = {
|
|
71
|
+
attachment: {
|
|
72
|
+
type: "image",
|
|
73
|
+
payload: {
|
|
74
|
+
url: tiledeskChannelMessage.metadata.src,
|
|
75
|
+
is_reusable: true
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
else if ((tiledeskChannelMessage.metadata.type && tiledeskChannelMessage.metadata.type.startsWith('video')) || tiledeskChannelMessage.type.startsWith('video')) {
|
|
82
|
+
|
|
83
|
+
instagram_message.message = {
|
|
84
|
+
attachment: {
|
|
85
|
+
type: "template",
|
|
86
|
+
payload: {
|
|
87
|
+
template_type: "open_graph",
|
|
88
|
+
elements: [
|
|
89
|
+
{
|
|
90
|
+
//media_type: "video",
|
|
91
|
+
url: tiledeskChannelMessage.metadata.src
|
|
92
|
+
//url: tiledeskChannelMessage.metadata.src
|
|
93
|
+
//url: // facebook url:
|
|
94
|
+
// https://developers.facebook.com/docs/instagram-platform/send-messages/template/media
|
|
95
|
+
// https://developers.facebook.com/docs/instagram-platform/reference/attachment-upload-api
|
|
96
|
+
}
|
|
97
|
+
]
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/*
|
|
103
|
+
instagram_message.type = 'video'
|
|
104
|
+
instagram_message.video = {
|
|
105
|
+
link: tiledeskChannelMessage.metadata.src,
|
|
106
|
+
//caption: tiledeskChannelMessage.metadata.name || tiledeskChannelMessage.text
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
*/
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
else if (tiledeskChannelMessage.metadata.type && tiledeskChannelMessage.metadata.type.startsWith('application')) {
|
|
113
|
+
|
|
114
|
+
instagram_message.type = 'document'
|
|
115
|
+
instagram_message.document = {
|
|
116
|
+
link: tiledeskChannelMessage.metadata.src,
|
|
117
|
+
caption: tiledeskChannelMessage.metadata.name || tiledeskChannelMessage.text
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
else {
|
|
122
|
+
winston.verbose("(fbm) [Translator] file type not supported")
|
|
123
|
+
return null
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
winston.debug("(fbm) [Translator] instagram_message: " + instagram_message)
|
|
127
|
+
return instagram_message;
|
|
128
|
+
|
|
129
|
+
} else if (tiledeskChannelMessage.attributes) {
|
|
130
|
+
if (tiledeskChannelMessage.attributes.attachment) {
|
|
131
|
+
if (tiledeskChannelMessage.attributes.attachment.buttons) {
|
|
132
|
+
|
|
133
|
+
let buttons = tiledeskChannelMessage.attributes.attachment.buttons;
|
|
134
|
+
|
|
135
|
+
let quick_replies = [];
|
|
136
|
+
let actions = [];
|
|
137
|
+
|
|
138
|
+
if (buttons.length == 0) {
|
|
139
|
+
instagram_message.message = {
|
|
140
|
+
text: text
|
|
141
|
+
};
|
|
142
|
+
return instagram_message;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
for (let btn of buttons) {
|
|
146
|
+
|
|
147
|
+
if (btn.type == 'text') {
|
|
148
|
+
let quick_reply = {
|
|
149
|
+
content_type: 'text',
|
|
150
|
+
title: btn.value,
|
|
151
|
+
payload: btn.value
|
|
152
|
+
}
|
|
153
|
+
quick_replies.push(quick_reply)
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
if (btn.type == 'action') {
|
|
157
|
+
let action = {
|
|
158
|
+
type: "postback",
|
|
159
|
+
title: btn.value,
|
|
160
|
+
payload: btn.action
|
|
161
|
+
}
|
|
162
|
+
actions.push(action);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if (btn.type == 'url') {
|
|
166
|
+
let web_url = {
|
|
167
|
+
type: "web_url",
|
|
168
|
+
title: btn.value,
|
|
169
|
+
url: btn.link
|
|
170
|
+
}
|
|
171
|
+
actions.push(web_url);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
instagram_message.messaging_type = "RESPONSE";
|
|
177
|
+
instagram_message.message = {};
|
|
178
|
+
if (actions.length > 0) {
|
|
179
|
+
if (actions.length > 3) {
|
|
180
|
+
winston.warn("(fbm) [Translator] Number of action buttons exceeds the Facebook limit of 3. Excess buttons will be ignored.")
|
|
181
|
+
actions.length = 3;
|
|
182
|
+
}
|
|
183
|
+
instagram_message.message.attachment = {
|
|
184
|
+
type: "template",
|
|
185
|
+
payload: {
|
|
186
|
+
template_type: "button",
|
|
187
|
+
text: text,
|
|
188
|
+
buttons: actions
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
if (quick_replies.length > 0) {
|
|
194
|
+
instagram_message.message.text = text;
|
|
195
|
+
if (quick_replies.length > 13) {
|
|
196
|
+
winston.warn("(fbm) [Translator] Number of quick replies exceeds the Facebook limit of 13. Excess replies will be ignored.")
|
|
197
|
+
quick_replies.length = 13;
|
|
198
|
+
}
|
|
199
|
+
instagram_message.message.quick_replies = quick_replies;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
if (quick_replies.length > 0 && actions.length > 0) {
|
|
203
|
+
delete instagram_message.message.text;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
return instagram_message;
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
else if (tiledeskChannelMessage.attributes.attachment.gallery) {
|
|
214
|
+
winston.warn("(fbm) [Translator] Messages of type 'gallery' are not supported.")
|
|
215
|
+
return null;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
else {
|
|
219
|
+
|
|
220
|
+
instagram_message.message = {
|
|
221
|
+
text: text
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
winston.debug("(fbm) [Translator] instagram_message: ", instagram_message)
|
|
225
|
+
return instagram_message;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
} else {
|
|
229
|
+
|
|
230
|
+
instagram_message.message = {
|
|
231
|
+
text: text
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
winston.debug("(fbm) [Translator] simple whatsapp message: ", instagram_message)
|
|
235
|
+
return instagram_message;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
} else {
|
|
239
|
+
instagram_message.message = {
|
|
240
|
+
text: text
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
winston.debug("(fbm) [Translator] whatsapp message: ", instagram_message)
|
|
244
|
+
return instagram_message;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
async toTiledesk(instagramChannelMessage) {
|
|
249
|
+
|
|
250
|
+
winston.debug("(fbm) [Translator] instagramChannelMessage: ", instagramChannelMessage);
|
|
251
|
+
|
|
252
|
+
let tiledeskMessage = {
|
|
253
|
+
senderFullname: instagramChannelMessage.sender.fullname,
|
|
254
|
+
channel: { name: TiledeskInstagramTranslator.CHANNEL_NAME }
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
if (instagramChannelMessage.postback) {
|
|
258
|
+
tiledeskMessage.text = "",
|
|
259
|
+
tiledeskMessage.type = 'text',
|
|
260
|
+
tiledeskMessage.attributes = {
|
|
261
|
+
action: instagramChannelMessage.postback.payload,
|
|
262
|
+
subtype: 'info'
|
|
263
|
+
}
|
|
264
|
+
return tiledeskMessage;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
else if (instagramChannelMessage.message) {
|
|
268
|
+
if (instagramChannelMessage.message.attachments) {
|
|
269
|
+
|
|
270
|
+
if (instagramChannelMessage.message.attachments[0]) {
|
|
271
|
+
let attachment = instagramChannelMessage.message.attachments[0];
|
|
272
|
+
|
|
273
|
+
if (attachment.type === "image") {
|
|
274
|
+
|
|
275
|
+
tiledeskMessage.type = 'image';
|
|
276
|
+
tiledeskMessage.text = instagramChannelMessage.message.text || "Attached image";
|
|
277
|
+
tiledeskMessage.metadata = {
|
|
278
|
+
src: attachment.payload.url
|
|
279
|
+
}
|
|
280
|
+
return tiledeskMessage;
|
|
281
|
+
|
|
282
|
+
} else if (attachment.type === "video") {
|
|
283
|
+
tiledeskMessage.text = "[Download video](" + attachment.payload.url + ")";
|
|
284
|
+
tiledeskMessage.type = "file";
|
|
285
|
+
tiledeskMessage.metadata = {
|
|
286
|
+
src: attachment.payload.url,
|
|
287
|
+
name: "video.mp4",
|
|
288
|
+
type: "video/mp4"
|
|
289
|
+
}
|
|
290
|
+
return tiledeskMessage
|
|
291
|
+
|
|
292
|
+
} else if (attachment.type === "file") {
|
|
293
|
+
tiledeskMessage.type = "file";
|
|
294
|
+
tiledeskMessage.text = "[Download document](" + attachment.payload.url + ")";
|
|
295
|
+
tiledeskMessage.metadata = {
|
|
296
|
+
src: attachment.payload.url,
|
|
297
|
+
name: "document.pdf",
|
|
298
|
+
type: "application/pdf"
|
|
299
|
+
}
|
|
300
|
+
return tiledeskMessage
|
|
301
|
+
|
|
302
|
+
} else {
|
|
303
|
+
winston.verbose("(fbm) Attachment type not supported.");
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
} else if (instagramChannelMessage.message.quick_reply) {
|
|
309
|
+
tiledeskMessage.text = instagramChannelMessage.message.quick_reply.payload;
|
|
310
|
+
return tiledeskMessage;
|
|
311
|
+
} else {
|
|
312
|
+
tiledeskMessage.text = instagramChannelMessage.message.text;
|
|
313
|
+
return tiledeskMessage;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
} else {
|
|
317
|
+
winston.verbose("(fbm) message type not supported");
|
|
318
|
+
return null;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
/*
|
|
325
|
+
async toTiledeskOld(instagramChannelMessage, message_info, settings, API_URL) {
|
|
326
|
+
|
|
327
|
+
if (this.log) {
|
|
328
|
+
console.log("(fbm) [Translator] instagram message: ", JSON.stringify(instagramChannelMessage));
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
let tiledeskMessage = {
|
|
332
|
+
senderFullname: instagramChannelMessage.sender.fullname,
|
|
333
|
+
channel: { name: TiledeskInstagramTranslator.CHANNEL_NAME }
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
if (instagramChannelMessage.postback) {
|
|
337
|
+
tiledeskMessage.text = ' ',
|
|
338
|
+
tiledeskMessage.type = 'text',
|
|
339
|
+
tiledeskMessage.attributes = {
|
|
340
|
+
action: instagramChannelMessage.postback.payload,
|
|
341
|
+
subtype: 'info'
|
|
342
|
+
}
|
|
343
|
+
return tiledeskMessage;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
else if (instagramChannelMessage.message) {
|
|
347
|
+
|
|
348
|
+
// attachments
|
|
349
|
+
if (instagramChannelMessage.message.attachments) {
|
|
350
|
+
|
|
351
|
+
const tdChannel = new TiledeskChannel({ settings: settings, API_URL: API_URL })
|
|
352
|
+
|
|
353
|
+
for (let attachment of instagramChannelMessage.message.attachments) {
|
|
354
|
+
|
|
355
|
+
// image
|
|
356
|
+
if (attachment.type === "image") {
|
|
357
|
+
TiledeskInstagramTranslator.getImageSize(attachment.payload.url).then( async (size) => {
|
|
358
|
+
tiledeskMessage.type = 'image';
|
|
359
|
+
tiledeskMessage.text = instagramChannelMessage.message.text || "Attached image";
|
|
360
|
+
tiledeskMessage.metadata = {
|
|
361
|
+
src: attachment.payload.url,
|
|
362
|
+
width: size.width,
|
|
363
|
+
height: size.height
|
|
364
|
+
}
|
|
365
|
+
const response = await tdChannel.send(tiledeskMessage, message_info, settings.department_id);
|
|
366
|
+
if (this.log) {
|
|
367
|
+
console.log("(instagram) /instagram Send response: ", response)
|
|
368
|
+
}
|
|
369
|
+
}).catch((err) => {
|
|
370
|
+
console.error("error getting image size: ", err)
|
|
371
|
+
})
|
|
372
|
+
|
|
373
|
+
// video
|
|
374
|
+
} else if (attachment.type === 'video') {
|
|
375
|
+
tiledeskMessage.text = "[Download video](" + attachment.payload.url + ")";
|
|
376
|
+
tiledeskMessage.type = "file";
|
|
377
|
+
tiledeskMessage.metadata = {
|
|
378
|
+
src: attachment.payload.url,
|
|
379
|
+
name: "video.mp4",
|
|
380
|
+
type: "video/mp4"
|
|
381
|
+
}
|
|
382
|
+
const response = await tdChannel.send(tiledeskMessage, message_info, settings.department_id);
|
|
383
|
+
if (this.log) {
|
|
384
|
+
console.log("(instagram) /instagram Send response: ", response)
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
// file
|
|
388
|
+
} else if (attachment.type === 'file') {
|
|
389
|
+
tiledeskMessage.type = "file";
|
|
390
|
+
tiledeskMessage.text = "[Dowload document](" + attachment.payload.url + ")";
|
|
391
|
+
tiledeskMessage.metadata = {
|
|
392
|
+
src: attachment.payload.url,
|
|
393
|
+
name: "document.pdf",
|
|
394
|
+
type: "application/pdf"
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
const response = await tdChannel.send(tiledeskMessage, message_info, settings.department_id);
|
|
398
|
+
if (this.log) {
|
|
399
|
+
console.log("(instagram) /instagram Send response: ", response)
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
// others
|
|
403
|
+
} else {
|
|
404
|
+
console.log("attachment type not supported: ", attachment.type)
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
return true;
|
|
410
|
+
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
else if (instagramChannelMessage.message.quick_reply) {
|
|
414
|
+
tiledeskMessage.text = instagramChannelMessage.message.quick_reply.payload;
|
|
415
|
+
return tiledeskMessage;
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
else {
|
|
419
|
+
tiledeskMessage.text = instagramChannelMessage.message.text;
|
|
420
|
+
return tiledeskMessage;
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
else {
|
|
425
|
+
console.log("message type not supported")
|
|
426
|
+
return null
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
}
|
|
430
|
+
*/
|
|
431
|
+
|
|
432
|
+
static async getImageSize(url) {
|
|
433
|
+
|
|
434
|
+
return new Promise((resolve, reject) => {
|
|
435
|
+
probe(url).then((size) => {
|
|
436
|
+
resolve(size);
|
|
437
|
+
}).catch((err) => {
|
|
438
|
+
reject(err);
|
|
439
|
+
})
|
|
440
|
+
})
|
|
441
|
+
//return await probe(url);
|
|
442
|
+
}
|
|
443
|
+
/*
|
|
444
|
+
*************************************
|
|
445
|
+
********* FACEBOOK INSTAGRAM *********
|
|
446
|
+
**************** END ****************
|
|
447
|
+
*/
|
|
448
|
+
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
module.exports = { TiledeskInstagramTranslator };
|
|
452
|
+
|