@arcaelas/whatsapp 1.2.3 → 1.4.0
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/build/{Chat.js → cjs/Chat.js} +1 -1
- package/build/cjs/Chat.js.map +1 -0
- package/build/{Contact.js → cjs/Contact.js} +6 -5
- package/build/cjs/Contact.js.map +1 -0
- package/build/{Message.js → cjs/Message.js} +5 -3
- package/build/cjs/Message.js.map +1 -0
- package/build/{WhatsApp.js → cjs/WhatsApp.js} +11 -6
- package/build/cjs/WhatsApp.js.map +1 -0
- package/build/cjs/index.js +17 -0
- package/build/cjs/index.js.map +1 -0
- package/build/cjs/package.json +1 -0
- package/build/{store → cjs/store}/driver/FileEngine.d.ts +1 -1
- package/build/{store → cjs/store}/driver/FileEngine.js +2 -2
- package/build/cjs/store/driver/FileEngine.js.map +1 -0
- package/build/{store → cjs/store}/driver/RedisEngine.d.ts +1 -1
- package/build/cjs/store/driver/RedisEngine.js.map +1 -0
- package/build/cjs/store/engine.js.map +1 -0
- package/build/cjs/store/index.d.ts +8 -0
- package/build/{store → cjs/store}/index.js +2 -2
- package/build/cjs/store/index.js.map +1 -0
- package/build/esm/Chat.d.ts +335 -0
- package/build/esm/Chat.js +390 -0
- package/build/esm/Chat.js.map +1 -0
- package/build/esm/Contact.d.ts +856 -0
- package/build/esm/Contact.js +183 -0
- package/build/esm/Contact.js.map +1 -0
- package/build/esm/Message.d.ts +580 -0
- package/build/esm/Message.js +459 -0
- package/build/esm/Message.js.map +1 -0
- package/build/esm/WhatsApp.d.ts +68 -0
- package/build/esm/WhatsApp.js +364 -0
- package/build/esm/WhatsApp.js.map +1 -0
- package/build/esm/index.d.ts +13 -0
- package/build/esm/index.js +10 -0
- package/build/esm/index.js.map +1 -0
- package/build/esm/package.json +1 -0
- package/build/esm/store/driver/FileEngine.d.ts +23 -0
- package/build/esm/store/driver/FileEngine.js +86 -0
- package/build/esm/store/driver/FileEngine.js.map +1 -0
- package/build/esm/store/driver/RedisEngine.d.ts +38 -0
- package/build/esm/store/driver/RedisEngine.js +65 -0
- package/build/esm/store/driver/RedisEngine.js.map +1 -0
- package/build/esm/store/engine.d.ts +54 -0
- package/build/esm/store/engine.js +6 -0
- package/build/{store → esm/store}/engine.js.map +1 -1
- package/build/esm/store/index.d.ts +8 -0
- package/build/esm/store/index.js +7 -0
- package/build/esm/store/index.js.map +1 -0
- package/package.json +70 -58
- package/API.md +0 -776
- package/CHANGELOG.md +0 -53
- package/DOC.md +0 -532
- package/build/Chat.js.map +0 -1
- package/build/Contact.js.map +0 -1
- package/build/Message.js.map +0 -1
- package/build/WhatsApp.js.map +0 -1
- package/build/index.js +0 -2
- package/build/index.js.map +0 -7
- package/build/store/driver/FileEngine.js.map +0 -1
- package/build/store/driver/RedisEngine.js.map +0 -1
- package/build/store/index.d.ts +0 -8
- package/build/store/index.js.map +0 -1
- package/context7.json +0 -4
- package/tsconfig.json +0 -27
- /package/build/{Chat.d.ts → cjs/Chat.d.ts} +0 -0
- /package/build/{Contact.d.ts → cjs/Contact.d.ts} +0 -0
- /package/build/{Message.d.ts → cjs/Message.d.ts} +0 -0
- /package/build/{WhatsApp.d.ts → cjs/WhatsApp.d.ts} +0 -0
- /package/build/{index.d.ts → cjs/index.d.ts} +0 -0
- /package/build/{store → cjs/store}/driver/RedisEngine.js +0 -0
- /package/build/{store → cjs/store}/engine.d.ts +0 -0
- /package/build/{store → cjs/store}/engine.js +0 -0
|
@@ -0,0 +1,459 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Message.ts
|
|
3
|
+
* @description Clase Message para gestión de mensajes de WhatsApp
|
|
4
|
+
*/
|
|
5
|
+
import { BufferJSON, downloadMediaMessage, generateForwardMessageContent, generateWAMessageFromContent, getContentType } from 'baileys';
|
|
6
|
+
import { Readable } from 'node:stream';
|
|
7
|
+
/**
|
|
8
|
+
* @description Estados de un mensaje.
|
|
9
|
+
*/
|
|
10
|
+
export var MESSAGE_STATUS;
|
|
11
|
+
(function (MESSAGE_STATUS) {
|
|
12
|
+
/** Error al enviar */
|
|
13
|
+
MESSAGE_STATUS[MESSAGE_STATUS["ERROR"] = 0] = "ERROR";
|
|
14
|
+
/** Mensaje pendiente de envío */
|
|
15
|
+
MESSAGE_STATUS[MESSAGE_STATUS["PENDING"] = 1] = "PENDING";
|
|
16
|
+
/** Servidor recibió el mensaje */
|
|
17
|
+
MESSAGE_STATUS[MESSAGE_STATUS["SERVER_ACK"] = 2] = "SERVER_ACK";
|
|
18
|
+
/** Mensaje entregado al destinatario */
|
|
19
|
+
MESSAGE_STATUS[MESSAGE_STATUS["DELIVERED"] = 3] = "DELIVERED";
|
|
20
|
+
/** Mensaje leído por el destinatario */
|
|
21
|
+
MESSAGE_STATUS[MESSAGE_STATUS["READ"] = 4] = "READ";
|
|
22
|
+
/** Mensaje reproducido (audio/video) */
|
|
23
|
+
MESSAGE_STATUS[MESSAGE_STATUS["PLAYED"] = 5] = "PLAYED";
|
|
24
|
+
})(MESSAGE_STATUS || (MESSAGE_STATUS = {}));
|
|
25
|
+
/**
|
|
26
|
+
* @description Mapeo de tipos de contenido de Baileys a MessageType.
|
|
27
|
+
*/
|
|
28
|
+
export const MESSAGE_TYPE_MAP = {
|
|
29
|
+
conversation: 'text',
|
|
30
|
+
extendedTextMessage: 'text',
|
|
31
|
+
imageMessage: 'image',
|
|
32
|
+
videoMessage: 'video',
|
|
33
|
+
audioMessage: 'audio',
|
|
34
|
+
locationMessage: 'location',
|
|
35
|
+
liveLocationMessage: 'location',
|
|
36
|
+
pollCreationMessage: 'poll',
|
|
37
|
+
pollCreationMessageV2: 'poll',
|
|
38
|
+
pollCreationMessageV3: 'poll',
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* @description Construye IMessageIndex desde WAMessage.
|
|
42
|
+
* @param raw WAMessage del protocolo.
|
|
43
|
+
* @param edited Si el mensaje fue editado.
|
|
44
|
+
* @returns IMessageIndex normalizado.
|
|
45
|
+
*/
|
|
46
|
+
export function build_message_index(raw, edited = false) {
|
|
47
|
+
const content_type = getContentType(raw.message ?? {});
|
|
48
|
+
const msg_type = MESSAGE_TYPE_MAP[content_type ?? ''] ?? 'text';
|
|
49
|
+
const msg_content = raw.message?.[content_type];
|
|
50
|
+
const context_info = msg_content?.contextInfo;
|
|
51
|
+
// Calcular MIME
|
|
52
|
+
let mime = 'text/plain';
|
|
53
|
+
if (msg_type === 'location' || msg_type === 'poll') {
|
|
54
|
+
mime = 'application/json';
|
|
55
|
+
}
|
|
56
|
+
else if (msg_type !== 'text' && typeof msg_content === 'object') {
|
|
57
|
+
mime = msg_content?.mimetype ?? 'application/octet-stream';
|
|
58
|
+
}
|
|
59
|
+
// Calcular caption
|
|
60
|
+
let caption = '';
|
|
61
|
+
if (typeof msg_content === 'string') {
|
|
62
|
+
caption = msg_content;
|
|
63
|
+
}
|
|
64
|
+
else if (msg_content) {
|
|
65
|
+
caption = msg_content.caption ?? msg_content.text ?? msg_content.name ?? '';
|
|
66
|
+
}
|
|
67
|
+
// Calcular deleted_at
|
|
68
|
+
const ephemeral_duration = raw.ephemeralDuration ?? context_info?.expiration;
|
|
69
|
+
const deleted_at = raw.ephemeralStartTimestamp && ephemeral_duration ? (Number(raw.ephemeralStartTimestamp) + ephemeral_duration) * 1000 : null;
|
|
70
|
+
return {
|
|
71
|
+
id: raw.key.id,
|
|
72
|
+
cid: raw.key.remoteJid,
|
|
73
|
+
mid: context_info?.stanzaId ?? null,
|
|
74
|
+
me: raw.key.fromMe ?? false,
|
|
75
|
+
type: msg_type,
|
|
76
|
+
author: raw.key.participant ?? raw.key.remoteJid,
|
|
77
|
+
status: raw.status ?? MESSAGE_STATUS.PENDING,
|
|
78
|
+
starred: raw.starred ?? false,
|
|
79
|
+
forwarded: context_info?.isForwarded ?? false,
|
|
80
|
+
created_at: (Number(raw.messageTimestamp) || Math.floor(Date.now() / 1000)) * 1000,
|
|
81
|
+
deleted_at,
|
|
82
|
+
mime,
|
|
83
|
+
caption,
|
|
84
|
+
edited,
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* @description
|
|
89
|
+
* Clase base para representar un mensaje de WhatsApp.
|
|
90
|
+
* Expone getters sobre el índice, raw accesible para operaciones avanzadas.
|
|
91
|
+
*/
|
|
92
|
+
export class Message {
|
|
93
|
+
constructor(data) {
|
|
94
|
+
this.index = data.index;
|
|
95
|
+
this.raw = data.raw;
|
|
96
|
+
}
|
|
97
|
+
/** ID único del mensaje */
|
|
98
|
+
get id() {
|
|
99
|
+
return this.index.id;
|
|
100
|
+
}
|
|
101
|
+
/** JID del chat al que pertenece */
|
|
102
|
+
get cid() {
|
|
103
|
+
return this.index.cid;
|
|
104
|
+
}
|
|
105
|
+
/** ID del mensaje padre (reply) */
|
|
106
|
+
get mid() {
|
|
107
|
+
return this.index.mid;
|
|
108
|
+
}
|
|
109
|
+
/** true si el mensaje fue enviado por el usuario autenticado */
|
|
110
|
+
get me() {
|
|
111
|
+
return this.index.me;
|
|
112
|
+
}
|
|
113
|
+
/** Autor del mensaje */
|
|
114
|
+
get author() {
|
|
115
|
+
return this.index.author;
|
|
116
|
+
}
|
|
117
|
+
/** true si el mensaje fue editado */
|
|
118
|
+
get edited() {
|
|
119
|
+
return this.index.edited;
|
|
120
|
+
}
|
|
121
|
+
/** Tipo de contenido del mensaje */
|
|
122
|
+
get type() {
|
|
123
|
+
return this.index.type;
|
|
124
|
+
}
|
|
125
|
+
/** Texto o caption del mensaje */
|
|
126
|
+
get caption() {
|
|
127
|
+
return this.index.caption;
|
|
128
|
+
}
|
|
129
|
+
/** Tipo MIME del contenido */
|
|
130
|
+
get mime() {
|
|
131
|
+
return this.index.mime;
|
|
132
|
+
}
|
|
133
|
+
/** Estado del mensaje */
|
|
134
|
+
get status() {
|
|
135
|
+
return this.index.status;
|
|
136
|
+
}
|
|
137
|
+
/** true si el mensaje está destacado */
|
|
138
|
+
get starred() {
|
|
139
|
+
return this.index.starred;
|
|
140
|
+
}
|
|
141
|
+
/** true si el mensaje fue reenviado */
|
|
142
|
+
get forwarded() {
|
|
143
|
+
return this.index.forwarded;
|
|
144
|
+
}
|
|
145
|
+
/** Timestamp de creación (ms) */
|
|
146
|
+
get created_at() {
|
|
147
|
+
return this.index.created_at;
|
|
148
|
+
}
|
|
149
|
+
/** Timestamp de expiración (ms) o null */
|
|
150
|
+
get deleted_at() {
|
|
151
|
+
return this.index.deleted_at;
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* @description Obtiene el contenido como Readable stream.
|
|
155
|
+
* @returns Readable stream (vacío en clase base).
|
|
156
|
+
*/
|
|
157
|
+
stream() {
|
|
158
|
+
return Promise.resolve(Readable.from(Buffer.alloc(0)));
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* @description Obtiene el contenido del mensaje como Buffer.
|
|
162
|
+
* @returns Buffer con el contenido (vacío en clase base).
|
|
163
|
+
*/
|
|
164
|
+
content() {
|
|
165
|
+
return Promise.resolve(Buffer.alloc(0));
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* @description Factory que retorna la clase Message enlazada al contexto.
|
|
170
|
+
* @param wa Instancia de WhatsApp.
|
|
171
|
+
* @returns Clase _Message que extiende Message.
|
|
172
|
+
*/
|
|
173
|
+
export function message(wa) {
|
|
174
|
+
/** Subscribers para watch() */
|
|
175
|
+
const _watchers = new Map();
|
|
176
|
+
/** Función interna para envío de mensajes */
|
|
177
|
+
async function _send(cid, content, binary) {
|
|
178
|
+
if (!wa.socket)
|
|
179
|
+
return null;
|
|
180
|
+
const raw = await wa.socket.sendMessage(cid, content);
|
|
181
|
+
if (!raw?.key?.id)
|
|
182
|
+
return null;
|
|
183
|
+
const index = build_message_index(raw);
|
|
184
|
+
await wa.engine.set(`chat/${cid}/message/${raw.key.id}/index`, JSON.stringify(index, BufferJSON.replacer));
|
|
185
|
+
await wa.engine.set(`chat/${cid}/message/${raw.key.id}/raw`, JSON.stringify(raw, BufferJSON.replacer));
|
|
186
|
+
if (binary)
|
|
187
|
+
await wa.engine.set(`chat/${cid}/message/${raw.key.id}/content`, binary.toString('base64'));
|
|
188
|
+
await wa.Chat.add_message(cid, raw.key.id, index.created_at);
|
|
189
|
+
return new _Message({ index, raw });
|
|
190
|
+
}
|
|
191
|
+
const _Message = class extends Message {
|
|
192
|
+
/**
|
|
193
|
+
* @description Obtiene un mensaje por CID y MID.
|
|
194
|
+
* @param cid ID del chat.
|
|
195
|
+
* @param mid ID del mensaje.
|
|
196
|
+
* @returns Mensaje o null si no existe.
|
|
197
|
+
*/
|
|
198
|
+
static async get(cid, mid) {
|
|
199
|
+
const stored_index = await wa.engine.get(`chat/${cid}/message/${mid}/index`);
|
|
200
|
+
if (!stored_index)
|
|
201
|
+
return null;
|
|
202
|
+
const index = JSON.parse(stored_index, BufferJSON.reviver);
|
|
203
|
+
// Obtener raw si existe
|
|
204
|
+
const stored_raw = await wa.engine.get(`chat/${cid}/message/${mid}/raw`);
|
|
205
|
+
const raw = stored_raw ? JSON.parse(stored_raw, BufferJSON.reviver) : { key: { remoteJid: cid, id: mid, fromMe: index.me } };
|
|
206
|
+
return new _Message({ index, raw });
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* @description Reacciona a un mensaje.
|
|
210
|
+
* @param cid ID del chat.
|
|
211
|
+
* @param mid ID del mensaje.
|
|
212
|
+
* @param emoji Emoji de reacción (vacío para quitar).
|
|
213
|
+
* @returns true si se envió correctamente.
|
|
214
|
+
*/
|
|
215
|
+
static async react(cid, mid, emoji) {
|
|
216
|
+
if (!wa.socket)
|
|
217
|
+
return false;
|
|
218
|
+
const msg = await _Message.get(cid, mid);
|
|
219
|
+
if (!msg)
|
|
220
|
+
return false;
|
|
221
|
+
await wa.socket.sendMessage(cid, {
|
|
222
|
+
react: { text: emoji, key: { remoteJid: cid, id: mid, fromMe: msg.me } },
|
|
223
|
+
});
|
|
224
|
+
return true;
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* @description Elimina un mensaje para todos.
|
|
228
|
+
* @param cid ID del chat.
|
|
229
|
+
* @param mid ID del mensaje.
|
|
230
|
+
* @returns true si se eliminó correctamente.
|
|
231
|
+
*/
|
|
232
|
+
static async remove(cid, mid) {
|
|
233
|
+
if (!wa.socket)
|
|
234
|
+
return false;
|
|
235
|
+
const msg = await _Message.get(cid, mid);
|
|
236
|
+
if (!msg)
|
|
237
|
+
return false;
|
|
238
|
+
await wa.socket.sendMessage(cid, { delete: { remoteJid: cid, id: mid, fromMe: msg.me } });
|
|
239
|
+
// Eliminar del índice del chat
|
|
240
|
+
await wa.Chat.remove_message(cid, mid);
|
|
241
|
+
// Eliminar archivos
|
|
242
|
+
await wa.engine.set(`chat/${cid}/message/${mid}/index`, null);
|
|
243
|
+
await wa.engine.set(`chat/${cid}/message/${mid}/content`, null);
|
|
244
|
+
await wa.engine.set(`chat/${cid}/message/${mid}/raw`, null);
|
|
245
|
+
return true;
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* @description Reenvía un mensaje a otro chat.
|
|
249
|
+
* @param cid ID del chat origen.
|
|
250
|
+
* @param mid ID del mensaje.
|
|
251
|
+
* @param to_cid ID del chat destino.
|
|
252
|
+
* @returns true si se reenvió correctamente.
|
|
253
|
+
*/
|
|
254
|
+
static async forward(cid, mid, to_cid) {
|
|
255
|
+
if (!wa.socket)
|
|
256
|
+
return false;
|
|
257
|
+
const msg = await _Message.get(cid, mid);
|
|
258
|
+
if (!msg)
|
|
259
|
+
return false;
|
|
260
|
+
// Forward nativo con raw
|
|
261
|
+
if (msg.raw.message) {
|
|
262
|
+
try {
|
|
263
|
+
const wa_msg = generateWAMessageFromContent(to_cid, generateForwardMessageContent(msg.raw, false), { userJid: wa.socket.user?.id ?? '' });
|
|
264
|
+
await wa.socket.relayMessage(to_cid, wa_msg.message, { messageId: wa_msg.key.id });
|
|
265
|
+
return true;
|
|
266
|
+
}
|
|
267
|
+
catch {
|
|
268
|
+
// Fallback si falla el forward nativo
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
// Fallback: obtener contenido y reenviar
|
|
272
|
+
const content = await msg.content();
|
|
273
|
+
if (!content.length)
|
|
274
|
+
return false;
|
|
275
|
+
if (msg.type === 'text')
|
|
276
|
+
return (await _Message.text(to_cid, content.toString())) !== null;
|
|
277
|
+
if (msg.type === 'image')
|
|
278
|
+
return (await _Message.image(to_cid, content, msg.caption || undefined)) !== null;
|
|
279
|
+
if (msg.type === 'video')
|
|
280
|
+
return (await _Message.video(to_cid, content, msg.caption || undefined)) !== null;
|
|
281
|
+
if (msg.type === 'audio')
|
|
282
|
+
return (await _Message.audio(to_cid, content)) !== null;
|
|
283
|
+
if (msg.type === 'location') {
|
|
284
|
+
try {
|
|
285
|
+
return (await _Message.location(to_cid, JSON.parse(content.toString()))) !== null;
|
|
286
|
+
}
|
|
287
|
+
catch {
|
|
288
|
+
return false;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
return false;
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* @description Edita un mensaje (solo texto o caption).
|
|
295
|
+
* @param cid ID del chat.
|
|
296
|
+
* @param mid ID del mensaje.
|
|
297
|
+
* @param text Nuevo contenido.
|
|
298
|
+
* @returns true si se editó correctamente.
|
|
299
|
+
*/
|
|
300
|
+
static async edit(cid, mid, text) {
|
|
301
|
+
if (!wa.socket)
|
|
302
|
+
return false;
|
|
303
|
+
const msg = await _Message.get(cid, mid);
|
|
304
|
+
if (!msg?.me)
|
|
305
|
+
return false;
|
|
306
|
+
await wa.socket.sendMessage(cid, {
|
|
307
|
+
text,
|
|
308
|
+
edit: { remoteJid: cid, id: mid, fromMe: true },
|
|
309
|
+
});
|
|
310
|
+
// Actualizar el mensaje almacenado con el nuevo contenido
|
|
311
|
+
const content_type = getContentType(msg.raw.message ?? {});
|
|
312
|
+
if (content_type === 'conversation') {
|
|
313
|
+
msg.raw.message = { conversation: text };
|
|
314
|
+
}
|
|
315
|
+
else if (content_type === 'extendedTextMessage') {
|
|
316
|
+
msg.raw.message = { extendedTextMessage: { text } };
|
|
317
|
+
}
|
|
318
|
+
// Actualizar índice
|
|
319
|
+
msg.index.caption = text;
|
|
320
|
+
msg.index.edited = true;
|
|
321
|
+
await wa.engine.set(`chat/${cid}/message/${mid}/index`, JSON.stringify(msg.index, BufferJSON.replacer));
|
|
322
|
+
await wa.engine.set(`chat/${cid}/message/${mid}/raw`, JSON.stringify(msg.raw, BufferJSON.replacer));
|
|
323
|
+
return true;
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* @description Envía un mensaje de texto.
|
|
327
|
+
* @param cid ID del chat destino.
|
|
328
|
+
* @param text Contenido del mensaje.
|
|
329
|
+
* @returns Mensaje enviado o null.
|
|
330
|
+
*/
|
|
331
|
+
static async text(cid, text) {
|
|
332
|
+
return _send(cid, { text });
|
|
333
|
+
}
|
|
334
|
+
/**
|
|
335
|
+
* @description Envía una imagen.
|
|
336
|
+
* @param cid ID del chat destino.
|
|
337
|
+
* @param buffer Buffer de la imagen.
|
|
338
|
+
* @param caption Caption opcional.
|
|
339
|
+
* @returns Mensaje enviado o null.
|
|
340
|
+
*/
|
|
341
|
+
static async image(cid, buffer, caption) {
|
|
342
|
+
return _send(cid, { image: buffer, caption }, buffer);
|
|
343
|
+
}
|
|
344
|
+
/**
|
|
345
|
+
* @description Envía un video.
|
|
346
|
+
* @param cid ID del chat destino.
|
|
347
|
+
* @param buffer Buffer del video.
|
|
348
|
+
* @param caption Caption opcional.
|
|
349
|
+
* @returns Mensaje enviado o null.
|
|
350
|
+
*/
|
|
351
|
+
static async video(cid, buffer, caption) {
|
|
352
|
+
return _send(cid, { video: buffer, caption }, buffer);
|
|
353
|
+
}
|
|
354
|
+
/**
|
|
355
|
+
* @description Envía un audio.
|
|
356
|
+
* @param cid ID del chat destino.
|
|
357
|
+
* @param buffer Buffer del audio.
|
|
358
|
+
* @param ptt true para nota de voz (default true).
|
|
359
|
+
* @returns Mensaje enviado o null.
|
|
360
|
+
*/
|
|
361
|
+
static async audio(cid, buffer, ptt = true) {
|
|
362
|
+
return _send(cid, { audio: buffer, ptt }, buffer);
|
|
363
|
+
}
|
|
364
|
+
/**
|
|
365
|
+
* @description Envía una ubicación.
|
|
366
|
+
* @param cid ID del chat destino.
|
|
367
|
+
* @param opts Opciones de ubicación (lat, lng, live).
|
|
368
|
+
* @returns Mensaje enviado o null.
|
|
369
|
+
*/
|
|
370
|
+
static async location(cid, opts) {
|
|
371
|
+
return _send(cid, { location: { degreesLatitude: opts.lat, degreesLongitude: opts.lng } });
|
|
372
|
+
}
|
|
373
|
+
/**
|
|
374
|
+
* @description Envía una encuesta.
|
|
375
|
+
* @param cid ID del chat destino.
|
|
376
|
+
* @param opts Opciones de encuesta (content, options).
|
|
377
|
+
* @returns Mensaje enviado o null.
|
|
378
|
+
*/
|
|
379
|
+
static async poll(cid, opts) {
|
|
380
|
+
return _send(cid, { poll: { name: opts.content, values: opts.options.map((o) => o.content), selectableCount: 1 } });
|
|
381
|
+
}
|
|
382
|
+
/**
|
|
383
|
+
* @description Observa cambios en un mensaje específico.
|
|
384
|
+
* @param cid ID del chat.
|
|
385
|
+
* @param mid ID del mensaje.
|
|
386
|
+
* @param handler Callback con el mensaje actualizado.
|
|
387
|
+
* @returns Función para dejar de observar.
|
|
388
|
+
*/
|
|
389
|
+
static watch(cid, mid, handler) {
|
|
390
|
+
const key = `${cid}:${mid}`;
|
|
391
|
+
if (!_watchers.has(key))
|
|
392
|
+
_watchers.set(key, new Set());
|
|
393
|
+
_watchers.get(key).add(handler);
|
|
394
|
+
return () => {
|
|
395
|
+
_watchers.get(key)?.delete(handler);
|
|
396
|
+
if (_watchers.get(key)?.size === 0)
|
|
397
|
+
_watchers.delete(key);
|
|
398
|
+
};
|
|
399
|
+
}
|
|
400
|
+
/**
|
|
401
|
+
* @description Notifica a los watchers de un mensaje.
|
|
402
|
+
* @param cid ID del chat.
|
|
403
|
+
* @param mid ID del mensaje.
|
|
404
|
+
*/
|
|
405
|
+
static notify(cid, mid) {
|
|
406
|
+
const handlers = _watchers.get(`${cid}:${mid}`);
|
|
407
|
+
handlers?.forEach((h) => _Message.get(cid, mid).then((msg) => msg && h(msg)));
|
|
408
|
+
}
|
|
409
|
+
/**
|
|
410
|
+
* @description Obtiene el contenido como Readable stream.
|
|
411
|
+
* Para media (image/video/audio) retorna stream directo de Baileys sin cargar en memoria.
|
|
412
|
+
* Para text/location/poll retorna stream desde buffer pequeño.
|
|
413
|
+
* @returns Readable stream con el contenido.
|
|
414
|
+
*/
|
|
415
|
+
async stream() {
|
|
416
|
+
if (this.type === 'text') {
|
|
417
|
+
return Readable.from(Buffer.from(this.caption, 'utf-8'));
|
|
418
|
+
}
|
|
419
|
+
if (this.type === 'location') {
|
|
420
|
+
const loc = this.raw.message?.locationMessage ?? this.raw.message?.liveLocationMessage;
|
|
421
|
+
return Readable.from(Buffer.from(JSON.stringify({ lat: loc?.degreesLatitude, lng: loc?.degreesLongitude }), 'utf-8'));
|
|
422
|
+
}
|
|
423
|
+
if (this.type === 'poll') {
|
|
424
|
+
const poll = this.raw.message?.pollCreationMessage ?? this.raw.message?.pollCreationMessageV2 ?? this.raw.message?.pollCreationMessageV3;
|
|
425
|
+
return Readable.from(Buffer.from(JSON.stringify({ content: poll?.name ?? '', options: poll?.options?.map((o) => ({ content: o.optionName })) ?? [] }), 'utf-8'));
|
|
426
|
+
}
|
|
427
|
+
if (wa.socket) {
|
|
428
|
+
try {
|
|
429
|
+
return await downloadMediaMessage(this.raw, 'stream', {});
|
|
430
|
+
}
|
|
431
|
+
catch {
|
|
432
|
+
return Readable.from(Buffer.alloc(0));
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
return Readable.from(Buffer.alloc(0));
|
|
436
|
+
}
|
|
437
|
+
/**
|
|
438
|
+
* @description Obtiene el contenido del mensaje como Buffer.
|
|
439
|
+
* Usa stream() internamente y cachea el resultado en el engine.
|
|
440
|
+
* @returns Buffer con el contenido.
|
|
441
|
+
*/
|
|
442
|
+
async content() {
|
|
443
|
+
const cached = await wa.engine.get(`chat/${this.cid}/message/${this.id}/content`);
|
|
444
|
+
if (cached)
|
|
445
|
+
return Buffer.from(cached, 'base64');
|
|
446
|
+
const readable = await this.stream();
|
|
447
|
+
const chunks = [];
|
|
448
|
+
for await (const chunk of readable) {
|
|
449
|
+
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
|
|
450
|
+
}
|
|
451
|
+
const buffer = Buffer.concat(chunks);
|
|
452
|
+
if (buffer.length)
|
|
453
|
+
await wa.engine.set(`chat/${this.cid}/message/${this.id}/content`, buffer.toString('base64'));
|
|
454
|
+
return buffer;
|
|
455
|
+
}
|
|
456
|
+
};
|
|
457
|
+
return _Message;
|
|
458
|
+
}
|
|
459
|
+
//# sourceMappingURL=Message.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Message.js","sourceRoot":"","sources":["../../src/Message.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,EAAE,UAAU,EAAE,oBAAoB,EAAE,6BAA6B,EAAE,4BAA4B,EAAE,cAAc,EAAS,MAAM,SAAS,CAAC;AAC/I,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAQvC;;GAEG;AACH,MAAM,CAAN,IAAY,cAaX;AAbD,WAAY,cAAc;IACtB,sBAAsB;IACtB,qDAAS,CAAA;IACT,iCAAiC;IACjC,yDAAW,CAAA;IACX,kCAAkC;IAClC,+DAAc,CAAA;IACd,wCAAwC;IACxC,6DAAa,CAAA;IACb,wCAAwC;IACxC,mDAAQ,CAAA;IACR,wCAAwC;IACxC,uDAAU,CAAA;AACd,CAAC,EAbW,cAAc,KAAd,cAAc,QAazB;AAoED;;GAEG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAgC;IACzD,YAAY,EAAE,MAAM;IACpB,mBAAmB,EAAE,MAAM;IAC3B,YAAY,EAAE,OAAO;IACrB,YAAY,EAAE,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AG,YAAY,IAAI,CAAC,EAAE,UAAU,CAAC,CAAC;YAClF,IAAI,MAAM;gBAAE,OAAO,MAAM,CAAC,IAAI,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;YAEjD,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,MAAM,EAAE,CAAC;YACrC,MAAM,MAAM,GAAa,EAAE,CAAC;YAC5B,IAAI,KAAK,EAAE,MAAM,KAAK,IAAI,QAAQ,EAAE,CAAC;gBACjC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC;YACrE,CAAC;YACD,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;YAErC,IAAI,MAAM,CAAC,MAAM;gBAAE,MAAM,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,QAAQ,IAAI,CAAC,GAAG,YAAY,IAAI,CAAC,EAAE,UAAU,EAAE,MAAM,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC,CAAC;YACjH,OAAO,MAAM,CAAC;QAClB,CAAC;KACJ,CAAC;IAEF,OAAO,QAAQ,CAAC;AACpB,CAAC"}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file WhatsApp.ts
|
|
3
|
+
* @description Clase principal que gestiona la conexión y eventos de WhatsApp
|
|
4
|
+
*/
|
|
5
|
+
import { type WASocket } from 'baileys';
|
|
6
|
+
import { EventEmitter } from 'node:events';
|
|
7
|
+
import { chat } from './Chat';
|
|
8
|
+
import { contact } from './Contact';
|
|
9
|
+
import { message } from './Message';
|
|
10
|
+
import { type Engine } from './store';
|
|
11
|
+
/**
|
|
12
|
+
* @description Opciones de configuración de WhatsApp.
|
|
13
|
+
*/
|
|
14
|
+
export interface IWhatsApp {
|
|
15
|
+
/** Motor de almacenamiento personalizado */
|
|
16
|
+
engine?: Engine;
|
|
17
|
+
/** Número de teléfono para emparejamiento con código */
|
|
18
|
+
phone?: string | number;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* @description Mapa de eventos emitidos por WhatsApp.
|
|
22
|
+
*/
|
|
23
|
+
interface WhatsAppEventMap {
|
|
24
|
+
open: [];
|
|
25
|
+
close: [];
|
|
26
|
+
error: [Error];
|
|
27
|
+
'message:created': [InstanceType<ReturnType<typeof message>>];
|
|
28
|
+
'message:updated': [InstanceType<ReturnType<typeof message>>];
|
|
29
|
+
'message:reacted': [cid: string, mid: string, emoji: string];
|
|
30
|
+
'message:deleted': [cid: string, mid: string];
|
|
31
|
+
'chat:created': [InstanceType<ReturnType<typeof chat>>];
|
|
32
|
+
'chat:updated': [InstanceType<ReturnType<typeof chat>>];
|
|
33
|
+
'chat:pined': [cid: string, pined: number | null];
|
|
34
|
+
'chat:archived': [cid: string, archived: boolean];
|
|
35
|
+
'chat:muted': [cid: string, muted: number | null];
|
|
36
|
+
'chat:deleted': [cid: string];
|
|
37
|
+
'contact:created': [InstanceType<ReturnType<typeof contact>>];
|
|
38
|
+
'contact:updated': [InstanceType<ReturnType<typeof contact>>];
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* @description
|
|
42
|
+
* Clase principal para gestionar la conexión con WhatsApp.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* const { event, pair, Contact, Chat, Message } = new WhatsApp({ phone: '5491112345678' });
|
|
46
|
+
* event.on('open', () => console.log('Conectado'));
|
|
47
|
+
* await pair((code) => console.log('Código:', code));
|
|
48
|
+
*/
|
|
49
|
+
export declare class WhatsApp {
|
|
50
|
+
private readonly _phone?;
|
|
51
|
+
private _socket;
|
|
52
|
+
readonly engine: Engine;
|
|
53
|
+
readonly event: EventEmitter<WhatsAppEventMap>;
|
|
54
|
+
readonly Contact: ReturnType<typeof contact>;
|
|
55
|
+
readonly Chat: ReturnType<typeof chat>;
|
|
56
|
+
readonly Message: ReturnType<typeof message>;
|
|
57
|
+
constructor(options?: IWhatsApp);
|
|
58
|
+
/** @description Retorna el socket de Baileys (null si no está conectado). */
|
|
59
|
+
get socket(): WASocket | null;
|
|
60
|
+
/**
|
|
61
|
+
* @description Conecta a WhatsApp.
|
|
62
|
+
* - Con phone: callback recibe código de 8 dígitos (una vez)
|
|
63
|
+
* - Sin phone: callback recibe QR como Buffer PNG (periódicamente)
|
|
64
|
+
* @param callback Función que recibe el código o QR.
|
|
65
|
+
*/
|
|
66
|
+
pair: (callback: (code: string | Buffer) => void | Promise<void>) => Promise<void>;
|
|
67
|
+
}
|
|
68
|
+
export {};
|