@x4r1s3/baileys 0.0.0-stage → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (183) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +1262 -2
  3. package/SECURITY.md +47 -0
  4. package/WAProto/V +1 -0
  5. package/WAProto/WAProto.proto +8892 -0
  6. package/WAProto/index.d.ts +104128 -0
  7. package/WAProto/index.js +334920 -0
  8. package/assets/rolling-circle.svg +133 -0
  9. package/assets/x4r1s3-banner.svg +32 -0
  10. package/engine-requirements.js +27 -0
  11. package/lib/Builders/A2UI.js +245 -0
  12. package/lib/Builders/AIRich.js +179 -0
  13. package/lib/Builders/Button.js +108 -0
  14. package/lib/Builders/CFC.js +284 -0
  15. package/lib/Builders/Carousel.js +81 -0
  16. package/lib/Call/THIRD_PARTY_LICENSE.txt +21 -0
  17. package/lib/Call/audio-feeder.mjs +179 -0
  18. package/lib/Call/audio-recorder.mjs +250 -0
  19. package/lib/Call/fetch-wasm-resources.mjs +104 -0
  20. package/lib/Call/index.mjs +680 -0
  21. package/lib/Call/relay-transport.mjs +563 -0
  22. package/lib/Call/signaling.mjs +641 -0
  23. package/lib/Call/types.mjs +17 -0
  24. package/lib/Call/video-feeder.mjs +94 -0
  25. package/lib/Call/wasm-engine.mjs +1372 -0
  26. package/lib/Call/worker-bootstrap.mjs +1042 -0
  27. package/lib/Defaults/ban-checker.js +56 -0
  28. package/lib/Defaults/index.js +170 -0
  29. package/lib/Signal/Group/ciphertext-message.js +23 -0
  30. package/lib/Signal/Group/group-session-builder.js +41 -0
  31. package/lib/Signal/Group/group_cipher.js +93 -0
  32. package/lib/Signal/Group/index.js +23 -0
  33. package/lib/Signal/Group/keyhelper.js +29 -0
  34. package/lib/Signal/Group/sender-chain-key.js +37 -0
  35. package/lib/Signal/Group/sender-key-distribution-message.js +74 -0
  36. package/lib/Signal/Group/sender-key-message.js +77 -0
  37. package/lib/Signal/Group/sender-key-name.js +59 -0
  38. package/lib/Signal/Group/sender-key-record.js +52 -0
  39. package/lib/Signal/Group/sender-key-state.js +95 -0
  40. package/lib/Signal/Group/sender-message-key.js +37 -0
  41. package/lib/Signal/libsignal.js +440 -0
  42. package/lib/Signal/lid-mapping.js +292 -0
  43. package/lib/Socket/Client/index.js +14 -0
  44. package/lib/Socket/Client/types.js +22 -0
  45. package/lib/Socket/Client/websocket.js +65 -0
  46. package/lib/Socket/aigroups.js +327 -0
  47. package/lib/Socket/ban-checker.js +165 -0
  48. package/lib/Socket/business.js +419 -0
  49. package/lib/Socket/chats.js +1921 -0
  50. package/lib/Socket/communities.js +443 -0
  51. package/lib/Socket/graphql.js +911 -0
  52. package/lib/Socket/groups.js +561 -0
  53. package/lib/Socket/index.js +49 -0
  54. package/lib/Socket/interactive-handler.js +612 -0
  55. package/lib/Socket/interop.js +575 -0
  56. package/lib/Socket/managed-account.js +220 -0
  57. package/lib/Socket/messages-recv.js +2191 -0
  58. package/lib/Socket/messages-send.js +4104 -0
  59. package/lib/Socket/mex.js +53 -0
  60. package/lib/Socket/newsletter.js +291 -0
  61. package/lib/Socket/privacy.js +458 -0
  62. package/lib/Socket/registration.js +440 -0
  63. package/lib/Socket/socket.js +1029 -0
  64. package/lib/Socket/status-actions.js +287 -0
  65. package/lib/Socket/username.js +241 -0
  66. package/lib/Store/index.js +16 -0
  67. package/lib/Store/make-cache-manager-store.js +101 -0
  68. package/lib/Store/make-in-memory-store.js +432 -0
  69. package/lib/Store/make-ordered-dictionary.js +90 -0
  70. package/lib/Store/object-repository.js +35 -0
  71. package/lib/Types/Auth.js +13 -0
  72. package/lib/Types/Bussines.js +13 -0
  73. package/lib/Types/Call.js +13 -0
  74. package/lib/Types/Chat.js +19 -0
  75. package/lib/Types/Contact.js +13 -0
  76. package/lib/Types/Events.js +13 -0
  77. package/lib/Types/GroupMetadata.js +13 -0
  78. package/lib/Types/Label.js +36 -0
  79. package/lib/Types/LabelAssociation.js +18 -0
  80. package/lib/Types/Message.js +30 -0
  81. package/lib/Types/Mex.js +53 -0
  82. package/lib/Types/Product.js +13 -0
  83. package/lib/Types/RichType.js +34 -0
  84. package/lib/Types/Signal.js +13 -0
  85. package/lib/Types/Socket.js +14 -0
  86. package/lib/Types/State.js +60 -0
  87. package/lib/Types/USync.js +13 -0
  88. package/lib/Types/index.js +37 -0
  89. package/lib/Utils/auth-utils.js +313 -0
  90. package/lib/Utils/ban-checker.js +48 -0
  91. package/lib/Utils/bot-capabilities.js +96 -0
  92. package/lib/Utils/bot-planning-replay.js +177 -0
  93. package/lib/Utils/browser-utils.js +40 -0
  94. package/lib/Utils/business.js +242 -0
  95. package/lib/Utils/c2582.js +147 -0
  96. package/lib/Utils/chat-utils.js +905 -0
  97. package/lib/Utils/companion-reg-client-utils.js +44 -0
  98. package/lib/Utils/consumer-application.js +114 -0
  99. package/lib/Utils/crypto.js +129 -0
  100. package/lib/Utils/decode-wa-message.js +496 -0
  101. package/lib/Utils/direct-chat-theme.js +164 -0
  102. package/lib/Utils/event-buffer.js +601 -0
  103. package/lib/Utils/experimental-proto.js +206 -0
  104. package/lib/Utils/games/miniapp.js +17 -0
  105. package/lib/Utils/games/slot-machine.js +270 -0
  106. package/lib/Utils/games/website-preview.js +306 -0
  107. package/lib/Utils/generics.js +402 -0
  108. package/lib/Utils/group-history.js +68 -0
  109. package/lib/Utils/group-wallpaper.js +168 -0
  110. package/lib/Utils/history.js +162 -0
  111. package/lib/Utils/identity-change-handler.js +60 -0
  112. package/lib/Utils/index.js +61 -0
  113. package/lib/Utils/jid-display-normalization.js +221 -0
  114. package/lib/Utils/link-preview.js +96 -0
  115. package/lib/Utils/logger.js +14 -0
  116. package/lib/Utils/lt-hash.js +19 -0
  117. package/lib/Utils/make-mutex.js +44 -0
  118. package/lib/Utils/message-composer.js +709 -0
  119. package/lib/Utils/message-inspect.js +79 -0
  120. package/lib/Utils/message-retry-manager.js +253 -0
  121. package/lib/Utils/message-security.js +167 -0
  122. package/lib/Utils/messages-media.js +1027 -0
  123. package/lib/Utils/messages.js +2467 -0
  124. package/lib/Utils/meta-ai-msmsg.js +215 -0
  125. package/lib/Utils/meta-compositing.js +239 -0
  126. package/lib/Utils/native-flow.js +158 -0
  127. package/lib/Utils/newsletter-live-update-utils.js +74 -0
  128. package/lib/Utils/newsletter-status-utils.js +152 -0
  129. package/lib/Utils/noise-handler.js +212 -0
  130. package/lib/Utils/offline-node-processor.js +51 -0
  131. package/lib/Utils/poll-manager.js +206 -0
  132. package/lib/Utils/portable-compat.js +12 -0
  133. package/lib/Utils/pre-key-manager.js +117 -0
  134. package/lib/Utils/process-message.js +610 -0
  135. package/lib/Utils/reporting-utils.js +269 -0
  136. package/lib/Utils/rich-message-utils.js +2841 -0
  137. package/lib/Utils/scheduled-message.js +107 -0
  138. package/lib/Utils/signal.js +212 -0
  139. package/lib/Utils/source-grep.js +57 -0
  140. package/lib/Utils/stanza-ack.js +49 -0
  141. package/lib/Utils/sticker-annotation.js +87 -0
  142. package/lib/Utils/sync-action-utils.js +59 -0
  143. package/lib/Utils/tc-token-utils.js +174 -0
  144. package/lib/Utils/use-multi-file-auth-state.js +132 -0
  145. package/lib/Utils/use-single-file-auth-state.js +108 -0
  146. package/lib/Utils/use-sqlite-auth-state.js +120 -0
  147. package/lib/Utils/validate-connection.js +220 -0
  148. package/lib/Utils/view-once-utils.js +218 -0
  149. package/lib/Utils/voip-rekey.js +32 -0
  150. package/lib/Utils/website-preview.js +27 -0
  151. package/lib/Utils/welcome-flow.js +228 -0
  152. package/lib/Utils/x4r1s3.js +144 -0
  153. package/lib/WABinary/constants.js +1475 -0
  154. package/lib/WABinary/decode.js +273 -0
  155. package/lib/WABinary/encode.js +231 -0
  156. package/lib/WABinary/generic-utils.js +242 -0
  157. package/lib/WABinary/index.js +17 -0
  158. package/lib/WABinary/jid-utils.js +107 -0
  159. package/lib/WABinary/types.js +13 -0
  160. package/lib/WAM/BinaryInfo.js +21 -0
  161. package/lib/WAM/constants.js +22864 -0
  162. package/lib/WAM/encode.js +161 -0
  163. package/lib/WAM/index.js +15 -0
  164. package/lib/WAUSync/Protocols/USyncBusinessProtocol.js +108 -0
  165. package/lib/WAUSync/Protocols/USyncContactProtocol.js +68 -0
  166. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +65 -0
  167. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +38 -0
  168. package/lib/WAUSync/Protocols/USyncFeatureProtocol.js +82 -0
  169. package/lib/WAUSync/Protocols/USyncPictureProtocol.js +40 -0
  170. package/lib/WAUSync/Protocols/USyncSidelistProtocol.js +37 -0
  171. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +49 -0
  172. package/lib/WAUSync/Protocols/USyncTextStatusProtocol.js +46 -0
  173. package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +36 -0
  174. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +62 -0
  175. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +40 -0
  176. package/lib/WAUSync/Protocols/index.js +24 -0
  177. package/lib/WAUSync/USyncQuery.js +213 -0
  178. package/lib/WAUSync/USyncUser.js +66 -0
  179. package/lib/WAUSync/index.js +15 -0
  180. package/lib/antiban.cjs +4726 -0
  181. package/lib/index.d.ts +1285 -0
  182. package/lib/index.js +30 -0
  183. package/package.json +125 -4
