@rexxhayanasi/elaina-baileys 1.2.0-rc.7 → 1.2.0-rc.9
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/lib/Socket/business.js +2 -1
- package/lib/Utils/newslettermedia.js +85 -78
- package/package.json +4 -1
package/lib/Socket/business.js
CHANGED
|
@@ -12,7 +12,7 @@ const makeBusinessSocket = (config) => {
|
|
|
12
12
|
syncFullHistory: false,
|
|
13
13
|
shouldIgnoreJid: () => false
|
|
14
14
|
});
|
|
15
|
-
const { authState, query, waUploadToServer } = sock;
|
|
15
|
+
const { authState, query, waUploadToServer, refreshMediaConn } = sock;
|
|
16
16
|
const getCatalog = async ({ jid, limit, cursor }) => {
|
|
17
17
|
var _a;
|
|
18
18
|
jid = jid || ((_a = authState.creds.me) === null || _a === void 0 ? void 0 : _a.id);
|
|
@@ -254,6 +254,7 @@ const makeBusinessSocket = (config) => {
|
|
|
254
254
|
return {
|
|
255
255
|
...sock,
|
|
256
256
|
logger: config.logger,
|
|
257
|
+
refreshMediaConn,
|
|
257
258
|
getOrderDetails,
|
|
258
259
|
getCatalog,
|
|
259
260
|
getCollections,
|
|
@@ -1,103 +1,110 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const { WAProto } = require("../../WAProto");
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.getWAUploadToServerNewsletter = exports.prepareWAMessageMediaNewsletter = exports.getRawMediaUploadDataNewsletter = void 0;
|
|
6
5
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
return async (buffer, uploadOpts = {}) => {
|
|
13
|
-
if (!Buffer.isBuffer(buffer)) {
|
|
14
|
-
throw new Boom("Newsletter upload expects Buffer");
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
const mediaConn = await refreshMediaConn();
|
|
18
|
-
if (!mediaConn || !mediaConn.hosts?.length) {
|
|
19
|
-
throw new Boom("Failed to obtain media connection for newsletter");
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
const host = mediaConn.hosts[0];
|
|
23
|
-
const url = `https://${host.hostname}${host.uploadPath}`;
|
|
24
|
-
|
|
25
|
-
const res = await fetch(url, {
|
|
26
|
-
method: "POST",
|
|
27
|
-
body: buffer,
|
|
28
|
-
headers: {
|
|
29
|
-
"Content-Type": "application/octet-stream",
|
|
30
|
-
"Content-Length": buffer.length,
|
|
31
|
-
},
|
|
32
|
-
agent: config.fetchAgent,
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
if (!res.ok) {
|
|
36
|
-
throw new Boom(`Newsletter upload failed: ${res.status}`);
|
|
37
|
-
}
|
|
6
|
+
const crypto_1 = require("crypto");
|
|
7
|
+
const boom_1 = require("@hapi/boom");
|
|
8
|
+
const WAProto_1 = require("../../WAProto");
|
|
9
|
+
const axios_1 = require("axios");
|
|
10
|
+
const Defaults_1 = require("../Defaults");
|
|
38
11
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
};
|
|
45
|
-
};
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Ambil metadata RAW newsletter
|
|
50
|
-
*/
|
|
51
|
-
async function getRawMediaUploadDataNewsletter(buffer) {
|
|
52
|
-
const fileSha256 = Crypto.createHash("sha256").update(buffer).digest();
|
|
12
|
+
function getRawMediaUploadDataNewsletter(buffer) {
|
|
13
|
+
if (!Buffer.isBuffer(buffer)) {
|
|
14
|
+
throw new boom_1.Boom("Newsletter media must be Buffer");
|
|
15
|
+
}
|
|
16
|
+
const fileSha256 = (0, crypto_1.createHash)("sha256").update(buffer).digest();
|
|
53
17
|
return {
|
|
54
18
|
buffer,
|
|
55
19
|
fileSha256,
|
|
56
|
-
fileLength: buffer.length
|
|
20
|
+
fileLength: buffer.length
|
|
57
21
|
};
|
|
58
22
|
}
|
|
23
|
+
exports.getRawMediaUploadDataNewsletter = getRawMediaUploadDataNewsletter;
|
|
59
24
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
25
|
+
function getWAUploadToServerNewsletter({ customUploadHosts = [], fetchAgent, logger, options = {} }, refreshMediaConn) {
|
|
26
|
+
return async (buffer, { mediaType, fileEncSha256B64 }) => {
|
|
27
|
+
if (!Buffer.isBuffer(buffer)) {
|
|
28
|
+
throw new boom_1.Boom("Upload expects Buffer");
|
|
29
|
+
}
|
|
30
|
+
let uploadInfo = await refreshMediaConn(false);
|
|
31
|
+
const hosts = [...customUploadHosts, ...uploadInfo.hosts];
|
|
32
|
+
let urls;
|
|
33
|
+
for (const { hostname, maxContentLengthBytes } of hosts) {
|
|
34
|
+
const auth = encodeURIComponent(uploadInfo.auth);
|
|
35
|
+
let mediaPath = Defaults_1.MEDIA_PATH_MAP[mediaType];
|
|
36
|
+
if (!mediaPath) {
|
|
37
|
+
mediaPath = "/newsletter/newsletter-image";
|
|
38
|
+
} else {
|
|
39
|
+
mediaPath = mediaPath.replace("/mms/", "/newsletter/newsletter-");
|
|
40
|
+
}
|
|
41
|
+
const url = `https://${hostname}${mediaPath}/${fileEncSha256B64}?auth=${auth}&token=${fileEncSha256B64}`;
|
|
42
|
+
try {
|
|
43
|
+
if (maxContentLengthBytes && buffer.length > maxContentLengthBytes) {
|
|
44
|
+
throw new boom_1.Boom("Body too large", { statusCode: 413 });
|
|
45
|
+
}
|
|
46
|
+
const res = await axios_1.default.post(url, buffer, {
|
|
47
|
+
...options,
|
|
48
|
+
headers: {
|
|
49
|
+
"Content-Type": "application/octet-stream",
|
|
50
|
+
Origin: Defaults_1.DEFAULT_ORIGIN,
|
|
51
|
+
...(options.headers || {})
|
|
52
|
+
},
|
|
53
|
+
httpsAgent: fetchAgent,
|
|
54
|
+
responseType: "json",
|
|
55
|
+
maxBodyLength: Infinity,
|
|
56
|
+
maxContentLength: Infinity
|
|
57
|
+
});
|
|
58
|
+
const result = res.data;
|
|
59
|
+
if (result?.url || result?.direct_path) {
|
|
60
|
+
urls = {
|
|
61
|
+
mediaUrl: result.url,
|
|
62
|
+
directPath: result.direct_path
|
|
63
|
+
};
|
|
64
|
+
break;
|
|
65
|
+
}
|
|
66
|
+
uploadInfo = await refreshMediaConn(true);
|
|
67
|
+
} catch (err) {
|
|
68
|
+
logger?.warn?.({ err }, `Newsletter upload failed on ${hostname}`);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
if (!urls) {
|
|
72
|
+
throw new boom_1.Boom("Newsletter media upload failed", { statusCode: 500 });
|
|
73
|
+
}
|
|
74
|
+
return urls;
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
exports.getWAUploadToServerNewsletter = getWAUploadToServerNewsletter;
|
|
66
78
|
|
|
79
|
+
async function prepareWAMessageMediaNewsletter(message, options) {
|
|
80
|
+
if (!options || typeof options.upload !== "function") {
|
|
81
|
+
throw new boom_1.Boom("upload function is required");
|
|
82
|
+
}
|
|
83
|
+
const mediaType = Object.keys(message || {})[0];
|
|
84
|
+
if (!mediaType) {
|
|
85
|
+
throw new boom_1.Boom("Invalid newsletter media");
|
|
86
|
+
}
|
|
67
87
|
const buffer = message[mediaType];
|
|
68
88
|
if (!Buffer.isBuffer(buffer)) {
|
|
69
|
-
throw new Boom("Newsletter media must be Buffer");
|
|
89
|
+
throw new boom_1.Boom("Newsletter media must be Buffer");
|
|
70
90
|
}
|
|
71
|
-
|
|
72
|
-
const
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
},
|
|
78
|
-
options.refreshMediaConn
|
|
79
|
-
);
|
|
80
|
-
|
|
81
|
-
const uploaded = await upload(buffer);
|
|
82
|
-
|
|
91
|
+
const uploadData = getRawMediaUploadDataNewsletter(buffer);
|
|
92
|
+
const uploaded = await options.upload(buffer, {
|
|
93
|
+
mediaType,
|
|
94
|
+
fileEncSha256B64: uploadData.fileSha256.toString("base64"),
|
|
95
|
+
newsletter: true
|
|
96
|
+
});
|
|
83
97
|
const proto = {};
|
|
84
98
|
proto[`${mediaType}Message`] = {
|
|
85
99
|
url: uploaded.mediaUrl,
|
|
86
100
|
directPath: uploaded.directPath,
|
|
87
101
|
fileSha256: uploadData.fileSha256,
|
|
88
102
|
fileLength: uploadData.fileLength,
|
|
89
|
-
mimetype: options.mimetype
|
|
103
|
+
mimetype: options.mimetype
|
|
90
104
|
};
|
|
91
|
-
|
|
92
105
|
if (mediaType === "video") {
|
|
93
106
|
proto.videoMessage.ptv = true;
|
|
94
107
|
}
|
|
95
|
-
|
|
96
|
-
return WAProto.Message.fromObject(proto);
|
|
108
|
+
return WAProto_1.WAProto.Message.fromObject(proto);
|
|
97
109
|
}
|
|
98
|
-
|
|
99
|
-
module.exports = {
|
|
100
|
-
prepareWAMessageMediaNewsletter,
|
|
101
|
-
getRawMediaUploadDataNewsletter,
|
|
102
|
-
getWAUploadToServerNewsletter,
|
|
103
|
-
};
|
|
110
|
+
exports.prepareWAMessageMediaNewsletter = prepareWAMessageMediaNewsletter;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rexxhayanasi/elaina-baileys",
|
|
3
|
-
"version": "1.2.0-rc.
|
|
3
|
+
"version": "1.2.0-rc.9",
|
|
4
4
|
"description": "Custom Baileys WhatsApp API",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"baileys",
|
|
@@ -17,6 +17,9 @@
|
|
|
17
17
|
],
|
|
18
18
|
"homepage": "https://whatsapp.com/channel/0029Vb70uHbD8SE2w5Q9M107",
|
|
19
19
|
"license": "MIT",
|
|
20
|
+
"publishConfig": {
|
|
21
|
+
"tag": "experimental"
|
|
22
|
+
},
|
|
20
23
|
"author": "RexxHayanasi",
|
|
21
24
|
"main": "lib/index.js",
|
|
22
25
|
"types": "lib/index.d.ts",
|