@rexxhayanasi/elaina-baileys 1.2.0-rc.6 → 1.2.0-rc.8
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 +38 -126
- 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,142 +1,54 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.prepareWAMessageMediaNewsletter =
|
|
5
|
-
exports.getWAUploadToServerNewsletter = getWAUploadToServerNewsletter;
|
|
6
|
-
|
|
7
|
-
const fs = require("fs");
|
|
8
|
-
const Boom = require("@hapi/boom");
|
|
9
|
-
const { WAProto } = require("../../WAProto");
|
|
10
|
-
const {
|
|
11
|
-
MEDIA_PATH_MAP,
|
|
12
|
-
uploadMedia,
|
|
13
|
-
generateThumbnail,
|
|
14
|
-
encryptedStream,
|
|
15
|
-
getAudioDuration,
|
|
16
|
-
getAudioWaveform,
|
|
17
|
-
assertColor,
|
|
18
|
-
unixTimestampSeconds
|
|
19
|
-
} = require("./index");
|
|
20
|
-
const { DEFAULT_ORIGIN } = require("../Defaults");
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Encode string ke Base64
|
|
24
|
-
*/
|
|
25
|
-
function encodeBase64EncodedStringForUpload(str) {
|
|
26
|
-
if (!str) return "";
|
|
27
|
-
return Buffer.from(str).toString("base64");
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Placeholder function, harus diimplementasi sesuai Baileys v6
|
|
32
|
-
*/
|
|
33
|
-
async function getRawMediaUploadData(media, mediaType, logger) {
|
|
34
|
-
throw new Error("Implement getRawMediaUploadData sesuai Baileys v6 mu");
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Upload ke server newsletter
|
|
39
|
-
*/
|
|
40
|
-
function getWAUploadToServerNewsletter({ customUploadHosts, fetchAgent, logger, options }, refreshMediaConn) {
|
|
41
|
-
return async (filePath, { mediaType, fileEncSha256B64, timeoutMs }) => {
|
|
42
|
-
let uploadInfo = await refreshMediaConn(false);
|
|
43
|
-
let urls;
|
|
44
|
-
const hosts = [...customUploadHosts, ...uploadInfo.hosts];
|
|
4
|
+
exports.prepareWAMessageMediaNewsletter = exports.getRawMediaUploadDataNewsletter = void 0;
|
|
45
5
|
|
|
46
|
-
|
|
6
|
+
const crypto_1 = require("crypto");
|
|
7
|
+
const boom_1 = require("@hapi/boom");
|
|
8
|
+
const WAProto_1 = require("../../WAProto");
|
|
47
9
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
const url = `https://${hostname}${MEDIA_PATH_MAP[mediaType]}/${fileEncSha256B64}?auth=${auth}&token=${fileEncSha256B64}`;
|
|
58
|
-
|
|
59
|
-
let result;
|
|
60
|
-
try {
|
|
61
|
-
result = await uploadMedia({ url, filePath, headers, timeoutMs, agent: fetchAgent }, logger);
|
|
62
|
-
|
|
63
|
-
if (result?.url || result?.direct_path) {
|
|
64
|
-
urls = {
|
|
65
|
-
mediaUrl: result.url,
|
|
66
|
-
directPath: result.direct_path,
|
|
67
|
-
meta_hmac: result.meta_hmac,
|
|
68
|
-
fbid: result.fbid,
|
|
69
|
-
ts: result.ts,
|
|
70
|
-
};
|
|
71
|
-
break;
|
|
72
|
-
} else {
|
|
73
|
-
uploadInfo = await refreshMediaConn(true);
|
|
74
|
-
throw new Error(`upload failed, reason: ${JSON.stringify(result)}`);
|
|
75
|
-
}
|
|
76
|
-
} catch (error) {
|
|
77
|
-
const isLast = hostname === hosts[uploadInfo.hosts.length - 1]?.hostname;
|
|
78
|
-
logger.warn({ trace: error?.stack, uploadResult: result }, `Error in uploading to ${hostname}${isLast ? "" : ", retrying..."}`);
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
if (!urls) throw new Boom("Media upload failed on all hosts", { statusCode: 500 });
|
|
83
|
-
return urls;
|
|
10
|
+
function getRawMediaUploadDataNewsletter(buffer) {
|
|
11
|
+
if (!Buffer.isBuffer(buffer)) {
|
|
12
|
+
throw new boom_1.Boom("Newsletter media must be Buffer");
|
|
13
|
+
}
|
|
14
|
+
const fileSha256 = (0, crypto_1.createHash)("sha256").update(buffer).digest();
|
|
15
|
+
return {
|
|
16
|
+
buffer,
|
|
17
|
+
fileSha256,
|
|
18
|
+
fileLength: buffer.length
|
|
84
19
|
};
|
|
85
20
|
}
|
|
21
|
+
exports.getRawMediaUploadDataNewsletter = getRawMediaUploadDataNewsletter;
|
|
86
22
|
|
|
87
|
-
/**
|
|
88
|
-
* Prepare WA message untuk newsletter
|
|
89
|
-
*/
|
|
90
23
|
async function prepareWAMessageMediaNewsletter(message, options) {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
let mediaType;
|
|
94
|
-
for (const key of Object.keys(MEDIA_PATH_MAP)) {
|
|
95
|
-
if (key in message) {
|
|
96
|
-
mediaType = key;
|
|
97
|
-
break;
|
|
98
|
-
}
|
|
24
|
+
if (!options || typeof options.upload !== "function") {
|
|
25
|
+
throw new boom_1.Boom("upload function is required");
|
|
99
26
|
}
|
|
100
|
-
|
|
27
|
+
const mediaType = Object.keys(message || {})[0];
|
|
101
28
|
if (!mediaType) {
|
|
102
|
-
throw new Boom("Invalid media
|
|
29
|
+
throw new boom_1.Boom("Invalid newsletter media");
|
|
103
30
|
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
if (mediaType === "document" && !uploadData.fileName) uploadData.fileName = "file";
|
|
109
|
-
if (!uploadData.mimetype) uploadData.mimetype = MEDIA_PATH_MAP[mediaType]?.mimetype || "application/octet-stream";
|
|
110
|
-
|
|
111
|
-
const isNewsletter = options.newsletter || (options.jid && options.jid.endsWith("@newsletter"));
|
|
112
|
-
if (!isNewsletter) {
|
|
113
|
-
throw new Boom("This function is only for newsletter", { statusCode: 400 });
|
|
31
|
+
const buffer = message[mediaType];
|
|
32
|
+
if (!Buffer.isBuffer(buffer)) {
|
|
33
|
+
throw new boom_1.Boom("Newsletter media must be Buffer");
|
|
114
34
|
}
|
|
115
|
-
|
|
116
|
-
const
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
url: urls.mediaUrl,
|
|
129
|
-
directPath: urls.directPath,
|
|
130
|
-
fileSha256,
|
|
131
|
-
fileLength,
|
|
132
|
-
...uploadData,
|
|
133
|
-
media: undefined,
|
|
35
|
+
const uploadData = getRawMediaUploadDataNewsletter(buffer);
|
|
36
|
+
const uploaded = await options.upload(buffer, {
|
|
37
|
+
mediaType,
|
|
38
|
+
fileEncSha256B64: uploadData.fileSha256.toString("base64"),
|
|
39
|
+
newsletter: true
|
|
40
|
+
});
|
|
41
|
+
const proto = {};
|
|
42
|
+
proto[`${mediaType}Message`] = {
|
|
43
|
+
url: uploaded.mediaUrl,
|
|
44
|
+
directPath: uploaded.directPath,
|
|
45
|
+
fileSha256: uploadData.fileSha256,
|
|
46
|
+
fileLength: uploadData.fileLength,
|
|
47
|
+
mimetype: options.mimetype
|
|
134
48
|
};
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
messageObj.ptvMessage = messageObj.videoMessage;
|
|
138
|
-
delete messageObj.videoMessage;
|
|
49
|
+
if (mediaType === "video") {
|
|
50
|
+
proto.videoMessage.ptv = true;
|
|
139
51
|
}
|
|
140
|
-
|
|
141
|
-
return WAProto.Message.fromObject(messageObj);
|
|
52
|
+
return WAProto_1.WAProto.Message.fromObject(proto);
|
|
142
53
|
}
|
|
54
|
+
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.8",
|
|
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",
|