@arcaelas/whatsapp 1.4.0 → 2.0.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/cjs/Chat.d.ts +187 -205
- package/build/cjs/Chat.js +151 -274
- package/build/cjs/Chat.js.map +1 -1
- package/build/cjs/Contact.d.ts +317 -304
- package/build/cjs/Contact.js +58 -103
- package/build/cjs/Contact.js.map +1 -1
- package/build/cjs/Message.d.ts +567 -265
- package/build/cjs/Message.js +274 -256
- package/build/cjs/Message.js.map +1 -1
- package/build/cjs/WhatsApp.d.ts +16 -4
- package/build/cjs/WhatsApp.js +77 -79
- package/build/cjs/WhatsApp.js.map +1 -1
- package/build/cjs/index.d.ts +2 -2
- package/build/cjs/store/driver/FileEngine.d.ts +13 -2
- package/build/cjs/store/driver/FileEngine.js +15 -19
- package/build/cjs/store/driver/FileEngine.js.map +1 -1
- package/build/cjs/store/driver/RedisEngine.d.ts +13 -4
- package/build/cjs/store/driver/RedisEngine.js +23 -22
- package/build/cjs/store/driver/RedisEngine.js.map +1 -1
- package/build/cjs/store/engine.d.ts +9 -10
- package/build/esm/Chat.d.ts +187 -205
- package/build/esm/Chat.js +151 -273
- package/build/esm/Chat.js.map +1 -1
- package/build/esm/Contact.d.ts +317 -304
- package/build/esm/Contact.js +58 -102
- package/build/esm/Contact.js.map +1 -1
- package/build/esm/Message.d.ts +567 -265
- package/build/esm/Message.js +275 -256
- package/build/esm/Message.js.map +1 -1
- package/build/esm/WhatsApp.d.ts +16 -4
- package/build/esm/WhatsApp.js +80 -82
- package/build/esm/WhatsApp.js.map +1 -1
- package/build/esm/index.d.ts +2 -2
- package/build/esm/store/driver/FileEngine.d.ts +13 -2
- package/build/esm/store/driver/FileEngine.js +15 -19
- package/build/esm/store/driver/FileEngine.js.map +1 -1
- package/build/esm/store/driver/RedisEngine.d.ts +13 -4
- package/build/esm/store/driver/RedisEngine.js +23 -22
- package/build/esm/store/driver/RedisEngine.js.map +1 -1
- package/build/esm/store/engine.d.ts +9 -10
- package/package.json +2 -2
package/build/esm/Message.js
CHANGED
|
@@ -6,24 +6,26 @@ import { BufferJSON, downloadMediaMessage, generateForwardMessageContent, genera
|
|
|
6
6
|
import { Readable } from 'node:stream';
|
|
7
7
|
/**
|
|
8
8
|
* @description Estados de un mensaje.
|
|
9
|
+
* Message delivery statuses.
|
|
9
10
|
*/
|
|
10
11
|
export var MESSAGE_STATUS;
|
|
11
12
|
(function (MESSAGE_STATUS) {
|
|
12
|
-
/** Error al enviar */
|
|
13
|
+
/** Error al enviar / Send error */
|
|
13
14
|
MESSAGE_STATUS[MESSAGE_STATUS["ERROR"] = 0] = "ERROR";
|
|
14
|
-
/** Mensaje pendiente de envío */
|
|
15
|
+
/** Mensaje pendiente de envío / Pending send */
|
|
15
16
|
MESSAGE_STATUS[MESSAGE_STATUS["PENDING"] = 1] = "PENDING";
|
|
16
|
-
/** Servidor recibió el mensaje */
|
|
17
|
+
/** Servidor recibió el mensaje / Server acknowledged */
|
|
17
18
|
MESSAGE_STATUS[MESSAGE_STATUS["SERVER_ACK"] = 2] = "SERVER_ACK";
|
|
18
|
-
/** Mensaje entregado al destinatario */
|
|
19
|
+
/** Mensaje entregado al destinatario / Delivered */
|
|
19
20
|
MESSAGE_STATUS[MESSAGE_STATUS["DELIVERED"] = 3] = "DELIVERED";
|
|
20
|
-
/** Mensaje leído por el destinatario */
|
|
21
|
+
/** Mensaje leído por el destinatario / Read */
|
|
21
22
|
MESSAGE_STATUS[MESSAGE_STATUS["READ"] = 4] = "READ";
|
|
22
|
-
/** Mensaje reproducido (audio/video) */
|
|
23
|
+
/** Mensaje reproducido (audio/video) / Played */
|
|
23
24
|
MESSAGE_STATUS[MESSAGE_STATUS["PLAYED"] = 5] = "PLAYED";
|
|
24
25
|
})(MESSAGE_STATUS || (MESSAGE_STATUS = {}));
|
|
25
26
|
/**
|
|
26
27
|
* @description Mapeo de tipos de contenido de Baileys a MessageType.
|
|
28
|
+
* Baileys content type to MessageType mapping.
|
|
27
29
|
*/
|
|
28
30
|
export const MESSAGE_TYPE_MAP = {
|
|
29
31
|
conversation: 'text',
|
|
@@ -39,41 +41,33 @@ export const MESSAGE_TYPE_MAP = {
|
|
|
39
41
|
};
|
|
40
42
|
/**
|
|
41
43
|
* @description Construye IMessageIndex desde WAMessage.
|
|
42
|
-
*
|
|
43
|
-
* @param edited Si el mensaje fue editado.
|
|
44
|
-
* @returns IMessageIndex normalizado.
|
|
44
|
+
* Builds IMessageIndex from WAMessage.
|
|
45
45
|
*/
|
|
46
|
-
|
|
46
|
+
function build_message_index(raw, edited = false) {
|
|
47
|
+
const key = raw.key ?? {};
|
|
47
48
|
const content_type = getContentType(raw.message ?? {});
|
|
48
49
|
const msg_type = MESSAGE_TYPE_MAP[content_type ?? ''] ?? 'text';
|
|
49
50
|
const msg_content = raw.message?.[content_type];
|
|
50
51
|
const context_info = msg_content?.contextInfo;
|
|
51
|
-
|
|
52
|
+
const ephemeral_duration = raw.ephemeralDuration ?? context_info?.expiration;
|
|
53
|
+
const deleted_at = raw.ephemeralStartTimestamp && ephemeral_duration ? (Number(raw.ephemeralStartTimestamp) + ephemeral_duration) * 1000 : null;
|
|
52
54
|
let mime = 'text/plain';
|
|
53
|
-
if (msg_type === 'location' || msg_type === 'poll')
|
|
55
|
+
if (msg_type === 'location' || msg_type === 'poll')
|
|
54
56
|
mime = 'application/json';
|
|
55
|
-
|
|
56
|
-
else if (msg_type !== 'text' && typeof msg_content === 'object') {
|
|
57
|
+
else if (msg_type !== 'text' && typeof msg_content === 'object')
|
|
57
58
|
mime = msg_content?.mimetype ?? 'application/octet-stream';
|
|
58
|
-
}
|
|
59
|
-
// Calcular caption
|
|
60
59
|
let caption = '';
|
|
61
|
-
if (typeof msg_content === 'string')
|
|
60
|
+
if (typeof msg_content === 'string')
|
|
62
61
|
caption = msg_content;
|
|
63
|
-
|
|
64
|
-
else if (msg_content) {
|
|
62
|
+
else if (msg_content)
|
|
65
63
|
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
64
|
return {
|
|
71
|
-
id:
|
|
72
|
-
cid:
|
|
65
|
+
id: key.id ?? '',
|
|
66
|
+
cid: key.remoteJid ?? '',
|
|
73
67
|
mid: context_info?.stanzaId ?? null,
|
|
74
|
-
me:
|
|
68
|
+
me: key.fromMe ?? false,
|
|
75
69
|
type: msg_type,
|
|
76
|
-
author:
|
|
70
|
+
author: key.participant ?? key.remoteJid ?? '',
|
|
77
71
|
status: raw.status ?? MESSAGE_STATUS.PENDING,
|
|
78
72
|
starred: raw.starred ?? false,
|
|
79
73
|
forwarded: context_info?.isForwarded ?? false,
|
|
@@ -87,79 +81,51 @@ export function build_message_index(raw, edited = false) {
|
|
|
87
81
|
/**
|
|
88
82
|
* @description
|
|
89
83
|
* Clase base para representar un mensaje de WhatsApp.
|
|
90
|
-
*
|
|
84
|
+
* Base class representing a WhatsApp message.
|
|
91
85
|
*/
|
|
92
86
|
export class Message {
|
|
93
87
|
constructor(data) {
|
|
94
88
|
this.index = data.index;
|
|
95
89
|
this.raw = data.raw;
|
|
96
90
|
}
|
|
97
|
-
/** ID único del mensaje */
|
|
98
|
-
get id() {
|
|
99
|
-
|
|
100
|
-
}
|
|
101
|
-
/**
|
|
102
|
-
get
|
|
103
|
-
|
|
104
|
-
}
|
|
105
|
-
/**
|
|
106
|
-
get
|
|
107
|
-
|
|
108
|
-
}
|
|
109
|
-
/**
|
|
110
|
-
get
|
|
111
|
-
|
|
112
|
-
}
|
|
113
|
-
/**
|
|
114
|
-
get
|
|
115
|
-
|
|
116
|
-
}
|
|
117
|
-
/** true si el mensaje
|
|
118
|
-
get
|
|
119
|
-
|
|
120
|
-
}
|
|
121
|
-
/**
|
|
122
|
-
get
|
|
123
|
-
|
|
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
|
-
}
|
|
91
|
+
/** ID único del mensaje / Unique message ID */
|
|
92
|
+
get id() { return this.index.id; }
|
|
93
|
+
/** JID del chat al que pertenece / Chat JID */
|
|
94
|
+
get cid() { return this.index.cid; }
|
|
95
|
+
/** ID del mensaje padre (reply) / Parent message ID (reply) */
|
|
96
|
+
get mid() { return this.index.mid; }
|
|
97
|
+
/** true si el mensaje fue enviado por el usuario autenticado / true if sent by authenticated user */
|
|
98
|
+
get me() { return this.index.me; }
|
|
99
|
+
/** Autor del mensaje / Message author */
|
|
100
|
+
get author() { return this.index.author; }
|
|
101
|
+
/** true si el mensaje fue editado / true if edited */
|
|
102
|
+
get edited() { return this.index.edited; }
|
|
103
|
+
/** Tipo de contenido del mensaje / Message content type */
|
|
104
|
+
get type() { return this.index.type; }
|
|
105
|
+
/** Texto o caption del mensaje / Message text or caption */
|
|
106
|
+
get caption() { return this.index.caption; }
|
|
107
|
+
/** Tipo MIME del contenido / Content MIME type */
|
|
108
|
+
get mime() { return this.index.mime; }
|
|
109
|
+
/** Estado del mensaje / Message status */
|
|
110
|
+
get status() { return this.index.status; }
|
|
111
|
+
/** true si el mensaje está destacado / true if starred */
|
|
112
|
+
get starred() { return this.index.starred; }
|
|
113
|
+
/** true si el mensaje fue reenviado / true if forwarded */
|
|
114
|
+
get forwarded() { return this.index.forwarded; }
|
|
115
|
+
/** Timestamp de creación (ms) / Creation timestamp (ms) */
|
|
116
|
+
get created_at() { return this.index.created_at; }
|
|
117
|
+
/** Timestamp de expiración (ms) o null / Expiration timestamp (ms) or null */
|
|
118
|
+
get deleted_at() { return this.index.deleted_at; }
|
|
153
119
|
/**
|
|
154
120
|
* @description Obtiene el contenido como Readable stream.
|
|
155
|
-
*
|
|
121
|
+
* Gets content as Readable stream.
|
|
156
122
|
*/
|
|
157
123
|
stream() {
|
|
158
124
|
return Promise.resolve(Readable.from(Buffer.alloc(0)));
|
|
159
125
|
}
|
|
160
126
|
/**
|
|
161
127
|
* @description Obtiene el contenido del mensaje como Buffer.
|
|
162
|
-
*
|
|
128
|
+
* Gets message content as Buffer.
|
|
163
129
|
*/
|
|
164
130
|
content() {
|
|
165
131
|
return Promise.resolve(Buffer.alloc(0));
|
|
@@ -167,122 +133,239 @@ export class Message {
|
|
|
167
133
|
}
|
|
168
134
|
/**
|
|
169
135
|
* @description Factory que retorna la clase Message enlazada al contexto.
|
|
136
|
+
* Factory that returns the Message class bound to context.
|
|
170
137
|
* @param wa Instancia de WhatsApp.
|
|
171
|
-
* @returns Clase _Message que extiende Message.
|
|
172
138
|
*/
|
|
173
139
|
export function message(wa) {
|
|
174
|
-
/** Subscribers para watch() */
|
|
175
140
|
const _watchers = new Map();
|
|
176
|
-
/**
|
|
177
|
-
|
|
178
|
-
|
|
141
|
+
/**
|
|
142
|
+
* @description Agrega un mensaje al índice del chat (interno).
|
|
143
|
+
* Adds a message to the chat index (internal).
|
|
144
|
+
*/
|
|
145
|
+
async function _add_to_index(cid, mid, timestamp) {
|
|
146
|
+
const key = `chat/${cid}/messages`;
|
|
147
|
+
const line = `${timestamp} ${mid}`;
|
|
148
|
+
const current = await wa.engine.get(key);
|
|
149
|
+
await wa.engine.set(key, current ? `${line}\n${current}` : line);
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* @description Elimina un mensaje del índice del chat (interno).
|
|
153
|
+
* Removes a message from the chat index (internal).
|
|
154
|
+
*/
|
|
155
|
+
async function _remove_from_index(cid, mid) {
|
|
156
|
+
const key = `chat/${cid}/messages`;
|
|
157
|
+
const content = await wa.engine.get(key);
|
|
158
|
+
if (!content)
|
|
159
|
+
return;
|
|
160
|
+
const filtered = content.trim().split('\n').filter((line) => !line.endsWith(` ${mid}`)).join('\n');
|
|
161
|
+
await wa.engine.set(key, filtered || null);
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* @description Notifica a los watchers de un mensaje (interno).
|
|
165
|
+
* Notifies message watchers (internal).
|
|
166
|
+
*/
|
|
167
|
+
function _notify(cid, mid) {
|
|
168
|
+
const handlers = _watchers.get(`${cid}:${mid}`);
|
|
169
|
+
handlers?.forEach((h) => _Message.get(cid, mid).then((msg) => msg && h(msg)));
|
|
170
|
+
}
|
|
171
|
+
async function _send(cid, content, binary, reply_to) {
|
|
172
|
+
const jid = await wa.resolveJID(cid);
|
|
173
|
+
if (!jid || !wa.socket)
|
|
179
174
|
return null;
|
|
180
|
-
|
|
181
|
-
if (
|
|
175
|
+
let quoted;
|
|
176
|
+
if (reply_to) {
|
|
177
|
+
const ref = await _Message.get(jid, reply_to);
|
|
178
|
+
if (ref)
|
|
179
|
+
quoted = ref.raw;
|
|
180
|
+
}
|
|
181
|
+
const raw = await wa.socket.sendMessage(jid, { ...content, ...(quoted && { quoted }) });
|
|
182
|
+
const sent_id = raw?.key?.id;
|
|
183
|
+
if (!raw || !sent_id)
|
|
182
184
|
return null;
|
|
183
185
|
const index = build_message_index(raw);
|
|
184
|
-
await wa.engine.set(`chat/${
|
|
185
|
-
await wa.engine.set(`chat/${
|
|
186
|
+
await wa.engine.set(`chat/${jid}/message/${sent_id}/index`, JSON.stringify(index, BufferJSON.replacer));
|
|
187
|
+
await wa.engine.set(`chat/${jid}/message/${sent_id}/raw`, JSON.stringify(raw, BufferJSON.replacer));
|
|
186
188
|
if (binary)
|
|
187
|
-
await wa.engine.set(`chat/${
|
|
188
|
-
await
|
|
189
|
+
await wa.engine.set(`chat/${jid}/message/${sent_id}/content`, binary.toString('base64'));
|
|
190
|
+
await _add_to_index(jid, sent_id, index.created_at);
|
|
189
191
|
return new _Message({ index, raw });
|
|
190
192
|
}
|
|
191
193
|
const _Message = class extends Message {
|
|
192
194
|
/**
|
|
193
195
|
* @description Obtiene un mensaje por CID y MID.
|
|
194
|
-
*
|
|
195
|
-
* @param mid ID del mensaje.
|
|
196
|
-
* @returns Mensaje o null si no existe.
|
|
196
|
+
* Retrieves a message by CID and MID.
|
|
197
197
|
*/
|
|
198
198
|
static async get(cid, mid) {
|
|
199
|
-
const
|
|
199
|
+
const jid = await wa.resolveJID(cid);
|
|
200
|
+
if (!jid)
|
|
201
|
+
return null;
|
|
202
|
+
const stored_index = await wa.engine.get(`chat/${jid}/message/${mid}/index`);
|
|
200
203
|
if (!stored_index)
|
|
201
204
|
return null;
|
|
202
205
|
const index = JSON.parse(stored_index, BufferJSON.reviver);
|
|
203
|
-
|
|
204
|
-
const
|
|
205
|
-
const raw = stored_raw ? JSON.parse(stored_raw, BufferJSON.reviver) : { key: { remoteJid: cid, id: mid, fromMe: index.me } };
|
|
206
|
+
const stored_raw = await wa.engine.get(`chat/${jid}/message/${mid}/raw`);
|
|
207
|
+
const raw = stored_raw ? JSON.parse(stored_raw, BufferJSON.reviver) : { key: { remoteJid: jid, id: mid, fromMe: index.me } };
|
|
206
208
|
return new _Message({ index, raw });
|
|
207
209
|
}
|
|
208
210
|
/**
|
|
209
|
-
* @description
|
|
210
|
-
*
|
|
211
|
-
* @param mid ID del mensaje.
|
|
212
|
-
* @param emoji Emoji de reacción (vacío para quitar).
|
|
213
|
-
* @returns true si se envió correctamente.
|
|
211
|
+
* @description Lista mensajes de un chat (paginado).
|
|
212
|
+
* Lists messages from a chat (paginated).
|
|
214
213
|
*/
|
|
215
|
-
static async
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
214
|
+
static async list(cid, offset = 0, limit = 50) {
|
|
215
|
+
const jid = await wa.resolveJID(cid);
|
|
216
|
+
if (!jid)
|
|
217
|
+
return [];
|
|
218
|
+
const content = await wa.engine.get(`chat/${jid}/messages`);
|
|
219
|
+
if (!content)
|
|
220
|
+
return [];
|
|
221
|
+
const ids = content.trim().split('\n').slice(offset, offset + limit).map((line) => line.split(' ')[1]).filter(Boolean);
|
|
222
|
+
const messages = [];
|
|
223
|
+
for (const mid of ids) {
|
|
224
|
+
const msg = await _Message.get(jid, mid);
|
|
225
|
+
if (msg)
|
|
226
|
+
messages.push(msg);
|
|
227
|
+
}
|
|
228
|
+
return messages;
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* @description Cuenta mensajes de un chat.
|
|
232
|
+
* Counts messages in a chat.
|
|
233
|
+
*/
|
|
234
|
+
static async count(cid) {
|
|
235
|
+
const jid = await wa.resolveJID(cid);
|
|
236
|
+
if (!jid)
|
|
237
|
+
return 0;
|
|
238
|
+
const content = await wa.engine.get(`chat/${jid}/messages`);
|
|
239
|
+
if (!content)
|
|
240
|
+
return 0;
|
|
241
|
+
return content.trim().split('\n').filter(Boolean).length;
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* @description Envía un mensaje de texto.
|
|
245
|
+
* Sends a text message.
|
|
246
|
+
*/
|
|
247
|
+
static async text(cid, text, mid) {
|
|
248
|
+
return _send(cid, { text }, undefined, mid);
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* @description Envía una imagen.
|
|
252
|
+
* Sends an image.
|
|
253
|
+
*/
|
|
254
|
+
static async image(cid, buffer, caption, mid) {
|
|
255
|
+
return _send(cid, { image: buffer, caption }, buffer, mid);
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* @description Envía un video.
|
|
259
|
+
* Sends a video.
|
|
260
|
+
*/
|
|
261
|
+
static async video(cid, buffer, caption, mid) {
|
|
262
|
+
return _send(cid, { video: buffer, caption }, buffer, mid);
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* @description Envía un audio.
|
|
266
|
+
* Sends an audio.
|
|
267
|
+
* @param ptt true para nota de voz (default true) / true for voice note (default true).
|
|
268
|
+
*/
|
|
269
|
+
static async audio(cid, buffer, ptt = true, mid) {
|
|
270
|
+
return _send(cid, { audio: buffer, ptt }, buffer, mid);
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* @description Envía una ubicación.
|
|
274
|
+
* Sends a location.
|
|
275
|
+
*/
|
|
276
|
+
static async location(cid, opts, mid) {
|
|
277
|
+
return _send(cid, { location: { degreesLatitude: opts.lat, degreesLongitude: opts.lng } }, undefined, mid);
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* @description Envía una encuesta.
|
|
281
|
+
* Sends a poll.
|
|
282
|
+
*/
|
|
283
|
+
static async poll(cid, opts, mid) {
|
|
284
|
+
return _send(cid, { poll: { name: opts.content, values: opts.options.map((o) => o.content), selectableCount: 1 } }, undefined, mid);
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* @description Observa cambios en un mensaje específico.
|
|
288
|
+
* Watches changes on a specific message.
|
|
289
|
+
* @returns Función para dejar de observar / Unsubscribe function.
|
|
290
|
+
*/
|
|
291
|
+
static watch(cid, mid, handler) {
|
|
292
|
+
const key = `${cid}:${mid}`;
|
|
293
|
+
if (!_watchers.has(key))
|
|
294
|
+
_watchers.set(key, new Set());
|
|
295
|
+
_watchers.get(key)?.add(handler);
|
|
296
|
+
return () => {
|
|
297
|
+
_watchers.get(key)?.delete(handler);
|
|
298
|
+
if (_watchers.get(key)?.size === 0)
|
|
299
|
+
_watchers.delete(key);
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
/**
|
|
303
|
+
* @description Edita el mensaje (solo texto o caption, solo mensajes propios).
|
|
304
|
+
* Edits the message (text or caption only, own messages only).
|
|
305
|
+
*/
|
|
306
|
+
async edit(text) {
|
|
307
|
+
if (!wa.socket || !this.me)
|
|
220
308
|
return false;
|
|
221
|
-
await wa.socket.sendMessage(cid, {
|
|
222
|
-
|
|
309
|
+
await wa.socket.sendMessage(this.cid, {
|
|
310
|
+
text,
|
|
311
|
+
edit: { remoteJid: this.cid, id: this.id, fromMe: true },
|
|
223
312
|
});
|
|
313
|
+
const content_type = getContentType(this.raw.message ?? {});
|
|
314
|
+
if (content_type === 'conversation') {
|
|
315
|
+
this.raw.message = { conversation: text };
|
|
316
|
+
}
|
|
317
|
+
else if (content_type === 'extendedTextMessage') {
|
|
318
|
+
this.raw.message = { extendedTextMessage: { text } };
|
|
319
|
+
}
|
|
320
|
+
this.index.caption = text;
|
|
321
|
+
this.index.edited = true;
|
|
322
|
+
await wa.engine.set(`chat/${this.cid}/message/${this.id}/index`, JSON.stringify(this.index, BufferJSON.replacer));
|
|
323
|
+
await wa.engine.set(`chat/${this.cid}/message/${this.id}/raw`, JSON.stringify(this.raw, BufferJSON.replacer));
|
|
224
324
|
return true;
|
|
225
325
|
}
|
|
226
326
|
/**
|
|
227
|
-
* @description Elimina
|
|
228
|
-
*
|
|
229
|
-
* @param mid ID del mensaje.
|
|
230
|
-
* @returns true si se eliminó correctamente.
|
|
327
|
+
* @description Elimina el mensaje para todos.
|
|
328
|
+
* Deletes the message for everyone.
|
|
231
329
|
*/
|
|
232
|
-
|
|
330
|
+
async remove() {
|
|
233
331
|
if (!wa.socket)
|
|
234
332
|
return false;
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
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);
|
|
333
|
+
await wa.socket.sendMessage(this.cid, { delete: { remoteJid: this.cid, id: this.id, fromMe: this.me } });
|
|
334
|
+
await _remove_from_index(this.cid, this.id);
|
|
335
|
+
await wa.engine.set(`chat/${this.cid}/message/${this.id}`, null);
|
|
245
336
|
return true;
|
|
246
337
|
}
|
|
247
338
|
/**
|
|
248
|
-
* @description Reenvía
|
|
249
|
-
*
|
|
250
|
-
* @param mid ID del mensaje.
|
|
251
|
-
* @param to_cid ID del chat destino.
|
|
252
|
-
* @returns true si se reenvió correctamente.
|
|
339
|
+
* @description Reenvía el mensaje a otro chat.
|
|
340
|
+
* Forwards the message to another chat.
|
|
253
341
|
*/
|
|
254
|
-
|
|
342
|
+
async forward(to_cid) {
|
|
255
343
|
if (!wa.socket)
|
|
256
344
|
return false;
|
|
257
|
-
|
|
258
|
-
if (!msg)
|
|
259
|
-
return false;
|
|
260
|
-
// Forward nativo con raw
|
|
261
|
-
if (msg.raw.message) {
|
|
345
|
+
if (this.raw.message) {
|
|
262
346
|
try {
|
|
263
|
-
const wa_msg = generateWAMessageFromContent(to_cid, generateForwardMessageContent(
|
|
347
|
+
const wa_msg = generateWAMessageFromContent(to_cid, generateForwardMessageContent(this.raw, false), { userJid: wa.socket.user?.id ?? '' });
|
|
348
|
+
if (!wa_msg.message || !wa_msg.key?.id)
|
|
349
|
+
return false;
|
|
264
350
|
await wa.socket.relayMessage(to_cid, wa_msg.message, { messageId: wa_msg.key.id });
|
|
265
351
|
return true;
|
|
266
352
|
}
|
|
267
|
-
catch {
|
|
268
|
-
// Fallback si falla el forward nativo
|
|
269
|
-
}
|
|
353
|
+
catch { /* fallback below */ }
|
|
270
354
|
}
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
if (!content.length)
|
|
355
|
+
const buf = await this.content();
|
|
356
|
+
if (!buf.length)
|
|
274
357
|
return false;
|
|
275
|
-
if (
|
|
276
|
-
return (await _Message.text(to_cid,
|
|
277
|
-
if (
|
|
278
|
-
return (await _Message.image(to_cid,
|
|
279
|
-
if (
|
|
280
|
-
return (await _Message.video(to_cid,
|
|
281
|
-
if (
|
|
282
|
-
return (await _Message.audio(to_cid,
|
|
283
|
-
if (
|
|
358
|
+
if (this.type === 'text')
|
|
359
|
+
return (await _Message.text(to_cid, buf.toString())) !== null;
|
|
360
|
+
if (this.type === 'image')
|
|
361
|
+
return (await _Message.image(to_cid, buf, this.caption || undefined)) !== null;
|
|
362
|
+
if (this.type === 'video')
|
|
363
|
+
return (await _Message.video(to_cid, buf, this.caption || undefined)) !== null;
|
|
364
|
+
if (this.type === 'audio')
|
|
365
|
+
return (await _Message.audio(to_cid, buf)) !== null;
|
|
366
|
+
if (this.type === 'location') {
|
|
284
367
|
try {
|
|
285
|
-
return (await _Message.location(to_cid, JSON.parse(
|
|
368
|
+
return (await _Message.location(to_cid, JSON.parse(buf.toString()))) !== null;
|
|
286
369
|
}
|
|
287
370
|
catch {
|
|
288
371
|
return false;
|
|
@@ -291,126 +374,63 @@ export function message(wa) {
|
|
|
291
374
|
return false;
|
|
292
375
|
}
|
|
293
376
|
/**
|
|
294
|
-
* @description
|
|
295
|
-
*
|
|
296
|
-
* @param
|
|
297
|
-
* @param text Nuevo contenido.
|
|
298
|
-
* @returns true si se editó correctamente.
|
|
377
|
+
* @description Reacciona al mensaje.
|
|
378
|
+
* Reacts to the message.
|
|
379
|
+
* @param emoji Emoji de reacción (vacío para quitar) / Reaction emoji (empty to remove).
|
|
299
380
|
*/
|
|
300
|
-
|
|
381
|
+
async react(emoji) {
|
|
301
382
|
if (!wa.socket)
|
|
302
383
|
return false;
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
return false;
|
|
306
|
-
await wa.socket.sendMessage(cid, {
|
|
307
|
-
text,
|
|
308
|
-
edit: { remoteJid: cid, id: mid, fromMe: true },
|
|
384
|
+
await wa.socket.sendMessage(this.cid, {
|
|
385
|
+
react: { text: emoji, key: { remoteJid: this.cid, id: this.id, fromMe: this.me } },
|
|
309
386
|
});
|
|
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
387
|
return true;
|
|
324
388
|
}
|
|
325
389
|
/**
|
|
326
|
-
* @description
|
|
327
|
-
*
|
|
328
|
-
* @param text Contenido del mensaje.
|
|
329
|
-
* @returns Mensaje enviado o null.
|
|
390
|
+
* @description Responde con un mensaje de texto.
|
|
391
|
+
* Replies with a text message.
|
|
330
392
|
*/
|
|
331
|
-
|
|
332
|
-
return
|
|
393
|
+
async text(content) {
|
|
394
|
+
return _Message.text(this.cid, content, this.id);
|
|
333
395
|
}
|
|
334
396
|
/**
|
|
335
|
-
* @description
|
|
336
|
-
*
|
|
337
|
-
* @param buffer Buffer de la imagen.
|
|
338
|
-
* @param caption Caption opcional.
|
|
339
|
-
* @returns Mensaje enviado o null.
|
|
397
|
+
* @description Responde con una imagen.
|
|
398
|
+
* Replies with an image.
|
|
340
399
|
*/
|
|
341
|
-
|
|
342
|
-
return
|
|
400
|
+
async image(buffer, caption) {
|
|
401
|
+
return _Message.image(this.cid, buffer, caption, this.id);
|
|
343
402
|
}
|
|
344
403
|
/**
|
|
345
|
-
* @description
|
|
346
|
-
*
|
|
347
|
-
* @param buffer Buffer del video.
|
|
348
|
-
* @param caption Caption opcional.
|
|
349
|
-
* @returns Mensaje enviado o null.
|
|
404
|
+
* @description Responde con un video.
|
|
405
|
+
* Replies with a video.
|
|
350
406
|
*/
|
|
351
|
-
|
|
352
|
-
return
|
|
407
|
+
async video(buffer, caption) {
|
|
408
|
+
return _Message.video(this.cid, buffer, caption, this.id);
|
|
353
409
|
}
|
|
354
410
|
/**
|
|
355
|
-
* @description
|
|
356
|
-
*
|
|
357
|
-
* @param buffer Buffer del audio.
|
|
358
|
-
* @param ptt true para nota de voz (default true).
|
|
359
|
-
* @returns Mensaje enviado o null.
|
|
411
|
+
* @description Responde con un audio.
|
|
412
|
+
* Replies with an audio.
|
|
360
413
|
*/
|
|
361
|
-
|
|
362
|
-
return
|
|
414
|
+
async audio(buffer, ptt = true) {
|
|
415
|
+
return _Message.audio(this.cid, buffer, ptt, this.id);
|
|
363
416
|
}
|
|
364
417
|
/**
|
|
365
|
-
* @description
|
|
366
|
-
*
|
|
367
|
-
* @param opts Opciones de ubicación (lat, lng, live).
|
|
368
|
-
* @returns Mensaje enviado o null.
|
|
418
|
+
* @description Responde con una ubicacion.
|
|
419
|
+
* Replies with a location.
|
|
369
420
|
*/
|
|
370
|
-
|
|
371
|
-
return
|
|
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
|
-
};
|
|
421
|
+
async location(opts) {
|
|
422
|
+
return _Message.location(this.cid, opts, this.id);
|
|
399
423
|
}
|
|
400
424
|
/**
|
|
401
|
-
* @description
|
|
402
|
-
*
|
|
403
|
-
* @param mid ID del mensaje.
|
|
425
|
+
* @description Responde con una encuesta.
|
|
426
|
+
* Replies with a poll.
|
|
404
427
|
*/
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
handlers?.forEach((h) => _Message.get(cid, mid).then((msg) => msg && h(msg)));
|
|
428
|
+
async poll(opts) {
|
|
429
|
+
return _Message.poll(this.cid, opts, this.id);
|
|
408
430
|
}
|
|
409
431
|
/**
|
|
410
432
|
* @description Obtiene el contenido como Readable stream.
|
|
411
|
-
*
|
|
412
|
-
* Para text/location/poll retorna stream desde buffer pequeño.
|
|
413
|
-
* @returns Readable stream con el contenido.
|
|
433
|
+
* Gets content as Readable stream.
|
|
414
434
|
*/
|
|
415
435
|
async stream() {
|
|
416
436
|
if (this.type === 'text') {
|
|
@@ -436,8 +456,7 @@ export function message(wa) {
|
|
|
436
456
|
}
|
|
437
457
|
/**
|
|
438
458
|
* @description Obtiene el contenido del mensaje como Buffer.
|
|
439
|
-
*
|
|
440
|
-
* @returns Buffer con el contenido.
|
|
459
|
+
* Gets message content as Buffer.
|
|
441
460
|
*/
|
|
442
461
|
async content() {
|
|
443
462
|
const cached = await wa.engine.get(`chat/${this.cid}/message/${this.id}/content`);
|
|
@@ -454,6 +473,6 @@ export function message(wa) {
|
|
|
454
473
|
return buffer;
|
|
455
474
|
}
|
|
456
475
|
};
|
|
457
|
-
return _Message;
|
|
476
|
+
return Object.assign(_Message, { _add_to_index, _remove_from_index, _notify, _build_index: build_message_index });
|
|
458
477
|
}
|
|
459
478
|
//# sourceMappingURL=Message.js.map
|