@arcaelas/whatsapp 4.5.0 → 5.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.
Files changed (95) hide show
  1. package/build/cjs/index.d.ts +6 -4
  2. package/build/cjs/index.js +22 -2
  3. package/build/cjs/index.js.map +1 -1
  4. package/build/cjs/lib/bot/decorator.d.ts +2 -0
  5. package/build/cjs/lib/bot/decorator.js +6 -0
  6. package/build/cjs/lib/bot/decorator.js.map +1 -1
  7. package/build/cjs/lib/chat/index.d.ts +340 -204
  8. package/build/cjs/lib/chat/index.js +225 -213
  9. package/build/cjs/lib/chat/index.js.map +1 -1
  10. package/build/cjs/lib/contact/index.d.ts +162 -179
  11. package/build/cjs/lib/contact/index.js +96 -190
  12. package/build/cjs/lib/contact/index.js.map +1 -1
  13. package/build/cjs/lib/internal.d.ts +40 -0
  14. package/build/cjs/lib/internal.js +39 -0
  15. package/build/cjs/lib/internal.js.map +1 -0
  16. package/build/cjs/lib/message/index.d.ts +462 -289
  17. package/build/cjs/lib/message/index.js +825 -741
  18. package/build/cjs/lib/message/index.js.map +1 -1
  19. package/build/cjs/lib/status/index.d.ts +60 -70
  20. package/build/cjs/lib/status/index.js +106 -111
  21. package/build/cjs/lib/status/index.js.map +1 -1
  22. package/build/cjs/lib/store/engine/index.d.ts +21 -7
  23. package/build/cjs/lib/store/engine/index.js +6 -3
  24. package/build/cjs/lib/store/engine/index.js.map +1 -1
  25. package/build/cjs/lib/store/engine/lib/file_system/index.d.ts +38 -8
  26. package/build/cjs/lib/store/engine/lib/file_system/index.js +92 -67
  27. package/build/cjs/lib/store/engine/lib/file_system/index.js.map +1 -1
  28. package/build/cjs/lib/store/engine/lib/index.d.ts +112 -0
  29. package/build/cjs/lib/store/engine/lib/index.js +213 -0
  30. package/build/cjs/lib/store/engine/lib/index.js.map +1 -0
  31. package/build/cjs/lib/store/engine/lib/redis/index.d.ts +28 -13
  32. package/build/cjs/lib/store/engine/lib/redis/index.js +56 -42
  33. package/build/cjs/lib/store/engine/lib/redis/index.js.map +1 -1
  34. package/build/cjs/lib/store/engine/lib/s3/index.d.ts +44 -1
  35. package/build/cjs/lib/store/engine/lib/s3/index.js +112 -64
  36. package/build/cjs/lib/store/engine/lib/s3/index.js.map +1 -1
  37. package/build/cjs/lib/store/engine/lib/sqlite/index.d.ts +98 -0
  38. package/build/cjs/lib/store/engine/lib/sqlite/index.js +114 -0
  39. package/build/cjs/lib/store/engine/lib/sqlite/index.js.map +1 -0
  40. package/build/cjs/lib/store/index.d.ts +7 -3
  41. package/build/cjs/lib/store/index.js +14 -4
  42. package/build/cjs/lib/store/index.js.map +1 -1
  43. package/build/cjs/lib/whatsapp/index.d.ts +106 -68
  44. package/build/cjs/lib/whatsapp/index.js +380 -170
  45. package/build/cjs/lib/whatsapp/index.js.map +1 -1
  46. package/build/cjs/test.js +5 -4
  47. package/build/cjs/test.js.map +1 -1
  48. package/build/esm/index.d.ts +6 -4
  49. package/build/esm/index.js +5 -2
  50. package/build/esm/index.js.map +1 -1
  51. package/build/esm/lib/bot/decorator.d.ts +2 -0
  52. package/build/esm/lib/bot/decorator.js +6 -0
  53. package/build/esm/lib/bot/decorator.js.map +1 -1
  54. package/build/esm/lib/chat/index.d.ts +340 -204
  55. package/build/esm/lib/chat/index.js +225 -213
  56. package/build/esm/lib/chat/index.js.map +1 -1
  57. package/build/esm/lib/contact/index.d.ts +162 -179
  58. package/build/esm/lib/contact/index.js +96 -189
  59. package/build/esm/lib/contact/index.js.map +1 -1
  60. package/build/esm/lib/internal.d.ts +40 -0
  61. package/build/esm/lib/internal.js +35 -0
  62. package/build/esm/lib/internal.js.map +1 -0
  63. package/build/esm/lib/message/index.d.ts +462 -289
  64. package/build/esm/lib/message/index.js +820 -736
  65. package/build/esm/lib/message/index.js.map +1 -1
  66. package/build/esm/lib/status/index.d.ts +60 -70
  67. package/build/esm/lib/status/index.js +105 -110
  68. package/build/esm/lib/status/index.js.map +1 -1
  69. package/build/esm/lib/store/engine/index.d.ts +21 -7
  70. package/build/esm/lib/store/engine/index.js +4 -2
  71. package/build/esm/lib/store/engine/index.js.map +1 -1
  72. package/build/esm/lib/store/engine/lib/file_system/index.d.ts +38 -8
  73. package/build/esm/lib/store/engine/lib/file_system/index.js +92 -67
  74. package/build/esm/lib/store/engine/lib/file_system/index.js.map +1 -1
  75. package/build/esm/lib/store/engine/lib/index.d.ts +112 -0
  76. package/build/esm/lib/store/engine/lib/index.js +206 -0
  77. package/build/esm/lib/store/engine/lib/index.js.map +1 -0
  78. package/build/esm/lib/store/engine/lib/redis/index.d.ts +28 -13
  79. package/build/esm/lib/store/engine/lib/redis/index.js +54 -40
  80. package/build/esm/lib/store/engine/lib/redis/index.js.map +1 -1
  81. package/build/esm/lib/store/engine/lib/s3/index.d.ts +44 -1
  82. package/build/esm/lib/store/engine/lib/s3/index.js +111 -63
  83. package/build/esm/lib/store/engine/lib/s3/index.js.map +1 -1
  84. package/build/esm/lib/store/engine/lib/sqlite/index.d.ts +98 -0
  85. package/build/esm/lib/store/engine/lib/sqlite/index.js +110 -0
  86. package/build/esm/lib/store/engine/lib/sqlite/index.js.map +1 -0
  87. package/build/esm/lib/store/index.d.ts +7 -3
  88. package/build/esm/lib/store/index.js +13 -4
  89. package/build/esm/lib/store/index.js.map +1 -1
  90. package/build/esm/lib/whatsapp/index.d.ts +106 -68
  91. package/build/esm/lib/whatsapp/index.js +382 -172
  92. package/build/esm/lib/whatsapp/index.js.map +1 -1
  93. package/build/esm/test.js +5 -4
  94. package/build/esm/test.js.map +1 -1
  95. package/package.json +2 -2
@@ -1,28 +1,26 @@
1
1
  /**
2
2
  * @file message/index.ts
3
- * @description Entidad Message — una sola clase base que recibe `{wa, doc}` en el constructor.
4
- * Subclases especializadas (Text, Image, Video, Audio, Gps, Poll) override `content()` según su tipo.
3
+ * @description Entidad Message — clase base con getters puros y subclases por tipo
4
+ * (Text, Image, Video, Audio, Sticker, Document, Location, Poll, VCard, Event);
5
+ * `message(wa, raw)` evalúa el tipo y retorna la instancia correcta.
6
+ * Message entity — pure-getter base class with per-type subclasses;
7
+ * `message(wa, raw)` evaluates the type and returns the right instance.
5
8
  */
6
9
  import { aesEncryptGCM, downloadMediaMessage, generateForwardMessageContent, generateMessageID, generateWAMessageFromContent, getAggregateVotesInPollMessage, getContentType, getKeyAuthor, hmacSign, jidNormalizedUser, proto, sha256, updateMessageWithPollUpdate, } from 'baileys';
7
10
  import { randomBytes } from 'node:crypto';
8
11
  import { Readable } from 'node:stream';
12
+ import { internals } from '../../lib/internal.js';
9
13
  import { Chat } from '../../lib/chat/index.js';
10
14
  import { deserialize, serialize } from '../../lib/store/index.js';
