@areumtecnologia/baileys 1.0.1 → 1.0.2
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/handlers/messages.js +82 -16
- package/index.js +1 -1
- package/package.json +1 -1
- package/utils/message-normalizer.js +2 -5
package/handlers/messages.js
CHANGED
|
@@ -174,7 +174,6 @@ class MessageHandler {
|
|
|
174
174
|
* @param {object} [options={}] - Opções adicionais.
|
|
175
175
|
*/
|
|
176
176
|
async sendLink(jid, url, options = {}) {
|
|
177
|
-
|
|
178
177
|
return this.sendMessage(jid, { text: url }, options);
|
|
179
178
|
}
|
|
180
179
|
|
|
@@ -245,7 +244,7 @@ class MessageHandler {
|
|
|
245
244
|
return this.client.sock.readMessages([messageKey]);
|
|
246
245
|
}
|
|
247
246
|
|
|
248
|
-
/** Funcional apenas usando baileys e baileys_helper - Envia uma mensagem com botões interativos. */
|
|
247
|
+
/** Deprecated - Funcional apenas usando baileys e baileys_helper - Envia uma mensagem com botões interativos. */
|
|
249
248
|
async sendInteractiveMessage(jid, interactiveMessage, more) {
|
|
250
249
|
const verifiedJid = await this.client.users.isOnWhatsApp(jid);
|
|
251
250
|
if (verifiedJid && verifiedJid.exists) {
|
|
@@ -256,7 +255,7 @@ class MessageHandler {
|
|
|
256
255
|
}
|
|
257
256
|
}
|
|
258
257
|
|
|
259
|
-
/** Funcional apenas usando baileys e baileys_helper - Envia uma mensagem com botões interativos. */
|
|
258
|
+
/** Deprecated - Funcional apenas usando baileys e baileys_helper - Envia uma mensagem com botões interativos. */
|
|
260
259
|
async sendButtons(jid, messageButton) {
|
|
261
260
|
const verifiedJid = await this.client.users.isOnWhatsApp(jid);
|
|
262
261
|
if (verifiedJid && verifiedJid.exists) {
|
|
@@ -270,29 +269,96 @@ class MessageHandler {
|
|
|
270
269
|
/** Faz o download de mídia de uma mensagem. */
|
|
271
270
|
async getAttachments(message) {
|
|
272
271
|
this.client._validateConnection();
|
|
273
|
-
|
|
272
|
+
|
|
273
|
+
const type = Object.keys(message.message)[0]; // ex: imageMessage
|
|
274
274
|
const messageContent = message.message[type];
|
|
275
|
-
if (!messageContent.url)
|
|
276
|
-
return null;
|
|
277
275
|
|
|
278
|
-
|
|
276
|
+
if (!messageContent?.url) return null;
|
|
277
|
+
|
|
278
|
+
// baixa o stream
|
|
279
|
+
const stream = await this.client.downloadContentFromMessage(
|
|
280
|
+
messageContent,
|
|
281
|
+
type.replace('Message', '') // imageMessage → image
|
|
282
|
+
);
|
|
283
|
+
|
|
284
|
+
// monta o buffer
|
|
279
285
|
let buffer = Buffer.from([]);
|
|
280
286
|
for await (const chunk of stream) {
|
|
281
287
|
buffer = Buffer.concat([buffer, chunk]);
|
|
282
288
|
}
|
|
289
|
+
|
|
290
|
+
const mimetype = messageContent.mimetype;
|
|
291
|
+
const extension = mimetype.split('/')[1] || 'bin';
|
|
292
|
+
|
|
293
|
+
// conversões
|
|
294
|
+
const toBase64 = () => buffer.toString('base64');
|
|
295
|
+
|
|
296
|
+
const toDataUri = () =>
|
|
297
|
+
`data:${mimetype};base64,${buffer.toString('base64')}`;
|
|
298
|
+
|
|
299
|
+
const toArrayBuffer = () =>
|
|
300
|
+
buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength);
|
|
301
|
+
|
|
302
|
+
const toBlob = () => {
|
|
303
|
+
const arrayBuffer = toArrayBuffer();
|
|
304
|
+
return new Blob([arrayBuffer], { type: mimetype });
|
|
305
|
+
};
|
|
306
|
+
|
|
307
|
+
const toImageUrl = () => {
|
|
308
|
+
const blob = toBlob();
|
|
309
|
+
return URL.createObjectURL(blob);
|
|
310
|
+
};
|
|
311
|
+
|
|
312
|
+
const toImageBitmap = async () => {
|
|
313
|
+
const blob = toBlob();
|
|
314
|
+
return await createImageBitmap(blob);
|
|
315
|
+
};
|
|
316
|
+
|
|
317
|
+
const toStream = () => {
|
|
318
|
+
const { Readable } = require('stream');
|
|
319
|
+
return Readable.from(buffer);
|
|
320
|
+
};
|
|
321
|
+
|
|
322
|
+
const toSharp = () => {
|
|
323
|
+
const sharp = require('sharp');
|
|
324
|
+
return sharp(buffer); // o usuário pode continuar com .resize().png()...
|
|
325
|
+
};
|
|
326
|
+
|
|
327
|
+
const detectType = async () => {
|
|
328
|
+
const { fileTypeFromBuffer } = await import('file-type');
|
|
329
|
+
return await fileTypeFromBuffer(buffer);
|
|
330
|
+
};
|
|
331
|
+
|
|
332
|
+
const save = async (path) => {
|
|
333
|
+
const filename = `${message.from}-${Date.now()}.${extension}`;
|
|
334
|
+
const filepath = path
|
|
335
|
+
? path
|
|
336
|
+
: `${this.client.sessionPath}/media/${filename}`;
|
|
337
|
+
|
|
338
|
+
await fs.writeFile(filepath, buffer);
|
|
339
|
+
|
|
340
|
+
return { filename, filepath };
|
|
341
|
+
};
|
|
342
|
+
|
|
283
343
|
return {
|
|
284
|
-
type:
|
|
344
|
+
type: mimetype,
|
|
345
|
+
extension,
|
|
285
346
|
buffer,
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
347
|
+
|
|
348
|
+
// 🔥 todas as funções utilitárias
|
|
349
|
+
toBase64,
|
|
350
|
+
toDataUri,
|
|
351
|
+
toArrayBuffer,
|
|
352
|
+
toBlob,
|
|
353
|
+
toImageUrl,
|
|
354
|
+
toImageBitmap,
|
|
355
|
+
toStream,
|
|
356
|
+
toSharp,
|
|
357
|
+
detectType,
|
|
358
|
+
save,
|
|
294
359
|
};
|
|
295
360
|
}
|
|
361
|
+
|
|
296
362
|
}
|
|
297
363
|
|
|
298
364
|
module.exports = MessageHandler;
|
package/index.js
CHANGED
package/package.json
CHANGED
|
@@ -46,7 +46,7 @@ class MessageNormalizer {
|
|
|
46
46
|
isGroup: isGroup,
|
|
47
47
|
sender: {
|
|
48
48
|
id: from,
|
|
49
|
-
lid: rawMessage.key.remoteJid.includes('@lid') ? rawMessage.key.remoteJid : null,
|
|
49
|
+
lid: rawMessage.key.remoteJid.includes('@lid') ? rawMessage.key.remoteJid : rawMessage.key.remoteJidAlt && rawMessage.key.remoteJidAlt.includes('@lid') ? rawMessage.key.remoteJidAlt : null,
|
|
50
50
|
pushName: rawMessage.pushName || ''
|
|
51
51
|
},
|
|
52
52
|
type,
|
|
@@ -66,9 +66,6 @@ class MessageNormalizer {
|
|
|
66
66
|
reaction: this._extractReaction(rawMessage.message),
|
|
67
67
|
pollUpdate: await this._extractPollUpdate(rawMessage, client),
|
|
68
68
|
poll: this._extractPollCreation(rawMessage),
|
|
69
|
-
getAttachments: async () => {
|
|
70
|
-
return await client.messages.getAttachments(rawMessage);
|
|
71
|
-
},
|
|
72
69
|
raw: rawMessage // Referência ao objeto original para acesso avançado
|
|
73
70
|
};
|
|
74
71
|
|
|
@@ -139,7 +136,7 @@ class MessageNormalizer {
|
|
|
139
136
|
isPtt: messageContent.ptt || false,
|
|
140
137
|
isGif: messageContent.gifPlayback || false,
|
|
141
138
|
isViewOnce: messageContent.viewOnce || false,
|
|
142
|
-
|
|
139
|
+
getAttachments: () => client.messages.getAttachments(rawMessage)
|
|
143
140
|
};
|
|
144
141
|
}
|
|
145
142
|
|