@open-wa/wa-automate 4.27.4 → 4.27.5
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.
@@ -14,11 +14,17 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
15
15
|
exports.setupChatwootOutgoingMessageHandler = exports.chatwootMiddleware = void 0;
|
16
16
|
const axios_1 = __importDefault(require("axios"));
|
17
|
+
const form_data_1 = __importDefault(require("form-data"));
|
18
|
+
const mime_types_1 = __importDefault(require("mime-types"));
|
17
19
|
const chatwootMiddleware = (cliConfig, client) => {
|
18
20
|
return (req, res) => __awaiter(void 0, void 0, void 0, function* () {
|
19
21
|
const processMesssage = () => __awaiter(void 0, void 0, void 0, function* () {
|
20
22
|
const promises = [];
|
21
23
|
const { body } = req;
|
24
|
+
if (!body)
|
25
|
+
return;
|
26
|
+
if (!body.conversation)
|
27
|
+
return;
|
22
28
|
const m = body.conversation.messages[0];
|
23
29
|
const contact = (body.conversation.meta.sender.phone_number || "").replace('+', '');
|
24
30
|
if (body.message_type === "incoming" ||
|
@@ -81,16 +87,14 @@ const setupChatwootOutgoingMessageHandler = (cliConfig, client) => __awaiter(voi
|
|
81
87
|
const [accountId, inboxId] = u.match(/\/(app|(api\/v1))\/accounts\/\d*\/inbox\/\d*/g)[0].split('/').filter(Number);
|
82
88
|
// const accountId = u.match(/accounts\/\d*/g) && u.match(/accounts\/\d*/g)[0].replace('accounts/', '')
|
83
89
|
const resolvedInbox = inboxId || u.match(/inboxes\/\d*/g) && u.match(/inboxes\/\d*/g)[0].replace('inboxes/', '');
|
84
|
-
const cwReq = (path, method, data) => {
|
90
|
+
const cwReq = (path, method, data, _headers) => {
|
85
91
|
const url = `${origin}/api/v1/accounts/${accountId}/${path}`.replace('app.bentonow.com', 'chat.bentonow.com');
|
86
|
-
console.log(url,
|
92
|
+
// console.log(url,method,data)
|
87
93
|
return axios_1.default({
|
88
94
|
method,
|
89
95
|
data,
|
90
96
|
url,
|
91
|
-
headers: {
|
92
|
-
api_access_token
|
93
|
-
}
|
97
|
+
headers: Object.assign({ api_access_token }, _headers)
|
94
98
|
});
|
95
99
|
};
|
96
100
|
const contactReg = {
|
@@ -128,7 +132,7 @@ const setupChatwootOutgoingMessageHandler = (cliConfig, client) => __awaiter(voi
|
|
128
132
|
const getContactConversation = (number) => __awaiter(void 0, void 0, void 0, function* () {
|
129
133
|
try {
|
130
134
|
const { data } = yield cwReq(`contacts/${contactReg[number]}/conversations`, 'get');
|
131
|
-
return data.payload[0];
|
135
|
+
return data.payload.sort((a, b) => a.id - b.id)[0];
|
132
136
|
}
|
133
137
|
catch (error) {
|
134
138
|
return;
|
@@ -173,6 +177,25 @@ const setupChatwootOutgoingMessageHandler = (cliConfig, client) => __awaiter(voi
|
|
173
177
|
return;
|
174
178
|
}
|
175
179
|
});
|
180
|
+
const sendAttachmentMessage = (content, contactId, message) => __awaiter(void 0, void 0, void 0, function* () {
|
181
|
+
// decrypt message
|
182
|
+
const file = yield client.decryptMedia(message);
|
183
|
+
let formData = new form_data_1.default();
|
184
|
+
formData.append('attachments[]', Buffer.from(file.split(',')[1], 'base64'), {
|
185
|
+
knownLength: 1,
|
186
|
+
filename: `${message.t}.${mime_types_1.default.extension(message.mimetype)}`,
|
187
|
+
contentType: (file.match(/[^:\s*]\w+\/[\w-+\d.]+(?=[;| ])/) || ["application/octet-stream"])[0]
|
188
|
+
});
|
189
|
+
formData.append('content', content);
|
190
|
+
formData.append('message_type', 'incoming');
|
191
|
+
try {
|
192
|
+
const { data } = yield cwReq(`conversations/${convoReg[contactId]}/messages`, 'post', formData, formData.getHeaders());
|
193
|
+
return data;
|
194
|
+
}
|
195
|
+
catch (error) {
|
196
|
+
return;
|
197
|
+
}
|
198
|
+
});
|
176
199
|
// const inboxId = s.match(/conversations\/\d*/g) && s.match(/conversations\/\d*/g)[0].replace('conversations/','')
|
177
200
|
/**
|
178
201
|
* Update the chatwoot contact and conversation registries
|
@@ -209,6 +232,7 @@ const setupChatwootOutgoingMessageHandler = (cliConfig, client) => __awaiter(voi
|
|
209
232
|
* Does the conversation exist in
|
210
233
|
*/
|
211
234
|
let text = message.body;
|
235
|
+
let hasAttachments = false;
|
212
236
|
switch (message.type) {
|
213
237
|
case 'location':
|
214
238
|
text = `${message.lat},${message.lng}`;
|
@@ -221,14 +245,22 @@ const setupChatwootOutgoingMessageHandler = (cliConfig, client) => __awaiter(voi
|
|
221
245
|
case 'audio':
|
222
246
|
case 'ptt':
|
223
247
|
case 'video':
|
224
|
-
if (message.cloudUrl)
|
248
|
+
if (message.cloudUrl) {
|
225
249
|
text = `FILE:\t${message.cloudUrl}\n\nMESSAGE:\t${message.text}`;
|
250
|
+
}
|
251
|
+
else {
|
252
|
+
text = message.text;
|
253
|
+
hasAttachments = true;
|
254
|
+
}
|
226
255
|
break;
|
227
256
|
default:
|
228
257
|
text = message.body || "__UNHANDLED__";
|
229
258
|
break;
|
230
259
|
}
|
231
|
-
|
260
|
+
if (hasAttachments)
|
261
|
+
yield sendAttachmentMessage(text, message.from, message);
|
262
|
+
else
|
263
|
+
yield sendConversationMessage(text, message.from, message);
|
232
264
|
}));
|
233
265
|
});
|
234
266
|
exports.setupChatwootOutgoingMessageHandler = setupChatwootOutgoingMessageHandler;
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@open-wa/wa-automate",
|
3
|
-
"version": "4.27.
|
3
|
+
"version": "4.27.5",
|
4
4
|
"licenseCheckUrl": "https://openwa.dev/license-check",
|
5
5
|
"brokenMethodReportUrl": "https://openwa.dev/report-bm",
|
6
6
|
"patches": "https://cdn.openwa.dev/patches.json",
|
@@ -67,6 +67,7 @@
|
|
67
67
|
"@types/cross-spawn": "^6.0.2",
|
68
68
|
"@types/death": "^1.1.1",
|
69
69
|
"@types/express": "^4.17.11",
|
70
|
+
"@types/form-data": "^2.5.0",
|
70
71
|
"@types/fs-extra": "^9.0.11",
|
71
72
|
"@types/line-reader": "0.0.34",
|
72
73
|
"@types/localtunnel": "^2.0.1",
|
@@ -119,6 +120,7 @@
|
|
119
120
|
"express": "^4.17.1",
|
120
121
|
"express-robots-txt": "^1.0.0",
|
121
122
|
"find-up": "^5.0.0",
|
123
|
+
"form-data": "^4.0.0",
|
122
124
|
"fs-extra": "^10.0.0",
|
123
125
|
"get-port": "^5.1.1",
|
124
126
|
"hasha": "^5.2.0",
|