11
- export var MessageStatus;
12
- (function (MessageStatus) {
13
- MessageStatus[MessageStatus["ERROR"] = 0] = "ERROR";
14
- MessageStatus[MessageStatus["PENDING"] = 1] = "PENDING";
15
- MessageStatus[MessageStatus["SERVER_ACK"] = 2] = "SERVER_ACK";
16
- MessageStatus[MessageStatus["DELIVERED"] = 3] = "DELIVERED";
17
- MessageStatus[MessageStatus["READ"] = 4] = "READ";
18
- MessageStatus[MessageStatus["PLAYED"] = 5] = "PLAYED";
19
- })(MessageStatus || (MessageStatus = {}));
15
+ /** Estados legibles indexados por el status numérico de baileys. / Readable states indexed by the baileys numeric status. */
16
+ const STATUS = ['error', 'pending', 'sent', 'delivered', 'read', 'played'];
20
17
  const MESSAGE_TYPE_MAP = {
21
18
  conversation: 'text',
22
19
  extendedTextMessage: 'text',
23
20
  imageMessage: 'image',
24
21
  videoMessage: 'video',
25
22
  audioMessage: 'audio',
23
+ stickerMessage: 'sticker',
26
24
  locationMessage: 'location',
27
25
  liveLocationMessage: 'location',
28
26
  pollCreationMessage: 'poll',
@@ -35,292 +33,407 @@ const MESSAGE_TYPE_MAP = {
35
33
  eventMessage: 'event',
36
34
  };
37
35
  /**
38
- * Construye el documento IMessage desde el raw de baileys.
36
+ * Desenvuelve los wrappers de contenido (view-once, documento con caption).
37
+ * Unwraps content wrappers (view-once, captioned document).
39
38
  */
40
- export function build_message_doc(raw, self_id, edited = false) {
41
- const key = raw.key ?? {};
42
- const msg = raw.message ?? {};
43
- const unwrapped = msg.viewOnceMessage?.message ??
39
+ function unwrap(msg) {
40
+ return (msg.viewOnceMessage?.message ??
44
41
  msg.viewOnceMessageV2?.message ??
45
42
  msg.viewOnceMessageV2Extension?.message ??
46
43
  msg.documentWithCaptionMessage?.message ??
47
- msg;
48
- const content_type = getContentType(unwrapped);
49
- const msg_type = MESSAGE_TYPE_MAP[content_type ?? ''] ?? 'text';
50
- const msg_content = unwrapped[content_type];
51
- const context_info = msg_content
52
- ?.contextInfo;
53
- const ephemeral_duration = raw.ephemeralDuration ?? context_info?.expiration;
54
- const deleted_at = raw.ephemeralStartTimestamp != null && ephemeral_duration != null
55
- ? (Number(raw.ephemeralStartTimestamp) + ephemeral_duration) * 1000
56
- : null;
57
- let mime = 'text/plain';
58
- if (msg_type === 'location' || msg_type === 'poll' || msg_type === 'event') {
59
- mime = 'application/json';
60
- }
61
- else if (msg_type === 'vcard') {
62
- mime = 'text/vcard';
63
- }
64
- else if (msg_type !== 'text' && typeof msg_content === 'object') {
65
- mime = msg_content?.mimetype ?? 'application/octet-stream';
66
- }
67
- let caption = '';
68
- if (typeof msg_content === 'string') {
69
- caption = msg_content;
70
- }
71
- else if (msg_type === 'event') {
72
- caption = msg_content?.description ?? '';
73
- }
74
- else if (msg_content) {
75
- caption =
76
- msg_content.caption ??
77
- msg_content.text ??
78
- msg_content.name ??
79
- msg_content.displayName ??
80
- '';
81
- }
82
- const author = key.fromMe && self_id
83
- ? jidNormalizedUser(self_id)
84
- : key.participant || key.remoteJidAlt || key.remoteJid || '';
85
- return {
86
- id: key.id ?? '',
87
- cid: key.remoteJidAlt ?? key.remoteJid ?? '',
88
- mid: context_info?.stanzaId ?? null,
89
- me: key.fromMe ?? false,
90
- type: msg_type,
91
- author,
92
- status: raw.status ?? MessageStatus.PENDING,
93
- starred: raw.starred ?? false,
94
- forwarded: context_info?.isForwarded ?? false,
95
- created_at: (Number(raw.messageTimestamp) || Math.floor(Date.now() / 1000)) * 1000,
96
- deleted_at,
97
- mime,
98
- caption,
99
- edited,
100
- raw,
101
- };
44
+ msg);
45
+ }
46
+ /**
47
+ * Convierte un número o Long del proto a number plano.
48
+ * Converts a proto number or Long into a plain number.
49
+ */
50
+ function to_number(value) {
51
+ return Number(value?.toString() ?? 0);
52
+ }
53
+ /**
54
+ * Convierte los bytes del proto (Uint8Array runtime o base64 post-engine) a Buffer.
55
+ * Converts proto bytes (runtime Uint8Array or post-engine base64) into a Buffer.
56
+ */
57
+ function to_buffer(value) {
58
+ if (value instanceof Uint8Array && value.length > 0)
59
+ return Buffer.from(value);
60
+ if (typeof value === 'string' && value.length > 0)
61
+ return Buffer.from(value, 'base64');
62
+ return null;
63
+ }
64
+ /**
65
+ * Envío base: resuelve el destino, cita si hay `mid`, marca view-once, persiste el doc
66
+ * (con su binario cuando aplica) y retorna la instancia del mensaje enviado.
67
+ * Base send: resolves the target, quotes when `mid` is given, flags view-once, persists
68
+ * the doc (with its binary when it applies) and returns the sent message instance.
69
+ */
70
+ async function send(wa, cid, content, binary, extra = {}, doc_overrides) {
71
+ let result = null;
72
+ const jid = await internals(wa).resolve_jid(cid);
73
+ const socket = internals(wa).socket;
74
+ if (jid && socket) {
75
+ const quoted = extra.mid
76
+ ? (deserialize(await wa.engine.get(`/chat/${jid}/message/${extra.mid}`))?.raw ?? undefined)
77
+ : undefined;
78
+ const raw = await socket.sendMessage(jid, {
79
+ ...content,
80
+ ...(extra.once && { viewOnce: true }),
81
+ ...(quoted && { quoted }),
82
+ });
83
+ const sent_id = raw?.key?.id;
84
+ if (raw && sent_id) {
85
+ const instance = message(wa, raw);
86
+ if (doc_overrides) {
87
+ Object.assign(instance._raw, doc_overrides);
88
+ }
89
+ await wa.engine.set(`/chat/${jid}/message/${sent_id}`, serialize(instance._raw), instance._raw.created_at);
90
+ if (binary) {
91
+ await wa.engine.set(`/chat/${jid}/message/${sent_id}/content`, serialize({ data: binary.toString('base64') }));
92
+ }
93
+ result = instance;
94
+ }
95
+ }
96
+ return result;
97
+ }
98
+ /**
99
+ * Stream del binario del mensaje: cache del engine o descarga de baileys.
100
+ * Message binary stream: engine cache or baileys download.
101
+ */
102
+ async function media_stream(wa, doc) {
103
+ const cached = deserialize(await wa.engine.get(`/chat/${doc.cid}/message/${doc.id}/content`));
104
+ if (cached?.data) {
105
+ return Readable.from(Buffer.from(cached.data, 'base64'));
106
+ }
107
+ const socket = internals(wa).socket;
108
+ if (socket) {
109
+ try {
110
+ return (await downloadMediaMessage(doc.raw, 'stream', {}));
111
+ }
112
+ catch {
113
+ /* media may be expired */
114
+ }
115
+ }
116
+ return Readable.from(Buffer.alloc(0));
117
+ }
118
+ /**
119
+ * Acumula un Readable en Buffer.
120
+ * Drains a Readable into a Buffer.
121
+ */
122
+ async function drain(readable) {
123
+ const chunks = [];
124
+ for await (const chunk of readable) {
125
+ chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
126
+ }
127
+ return Buffer.concat(chunks);
102
128
  }
103
129
  /**
104
- * Clase base del mensaje. Una sola clase toda la API de instancia vive aquí.
105
- * Base message class. Single class all instance API lives here.
130
+ * Clase base del mensaje: recibe el cliente y el raw, deriva el estado con getters y
131
+ * opera contra WhatsApp con sus métodos. Los estáticos reciben el cliente como primer
132
+ * argumento (`Message.text(wa, cid, …)`).
133
+ * Base message class: receives the client and the raw, derives state via getters and
134
+ * operates against WhatsApp with its methods. Statics take the client as their first
135
+ * argument (`Message.text(wa, cid, …)`).
106
136
  */
107
137
  export class Message {
108
- constructor(options) {
109
- /** @internal Cache de la promesa de `author()` para evitar engine round-trips repetidos. */
110
- this._author_cache = null;
111
- /** @internal Cache de la promesa de `chat()` para evitar engine round-trips repetidos. */
112
- this._chat_cache = null;
113
- this._wa = options.wa;
114
- this._doc = options.doc;
138
+ /**
139
+ * @internal Cliente dueño y documento persistido. `status`, `created_at` y
140
+ * `deleted_at` guardan los valores numéricos del protocolo; los getters los
141
+ * presentan legibles. `viewed` existe únicamente en los status broadcast (Feed).
142
+ * Owner client and persisted document. `status`, `created_at` and `deleted_at`
143
+ * keep the protocol numeric values; getters expose them readable. `viewed` only
144
+ * exists on status broadcasts (Feed).
145
+ */
146
+ constructor(_wa, _raw) {
147
+ this._wa = _wa;
148
+ this._raw = _raw;
115
149
  }
150
+ /** Identificador del mensaje. / Message identifier. */
116
151
  get id() {
117
- return this._doc.id;
152
+ return this._raw.id;
118
153
  }
154
+ /** Identificador del chat del mensaje. / Message chat identifier. */
119
155
  get cid() {
120
- return this._doc.cid;
156
+ return this._raw.cid;
121
157
  }
122
- get type() {
123
- return this._doc.type;
158
+ /** Identificador del mensaje citado, o null. / Quoted message identifier, or null. */
159
+ get mid() {
160
+ return this._raw.mid;
124
161
  }
125
- /** JID del autor del mensaje. Para acceso síncrono sin hidratar el Contact. */
162
+ /** JID del autor, para acceso síncrono. / Author JID, for sync access. */
126
163
  get from() {
127
- return this._doc.author;
128
- }
129
- /** MIME type del contenido. `text/plain` para texto, `application/json` para location/poll, mimetype real para media. */
130
- get mime() {
131
- return this._doc.mime;
132
- }
133
- get mid() {
134
- return this._doc.mid;
164
+ return this._raw.author;
135
165
  }
166
+ /** true si soy el autor. / true when I am the author. */
136
167
  get me() {
137
- return this._doc.me;
168
+ return this._raw.me;
138
169
  }
170
+ /** Tipo del mensaje. / Message type. */
171
+ get type() {
172
+ return this._raw.type;
173
+ }
174
+ /** MIME: text/plain en texto, text/json en poll/location/vcard/event, real en media. / MIME: text/plain for text, text/json for poll/location/vcard/event, actual for media. */
175
+ get mime() {
176
+ const { type } = this._raw;
177
+ if (type === 'text')
178
+ return 'text/plain';
179
+ if (type === 'poll' || type === 'location' || type === 'vcard' || type === 'event')
180
+ return 'text/json';
181
+ return this._raw.mime;
182
+ }
183
+ /** Texto del mensaje o caption del media. / Message text or media caption. */
139
184
  get caption() {
140
- return this._doc.caption;
185
+ return this._raw.caption;
186
+ }
187
+ /** Estado legible del mensaje. / Readable message state. */
188
+ get status() {
189
+ return STATUS[this._raw.status] ?? 'pending';
141
190
  }
191
+ /** true si el mensaje fue visto. / true when the message was seen. */
192
+ get read() {
193
+ return this._raw.status >= 4;
194
+ }
195
+ /** true si está destacado. / true when starred. */
142
196
  get starred() {
143
- return this._doc.starred;
197
+ return this._raw.starred;
144
198
  }
199
+ /** true si fue reenviado. / true when forwarded. */
145
200
  get forwarded() {
146
- return this._doc.forwarded;
201
+ return this._raw.forwarded;
202
+ }
203
+ /** true si fue editado. / true when edited. */
204
+ get edited() {
205
+ return this._raw.edited;
147
206
  }
207
+ /** true si es de una sola lectura (view-once). / true when view-once. */
148
208
  get once() {
149
- return this._doc.deleted_at !== null;
209
+ const msg = this._raw.raw.message;
210
+ return Boolean(msg?.viewOnceMessage ?? msg?.viewOnceMessageV2 ?? msg?.viewOnceMessageV2Extension);
150
211
  }
212
+ /** Fecha de creación en ISO UTC. / Creation date as ISO UTC. */
151
213
  get created_at() {
152
- return this._doc.created_at;
153
- }
154
- get deleted_at() {
155
- return this._doc.deleted_at;
214
+ return new Date(this._raw.created_at).toISOString();
156
215
  }
157
- get status() {
158
- return this._doc.status;
159
- }
160
- get edited() {
161
- return this._doc.edited;
216
+ /** Vencimiento del mensaje temporal en ISO UTC, o null. / Ephemeral expiration as ISO UTC, or null. */
217
+ get expires_at() {
218
+ return this._raw.deleted_at != null ? new Date(this._raw.deleted_at).toISOString() : null;
162
219
  }
163
220
  /**
164
- * Contacto del autor (resuelve vía `wa.Contact.get`; fallback a instancia mínima).
165
- * Memoizado por instancia: la primera llamada va al engine; las siguientes
166
- * reutilizan la misma promesa sin nuevos round-trips.
167
- * Author contact (resolves via `wa.Contact.get`; falls back to a minimal instance).
168
- * Instance-memoized: first call hits the engine; subsequent calls reuse the
169
- * same promise without further round-trips.
221
+ * Contacto autor del mensaje, desde el engine (ficha mínima si no está persistido).
222
+ * Message author contact, from the engine (minimal card when not persisted).
223
+ *
224
+ * @returns Contacto / Contact
170
225
  */
171
226
  async author() {
172
- return (this._author_cache ??= (async () => {
173
- const { _wa, _doc } = this;
174
- const fetched = _doc.author ? await _wa.Contact.get(_doc.author) : null;
175
- return (fetched ??
176
- new _wa.Contact({
177
- id: _doc.author,
178
- lid: null,
179
- name: null,
180
- notify: null,
181
- verified_name: null,
182
- img_url: null,
183
- status: null,
184
- }, new _wa.Chat({ id: _doc.author, name: _doc.author.split('@')[0] })));
185
- })());
227
+ const cached = deserialize(await this._wa.engine.get(`/contact/${this._raw.author}`));
228
+ return new this._wa.Contact(cached ?? { id: this._raw.author });
186
229
  }
187
230
  /**
188
- * Chat al que pertenece el mensaje. Memoizado por instancia: la primera llamada
189
- * va al engine; las siguientes reutilizan la misma promesa.
190
- * Chat the message belongs to. Instance-memoized: first call hits the engine;
191
- * subsequent calls reuse the same promise.
231
+ * Chat al que pertenece el mensaje.
232
+ * Chat the message belongs to.
233
+ *
234
+ * @returns Chat / Chat
192
235
  */
193
236
  async chat() {
194
- return (this._chat_cache ??= (async () => {
195
- const cached = deserialize(await this._wa.engine.get(`/chat/${this._doc.cid}`));
196
- return new this._wa.Chat(cached ?? { id: this._doc.cid, name: this._doc.cid.split('@')[0] });
197
- })());
237
+ const cached = deserialize(await this._wa.engine.get(`/chat/${this._raw.cid}`));
238
+ return new this._wa.Chat(cached ?? { id: this._raw.cid });
239
+ }
240
+ /**
241
+ * Mensaje citado cuando hay `mid`, o null.
242
+ * Quoted message when `mid` exists, or null.
243
+ *
244
+ * @returns Mensaje citado o null / Quoted message or null
245
+ */
246
+ async message() {
247
+ return this._raw.mid ? Message.get(this._wa, this._raw.cid, this._raw.mid) : null;
198
248
  }
199
- /** Contenido sin parsear, directo del engine. Las subclases override según su tipo. */
249
+ /**
250
+ * Contenido del mensaje: texto como su caption, media como binario, y
251
+ * poll/location/vcard/event como JSON.
252
+ * Message content: text as its caption, media as binary, and
253
+ * poll/location/vcard/event as JSON.
254
+ *
255
+ * @returns Buffer del contenido / Content buffer
256
+ */
200
257
  async content() {
201
- const cached = deserialize(await this._wa.engine.get(`/chat/${this._doc.cid}/message/${this._doc.id}/content`));
258
+ const cached = deserialize(await this._wa.engine.get(`/chat/${this._raw.cid}/message/${this._raw.id}/content`));
202
259
  return cached?.data ? Buffer.from(cached.data, 'base64') : Buffer.alloc(0);
203
260
  }
204
261
  /**
205
- * Devuelve un `Readable` con los bytes del mensaje. Para media (image/video/audio)
206
- * lee desde el cache del engine o, si falta, descarga desde baileys. Para text/location/poll
207
- * retorna el mismo binario que `content()` envuelto en un Readable.
208
- * Returns a `Readable` of the message bytes. For media (image/video/audio) it reads
209
- * from the engine cache or, if missing, downloads from baileys. For text/location/poll
210
- * it wraps the `content()` buffer in a Readable.
262
+ * Stream del contenido; para media descarga de baileys si el cache falta.
263
+ * Content stream; media falls back to a baileys download when the cache is missing.
264
+ *
265
+ * @returns Readable del contenido / Content readable
211
266
  */
212
267
  async stream() {
213
- return _media_stream(this);
268
+ return media_stream(this._wa, this._raw);
269
+ }
270
+ /**
271
+ * Reacciones del mensaje agrupadas por emoji con su conteo.
272
+ * Message reactions grouped by emoji with their count.
273
+ *
274
+ * @returns Conteo por emoji / Per-emoji count
275
+ */
276
+ async reactions() {
277
+ const counts = new Map();
278
+ for (const { emoji } of this._raw.reactions ?? []) {
279
+ counts.set(emoji, (counts.get(emoji) ?? 0) + 1);
280
+ }
281
+ return [...counts.entries()].map(([emoji, count]) => ({ emoji, count }));
214
282
  }
215
- /** Reacciona al mensaje (emoji vacío para retirar). */
283
+ /**
284
+ * Reacciona al mensaje; el emoji vacío retira la reacción.
285
+ * Reacts to the message; an empty emoji removes the reaction.
286
+ *
287
+ * @param emoji - Emoji o '' / Emoji or ''
288
+ * @returns true si se envió / true when sent
289
+ */
216
290
  async react(emoji) {
217
- const { _wa, _doc } = this;
218
291
  let ok = false;
219
- if (_wa._socket) {
220
- await _wa._socket.sendMessage(_doc.cid, {
221
- react: { text: emoji, key: { remoteJid: _doc.cid, id: _doc.id, fromMe: _doc.me } },
292
+ const socket = internals(this._wa).socket;
293
+ if (socket) {
294
+ const { cid, id, me, author } = this._raw;
295
+ await socket.sendMessage(cid, {
296
+ react: {
297
+ text: emoji,
298
+ key: { remoteJid: cid, id, fromMe: me, ...(cid.endsWith('@g.us') && !me && { participant: author }) },
299
+ },
222
300
  });
223
301
  ok = true;
224
302
  }
225
303
  return ok;
226
304
  }
227
- /** Reenvía el mensaje a un destino (CID, Chat o Contact). */
228
- async forward(target) {
229
- const { _wa, _doc } = this;
230
- const to_cid = typeof target === 'string'
231
- ? target
232
- : target instanceof Chat
233
- ? target.id
234
- : (await target.chat()).id;
305
+ /**
306
+ * Destaca o quita el destacado del mensaje.
307
+ * Stars or unstars the message.
308
+ *
309
+ * @param value - true destaca / true stars
310
+ * @returns true si se envió / true when sent
311
+ */
312
+ async star(value) {
235
313
  let ok = false;
236
- if (_wa._socket) {
237
- const jid = await _wa._resolve_jid(to_cid);
238
- if (jid) {
239
- if (_doc.raw.message) {
240
- try {
241
- const wa_msg = generateWAMessageFromContent(jid, generateForwardMessageContent(_doc.raw, false), { userJid: _wa._socket.user?.id ?? '' });
242
- if (wa_msg.message && wa_msg.key?.id) {
243
- await _wa._socket.relayMessage(jid, wa_msg.message, { messageId: wa_msg.key.id });
244
- ok = true;
245
- }
246
- }
247
- catch {
248
- /* fallback below */
249
- }
250
- }
251
- if (!ok) {
252
- const buf = await this.content();
253
- if (buf.length > 0) {
254
- if (_doc.type === 'text') {
255
- ok = (await _wa.Message.text(jid, buf.toString())) !== null;
256
- }
257
- else if (_doc.type === 'image') {
258
- ok =
259
- (await _wa.Message.image(jid, buf, { caption: _doc.caption || undefined })) !==
260
- null;
261
- }
262
- else if (_doc.type === 'video') {
263
- ok =
264
- (await _wa.Message.video(jid, buf, { caption: _doc.caption || undefined })) !==
265
- null;
266
- }
267
- else if (_doc.type === 'audio') {
268
- ok = (await _wa.Message.audio(jid, buf)) !== null;
269
- }
270
- else if (_doc.type === 'location') {
271
- try {
272
- ok =
273
- (await _wa.Message.location(jid, JSON.parse(buf.toString()))) !== null;
274
- }
275
- catch {
276
- /* invalid payload */
277
- }
278
- }
279
- }
280
- }
281
- }
314
+ const socket = internals(this._wa).socket;
315
+ if (socket) {
316
+ const doc = this._raw;
317
+ await socket.chatModify({ star: { messages: [{ id: doc.id, fromMe: doc.me }], star: value } }, doc.cid);
318
+ doc.starred = value;
319
+ await this._wa.engine.set(`/chat/${doc.cid}/message/${doc.id}`, serialize(doc), doc.created_at);
320
+ ok = true;
282
321
  }
283
322
  return ok;
284
323
  }
285
- /** Marca el mensaje como leído. */
324
+ /**
325
+ * Marca el mensaje como leído.
326
+ * Marks the message as read.
327
+ *
328
+ * @returns true si se envió / true when sent
329
+ */
286
330
  async seen() {
287
- const { _wa, _doc } = this;
288
331
  let ok = false;
289
- if (_wa._socket) {
290
- const is_group = _doc.cid.endsWith('@g.us');
291
- await _wa._socket.readMessages([
292
- { remoteJid: _doc.cid, id: _doc.id, participant: is_group ? _doc.author : undefined },
332
+ const socket = internals(this._wa).socket;
333
+ if (socket) {
334
+ const { cid, id, author } = this._raw;
335
+ await socket.readMessages([
336
+ { remoteJid: cid, id, participant: cid.endsWith('@g.us') ? author : undefined },
293
337
  ]);
294
338
  ok = true;
295
339
  }
296
340
  return ok;
297
341
  }
298
- /** Destaca/retira destacado del mensaje. */
299
- async star(value) {
300
- const { _wa: wa, _doc: doc } = this;
342
+ /**
343
+ * Edita el caption del mensaje propio (texto, imagen o video).
344
+ * Edits the own message caption (text, image or video).
345
+ *
346
+ * @param caption - Texto nuevo / New text
347
+ * @returns true si se editó / true when edited
348
+ */
349
+ async edit(caption) {
350
+ const doc = this._raw;
301
351
  let ok = false;
302
- if (wa._socket) {
303
- await wa._socket.chatModify({
304
- star: { messages: [{ id: doc.id, fromMe: doc.me }], star: value },
305
- }, doc.cid);
306
- doc.starred = value;
307
- await wa.engine.set(`/chat/${doc.cid}/message/${doc.id}`, serialize(doc));
308
- ok = true;
352
+ const socket = internals(this._wa).socket;
353
+ if (socket && doc.me) {
354
+ if (doc.type === 'text') {
355
+ await socket.sendMessage(doc.cid, {
356
+ text: caption,
357
+ edit: { remoteJid: doc.cid, id: doc.id, fromMe: true },
358
+ });
359
+ const content_type = getContentType(doc.raw.message ?? {});
360
+ doc.raw.message =
361
+ content_type === 'conversation' ? { conversation: caption } : { extendedTextMessage: { text: caption } };
362
+ ok = true;
363
+ }
364
+ else if (doc.type === 'image' || doc.type === 'video') {
365
+ const media_key = doc.type === 'image' ? 'imageMessage' : 'videoMessage';
366
+ const media = doc.raw.message?.[media_key];
367
+ if (media) {
368
+ const edited = { [media_key]: { ...media, caption } };
369
+ await socket.relayMessage(doc.cid, {
370
+ protocolMessage: {
371
+ key: { remoteJid: doc.cid, id: doc.id, fromMe: true },
372
+ type: proto.Message.ProtocolMessage.Type.MESSAGE_EDIT,
373
+ editedMessage: edited,
374
+ timestampMs: Date.now(),
375
+ },
376
+ }, { messageId: generateMessageID() });
377
+ doc.raw.message = { ...doc.raw.message, ...edited };
378
+ ok = true;
379
+ }
380
+ }
381
+ if (ok) {
382
+ doc.caption = caption;
383
+ doc.edited = true;
384
+ await this._wa.engine.set(`/chat/${doc.cid}/message/${doc.id}`, serialize(doc), doc.created_at);
385
+ }
309
386
  }
310
387
  return ok;
311
388
  }
312
- /** Elimina el mensaje. `all=true` (default) borra para todos; `all=false` borra sólo en mi dispositivo. */
313
- async delete(all = true) {
314
- const { _wa: wa, _doc: doc } = this;
389
+ /**
390
+ * Reenvía el mensaje a otro chat.
391
+ * Forwards the message to another chat.
392
+ *
393
+ * @param target - CID, Chat o Contact destino / Target CID, Chat or Contact
394
+ * @returns true si se reenvió / true when forwarded
395
+ */
396
+ async forward(target) {
397
+ const doc = this._raw;
398
+ const to_cid = typeof target === 'string' ? target : target instanceof Chat ? target._raw.id : (target.jid ?? target.lid ?? '');
399
+ let ok = false;
400
+ const socket = internals(this._wa).socket;
401
+ if (socket && to_cid) {
402
+ const jid = await internals(this._wa).resolve_jid(to_cid);
403
+ if (jid && doc.raw.message) {
404
+ try {
405
+ const wa_msg = generateWAMessageFromContent(jid, generateForwardMessageContent(doc.raw, false), {
406
+ userJid: socket.user?.id ?? '',
407
+ });
408
+ if (wa_msg.message && wa_msg.key?.id) {
409
+ await socket.relayMessage(jid, wa_msg.message, { messageId: wa_msg.key.id });
410
+ ok = true;
411
+ }
412
+ }
413
+ catch {
414
+ /* forward content may be unsupported */
415
+ }
416
+ }
417
+ }
418
+ return ok;
419
+ }
420
+ /**
421
+ * Elimina el mensaje: solo en mi dispositivo por defecto, para todos con `true`.
422
+ * Deletes the message: this device only by default, for everyone with `true`.
423
+ *
424
+ * @param all - true elimina para todos / true deletes for everyone
425
+ * @returns true si se eliminó / true when deleted
426
+ */
427
+ async delete(all = false) {
428
+ const doc = this._raw;
315
429
  let ok = false;
316
- if (wa._socket) {
430
+ const socket = internals(this._wa).socket;
431
+ if (socket) {
317
432
  if (all) {
318
- await wa._socket.sendMessage(doc.cid, {
319
- delete: { remoteJid: doc.cid, id: doc.id, fromMe: doc.me },
320
- });
433
+ await socket.sendMessage(doc.cid, { delete: { remoteJid: doc.cid, id: doc.id, fromMe: doc.me } });
321
434
  }
322
435
  else {
323
- await wa._socket.chatModify({
436
+ await socket.chatModify({
324
437
  deleteForMe: {
325
438
  deleteMedia: false,
326
439
  key: { remoteJid: doc.cid, id: doc.id, fromMe: doc.me },
@@ -328,161 +441,361 @@ export class Message {
328
441
  },
329
442
  }, doc.cid);
330
443
  }
331
- await wa.engine.unset(`/chat/${doc.cid}/message/${doc.id}`);
444
+ await this._wa.engine.unset(`/chat/${doc.cid}/message/${doc.id}`);
332
445
  ok = true;
333
446
  }
334
447
  return ok;
335
448
  }
336
- /** Edita el texto/caption del mensaje (solo propios editables). */
337
- async edit(text) {
338
- const { _wa: wa, _doc: doc } = this;
339
- let ok = false;
340
- if (wa._socket && doc.me) {
341
- try {
342
- await wa._socket.sendMessage(doc.cid, {
343
- text,
344
- edit: { remoteJid: doc.cid, id: doc.id, fromMe: true },
345
- });
346
- const content_type = getContentType(doc.raw.message ?? {});
347
- if (content_type === 'conversation') {
348
- doc.raw.message = { conversation: text };
349
- }
350
- else if (content_type === 'extendedTextMessage') {
351
- doc.raw.message = { extendedTextMessage: { text } };
352
- }
353
- doc.caption = text;
354
- doc.edited = true;
355
- await wa.engine.set(`/chat/${doc.cid}/message/${doc.id}`, serialize(doc));
356
- ok = true;
357
- }
358
- catch {
359
- ok = false;
360
- }
361
- }
362
- return ok;
449
+ /** Responde con texto. / Replies with text. */
450
+ async text(caption, extra = {}) {
451
+ return Message.text(this._wa, this._raw.cid, caption, { ...extra, mid: this._raw.id });
363
452
  }
364
- /** Observa cambios sobre este mensaje. Retorna unsubscribe. / Watches this message. Returns unsubscribe. */
365
- watch(handler) {
366
- const cid = this.cid;
367
- const id = this.id;
368
- return this._wa.on('message:updated', (updated) => {
369
- if (updated.cid === cid && updated.id === id) {
370
- handler(updated);
371
- }
372
- });
453
+ /** Responde con imagen. / Replies with an image. */
454
+ async image(buf, extra = {}) {
455
+ return Message.image(this._wa, this._raw.cid, buf, { ...extra, mid: this._raw.id });
373
456
  }
374
- /** Responde con texto. */
375
- async text(caption, opts = {}) {
376
- return this._wa.Message.text(this.cid, caption, { ...opts, mid: this.id });
457
+ /** Responde con video. / Replies with a video. */
458
+ async video(buf, extra = {}) {
459
+ return Message.video(this._wa, this._raw.cid, buf, { ...extra, mid: this._raw.id });
377
460
  }
378
- /** Responde con imagen. */
379
- async image(buf, opts = {}) {
380
- return this._wa.Message.image(this.cid, buf, { ...opts, mid: this.id });
461
+ /** Responde con audio. / Replies with audio. */
462
+ async audio(buf, extra = {}) {
463
+ return Message.audio(this._wa, this._raw.cid, buf, { ...extra, mid: this._raw.id });
381
464
  }
382
- /** Responde con video. */
383
- async video(buf, opts = {}) {
384
- return this._wa.Message.video(this.cid, buf, { ...opts, mid: this.id });
465
+ /** Responde con ubicación. / Replies with a location. */
466
+ async location(loc, extra = {}) {
467
+ return Message.location(this._wa, this._raw.cid, loc, { ...extra, mid: this._raw.id });
385
468
  }
386
- /** Responde con audio. */
387
- async audio(buf, opts = {}) {
388
- return this._wa.Message.audio(this.cid, buf, { ...opts, mid: this.id });
469
+ /** Responde con encuesta. / Replies with a poll. */
470
+ async poll(input, extra = {}) {
471
+ return Message.poll(this._wa, this._raw.cid, input, { ...extra, mid: this._raw.id });
389
472
  }
390
- /** Responde con ubicación. */
391
- async location(loc, opts = {}) {
392
- return this._wa.Message.location(this.cid, loc, { ...opts, mid: this.id });
473
+ /** Responde con documento. / Replies with a document. */
474
+ async document(buf, extra) {
475
+ return Message.document(this._wa, this._raw.cid, buf, { ...extra, mid: this._raw.id });
393
476
  }
394
- /** Responde con encuesta. */
395
- async poll(poll, opts = {}) {
396
- return this._wa.Message.poll(this.cid, poll, { ...opts, mid: this.id });
477
+ /** Responde con tarjeta(s) de contacto. / Replies with contact card(s). */
478
+ async vcard(contacts, extra = {}) {
479
+ return Message.vcard(this._wa, this._raw.cid, contacts, { ...extra, mid: this._raw.id });
397
480
  }
398
- /** Responde con documento. */
399
- async document(buf, opts) {
400
- return this._wa.Message.document(this.cid, buf, { ...opts, mid: this.id });
481
+ /** Responde con evento. / Replies with an event. */
482
+ async event(data, extra = {}) {
483
+ return Message.event(this._wa, this._raw.cid, data, { ...extra, mid: this._raw.id });
484
+ }
485
+ /**
486
+ * Obtiene un mensaje por chat y id.
487
+ * Retrieves a message by chat and id.
488
+ *
489
+ * @param wa - Cliente dueño / Owner client
490
+ * @param cid - Identificador del chat / Chat identifier
491
+ * @param mid - Identificador del mensaje / Message identifier
492
+ * @returns Mensaje o null / Message or null
493
+ */
494
+ static async get(wa, cid, mid) {
495
+ const jid = await internals(wa).resolve_jid(cid);
496
+ if (jid) {
497
+ const doc = deserialize(await wa.engine.get(`/chat/${jid}/message/${mid}`));
498
+ if (doc)
499
+ return message(wa, doc);
500
+ }
501
+ return null;
401
502
  }
402
- /** Responde con tarjeta(s) de contacto. */
403
- async vcard(contacts, opts = {}) {
404
- return this._wa.Message.vcard(this.cid, contacts, { ...opts, mid: this.id });
503
+ /**
504
+ * Pagina los mensajes de un chat desde el más reciente.
505
+ * Paginates chat messages from the most recent one.
506
+ *
507
+ * @param wa - Cliente dueño / Owner client
508
+ * @param cid - Identificador del chat / Chat identifier
509
+ * @param offset - Desplazamiento / Offset
510
+ * @param limit - Tamaño de página / Page size
511
+ * @returns Página de mensajes / Message page
512
+ */
513
+ static async list(wa, cid, offset = 0, limit = 50) {
514
+ const jid = await internals(wa).resolve_jid(cid);
515
+ if (jid) {
516
+ return (await wa.engine.list(`/chat/${jid}/message`, offset, limit))
517
+ .map((raw) => deserialize(raw))
518
+ .filter((doc) => doc !== null)
519
+ .map((doc) => message(wa, doc));
520
+ }
521
+ return [];
522
+ }
523
+ /** Envía texto. / Sends text. */
524
+ static async text(wa, cid, caption, extra = {}) {
525
+ return send(wa, cid, { text: caption }, undefined, extra);
526
+ }
527
+ /** Envía imagen. / Sends an image. */
528
+ static async image(wa, cid, buf, extra = {}) {
529
+ return send(wa, cid, { image: buf, caption: extra.caption }, buf, extra);
530
+ }
531
+ /** Envía video. / Sends a video. */
532
+ static async video(wa, cid, buf, extra = {}) {
533
+ return send(wa, cid, { video: buf, caption: extra.caption }, buf, extra);
534
+ }
535
+ /** Envía audio. / Sends audio. */
536
+ static async audio(wa, cid, buf, extra = {}) {
537
+ return send(wa, cid, { audio: buf, ptt: extra.ptt ?? true }, buf, extra);
538
+ }
539
+ /** Envía ubicación. / Sends a location. */
540
+ static async location(wa, cid, loc, extra = {}) {
541
+ return send(wa, cid, { location: { degreesLatitude: loc.lat, degreesLongitude: loc.lng } }, undefined, extra);
542
+ }
543
+ /** Envía encuesta. / Sends a poll. */
544
+ static async poll(wa, cid, input, extra = {}) {
545
+ const multiple = extra.multiple ?? false;
546
+ return send(wa, cid, {
547
+ poll: {
548
+ name: input.content,
549
+ values: input.options.map((option) => option.content),
550
+ selectableCount: multiple ? 0 : 1,
551
+ },
552
+ }, undefined, extra, { multiple });
553
+ }
554
+ /** Envía documento. / Sends a document. */
555
+ static async document(wa, cid, buf, extra) {
556
+ return send(wa, cid, { document: buf, fileName: extra.file_name, mimetype: extra.mimetype ?? 'application/octet-stream', caption: extra.caption }, buf, extra);
557
+ }
558
+ /** Envía tarjeta(s) de contacto. / Sends contact card(s). */
559
+ static async vcard(wa, cid, contacts, extra = {}) {
560
+ const built = contacts.map((entry) => ({
561
+ displayName: entry.name,
562
+ vcard: [
563
+ 'BEGIN:VCARD',
564
+ 'VERSION:3.0',
565
+ `FN:${entry.name}`,
566
+ `TEL;type=CELL;waid=${entry.phone.replace(/\D+/g, '')}:${entry.phone}`,
567
+ 'END:VCARD',
568
+ ].join('\n'),
569
+ }));
570
+ return send(wa, cid, { contacts: { contacts: built } }, Buffer.from(built.map((entry) => entry.vcard).join('\n'), 'utf-8'), extra);
405
571
  }
406
- /** Responde con evento. */
407
- async event(data, opts = {}) {
408
- return this._wa.Message.event(this.cid, data, { ...opts, mid: this.id });
572
+ /** Envía evento. / Sends an event. */
573
+ static async event(wa, cid, data, extra = {}) {
574
+ const binary = Buffer.from(JSON.stringify({ name: data.name, caption: data.caption, start: data.start.getTime(), end: data.end?.getTime() ?? null }), 'utf-8');
575
+ return send(wa, cid, {
576
+ event: {
577
+ name: data.name,
578
+ description: data.caption,
579
+ startDate: data.start,
580
+ endDate: data.end,
581
+ location: data.place ? { degreesLatitude: data.place.lat, degreesLongitude: data.place.lng } : undefined,
582
+ },
583
+ }, binary, extra);
584
+ }
585
+ /** Reacciona a un mensaje por id. / Reacts to a message by id. */
586
+ static async react(wa, cid, mid, emoji) {
587
+ const msg = await Message.get(wa, cid, mid);
588
+ return msg ? msg.react(emoji) : false;
589
+ }
590
+ /** Destaca un mensaje por id. / Stars a message by id. */
591
+ static async star(wa, cid, mid, value) {
592
+ const msg = await Message.get(wa, cid, mid);
593
+ return msg ? msg.star(value) : false;
594
+ }
595
+ /** Marca un mensaje como leído por id. / Marks a message as read by id. */
596
+ static async seen(wa, cid, mid) {
597
+ const msg = await Message.get(wa, cid, mid);
598
+ return msg ? msg.seen() : false;
599
+ }
600
+ /** Edita un mensaje por id. / Edits a message by id. */
601
+ static async edit(wa, cid, mid, caption) {
602
+ const msg = await Message.get(wa, cid, mid);
603
+ return msg ? msg.edit(caption) : false;
604
+ }
605
+ /** Reenvía un mensaje por id. / Forwards a message by id. */
606
+ static async forward(wa, cid, mid, target) {
607
+ const msg = await Message.get(wa, cid, mid);
608
+ return msg ? msg.forward(target) : false;
609
+ }
610
+ /** Elimina un mensaje por id. / Deletes a message by id. */
611
+ static async delete(wa, cid, mid, all = false) {
612
+ const msg = await Message.get(wa, cid, mid);
613
+ return msg ? msg.delete(all) : false;
614
+ }
615
+ /** Reacciones de un mensaje por id. / Reactions of a message by id. */
616
+ static async reactions(wa, cid, mid) {
617
+ const msg = await Message.get(wa, cid, mid);
618
+ return msg ? msg.reactions() : [];
409
619
  }
410
620
  }
411
621
  /**
412
- * Mensaje de texto. Override `content()` para retornar el caption como Buffer sin pasar por engine.
413
- * Text message. Overrides `content()` returning caption as Buffer directly.
622
+ * Mensaje de texto; `preview()` expone la tarjeta del enlace embebido.
623
+ * Text message; `preview()` exposes the embedded link card.
414
624
  */
415
625
  export class Text extends Message {
416
- async content() {
417
- return Buffer.from(this.caption, 'utf-8');
418
- }
419
- }
420
- /** @internal Helper compartido por media classes: stream del binario (cache download). */
421
- async function _media_stream(msg) {
422
- const wa = msg._wa;
423
- const doc = msg._doc;
424
- const cached = deserialize(await wa.engine.get(`/chat/${doc.cid}/message/${doc.id}/content`));
425
- if (cached?.data) {
426
- return Readable.from(Buffer.from(cached.data, 'base64'));
427
- }
428
- if (wa._socket) {
429
- try {
430
- return (await downloadMediaMessage(doc.raw, 'stream', {}));
431
- }
432
- catch {
433
- /* fallback empty */
626
+ /**
627
+ * Preview del enlace del texto, o null cuando no trae metadata.
628
+ * Text link preview, or null when it carries no metadata.
629
+ *
630
+ * @returns { link, name, content, thumb } o null / { link, name, content, thumb } or null
631
+ */
632
+ async preview() {
633
+ const ext = this._raw.raw.message?.extendedTextMessage;
634
+ if (ext?.matchedText && (ext.title || ext.description)) {
635
+ return {
636
+ link: ext.matchedText,
637
+ name: ext.title ?? null,
638
+ content: ext.description ?? null,
639
+ thumb: to_buffer(ext.jpegThumbnail),
640
+ };
434
641
  }
642
+ return null;
435
643
  }
436
- return Readable.from(Buffer.alloc(0));
437
- }
438
- /** @internal Acumula un Readable en Buffer. */
439
- async function _drain(readable) {
440
- const chunks = [];
441
- for await (const chunk of readable) {
442
- chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
443
- }
444
- return Buffer.concat(chunks);
445
644
  }
446
645
  /**
447
- * Mensaje de imagen. Hereda `stream()` de la clase base; `content()` drena el stream.
448
- * Image message. Inherits `stream()` from the base class; `content()` drains the stream.
646
+ * Mensaje de imagen: dimensiones, peso y miniatura.
647
+ * Image message: dimensions, size and thumbnail.
449
648
  */
450
649
  export class Image extends Message {
650
+ /** @internal */
651
+ get _media() {
652
+ return unwrap(this._raw.raw.message ?? {}).imageMessage;
653
+ }
654
+ /** Ancho en píxeles. / Width in pixels. */
655
+ get width() {
656
+ return this._media?.width ?? 0;
657
+ }
658
+ /** Alto en píxeles. / Height in pixels. */
659
+ get height() {
660
+ return this._media?.height ?? 0;
661
+ }
662
+ /** Peso en bytes. / Size in bytes. */
663
+ get size() {
664
+ return to_number(this._media?.fileLength);
665
+ }
666
+ /**
667
+ * Miniatura JPEG embebida, o null.
668
+ * Embedded JPEG thumbnail, or null.
669
+ *
670
+ * @returns Buffer de la miniatura o null / Thumbnail buffer or null
671
+ */
672
+ async thumb() {
673
+ return to_buffer(this._media?.jpegThumbnail);
674
+ }
451
675
  async content() {
452
- return _drain(await this.stream());
676
+ return drain(await this.stream());
453
677
  }
454
678
  }
455
679
  /**
456
- * Mensaje de video. Hereda `stream()` de la clase base; `content()` drena el stream.
457
- * Video message. Inherits `stream()` from the base class; `content()` drains the stream.
680
+ * Mensaje de video: dimensiones, duración, peso y miniatura.
681
+ * Video message: dimensions, duration, size and thumbnail.
458
682
  */
459
683
  export class Video extends Message {
684
+ /** @internal */
685
+ get _media() {
686
+ return unwrap(this._raw.raw.message ?? {}).videoMessage;
687
+ }
688
+ /** Ancho en píxeles. / Width in pixels. */
689
+ get width() {
690
+ return this._media?.width ?? 0;
691
+ }
692
+ /** Alto en píxeles. / Height in pixels. */
693
+ get height() {
694
+ return this._media?.height ?? 0;
695
+ }
696
+ /** Duración en segundos. / Duration in seconds. */
697
+ get duration() {
698
+ return this._media?.seconds ?? 0;
699
+ }
700
+ /** Peso en bytes. / Size in bytes. */
701
+ get size() {
702
+ return to_number(this._media?.fileLength);
703
+ }
704
+ /**
705
+ * Miniatura JPEG embebida, o null.
706
+ * Embedded JPEG thumbnail, or null.
707
+ *
708
+ * @returns Buffer de la miniatura o null / Thumbnail buffer or null
709
+ */
710
+ async thumb() {
711
+ return to_buffer(this._media?.jpegThumbnail);
712
+ }
460
713
  async content() {
461
- return _drain(await this.stream());
714
+ return drain(await this.stream());
462
715
  }
463
716
  }
464
717
  /**
465
- * Mensaje de audio. Hereda `stream()` de la clase base; `content()` drena el stream.
466
- * `ptt` indica si es nota de voz (push-to-talk).
467
- * Audio message. Inherits `stream()` from the base class; `content()` drains the stream.
468
- * `ptt` flags voice notes.
718
+ * Mensaje de audio: nota de voz o archivo, con forma de onda del protocolo.
719
+ * Audio message: voice note or file, with the protocol waveform.
469
720
  */
470
721
  export class Audio extends Message {
471
- /** true si es nota de voz (push-to-talk). / true if push-to-talk voice note. */
722
+ /** @internal */
723
+ get _media() {
724
+ return unwrap(this._raw.raw.message ?? {}).audioMessage;
725
+ }
726
+ /** true si es nota de voz. / true when push-to-talk. */
472
727
  get ptt() {
473
- return this._doc.raw.message?.audioMessage?.ptt === true;
728
+ return this._media?.ptt === true;
729
+ }
730
+ /** Duración en segundos. / Duration in seconds. */
731
+ get duration() {
732
+ return this._media?.seconds ?? 0;
733
+ }
734
+ /** Peso en bytes. / Size in bytes. */
735
+ get size() {
736
+ return to_number(this._media?.fileLength);
737
+ }
738
+ /** Forma de onda 0-100 lista para pintar. / Paint-ready 0-100 waveform. */
739
+ get waveform() {
740
+ const wave = this._media?.waveform;
741
+ const bytes = wave instanceof Uint8Array ? wave : typeof wave === 'string' ? Buffer.from(wave, 'base64') : Buffer.alloc(0);
742
+ return Array.from(bytes);
743
+ }
744
+ async content() {
745
+ return drain(await this.stream());
746
+ }
747
+ }
748
+ /**
749
+ * Mensaje de sticker: dimensiones, peso y animación.
750
+ * Sticker message: dimensions, size and animation.
751
+ */
752
+ export class Sticker extends Message {
753
+ /** @internal */
754
+ get _media() {
755
+ return unwrap(this._raw.raw.message ?? {}).stickerMessage;
756
+ }
757
+ /** Ancho en píxeles. / Width in pixels. */
758
+ get width() {
759
+ return this._media?.width ?? 0;
760
+ }
761
+ /** Alto en píxeles. / Height in pixels. */
762
+ get height() {
763
+ return this._media?.height ?? 0;
764
+ }
765
+ /** true si el sticker es animado. / true when animated. */
766
+ get animated() {
767
+ return this._media?.isAnimated === true;
768
+ }
769
+ /** Peso en bytes. / Size in bytes. */
770
+ get size() {
771
+ return to_number(this._media?.fileLength);
772
+ }
773
+ async content() {
774
+ return drain(await this.stream());
775
+ }
776
+ }
777
+ /**
778
+ * Mensaje de documento; el MIME real vive en `mime` de la base.
779
+ * Document message; the actual MIME lives in the base `mime`.
780
+ */
781
+ export class Document extends Message {
782
+ /** Peso en bytes. / Size in bytes. */
783
+ get size() {
784
+ const media = unwrap(this._raw.raw.message ?? {}).documentMessage;
785
+ return to_number(media?.fileLength);
474
786
  }
475
787
  async content() {
476
- return _drain(await this.stream());
788
+ return drain(await this.stream());
477
789
  }
478
790
  }
479
791
  /**
480
- * Mensaje de ubicación. Agrega getters sync `lat`, `lng`, `link` derivados del raw.
481
- * Location message. Adds sync getters `lat`, `lng`, `link` derived from raw.
792
+ * Mensaje de ubicación, fija o en vivo.
793
+ * Location message, static or live.
482
794
  */
483
- export class Gps extends Message {
795
+ export class Location extends Message {
796
+ /** @internal */
484
797
  get _loc() {
485
- return this._doc.raw.message?.locationMessage ?? this._doc.raw.message?.liveLocationMessage;
798
+ return this._raw.raw.message?.locationMessage ?? this._raw.raw.message?.liveLocationMessage;
486
799
  }
487
800
  /** Latitud en grados. / Latitude in degrees. */
488
801
  get lat() {
@@ -492,283 +805,184 @@ export class Gps extends Message {
492
805
  get lng() {
493
806
  return this._loc?.degreesLongitude ?? 0;
494
807
  }
495
- /** URL de Google Maps apuntando a la coordenada. / Google Maps URL for the coordinate. */
496
- get link() {
497
- return `https://www.google.com/maps/@${this.lat},${this.lng},15z`;
498
- }
499
808
  /** true si es ubicación en tiempo real. / true when live location. */
500
809
  get live() {
501
- return Boolean(this._doc.raw.message?.liveLocationMessage);
810
+ return Boolean(this._raw.raw.message?.liveLocationMessage);
811
+ }
812
+ /** URL de Google Maps de la coordenada. / Google Maps URL for the coordinate. */
813
+ get link() {
814
+ return `https://www.google.com/maps/@${this.lat},${this.lng},15z`;
502
815
  }
503
816
  }
504
817
  /**
505
- * Mensaje de encuesta. `caption` es la pregunta (heredada); agrega `options[]` con conteo y `multiple`.
506
- * Poll message. `caption` is the question (inherited); adds `options[]` with counts and `multiple`.
818
+ * Mensaje de encuesta: opciones con conteo, votos por contacto y voto propio.
819
+ * Poll message: options with counts, per-contact votes and self-voting.
507
820
  */
508
821
  export class Poll extends Message {
822
+ /** @internal */
509
823
  get _poll() {
510
- const m = this._doc.raw.message;
511
- return m?.pollCreationMessage ?? m?.pollCreationMessageV2 ?? m?.pollCreationMessageV3;
824
+ const msg = this._raw.raw.message;
825
+ return msg?.pollCreationMessage ?? msg?.pollCreationMessageV2 ?? msg?.pollCreationMessageV3;
826
+ }
827
+ /** @internal Updates de voto con los bytes normalizados post-engine. / Vote updates with post-engine normalized bytes. */
828
+ get _updates() {
829
+ return (this._raw.raw.pollUpdates ?? []).map((update) => ({
830
+ ...update,
831
+ vote: update.vote
832
+ ? {
833
+ ...update.vote,
834
+ selectedOptions: (update.vote.selectedOptions ?? []).map((option) => typeof option === 'string' ? Buffer.from(option, 'base64') : option),
835
+ }
836
+ : update.vote,
837
+ }));
512
838
  }
513
- /**
514
- * true si admite múltiples respuestas. Lee primero el flag explícito persistido
515
- * al enviar (caso propio, donde WhatsApp pierde el discriminador en el echo);
516
- * fallback al `selectableOptionsCount` del proto (polls foráneos o de UI móvil).
517
- * true if the poll accepts multiple answers. Prefers the persisted send-time
518
- * flag (own polls, since WhatsApp drops the discriminator on echo); falls back
519
- * to the proto `selectableOptionsCount` (foreign or mobile-UI polls).
520
- */
839
+ /** true si admite varias respuestas. / true when multi-select. */
521
840
  get multiple() {
522
- if (typeof this._doc.multiple === 'boolean') {
523
- return this._doc.multiple;
841
+ if (typeof this._raw.multiple === 'boolean') {
842
+ return this._raw.multiple;
524
843
  }
525
844
  return (this._poll?.selectableOptionsCount ?? 1) !== 1;
526
845
  }
527
- /** Opciones con conteo de votos actualizado. / Options with up-to-date vote counts. */
846
+ /** Opciones con su conteo de votos. / Options with their vote counts. */
528
847
  get options() {
529
- const opts = this._poll?.options ?? [];
530
- const raw_updates = this._doc.raw.pollUpdates ?? [];
531
- // Normaliza selectedOptions de base64-string a Buffer (BufferJSON no reconstruye el tipo aquí)
532
- const updates = raw_updates.map((u) => ({
533
- ...u,
534
- vote: u.vote
535
- ? {
536
- ...u.vote,
537
- selectedOptions: (u.vote.selectedOptions ?? []).map((s) => typeof s === 'string' ? Buffer.from(s, 'base64') : s),
538
- }
539
- : u.vote,
540
- }));
541
- const aggregated = getAggregateVotesInPollMessage({ message: this._doc.raw.message ?? undefined, pollUpdates: updates }, this._wa._socket?.user?.id);
542
- const counts = new Map(aggregated.map((a) => [a.name, a.voters.length]));
543
- return opts.map((o) => ({
544
- content: o.optionName ?? '',
545
- count: counts.get(o.optionName ?? '') ?? 0,
848
+ const aggregated = getAggregateVotesInPollMessage({ message: this._raw.raw.message ?? undefined, pollUpdates: this._updates }, internals(this._wa).socket?.user?.id);
849
+ const counts = new Map(aggregated.map((entry) => [entry.name, entry.voters.length]));
850
+ return (this._poll?.options ?? []).map((option) => ({
851
+ name: option.optionName ?? '',
852
+ count: counts.get(option.optionName ?? '') ?? 0,
546
853
  }));
547
854
  }
548
855
  /**
549
- * Vota en la encuesta. Acepta un índice o lista de índices (para `multiple`).
550
- * Cifra el voto (HMAC+AES-GCM, igual que baileys.decryptPollVote) y lo enruta al chat
551
- * @lid del contacto con la identidad LID propia, replicando byte a byte un voto entrante
552
- * que descifra. El voto ENTRANTE (contacto → este cliente) funciona; el voto propio
553
- * de este cliente aplicado a una encuesta NO se refleja en el teléfono del destinatario
554
- * cuando este cliente es un dispositivo vinculado (companion): estructura, identidades y
555
- * addressing coinciden con un voto válido, pero WhatsApp no lo propaga. Best-effort.
856
+ * Votos individuales: opción elegida y teléfono del votante.
857
+ * Individual votes: chosen option and voter phone.
858
+ *
859
+ * @returns Lista de votos / Vote list
860
+ */
861
+ async votes() {
862
+ const aggregated = getAggregateVotesInPollMessage({ message: this._raw.raw.message ?? undefined, pollUpdates: this._updates }, internals(this._wa).socket?.user?.id);
863
+ const result = [];
864
+ for (const entry of aggregated) {
865
+ for (const voter of entry.voters) {
866
+ const jid = await internals(this._wa).resolve_jid(voter);
867
+ result.push({ name: entry.name, contact: (jid ?? voter).split('@')[0].split(':')[0] });
868
+ }
869
+ }
870
+ return result;
871
+ }
872
+ /**
873
+ * Vota en la encuesta con uno o varios índices. Cifra el voto (HMAC+AES-GCM) y lo
874
+ * enruta al chat @lid con la identidad LID propia. Best-effort: el voto entrante
875
+ * descifra correcto, pero WhatsApp no propaga el voto emitido desde un dispositivo
876
+ * vinculado (companion).
877
+ * Votes in the poll with one or more indexes. Encrypts the vote (HMAC+AES-GCM) and
878
+ * routes it to the @lid chat with the own LID identity. Best-effort: incoming votes
879
+ * decrypt fine, but WhatsApp does not propagate votes emitted from a linked
880
+ * (companion) device.
556
881
  *
557
- * Votes in the poll. Encrypts the vote (HMAC+AES-GCM, same as baileys.decryptPollVote)
558
- * and routes it to the contact's @lid chat with our own LID identity, mirroring a working
559
- * incoming vote byte-for-byte. INCOMING votes work; a self-vote from this client does NOT
560
- * show on the recipient's phone when this client is a linked (companion) device — the
561
- * structure, identities and addressing match a valid vote but WhatsApp won't propagate it.
562
- * Best-effort.
882
+ * @param index - Índice o índices de la opción / Option index or indexes
883
+ * @returns true si el voto se emitió / true when the vote was relayed
563
884
  */
564
885
  async select(index) {
565
- const { _wa, _doc } = this;
566
- const indices = Array.isArray(index) ? index : [index];
886
+ const doc = this._raw;
567
887
  const opts = this._poll?.options ?? [];
568
- const selected_options = indices
888
+ const selected_options = (Array.isArray(index) ? index : [index])
569
889
  .filter((i) => i >= 0 && i < opts.length)
570
890
  .map((i) => sha256(Buffer.from(opts[i].optionName ?? '')));
571
- // messageSecret puede llegar como Buffer (raw runtime) o string base64 (post-deserialize
572
- // del engine). Normaliza a Buffer antes de derivar el HMAC.
573
- // messageSecret may arrive as Buffer (runtime raw) or base64 string (post engine
574
- // deserialize). Normalize to Buffer before deriving the HMAC.
575
- const secret_raw = _doc.raw.message?.messageContextInfo?.messageSecret;
891
+ const secret_raw = doc.raw.message?.messageContextInfo?.messageSecret;
576
892
  const poll_enc_key = typeof secret_raw === 'string' ? Buffer.from(secret_raw, 'base64') : secret_raw;
577
- if (!poll_enc_key || !_wa._socket || selected_options.length === 0) {
893
+ const socket = internals(this._wa).socket;
894
+ if (!poll_enc_key || !socket || selected_options.length === 0) {
578
895
  return false;
579
896
  }
580
- const poll_key = _doc.raw.key ?? {};
581
- const self_id = _wa._socket.user?.id ?? '';
582
- const self_lid = _wa._socket.user?.lid ?? '';
583
- // El voto se enruta al chat @lid del contacto: los clientes migrados corren el chat
584
- // en @lid y derivan la clave del JID visible del stanza; enviarlo por PN lo deja
585
- // indescifrable en el teléfono. El @lid destino sale del propio poll (si ya es @lid),
586
- // del store de contactos, o del lidMapping de baileys.
587
- // The vote is routed to the contact's @lid chat: migrated clients run the chat on
588
- // @lid and derive the key from the stanza's visible JID; sending via PN leaves it
589
- // undecryptable on the phone. The @lid dest comes from the poll (if already @lid),
590
- // the contact store, or baileys' lidMapping.
591
- let dest = _doc.cid;
897
+ const poll_key = doc.raw.key ?? {};
898
+ const self_id = socket.user?.id ?? '';
899
+ const self_lid = socket.user?.lid ?? '';
900
+ let dest = doc.cid;
592
901
  if (poll_key.remoteJid?.endsWith('@lid')) {
593
902
  dest = poll_key.remoteJid;
594
903
  }
595
- else if (!_doc.cid.endsWith('@lid')) {
596
- const contact_raw = deserialize(await _wa.engine.get(`/contact/${_doc.cid}`));
597
- const mapped = await _wa._socket.signalRepository?.lidMapping?.getLIDForPN(_doc.cid).catch(() => null);
598
- dest = contact_raw?.lid ?? mapped ?? _doc.cid;
904
+ else if (!doc.cid.endsWith('@lid')) {
905
+ const contact_raw = deserialize(await this._wa.engine.get(`/contact/${doc.cid}`));
906
+ const mapped = await socket.signalRepository?.lidMapping?.getLIDForPN(doc.cid).catch(() => null);
907
+ dest = contact_raw?.lid ?? mapped ?? doc.cid;
599
908
  }
600
- // Identidad del HMAC atada al destino real: baileys firma con meLid cuando el envío
601
- // va por @lid, así que voter y (en polls propios) creator usan el LID propio para que
602
- // el receptor derive la misma clave. PN sólo si no se conoce ningún LID.
603
- // HMAC identity tied to the actual destination: baileys signs with meLid for @lid
604
- // sends, so voter and (on own polls) creator use our LID so the receiver derives the
605
- // same key. PN only when no LID is known.
606
909
  const self_for_send = dest.endsWith('@lid') && self_lid ? self_lid : self_id;
607
910
  const voter_jid = jidNormalizedUser(self_for_send);
608
- let poll_creator_raw;
609
- if (poll_key.fromMe) {
610
- poll_creator_raw = self_for_send;
611
- }
612
- else if (poll_key.remoteJid?.endsWith('@lid')) {
613
- poll_creator_raw = poll_key.remoteJid;
614
- }
615
- else {
616
- poll_creator_raw = getKeyAuthor(poll_key, self_id);
617
- }
618
- const poll_creator_jid = jidNormalizedUser(poll_creator_raw);
619
- const poll_msg_id = this.id;
620
- // Derivar clave y encriptar el voto (mismo algoritmo que baileys.decryptPollVote)
911
+ const poll_creator_jid = jidNormalizedUser(poll_key.fromMe
912
+ ? self_for_send
913
+ : poll_key.remoteJid?.endsWith('@lid')
914
+ ? poll_key.remoteJid
915
+ : getKeyAuthor(poll_key, self_id));
621
916
  const sign = Buffer.concat([
622
- Buffer.from(poll_msg_id),
917
+ Buffer.from(doc.id),
623
918
  Buffer.from(poll_creator_jid),
624
919
  Buffer.from(voter_jid),
625
920
  Buffer.from('Poll Vote'),
626
921
  new Uint8Array([1]),
627
922
  ]);
628
- const key0 = hmacSign(poll_enc_key, new Uint8Array(32), 'sha256');
629
- const enc_key = hmacSign(sign, key0, 'sha256');
630
- const payload = proto.Message.PollVoteMessage.encode({
631
- selectedOptions: selected_options,
632
- }).finish();
633
- const aad = Buffer.from(`${poll_msg_id}\x00${voter_jid}`);
923
+ const enc_key = hmacSign(sign, hmacSign(poll_enc_key, new Uint8Array(32), 'sha256'), 'sha256');
634
924
  const enc_iv = randomBytes(12);
635
- const enc_payload = aesEncryptGCM(payload, enc_key, enc_iv, aad);
925
+ const enc_payload = aesEncryptGCM(proto.Message.PollVoteMessage.encode({ selectedOptions: selected_options }).finish(), enc_key, enc_iv, Buffer.from(`${doc.id}\x00${voter_jid}`));
636
926
  const msg_id = generateMessageID();
637
- // La pollCreationMessageKey debe referenciar el poll con el mismo addressing que el
638
- // stanza (dest): un voto entrante que sí funciona trae la key con remoteJid = @lid del
639
- // creador, coincidiendo con el chat @lid del stanza. Enviarla con remoteJid PN mientras
640
- // el stanza va por @lid deja al receptor sin poder casar el voto con el poll.
641
- // The pollCreationMessageKey must reference the poll with the same addressing as the
642
- // stanza (dest): a working incoming vote carries the key with the creator's @lid
643
- // remoteJid, matching the @lid stanza chat. A PN remoteJid on an @lid stanza leaves the
644
- // receiver unable to match the vote to the poll.
645
- const creation_key = dest === _doc.cid ? _doc.raw.key : { ..._doc.raw.key, remoteJid: dest };
646
- await _wa._socket.relayMessage(dest, {
927
+ await socket.relayMessage(dest, {
647
928
  pollUpdateMessage: {
648
- pollCreationMessageKey: creation_key,
929
+ pollCreationMessageKey: dest === doc.cid ? doc.raw.key : { ...doc.raw.key, remoteJid: dest },
649
930
  vote: { encPayload: enc_payload, encIv: enc_iv },
650
931
  senderTimestampMs: Date.now(),
651
932
  },
652
933
  }, { messageId: msg_id });
653
- // baileys.relayMessage no echa upsert local al companion que envió el voto.
654
- // Mergemos el voto propio en el doc para que `options[].count` y el evento
655
- // `message:updated` reflejen el cambio sin esperar (nunca) un echo del servidor.
656
- // baileys.relayMessage doesn't emit a local upsert on the originating companion.
657
- // We merge the own vote into the doc so `options[].count` and the `message:updated`
658
- // event reflect the change without waiting for a server echo that never comes.
659
- const own_update_key = {
660
- remoteJid: _doc.raw.key?.remoteJid ?? _doc.cid,
661
- fromMe: true,
662
- id: msg_id,
663
- };
664
- updateMessageWithPollUpdate(_doc.raw, {
665
- pollUpdateMessageKey: own_update_key,
934
+ updateMessageWithPollUpdate(doc.raw, {
935
+ pollUpdateMessageKey: { remoteJid: doc.raw.key?.remoteJid ?? doc.cid, fromMe: true, id: msg_id },
666
936
  vote: { selectedOptions: selected_options },
667
937
  senderTimestampMs: Date.now(),
668
938
  });
669
- await _wa.engine.set(`/chat/${_doc.cid}/message/${_doc.id}`, serialize(_doc));
670
- _wa._event.emit('message:updated', this, await this.chat(), _wa);
939
+ await this._wa.engine.set(`/chat/${doc.cid}/message/${doc.id}`, serialize(doc), doc.created_at);
940
+ this._wa.emit('message:updated', this, await this.chat(), this._wa);
671
941
  return true;
672
942
  }
673
943
  }
674
944
  /**
675
- * Mensaje de documento. WhatsApp permite enviar cualquier archivo (PDF, imagen,
676
- * audio, video, etc.) como documento; lo que lo distingue de image/video/audio es
677
- * el tipo de mensaje (`documentMessage`), no el mimetype. Hereda `stream()` de la
678
- * clase base; `content()` drena el stream (cache → download).
679
- * Document message. WhatsApp lets you send any file (PDF, image, audio, video…)
680
- * as a document; what sets it apart from image/video/audio is the message type
681
- * (`documentMessage`), not the mimetype. Inherits `stream()` from the base class;
682
- * `content()` drains the stream (cache → download).
683
- */
684
- export class Document extends Message {
685
- get _doc_msg() {
686
- const m = this._doc.raw.message;
687
- return m?.documentMessage ?? m?.documentWithCaptionMessage?.message?.documentMessage;
688
- }
689
- /** Nombre del archivo. / File name. */
690
- get file_name() {
691
- return this._doc_msg?.fileName ?? '';
692
- }
693
- /** Tamaño en bytes (0 si se desconoce). / Size in bytes (0 if unknown). */
694
- get size() {
695
- return Number(this._doc_msg?.fileLength?.toString() ?? '0');
696
- }
697
- /** Número de páginas (PDF); 0 si no aplica. / Page count (PDF); 0 when not applicable. */
698
- get pages() {
699
- return this._doc_msg?.pageCount ?? 0;
700
- }
701
- /** Título del documento. / Document title. */
702
- get title() {
703
- return this._doc_msg?.title ?? '';
704
- }
705
- async content() {
706
- return _drain(await this.stream());
707
- }
708
- }
709
- /**
710
- * Construye un vCard 3.0 mínimo con el teléfono enlazado como contacto de WhatsApp.
711
- * Builds a minimal vCard 3.0 with the phone linked as a WhatsApp contact.
712
- *
713
- * @param name - Nombre mostrado / Display name
714
- * @param phone - Teléfono en formato internacional / Phone in international format
715
- * @returns String vCard (VCF) / vCard (VCF) string
716
- */
717
- function build_vcard(name, phone) {
718
- const waid = phone.replace(/\D+/g, '');
719
- return [
720
- 'BEGIN:VCARD',
721
- 'VERSION:3.0',
722
- `FN:${name}`,
723
- `TEL;type=CELL;waid=${waid}:${phone}`,
724
- 'END:VCARD',
725
- ].join('\n');
726
- }
727
- /**
728
- * Mensaje de tarjeta(s) de contacto (vCard). Unifica `contactMessage` (uno) y
729
- * `contactsArrayMessage` (varios) bajo una sola lista `contacts`.
730
- * Contact card message (vCard). Unifies `contactMessage` (single) and
731
- * `contactsArrayMessage` (multiple) under a single `contacts` list.
945
+ * Mensaje de tarjeta(s) de contacto.
946
+ * Contact card(s) message.
732
947
  */
733
948
  export class VCard extends Message {
734
- /** Contactos de la tarjeta con nombre y teléfono. / Card contacts with name and phone. */
949
+ /** Contactos de la tarjeta. / Card contacts. */
735
950
  get contacts() {
736
- const m = this._doc.raw.message;
737
- const raw = m?.contactsArrayMessage?.contacts ?? (m?.contactMessage ? [m.contactMessage] : []);
738
- return raw.map((c) => {
739
- const tel = (c.vcard ?? '').match(/TEL[^:]*:(.+)/);
740
- return { name: c.displayName ?? '', phone: tel ? tel[1].trim() : '' };
951
+ const msg = this._raw.raw.message;
952
+ const raw = msg?.contactsArrayMessage?.contacts ?? (msg?.contactMessage ? [msg.contactMessage] : []);
953
+ return raw.map((card) => {
954
+ const tel = (card.vcard ?? '').match(/TEL[^:]*:(.+)/);
955
+ return { name: card.displayName ?? '', phone: tel ? tel[1].trim() : '' };
741
956
  });
742
957
  }
743
958
  }
744
959
  /**
745
- * Mensaje de evento de calendario (`eventMessage`): nombre, descripción, rango de
746
- * fechas, estado de cancelación, link de unión y ubicación.
747
- * Calendar event message (`eventMessage`): name, description, date range,
748
- * cancellation state, join link and location.
960
+ * Mensaje de evento de calendario.
961
+ * Calendar event message.
749
962
  */
750
963
  export class Event extends Message {
964
+ /** @internal */
751
965
  get _event() {
752
- return this._doc.raw.message?.eventMessage;
966
+ return this._raw.raw.message?.eventMessage;
753
967
  }
754
968
  /** Nombre del evento. / Event name. */
755
969
  get name() {
756
970
  return this._event?.name ?? '';
757
971
  }
758
- /** Epoch ms de inicio. / Start epoch ms. */
972
+ /** Inicio en ISO UTC. / Start as ISO UTC. */
759
973
  get start() {
760
- return Number(this._event?.startTime?.toString() ?? '0') * 1000;
974
+ return new Date(to_number(this._event?.startTime) * 1000).toISOString();
761
975
  }
762
- /** Epoch ms de fin, o null. / End epoch ms, or null. */
976
+ /** Fin en ISO UTC, o null. / End as ISO UTC, or null. */
763
977
  get end() {
764
978
  const end = this._event?.endTime;
765
- return end == null ? null : Number(end.toString()) * 1000;
979
+ return end == null ? null : new Date(to_number(end) * 1000).toISOString();
766
980
  }
767
- /** true si el evento fue cancelado. / true if the event was canceled. */
981
+ /** true si fue cancelado. / true when canceled. */
768
982
  get canceled() {
769
983
  return this._event?.isCanceled ?? false;
770
984
  }
771
- /** Link de unión (audio/video), si aplica. / Join link (audio/video), if any. */
985
+ /** Link de unión, si aplica. / Join link, if any. */
772
986
  get link() {
773
987
  return this._event?.joinLink ?? '';
774
988
  }
@@ -779,205 +993,75 @@ export class Event extends Message {
779
993
  }
780
994
  }
781
995
  /**
782
- * Factoría que enlaza los statics a `wa` y expone las subclases.
783
- * Factory binding statics to `wa` and exposing subclasses.
996
+ * Factoría de Message: evalúa el tipo y retorna la instancia de la subclase correcta.
997
+ * Acepta el documento persistido o el mensaje crudo de baileys — en ese caso el
998
+ * documento se deriva aquí mismo (id, tipo, autor, caption, mime, fechas).
999
+ * Message factory: evaluates the type and returns the right subclass instance. Accepts
1000
+ * the persisted document or the raw baileys message — in that case the document is
1001
+ * derived right here (id, type, author, caption, mime, dates).
1002
+ *
1003
+ * @param wa - Instancia principal / Main WhatsApp instance
1004
+ * @param raw - Documento persistido o WAMessage crudo / Persisted document or raw WAMessage
1005
+ * @returns Instancia del tipo correcto / Right-type instance
784
1006
  */
785
- export function message(wa) {
786
- async function build_instance(doc) {
787
- const opts = { wa, doc };
788
- if (doc.type === 'text') {
789
- return new Text(opts);
790
- }
791
- if (doc.type === 'image') {
792
- return new Image(opts);
793
- }
794
- if (doc.type === 'video') {
795
- return new Video(opts);
796
- }
797
- if (doc.type === 'audio') {
798
- return new Audio(opts);
799
- }
800
- if (doc.type === 'location') {
801
- return new Gps(opts);
802
- }
803
- if (doc.type === 'poll') {
804
- return new Poll(opts);
805
- }
806
- if (doc.type === 'document') {
807
- return new Document(opts);
808
- }
809
- if (doc.type === 'vcard') {
810
- return new VCard(opts);
811
- }
812
- if (doc.type === 'event') {
813
- return new Event(opts);
814
- }
815
- return new Message(opts);
816
- }
817
- async function send(cid, content, binary, reply_to, doc_overrides) {
818
- let result = null;
819
- const jid = await wa._resolve_jid(cid);
820
- if (jid && wa._socket) {
821
- let quoted;
822
- if (reply_to) {
823
- const ref_doc = deserialize(await wa.engine.get(`/chat/${jid}/message/${reply_to}`));
824
- if (ref_doc) {
825
- quoted = ref_doc.raw;
826
- }
827
- }
828
- const raw = await wa._socket.sendMessage(jid, {
829
- ...content,
830
- ...(quoted ? { quoted } : {}),
831
- });
832
- const sent_id = raw?.key?.id;
833
- if (raw && sent_id) {
834
- const doc = build_message_doc(raw, wa._socket?.user?.id);
835
- if (doc_overrides) {
836
- Object.assign(doc, doc_overrides);
837
- }
838
- await wa.engine.set(`/chat/${jid}/message/${sent_id}`, serialize(doc));
839
- if (binary) {
840
- await wa.engine.set(`/chat/${jid}/message/${sent_id}/content`, serialize({ data: binary.toString('base64') }));
841
- }
842
- result = await build_instance(doc);
843
- }
844
- }
845
- return result;
1007
+ export function message(wa, raw) {
1008
+ let doc;
1009
+ if ('key' in raw) {
1010
+ const key = raw.key ?? {};
1011
+ const unwrapped = unwrap(raw.message ?? {});
1012
+ const content_type = getContentType(unwrapped);
1013
+ const msg_type = MESSAGE_TYPE_MAP[content_type] ?? 'text';
1014
+ const msg_content = unwrapped[content_type];
1015
+ const context_info = msg_content?.contextInfo;
1016
+ const ephemeral_duration = raw.ephemeralDuration ?? context_info?.expiration;
1017
+ const self_id = internals(wa).socket?.user?.id;
1018
+ doc = {
1019
+ id: key.id ?? '',
1020
+ cid: key.remoteJidAlt ?? key.remoteJid ?? '',
1021
+ mid: context_info?.stanzaId ?? null,
1022
+ me: key.fromMe ?? false,
1023
+ type: msg_type,
1024
+ author: key.fromMe && self_id
1025
+ ? jidNormalizedUser(self_id)
1026
+ : key.participant || key.remoteJidAlt || key.remoteJid || '',
1027
+ status: raw.status ?? 1,
1028
+ starred: raw.starred ?? false,
1029
+ forwarded: context_info?.isForwarded ?? false,
1030
+ created_at: (Number(raw.messageTimestamp) || Math.floor(Date.now() / 1000)) * 1000,
1031
+ deleted_at: raw.ephemeralStartTimestamp != null && ephemeral_duration != null
1032
+ ? (Number(raw.ephemeralStartTimestamp) + ephemeral_duration) * 1000
1033
+ : null,
1034
+ mime: typeof msg_content === 'object' && msg_type !== 'text'
1035
+ ? (msg_content?.mimetype ?? 'application/octet-stream')
1036
+ : 'text/plain',
1037
+ caption: typeof msg_content === 'string'
1038
+ ? msg_content
1039
+ : msg_type === 'event'
1040
+ ? (msg_content?.description ?? '')
1041
+ : (msg_content?.caption ??
1042
+ msg_content?.text ??
1043
+ msg_content?.name ??
1044
+ msg_content?.displayName ??
1045
+ ''),
1046
+ edited: false,
1047
+ raw,
1048
+ };
846
1049
  }
847
- const api = {
848
- // Clases expuestas para instanceof
849
- Text,
850
- Image,
851
- Video,
852
- Audio,
853
- Gps,
854
- Poll,
855
- Document,
856
- VCard,
857
- Event,
858
- // Helpers internos (usados por orquestador WhatsApp)
859
- build_message_doc,
860
- build_instance,
861
- /** Obtiene un mensaje por CID/MID. */
862
- async get(cid, mid) {
863
- const jid = await wa._resolve_jid(cid);
864
- if (!jid) {
865
- return null;
866
- }
867
- const doc = deserialize(await wa.engine.get(`/chat/${jid}/message/${mid}`));
868
- return doc ? build_instance(doc) : null;
869
- },
870
- /** Pagina mensajes de un chat. */
871
- async list(cid, offset = 0, limit = 50) {
872
- const jid = await wa._resolve_jid(cid);
873
- const result = [];
874
- if (jid) {
875
- for (const raw of await wa.engine.list(`/chat/${jid}/message`, offset, limit)) {
876
- const doc = deserialize(raw);
877
- if (doc) {
878
- result.push(await build_instance(doc));
879
- }
880
- }
881
- }
882
- return result;
883
- },
884
- /** Cuenta mensajes de un chat. */
885
- async count(cid) {
886
- const jid = await wa._resolve_jid(cid);
887
- return jid ? wa.engine.count(`/chat/${jid}/message`) : 0;
888
- },
889
- async text(cid, caption, opts = {}) {
890
- return send(cid, { text: caption }, undefined, opts.mid);
891
- },
892
- async image(cid, buf, opts = {}) {
893
- return send(cid, { image: buf, caption: opts.caption }, buf, opts.mid);
894
- },
895
- async video(cid, buf, opts = {}) {
896
- return send(cid, { video: buf, caption: opts.caption }, buf, opts.mid);
897
- },
898
- async audio(cid, buf, opts = {}) {
899
- return send(cid, { audio: buf, ptt: opts.ptt ?? true }, buf, opts.mid);
900
- },
901
- async location(cid, loc, opts = {}) {
902
- return send(cid, { location: { degreesLatitude: loc.lat, degreesLongitude: loc.lng } }, undefined, opts.mid);
903
- },
904
- async poll(cid, p, opts = {}) {
905
- const multiple = opts.multiple ?? false;
906
- return send(cid, {
907
- poll: {
908
- name: p.content,
909
- values: p.options.map((o) => o.content),
910
- selectableCount: multiple ? 0 : 1,
911
- },
912
- }, undefined, opts.mid, { multiple });
913
- },
914
- async document(cid, buf, opts) {
915
- return send(cid, {
916
- document: buf,
917
- fileName: opts.file_name,
918
- mimetype: opts.mimetype ?? 'application/octet-stream',
919
- caption: opts.caption,
920
- }, buf, opts.mid);
921
- },
922
- async vcard(cid, contacts, opts = {}) {
923
- const built = contacts.map((c) => ({
924
- displayName: c.name,
925
- vcard: build_vcard(c.name, c.phone),
926
- }));
927
- const binary = Buffer.from(built.map((c) => c.vcard).join('\n'), 'utf-8');
928
- return send(cid, { contacts: { contacts: built } }, binary, opts.mid);
929
- },
930
- async event(cid, data, opts = {}) {
931
- const binary = Buffer.from(JSON.stringify({
932
- name: data.name,
933
- caption: data.caption,
934
- start: data.start.getTime(),
935
- end: data.end?.getTime() ?? null,
936
- }), 'utf-8');
937
- return send(cid, {
938
- event: {
939
- name: data.name,
940
- description: data.caption,
941
- startDate: data.start,
942
- endDate: data.end,
943
- location: data.place
944
- ? { degreesLatitude: data.place.lat, degreesLongitude: data.place.lng }
945
- : undefined,
946
- },
947
- }, binary, opts.mid);
948
- },
949
- async edit(cid, mid, text) {
950
- const m = await api.get(cid, mid);
951
- return m ? m.edit(text) : false;
952
- },
953
- async delete(cid, mid, all = true) {
954
- const m = await api.get(cid, mid);
955
- return m ? m.delete(all) : false;
956
- },
957
- async react(cid, mid, emoji) {
958
- const m = await api.get(cid, mid);
959
- return m ? m.react(emoji) : false;
960
- },
961
- async forward(cid, mid, target) {
962
- const m = await api.get(cid, mid);
963
- return m ? m.forward(target) : false;
964
- },
965
- async seen(cid, mid) {
966
- const m = await api.get(cid, mid);
967
- return m ? m.seen() : false;
968
- },
969
- async star(cid, mid, value) {
970
- const m = await api.get(cid, mid);
971
- return m ? m.star(value) : false;
972
- },
973
- watch(cid, mid, handler) {
974
- return wa.on('message:updated', (updated) => {
975
- if (updated.cid === cid && updated.id === mid) {
976
- handler(updated);
977
- }
978
- });
979
- },
1050
+ else {
1051
+ doc = raw;
1052
+ }
1053
+ const types = {
1054
+ text: Text,
1055
+ image: Image,
1056
+ video: Video,
1057
+ audio: Audio,
1058
+ sticker: Sticker,
1059
+ document: Document,
1060
+ location: Location,
1061
+ poll: Poll,
1062
+ vcard: VCard,
1063
+ event: Event,
980
1064
  };
981
- return api;
1065
+ return new (types[doc.type] ?? Message)(wa, doc);
982
1066
  }
983
1067
  //# sourceMappingURL=index.js.map