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