@@ -0,0 +1,612 @@
1
+ /**
2
+ * @X4R1S3
3
+ *
4
+ * File: lib/Socket/interactive-handler.js
5
+ * About: Interactive handler module for the X4R1S3 WhatsApp protocol client.
6
+ * Important exports: Baron
7
+ * Directions: Import the listed exports and preserve protocol, validation, and compatibility invariants when modifying.
8
+ *
9
+ * Copyright and attribution notice.
10
+ * Do not copy, reproduce, redistribute, modify, or create derivative works
11
+ * from this source without explicit written permission.
12
+ */
13
+ import { randomBytes } from 'crypto';
14
+ import { proto } from '../../WAProto/index.js';
15
+ import {
16
+ delay,
17
+ generateMessageID,
18
+ generateWAMessage,
19
+ generateWAMessageContent,
20
+ generateWAMessageFromContent,
21
+ getUrlFromDirectPath,
22
+ normalizeMessageContent,
23
+ prepareWAMessageMedia
24
+ } from '../Utils/index.js';
25
+ import { getUrlInfo } from '../Utils/link-preview.js';
26
+ import { isJidGroup, isPnUser, jidNormalizedUser, STORIES_JID } from '../WABinary/index.js';
27
+
28
+ export class Baron {
29
+ constructor(waUploadToServer, relayMessageFn, config, sock) {
30
+ this.relayMessage = relayMessageFn;
31
+ this.waUploadToServer = waUploadToServer;
32
+ this.config = config;
33
+ this.sock = sock;
34
+ }
35
+
36
+ detectType(content) {
37
+ if (content.requestPaymentMessage) return 'PAYMENT';
38
+ if (content.productMessage) return 'PRODUCT';
39
+ if (content.interactiveButtons) return 'INTERACTIVE_BUTTONS';
40
+ if (content.interactiveMessage) return 'INTERACTIVE';
41
+ if (content.albumMessage || content.album) return 'ALBUM';
42
+ if (content.eventMessage) return 'EVENT';
43
+ if (content.pollResultMessage) return 'POLL_RESULT';
44
+ if (content.groupStatusMessage) return 'GROUP_STORY';
45
+ return null;
46
+ }
47
+
48
+ async handlePayment(content, quoted) {
49
+ const data = content.requestPaymentMessage;
50
+ let notes = {};
51
+ if (data.sticker?.stickerMessage) {
52
+ notes = {
53
+ stickerMessage: {
54
+ ...data.sticker.stickerMessage,
55
+ contextInfo: {
56
+ stanzaId: quoted?.key?.id,
57
+ participant: quoted?.key?.participant || content.sender,
58
+ quotedMessage: quoted?.message
59
+ }
60
+ }
61
+ };
62
+ }
63
+ else if (data.note) {
64
+ notes = {
65
+ extendedTextMessage: {
66
+ text: data.note,
67
+ contextInfo: {
68
+ stanzaId: quoted?.key?.id,
69
+ participant: quoted?.key?.participant || content.sender,
70
+ quotedMessage: quoted?.message
71
+ }
72
+ }
73
+ };
74
+ }
75
+ return {
76
+ requestPaymentMessage: proto.Message.RequestPaymentMessage.fromObject({
77
+ expiryTimestamp: data.expiry || 0,
78
+ amount1000: data.amount || 0,
79
+ currencyCodeIso4217: data.currency || 'IDR',
80
+ requestFrom: data.from || '0@s.whatsapp.net',
81
+ noteMessage: notes,
82
+ background: data.background ?? {
83
+ id: 'DEFAULT',
84
+ placeholderArgb: 0xfff0f0f0
85
+ }
86
+ })
87
+ };
88
+ }
89
+
90
+ async handleProduct(content, _jid, _quoted) {
91
+ const {
92
+ title,
93
+ description,
94
+ thumbnail,
95
+ productId,
96
+ retailerId,
97
+ url,
98
+ body = '',
99
+ footer = '',
100
+ buttons = [],
101
+ priceAmount1000 = null,
102
+ currencyCode = 'IDR'
103
+ } = content.productMessage;
104
+ let productImage;
105
+ if (Buffer.isBuffer(thumbnail)) {
106
+ const { imageMessage } = await generateWAMessageContent({ image: thumbnail }, { upload: this.waUploadToServer });
107
+ productImage = imageMessage;
108
+ }
109
+ else if (typeof thumbnail === 'object' && thumbnail.url) {
110
+ const { imageMessage } = await generateWAMessageContent(
111
+ { image: { url: thumbnail.url } },
112
+ { upload: this.waUploadToServer }
113
+ );
114
+ productImage = imageMessage;
115
+ }
116
+ return {
117
+ viewOnceMessage: {
118
+ message: {
119
+ interactiveMessage: {
120
+ body: { text: body },
121
+ footer: { text: footer },
122
+ header: {
123
+ title,
124
+ hasMediaAttachment: true,
125
+ productMessage: {
126
+ product: {
127
+ productImage,
128
+ productId,
129
+ title,
130
+ description,
131
+ currencyCode,
132
+ priceAmount1000,
133
+ retailerId,
134
+ url,
135
+ productImageCount: 1
136
+ },
137
+ businessOwnerJid: '0@s.whatsapp.net'
138
+ }
139
+ },
140
+ nativeFlowMessage: { buttons }
141
+ }
142
+ }
143
+ }
144
+ };
145
+ }
146
+
147
+ async handleInteractive(content, _jid, _quoted) {
148
+ const {
149
+ title,
150
+ footer,
151
+ thumbnail,
152
+ image,
153
+ video,
154
+ document,
155
+ mimetype,
156
+ fileName,
157
+ jpegThumbnail,
158
+ contextInfo,
159
+ externalAdReply,
160
+ buttons = [],
161
+ nativeFlowMessage,
162
+ header
163
+ } = content.interactiveMessage;
164
+ let media = null;
165
+ let _mediaType = null;
166
+ if (thumbnail) {
167
+ media = await prepareWAMessageMedia({ image: { url: thumbnail } }, { upload: this.waUploadToServer });
168
+ _mediaType = 'image';
169
+ }
170
+ else if (image) {
171
+ const src = typeof image === 'object' && image.url ? { image: { url: image.url } } : { image };
172
+ media = await prepareWAMessageMedia(src, { upload: this.waUploadToServer });
173
+ _mediaType = 'image';
174
+ }
175
+ else if (video) {
176
+ const src = typeof video === 'object' && video.url ? { video: { url: video.url } } : { video };
177
+ media = await prepareWAMessageMedia(src, { upload: this.waUploadToServer });
178
+ _mediaType = 'video';
179
+ }
180
+ else if (document) {
181
+ const docPayload = { document };
182
+ if (jpegThumbnail) {
183
+ docPayload.jpegThumbnail =
184
+ typeof jpegThumbnail === 'object' && jpegThumbnail.url ? { url: jpegThumbnail.url } : jpegThumbnail;
185
+ }
186
+ media = await prepareWAMessageMedia(docPayload, { upload: this.waUploadToServer });
187
+ if (fileName) media.documentMessage.fileName = fileName;
188
+ if (mimetype) media.documentMessage.mimetype = mimetype;
189
+ _mediaType = 'document';
190
+ }
191
+ const interactiveMessage = {
192
+ body: { text: title || '' },
193
+ footer: { text: footer || '' }
194
+ };
195
+ if (buttons && buttons.length > 0) {
196
+ interactiveMessage.nativeFlowMessage = { buttons };
197
+ if (nativeFlowMessage) {
198
+ interactiveMessage.nativeFlowMessage = {
199
+ ...interactiveMessage.nativeFlowMessage,
200
+ ...nativeFlowMessage
201
+ };
202
+ }
203
+ }
204
+ else if (nativeFlowMessage) {
205
+ interactiveMessage.nativeFlowMessage = nativeFlowMessage;
206
+ }
207
+ if (media) {
208
+ interactiveMessage.header = {
209
+ title: header || '',
210
+ hasMediaAttachment: true,
211
+ ...media
212
+ };
213
+ }
214
+ else {
215
+ interactiveMessage.header = {
216
+ title: header || '',
217
+ hasMediaAttachment: false
218
+ };
219
+ }
220
+ const finalContextInfo = {};
221
+ if (contextInfo) {
222
+ Object.assign(finalContextInfo, {
223
+ mentionedJid: contextInfo.mentionedJid || [],
224
+ forwardingScore: contextInfo.forwardingScore || 0,
225
+ isForwarded: contextInfo.isForwarded || false,
226
+ ...contextInfo
227
+ });
228
+ }
229
+ if (externalAdReply) {
230
+ finalContextInfo.externalAdReply = {
231
+ title: externalAdReply.title || '',
232
+ body: externalAdReply.body || '',
233
+ mediaType: externalAdReply.mediaType || 1,
234
+ thumbnailUrl: externalAdReply.thumbnailUrl || '',
235
+ mediaUrl: externalAdReply.mediaUrl || '',
236
+ sourceUrl: externalAdReply.sourceUrl || '',
237
+ showAdAttribution: externalAdReply.showAdAttribution || false,
238
+ renderLargerThumbnail: externalAdReply.renderLargerThumbnail || false,
239
+ ...externalAdReply
240
+ };
241
+ }
242
+ if (Object.keys(finalContextInfo).length > 0) {
243
+ interactiveMessage.contextInfo = finalContextInfo;
244
+ }
245
+ return { interactiveMessage };
246
+ }
247
+
248
+ async handleInteractiveButtons(content, _jid, _quoted) {
249
+ const {
250
+ text,
251
+ caption,
252
+ title,
253
+ subtitle,
254
+ footer,
255
+ interactiveButtons,
256
+ hasMediaAttachment,
257
+ image,
258
+ video,
259
+ document,
260
+ mimetype,
261
+ jpegThumbnail,
262
+ location,
263
+ product,
264
+ businessOwnerJid
265
+ } = content;
266
+ const bodyText = text || caption || '';
267
+ const buttons = interactiveButtons.map(btn => ({
268
+ name: btn.name,
269
+ buttonParamsJson:
270
+ typeof btn.buttonParamsJson === 'string' ? btn.buttonParamsJson : JSON.stringify(btn.buttonParamsJson)
271
+ }));
272
+ let headerContent = {};
273
+ let mediaAttached = typeof hasMediaAttachment === 'boolean' ? hasMediaAttachment : false;
274
+ if (image) {
275
+ const src = typeof image === 'object' && image.url ? { image: { url: image.url } } : { image };
276
+ const uploaded = await prepareWAMessageMedia(src, { upload: this.waUploadToServer });
277
+ headerContent = { ...uploaded };
278
+ mediaAttached = typeof hasMediaAttachment === 'boolean' ? hasMediaAttachment : true;
279
+ }
280
+ else if (video) {
281
+ const src = typeof video === 'object' && video.url ? { video: { url: video.url } } : { video };
282
+ const uploaded = await prepareWAMessageMedia(src, { upload: this.waUploadToServer });
283
+ headerContent = { ...uploaded };
284
+ mediaAttached = typeof hasMediaAttachment === 'boolean' ? hasMediaAttachment : true;
285
+ }
286
+ else if (document) {
287
+ const docPayload =
288
+ typeof document === 'object' && document.url ? { document: { url: document.url } } : { document };
289
+ if (mimetype) docPayload.mimetype = mimetype;
290
+ const uploaded = await prepareWAMessageMedia(docPayload, { upload: this.waUploadToServer });
291
+ if (jpegThumbnail) {
292
+ uploaded.documentMessage.jpegThumbnail =
293
+ typeof jpegThumbnail === 'string' ? Buffer.from(jpegThumbnail, 'base64') : jpegThumbnail;
294
+ }
295
+ headerContent = { ...uploaded };
296
+ mediaAttached = typeof hasMediaAttachment === 'boolean' ? hasMediaAttachment : true;
297
+ }
298
+ else if (location) {
299
+ headerContent = {
300
+ locationMessage: {
301
+ degreesLatitude: location.degressLatitude || location.degreesLatitude || 0,
302
+ degreesLongitude: location.degressLongitude || location.degreesLongitude || 0,
303
+ name: location.name || ''
304
+ }
305
+ };
306
+ mediaAttached = typeof hasMediaAttachment === 'boolean' ? hasMediaAttachment : true;
307
+ }
308
+ else if (product) {
309
+ let productImage;
310
+ if (product.productImage) {
311
+ const imgSrc =
312
+ typeof product.productImage === 'object' && product.productImage.url
313
+ ? { image: { url: product.productImage.url } }
314
+ : { image: product.productImage };
315
+ const uploaded = await prepareWAMessageMedia(imgSrc, { upload: this.waUploadToServer });
316
+ productImage = uploaded.imageMessage;
317
+ }
318
+ headerContent = {
319
+ productMessage: {
320
+ product: {
321
+ productImage,
322
+ productId: product.productId,
323
+ title: product.title,
324
+ description: product.description,
325
+ currencyCode: product.currencyCode || 'IDR',
326
+ priceAmount1000: product.priceAmount1000,
327
+ retailerId: product.retailerId,
328
+ url: product.url,
329
+ productImageCount: product.productImageCount || 1
330
+ },
331
+ businessOwnerJid: businessOwnerJid || '0@s.whatsapp.net'
332
+ }
333
+ };
334
+ mediaAttached = typeof hasMediaAttachment === 'boolean' ? hasMediaAttachment : true;
335
+ }
336
+ const interactiveMessage = {
337
+ body: { text: bodyText },
338
+ footer: { text: footer || '' },
339
+ header: {
340
+ title: title || '',
341
+ subtitle: subtitle || '',
342
+ hasMediaAttachment: mediaAttached,
343
+ ...headerContent
344
+ },
345
+ nativeFlowMessage: { buttons }
346
+ };
347
+ return {
348
+ viewOnceMessage: {
349
+ message: {
350
+ messageContextInfo: {
351
+ deviceListMetadata: {},
352
+ deviceListMetadataVersion: 2,
353
+ messageSecret: randomBytes(32)
354
+ },
355
+ interactiveMessage
356
+ }
357
+ }
358
+ };
359
+ }
360
+
361
+ async handleAlbum(content, jid, quoted) {
362
+ const array = content.albumMessage || content.album;
363
+ const ctxInfo = content.contextInfo || {};
364
+ const album = await generateWAMessageFromContent(
365
+ jid,
366
+ {
367
+ messageContextInfo: {
368
+ messageSecret: randomBytes(32)
369
+ },
370
+ albumMessage: {
371
+ expectedImageCount: array.filter(a => 'image' in a).length,
372
+ expectedVideoCount: array.filter(a => 'video' in a).length
373
+ }
374
+ },
375
+ { userJid: jidNormalizedUser(this.sock.authState?.creds?.me?.id || '') }
376
+ );
377
+ await this.relayMessage(jid, album.message, {
378
+ messageId: album.key.id
379
+ });
380
+ for (let item of array) {
381
+ if (ctxInfo && Object.keys(ctxInfo).length > 0 && !item.contextInfo) {
382
+ item = { ...item, contextInfo: ctxInfo };
383
+ }
384
+ const img = await generateWAMessage(jid, item, {
385
+ upload: this.waUploadToServer,
386
+ userJid: jidNormalizedUser(this.sock.authState?.creds?.me?.id || '')
387
+ });
388
+ img.message.messageContextInfo = {
389
+ messageSecret: randomBytes(32),
390
+ messageAssociation: {
391
+ associationType: 1,
392
+ parentMessageKey: album.key
393
+ }
394
+ };
395
+ await this.relayMessage(jid, img.message, {
396
+ messageId: img.key.id
397
+ });
398
+ }
399
+ return album;
400
+ }
401
+
402
+ async handleEvent(content, jid, quoted) {
403
+ const eventData = content.eventMessage;
404
+ const msg = await generateWAMessageFromContent(
405
+ jid,
406
+ {
407
+ viewOnceMessage: {
408
+ message: {
409
+ messageContextInfo: {
410
+ deviceListMetadata: {},
411
+ deviceListMetadataVersion: 2,
412
+ messageSecret: randomBytes(32)
413
+ },
414
+ eventMessage: {
415
+ isCanceled: eventData.isCanceled || false,
416
+ name: eventData.name,
417
+ description: eventData.description,
418
+ location: eventData.location || {
419
+ degreesLatitude: 0,
420
+ degreesLongitude: 0,
421
+ name: 'Location'
422
+ },
423
+ joinLink: eventData.joinLink || '',
424
+ startTime:
425
+ typeof eventData.startTime === 'string'
426
+ ? parseInt(eventData.startTime, 10)
427
+ : eventData.startTime || Date.now(),
428
+ endTime:
429
+ typeof eventData.endTime === 'string'
430
+ ? parseInt(eventData.endTime, 10)
431
+ : eventData.endTime || Date.now() + 3600000,
432
+ extraGuestsAllowed: eventData.extraGuestsAllowed !== false
433
+ }
434
+ }
435
+ }
436
+ },
437
+ { quoted, userJid: jidNormalizedUser(this.sock.authState?.creds?.me?.id || '') }
438
+ );
439
+ await this.relayMessage(jid, msg.message, {
440
+ messageId: msg.key.id
441
+ });
442
+ return msg;
443
+ }
444
+
445
+ async handlePollResult(content, jid, quoted) {
446
+ const pollData = content.pollResultMessage;
447
+ const msg = await generateWAMessageFromContent(
448
+ jid,
449
+ {
450
+ pollResultSnapshotMessage: {
451
+ name: pollData.name,
452
+ pollVotes: pollData.pollVotes.map(vote => ({
453
+ optionName: vote.optionName,
454
+ optionVoteCount:
455
+ typeof vote.optionVoteCount === 'number' ? vote.optionVoteCount.toString() : vote.optionVoteCount
456
+ }))
457
+ }
458
+ },
459
+ { quoted, userJid: jidNormalizedUser(this.sock.authState?.creds?.me?.id || '') }
460
+ );
461
+ await this.relayMessage(jid, msg.message, {
462
+ messageId: msg.key.id
463
+ });
464
+ return msg;
465
+ }
466
+
467
+ async handleGroupStory(content, jid, _quoted) {
468
+ const storyData = content.groupStatusMessage;
469
+ let waMsgContent;
470
+ if (storyData.message) {
471
+ waMsgContent = storyData;
472
+ }
473
+ else {
474
+ waMsgContent = await generateWAMessageContent(storyData, {
475
+ upload: this.waUploadToServer
476
+ });
477
+ }
478
+ const msg = {
479
+ message: {
480
+ groupStatusMessageV2: {
481
+ message: waMsgContent.message || waMsgContent
482
+ }
483
+ }
484
+ };
485
+ return await this.relayMessage(jid, msg.message, {
486
+ messageId: generateMessageID()
487
+ });
488
+ }
489
+
490
+ async sendStatusWhatsApp(content, jids = []) {
491
+ const userJid = jidNormalizedUser(this.sock.authState.creds.me.id);
492
+ const allUsers = new Set();
493
+ allUsers.add(userJid);
494
+ for (const id of jids) {
495
+ if (isJidGroup(id)) {
496
+ try {
497
+ const metadata = await this.sock.groupMetadata(id);
498
+ metadata.participants.forEach(p => allUsers.add(jidNormalizedUser(p.id)));
499
+ }
500
+ catch (error) {
501
+ this.config.logger.error(`Error getting metadata for group ${id}: ${error}`);
502
+ }
503
+ }
504
+ else if (isPnUser(id)) {
505
+ allUsers.add(jidNormalizedUser(id));
506
+ }
507
+ }
508
+ const uniqueUsers = Array.from(allUsers);
509
+ const getRandomHexColor = () =>
510
+ '#' +
511
+ Math.floor(Math.random() * 16777215)
512
+ .toString(16)
513
+ .padStart(6, '0');
514
+ const isMedia = content.image || content.video || content.audio;
515
+ const isAudio = !!content.audio;
516
+ const messageContent = { ...content };
517
+ if (isMedia && !isAudio) {
518
+ if (messageContent.text) {
519
+ messageContent.caption = messageContent.text;
520
+ delete messageContent.text;
521
+ }
522
+ delete messageContent.ptt;
523
+ delete messageContent.font;
524
+ delete messageContent.backgroundColor;
525
+ delete messageContent.textColor;
526
+ }
527
+ if (isAudio) {
528
+ delete messageContent.text;
529
+ delete messageContent.caption;
530
+ delete messageContent.font;
531
+ delete messageContent.textColor;
532
+ }
533
+ const font = !isMedia ? content.font || Math.floor(Math.random() * 9) : undefined;
534
+ const textColor = !isMedia ? content.textColor || getRandomHexColor() : undefined;
535
+ const backgroundColor = !isMedia || isAudio ? content.backgroundColor || getRandomHexColor() : undefined;
536
+ const ptt = isAudio ? (typeof content.ptt === 'boolean' ? content.ptt : true) : undefined;
537
+ const msg = await generateWAMessage(STORIES_JID, messageContent, {
538
+ logger: this.config.logger,
539
+ userJid,
540
+ getUrlInfo: text =>
541
+ getUrlInfo(text, {
542
+ thumbnailWidth: this.config.linkPreviewImageThumbnailWidth,
543
+ fetchOpts: { timeout: 3000, ...(this.config.options || {}) },
544
+ logger: this.config.logger,
545
+ uploadImage: this.config.generateHighQualityLinkPreview ? this.waUploadToServer : undefined
546
+ }),
547
+ upload: this.waUploadToServer,
548
+ mediaCache: this.config.mediaCache,
549
+ options: this.config.options,
550
+ font,
551
+ textColor,
552
+ backgroundColor,
553
+ ptt
554
+ });
555
+ await this.relayMessage(STORIES_JID, msg.message, {
556
+ messageId: msg.key.id,
557
+ statusJidList: uniqueUsers,
558
+ additionalNodes: [
559
+ {
560
+ tag: 'meta',
561
+ attrs: {},
562
+ content: [
563
+ {
564
+ tag: 'mentioned_users',
565
+ attrs: {},
566
+ content: jids.map(jid => ({
567
+ tag: 'to',
568
+ attrs: { jid: jidNormalizedUser(jid) }
569
+ }))
570
+ }
571
+ ]
572
+ }
573
+ ]
574
+ });
575
+ for (const id of jids) {
576
+ try {
577
+ const normalizedId = jidNormalizedUser(id);
578
+ const isPrivate = isPnUser(normalizedId);
579
+ const type = isPrivate ? 'statusMentionMessage' : 'groupStatusMentionMessage';
580
+ const protocolMessage = {
581
+ [type]: {
582
+ message: {
583
+ protocolMessage: {
584
+ key: msg.key,
585
+ type: 25
586
+ }
587
+ }
588
+ },
589
+ messageContextInfo: {
590
+ messageSecret: randomBytes(32)
591
+ }
592
+ };
593
+ const statusMsg = await generateWAMessageFromContent(normalizedId, protocolMessage, {
594
+ userJid: jidNormalizedUser(this.sock.authState?.creds?.me?.id || '')
595
+ });
596
+ await this.relayMessage(normalizedId, statusMsg.message, {
597
+ additionalNodes: [
598
+ {
599
+ tag: 'meta',
600
+ attrs: isPrivate ? { is_status_mention: 'true' } : { is_group_status_mention: 'true' }
601
+ }
602
+ ]
603
+ });
604
+ await delay(2000);
605
+ }
606
+ catch (error) {
607
+ this.config.logger.error(`Error sending to ${id}: ${error}`);
608
+ }
609
+ }
610
+ return msg;
611
+ }
612
+ }