@areumtecnologia/baileys 1.0.1 → 1.0.3
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 +85 -18
- package/index.js +1 -1
- package/package.json +1 -1
- package/utils/message-normalizer.js +2 -5
package/handlers/messages.js
CHANGED
|
@@ -99,11 +99,12 @@ class MessageHandler {
|
|
|
99
99
|
}
|
|
100
100
|
|
|
101
101
|
/** Envia um documento. */
|
|
102
|
-
async sendDocument(jid, media, mimetype, fileName = '
|
|
102
|
+
async sendDocument(jid, media, mimetype, fileName = '', caption = '', options = {}) {
|
|
103
103
|
const content = {
|
|
104
104
|
document: typeof media === 'string' ? { url: media } : media,
|
|
105
105
|
mimetype,
|
|
106
|
-
fileName
|
|
106
|
+
fileName,
|
|
107
|
+
caption
|
|
107
108
|
};
|
|
108
109
|
return this.sendMessage(jid, content, options);
|
|
109
110
|
}
|
|
@@ -174,7 +175,6 @@ class MessageHandler {
|
|
|
174
175
|
* @param {object} [options={}] - Opções adicionais.
|
|
175
176
|
*/
|
|
176
177
|
async sendLink(jid, url, options = {}) {
|
|
177
|
-
|
|
178
178
|
return this.sendMessage(jid, { text: url }, options);
|
|
179
179
|
}
|
|
180
180
|
|
|
@@ -245,7 +245,7 @@ class MessageHandler {
|
|
|
245
245
|
return this.client.sock.readMessages([messageKey]);
|
|
246
246
|
}
|
|
247
247
|
|
|
248
|
-
/** Funcional apenas usando baileys e baileys_helper - Envia uma mensagem com botões interativos. */
|
|
248
|
+
/** Deprecated - Funcional apenas usando baileys e baileys_helper - Envia uma mensagem com botões interativos. */
|
|
249
249
|
async sendInteractiveMessage(jid, interactiveMessage, more) {
|
|
250
250
|
const verifiedJid = await this.client.users.isOnWhatsApp(jid);
|
|
251
251
|
if (verifiedJid && verifiedJid.exists) {
|
|
@@ -256,7 +256,7 @@ class MessageHandler {
|
|
|
256
256
|
}
|
|
257
257
|
}
|
|
258
258
|
|
|
259
|
-
/** Funcional apenas usando baileys e baileys_helper - Envia uma mensagem com botões interativos. */
|
|
259
|
+
/** Deprecated - Funcional apenas usando baileys e baileys_helper - Envia uma mensagem com botões interativos. */
|
|
260
260
|
async sendButtons(jid, messageButton) {
|
|
261
261
|
const verifiedJid = await this.client.users.isOnWhatsApp(jid);
|
|
262
262
|
if (verifiedJid && verifiedJid.exists) {
|
|
@@ -270,29 +270,96 @@ class MessageHandler {
|
|
|
270
270
|
/** Faz o download de mídia de uma mensagem. */
|
|
271
271
|
async getAttachments(message) {
|
|
272
272
|
this.client._validateConnection();
|
|
273
|
-
|
|
273
|
+
|
|
274
|
+
const type = Object.keys(message.message)[0]; // ex: imageMessage
|
|
274
275
|
const messageContent = message.message[type];
|
|
275
|
-
if (!messageContent.url)
|
|
276
|
-
return null;
|
|
277
276
|
|
|
278
|
-
|
|
277
|
+
if (!messageContent?.url) return null;
|
|
278
|
+
|
|
279
|
+
// baixa o stream
|
|
280
|
+
const stream = await this.client.downloadContentFromMessage(
|
|
281
|
+
messageContent,
|
|
282
|
+
type.replace('Message', '') // imageMessage → image
|
|
283
|
+
);
|
|
284
|
+
|
|
285
|
+
// monta o buffer
|
|
279
286
|
let buffer = Buffer.from([]);
|
|
280
287
|
for await (const chunk of stream) {
|
|
281
288
|
buffer = Buffer.concat([buffer, chunk]);
|
|
282
289
|
}
|
|
290
|
+
|
|
291
|
+
const mimetype = messageContent.mimetype;
|
|
292
|
+
const extension = mimetype.split('/')[1] || 'bin';
|
|
293
|
+
|
|
294
|
+
// conversões
|
|
295
|
+
const toBase64 = () => buffer.toString('base64');
|
|
296
|
+
|
|
297
|
+
const toDataUri = () =>
|
|
298
|
+
`data:${mimetype};base64,${buffer.toString('base64')}`;
|
|
299
|
+
|
|
300
|
+
const toArrayBuffer = () =>
|
|
301
|
+
buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength);
|
|
302
|
+
|
|
303
|
+
const toBlob = () => {
|
|
304
|
+
const arrayBuffer = toArrayBuffer();
|
|
305
|
+
return new Blob([arrayBuffer], { type: mimetype });
|
|
306
|
+
};
|
|
307
|
+
|
|
308
|
+
const toImageUrl = () => {
|
|
309
|
+
const blob = toBlob();
|
|
310
|
+
return URL.createObjectURL(blob);
|
|
311
|
+
};
|
|
312
|
+
|
|
313
|
+
const toImageBitmap = async () => {
|
|
314
|
+
const blob = toBlob();
|
|
315
|
+
return await createImageBitmap(blob);
|
|
316
|
+
};
|
|
317
|
+
|
|
318
|
+
const toStream = () => {
|
|
319
|
+
const { Readable } = require('stream');
|
|
320
|
+
return Readable.from(buffer);
|
|
321
|
+
};
|
|
322
|
+
|
|
323
|
+
const toSharp = () => {
|
|
324
|
+
const sharp = require('sharp');
|
|
325
|
+
return sharp(buffer); // o usuário pode continuar com .resize().png()...
|
|
326
|
+
};
|
|
327
|
+
|
|
328
|
+
const detectType = async () => {
|
|
329
|
+
const { fileTypeFromBuffer } = await import('file-type');
|
|
330
|
+
return await fileTypeFromBuffer(buffer);
|
|
331
|
+
};
|
|
332
|
+
|
|
333
|
+
const save = async (path) => {
|
|
334
|
+
const filename = `${message.from}-${Date.now()}.${extension}`;
|
|
335
|
+
const filepath = path
|
|
336
|
+
? path
|
|
337
|
+
: `${this.client.sessionPath}/media/${filename}`;
|
|
338
|
+
|
|
339
|
+
await fs.writeFile(filepath, buffer);
|
|
340
|
+
|
|
341
|
+
return { filename, filepath };
|
|
342
|
+
};
|
|
343
|
+
|
|
283
344
|
return {
|
|
284
|
-
|
|
345
|
+
mimetype,
|
|
346
|
+
extension,
|
|
285
347
|
buffer,
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
348
|
+
|
|
349
|
+
// 🔥 todas as funções utilitárias
|
|
350
|
+
toBase64,
|
|
351
|
+
toDataUri,
|
|
352
|
+
toArrayBuffer,
|
|
353
|
+
toBlob,
|
|
354
|
+
toImageUrl,
|
|
355
|
+
toImageBitmap,
|
|
356
|
+
toStream,
|
|
357
|
+
toSharp,
|
|
358
|
+
detectType,
|
|
359
|
+
save,
|
|
294
360
|
};
|
|
295
361
|
}
|
|
362
|
+
|
|
296
363
|
}
|
|
297
364
|
|
|
298
365
|
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
|
|