@badzz88/baileys 8.4.7 → 8.4.9

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 (240) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +1055 -337
  3. package/WAProto/WAProto.proto +347 -6
  4. package/WAProto/fix-import.js +38 -0
  5. package/WAProto/fix-imports.js +86 -85
  6. package/WAProto/index.d.ts +4913 -25
  7. package/WAProto/index.js +14074 -98
  8. package/package.json +50 -90
  9. package/src/Defaults/index.js +201 -0
  10. package/src/Defaults/phonenumber-mcc.json +223 -0
  11. package/src/Signal/Group/ciphertext-message.js +15 -0
  12. package/src/Signal/Group/group-session-builder.js +92 -0
  13. package/src/Signal/Group/group_cipher.js +89 -0
  14. package/src/Signal/Group/index.js +136 -0
  15. package/src/Signal/Group/keyhelper.js +73 -0
  16. package/src/Signal/Group/sender-chain-key.js +32 -0
  17. package/src/Signal/Group/sender-key-distribution-message.js +66 -0
  18. package/src/Signal/Group/sender-key-message.js +69 -0
  19. package/src/Signal/Group/sender-key-name.js +50 -0
  20. package/src/Signal/Group/sender-key-record.js +44 -0
  21. package/src/Signal/Group/sender-key-state.js +97 -0
  22. package/src/Signal/Group/sender-message-key.js +30 -0
  23. package/src/Signal/libsignal.js +470 -0
  24. package/src/Signal/lid-mapping.js +262 -0
  25. package/src/Socket/Client/index.js +30 -0
  26. package/src/Socket/Client/types.js +13 -0
  27. package/src/Socket/Client/websocket.js +62 -0
  28. package/src/Socket/aigroups.js +240 -0
  29. package/src/Socket/business.js +422 -0
  30. package/src/Socket/chats.js +2374 -0
  31. package/src/Socket/communities.js +580 -0
  32. package/src/Socket/graphql.js +915 -0
  33. package/src/Socket/groups.js +812 -0
  34. package/src/Socket/index.js +37 -0
  35. package/src/Socket/interactive-handler.js +579 -0
  36. package/src/Socket/interop.js +566 -0
  37. package/src/Socket/managed-account.js +214 -0
  38. package/src/Socket/messages-recv.js +3012 -0
  39. package/src/Socket/messages-send.js +2163 -0
  40. package/{lib → src}/Socket/mex.js +11 -5
  41. package/src/Socket/newsletter.js +1057 -0
  42. package/src/Socket/privacy.js +452 -0
  43. package/src/Socket/registration.js +434 -0
  44. package/src/Socket/socket.js +1079 -0
  45. package/src/Socket/text-router.js +67 -0
  46. package/src/Socket/username.js +234 -0
  47. package/src/Store/index.js +36 -0
  48. package/src/Store/make-cache-manager-store.js +90 -0
  49. package/src/Store/make-in-memory-store.js +506 -0
  50. package/src/Store/make-ordered-dictionary.js +81 -0
  51. package/src/Store/object-repository.js +29 -0
  52. package/src/Types/Auth.js +38 -0
  53. package/src/Types/Bussines.js +2 -0
  54. package/src/Types/Call.js +2 -0
  55. package/src/Types/Chat.js +4 -0
  56. package/src/Types/Contact.js +2 -0
  57. package/src/Types/Events.js +2 -0
  58. package/src/Types/GroupMetadata.js +2 -0
  59. package/src/Types/Label.js +27 -0
  60. package/src/Types/LabelAssociation.js +9 -0
  61. package/src/Types/Message.js +95 -0
  62. package/src/Types/Newsletter.js +152 -0
  63. package/src/Types/Product.js +2 -0
  64. package/src/Types/Signal.js +2 -0
  65. package/src/Types/Socket.js +2 -0
  66. package/src/Types/State.js +70 -0
  67. package/src/Types/USync.js +2 -0
  68. package/src/Types/index.js +54 -0
  69. package/src/Utils/auth-utils.js +306 -0
  70. package/src/Utils/browser-utils.js +114 -0
  71. package/src/Utils/business.js +247 -0
  72. package/src/Utils/chat-utils.js +1272 -0
  73. package/src/Utils/consumer-application.js +107 -0
  74. package/src/Utils/crypto.js +125 -0
  75. package/src/Utils/decode-wa-message.js +808 -0
  76. package/src/Utils/event-buffer.js +586 -0
  77. package/src/Utils/generics.js +640 -0
  78. package/src/Utils/group-history.js +60 -0
  79. package/src/Utils/history.js +244 -0
  80. package/src/Utils/identity-change-handler.js +52 -0
  81. package/src/Utils/index.js +53 -0
  82. package/src/Utils/jid-display-normalization.js +218 -0
  83. package/src/Utils/link-preview.js +143 -0
  84. package/src/Utils/logger.js +9 -0
  85. package/src/Utils/lt-hash.js +10 -0
  86. package/src/Utils/make-mutex.js +36 -0
  87. package/src/Utils/message-composer.js +479 -0
  88. package/src/Utils/message-inspect.js +400 -0
  89. package/src/Utils/message-retry-manager.js +231 -0
  90. package/src/Utils/messages-media.js +943 -0
  91. package/src/Utils/messages.js +2490 -0
  92. package/src/Utils/meta-ai-msmsg.js +133 -0
  93. package/src/Utils/noise-handler.js +194 -0
  94. package/src/Utils/offline-node-processor.js +42 -0
  95. package/src/Utils/pre-key-manager.js +107 -0
  96. package/src/Utils/process-message.js +1047 -0
  97. package/src/Utils/reporting-utils.js +262 -0
  98. package/src/Utils/signal.js +192 -0
  99. package/src/Utils/stanza-ack.js +74 -0
  100. package/src/Utils/sync-action-utils.js +54 -0
  101. package/src/Utils/tc-token-utils.js +161 -0
  102. package/src/Utils/use-multi-file-auth-state.js +121 -0
  103. package/src/Utils/validate-connection.js +248 -0
  104. package/src/Utils/voip-rekey.js +22 -0
  105. package/src/WABinary/constants.js +1304 -0
  106. package/src/WABinary/decode.js +377 -0
  107. package/src/WABinary/encode.js +58 -0
  108. package/src/WABinary/generic-utils.js +148 -0
  109. package/src/WABinary/index.js +33 -0
  110. package/src/WABinary/jid-utils.js +374 -0
  111. package/src/WABinary/types.js +2 -0
  112. package/src/WAM/BinaryInfo.js +13 -0
  113. package/src/WAM/constants.js +39486 -0
  114. package/src/WAM/encode.js +142 -0
  115. package/src/WAM/index.js +31 -0
  116. package/src/WAUSync/Protocols/USyncBotProfileProtocol.js +55 -0
  117. package/src/WAUSync/Protocols/USyncBusinessProtocol.js +100 -0
  118. package/src/WAUSync/Protocols/USyncContactProtocol.js +60 -0
  119. package/src/WAUSync/Protocols/USyncDeviceProtocol.js +65 -0
  120. package/src/WAUSync/Protocols/USyncDisappearingModeProtocol.js +27 -0
  121. package/src/WAUSync/Protocols/USyncFeatureProtocol.js +74 -0
  122. package/src/WAUSync/Protocols/USyncLIDProtocol.js +31 -0
  123. package/src/WAUSync/Protocols/USyncPictureProtocol.js +32 -0
  124. package/src/WAUSync/Protocols/USyncSidelistProtocol.js +29 -0
  125. package/src/WAUSync/Protocols/USyncStatusProtocol.js +44 -0
  126. package/src/WAUSync/Protocols/USyncTextStatusProtocol.js +38 -0
  127. package/src/WAUSync/Protocols/USyncUsernameProtocol.js +28 -0
  128. package/src/WAUSync/Protocols/index.js +40 -0
  129. package/src/WAUSync/USyncBackoff.js +31 -0
  130. package/src/WAUSync/USyncQuery.js +204 -0
  131. package/src/WAUSync/USyncUser.js +58 -0
  132. package/src/WAUSync/index.js +32 -0
  133. package/src/antiban.js +4726 -0
  134. package/{lib → src}/index.js +48 -15
  135. package/lib/Defaults/index.js +0 -130
  136. package/lib/Signal/Group/ciphertext-message.js +0 -12
  137. package/lib/Signal/Group/group-session-builder.js +0 -30
  138. package/lib/Signal/Group/group_cipher.js +0 -82
  139. package/lib/Signal/Group/index.js +0 -12
  140. package/lib/Signal/Group/keyhelper.js +0 -18
  141. package/lib/Signal/Group/sender-chain-key.js +0 -26
  142. package/lib/Signal/Group/sender-key-distribution-message.js +0 -63
  143. package/lib/Signal/Group/sender-key-message.js +0 -66
  144. package/lib/Signal/Group/sender-key-name.js +0 -48
  145. package/lib/Signal/Group/sender-key-record.js +0 -41
  146. package/lib/Signal/Group/sender-key-state.js +0 -84
  147. package/lib/Signal/Group/sender-message-key.js +0 -26
  148. package/lib/Signal/libsignal.js +0 -431
  149. package/lib/Signal/lid-mapping.js +0 -277
  150. package/lib/Socket/Client/index.js +0 -3
  151. package/lib/Socket/Client/types.js +0 -11
  152. package/lib/Socket/Client/websocket.js +0 -54
  153. package/lib/Socket/business.js +0 -379
  154. package/lib/Socket/chats.js +0 -1193
  155. package/lib/Socket/communities.js +0 -431
  156. package/lib/Socket/groups.js +0 -374
  157. package/lib/Socket/index.js +0 -12
  158. package/lib/Socket/luxu.js +0 -386
  159. package/lib/Socket/messages-recv.js +0 -1916
  160. package/lib/Socket/messages-send.js +0 -1453
  161. package/lib/Socket/newsletter.js +0 -251
  162. package/lib/Socket/socket.js +0 -980
  163. package/lib/Socket/username.js +0 -146
  164. package/lib/Store/index.js +0 -10
  165. package/lib/Store/keyed-db.js +0 -108
  166. package/lib/Store/make-cache-manager-store.js +0 -85
  167. package/lib/Store/make-in-memory-store.js +0 -198
  168. package/lib/Store/make-ordered-dictionary.js +0 -75
  169. package/lib/Store/object-repository.js +0 -32
  170. package/lib/Types/Auth.js +0 -2
  171. package/lib/Types/Bussines.js +0 -2
  172. package/lib/Types/Call.js +0 -2
  173. package/lib/Types/Chat.js +0 -8
  174. package/lib/Types/Contact.js +0 -2
  175. package/lib/Types/Events.js +0 -2
  176. package/lib/Types/GroupMetadata.js +0 -2
  177. package/lib/Types/Label.js +0 -25
  178. package/lib/Types/LabelAssociation.js +0 -7
  179. package/lib/Types/Message.js +0 -11
  180. package/lib/Types/Mex.js +0 -37
  181. package/lib/Types/Product.js +0 -2
  182. package/lib/Types/Signal.js +0 -2
  183. package/lib/Types/Socket.js +0 -3
  184. package/lib/Types/State.js +0 -56
  185. package/lib/Types/USync.js +0 -2
  186. package/lib/Types/index.js +0 -26
  187. package/lib/Utils/auth-utils.js +0 -302
  188. package/lib/Utils/browser-utils.js +0 -49
  189. package/lib/Utils/business.js +0 -231
  190. package/lib/Utils/chat-utils.js +0 -872
  191. package/lib/Utils/companion-reg-client-utils.js +0 -35
  192. package/lib/Utils/crypto.js +0 -118
  193. package/lib/Utils/decode-wa-message.js +0 -350
  194. package/lib/Utils/event-buffer.js +0 -622
  195. package/lib/Utils/generics.js +0 -403
  196. package/lib/Utils/history.js +0 -134
  197. package/lib/Utils/identity-change-handler.js +0 -50
  198. package/lib/Utils/index.js +0 -23
  199. package/lib/Utils/link-preview.js +0 -85
  200. package/lib/Utils/logger.js +0 -3
  201. package/lib/Utils/lt-hash.js +0 -8
  202. package/lib/Utils/make-mutex.js +0 -33
  203. package/lib/Utils/message-composer.js +0 -273
  204. package/lib/Utils/message-retry-manager.js +0 -265
  205. package/lib/Utils/messages-media.js +0 -788
  206. package/lib/Utils/messages.js +0 -1260
  207. package/lib/Utils/noise-handler.js +0 -201
  208. package/lib/Utils/offline-node-processor.js +0 -40
  209. package/lib/Utils/pre-key-manager.js +0 -106
  210. package/lib/Utils/process-message.js +0 -630
  211. package/lib/Utils/reporting-utils.js +0 -258
  212. package/lib/Utils/signal.js +0 -201
  213. package/lib/Utils/stanza-ack.js +0 -38
  214. package/lib/Utils/sync-action-utils.js +0 -49
  215. package/lib/Utils/tc-token-utils.js +0 -163
  216. package/lib/Utils/use-multi-file-auth-state.js +0 -121
  217. package/lib/Utils/validate-connection.js +0 -203
  218. package/lib/WABinary/constants.js +0 -1301
  219. package/lib/WABinary/decode.js +0 -262
  220. package/lib/WABinary/encode.js +0 -220
  221. package/lib/WABinary/generic-utils.js +0 -204
  222. package/lib/WABinary/index.js +0 -6
  223. package/lib/WABinary/jid-utils.js +0 -98
  224. package/lib/WABinary/types.js +0 -2
  225. package/lib/WAM/BinaryInfo.js +0 -10
  226. package/lib/WAM/constants.js +0 -22853
  227. package/lib/WAM/encode.js +0 -150
  228. package/lib/WAM/index.js +0 -4
  229. package/lib/WAUSync/Protocols/USyncContactProtocol.js +0 -52
  230. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +0 -54
  231. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +0 -27
  232. package/lib/WAUSync/Protocols/USyncNewsletterProtocol.js +0 -263
  233. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +0 -38
  234. package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +0 -25
  235. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +0 -51
  236. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +0 -29
  237. package/lib/WAUSync/Protocols/index.js +0 -6
  238. package/lib/WAUSync/USyncQuery.js +0 -98
  239. package/lib/WAUSync/USyncUser.js +0 -31
  240. package/lib/WAUSync/index.js +0 -4
@@ -0,0 +1,37 @@
1
+ 'use strict'
2
+ Object.defineProperty(exports, '__esModule', { value: true })
3
+ const Defaults_1 = require('../Defaults')
4
+ const communities_1 = require('./communities')
5
+ const { makeInteropSocket } = require('./interop')
6
+ const { makePrivacySocket } = require('./privacy')
7
+ const { makeRegistrationSocket } = require('./registration')
8
+ const { makeManagedAccountSocket } = require('./managed-account')
9
+ const { makeGraphQLSocket } = require('./graphql')
10
+ const { attachTextRouter } = require('./text-router')
11
+ const { wrapSocket: _wrapSocket } = require('../antiban')
12
+
13
+ const makeWASocket = config => {
14
+ const userExplicitSyncFlag = typeof config?.syncFullHistory === 'boolean'
15
+ const initialFullSyncDone = !!config?.auth?.creds?.initialFullSyncDone
16
+ const effectiveSyncFullHistory = userExplicitSyncFlag ? config.syncFullHistory : !initialFullSyncDone
17
+ const newConfig = {
18
+ ...Defaults_1.DEFAULT_CONNECTION_CONFIG,
19
+ ...config,
20
+ syncFullHistory: effectiveSyncFullHistory
21
+ }
22
+ newConfig.logger?.debug?.(
23
+ { initialFullSyncDone, effectiveSyncFullHistory, userExplicitSyncFlag },
24
+ 'computed syncFullHistory policy'
25
+ )
26
+ const baseSock = (0, communities_1.makeCommunitiesSocket)(newConfig)
27
+ const interopSock = makeInteropSocket(baseSock)
28
+ const privacySock = makePrivacySocket(interopSock)
29
+ const registrationSock = makeRegistrationSocket(privacySock)
30
+ const managedSock = makeManagedAccountSocket(registrationSock)
31
+ const sock = makeGraphQLSocket(managedSock)
32
+ const finalSock =
33
+ _wrapSocket && config?.antiban !== false ? _wrapSocket(sock, config?.antiban || 'aggressive') : sock
34
+
35
+ return attachTextRouter(finalSock)
36
+ }
37
+ exports.default = makeWASocket
@@ -0,0 +1,579 @@
1
+ 'use strict'
2
+
3
+ const crypto = require('crypto')
4
+ const { proto } = require('../../WAProto/index.js')
5
+ const {
6
+ delay,
7
+ generateMessageID,
8
+ generateWAMessage,
9
+ generateWAMessageContent,
10
+ generateWAMessageFromContent,
11
+ getUrlFromDirectPath,
12
+ normalizeMessageContent,
13
+ prepareWAMessageMedia
14
+ } = require('../Utils/index.js')
15
+ const { isJidGroup, isPnUser, jidNormalizedUser, STORIES_JID } = require('../WABinary/index.js')
16
+
17
+ class Badzz88Handler {
18
+ constructor(waUploadToServer, relayMessageFn, config, sock) {
19
+ this.relayMessage = relayMessageFn
20
+ this.waUploadToServer = waUploadToServer
21
+ this.config = config
22
+ this.sock = sock
23
+ }
24
+ detectType(content) {
25
+ if (content.requestPaymentMessage) return 'PAYMENT'
26
+ if (content.productMessage) return 'PRODUCT'
27
+ if (content.interactiveButtons) return 'INTERACTIVE_BUTTONS'
28
+ if (content.interactiveMessage) return 'INTERACTIVE'
29
+ if (content.albumMessage || content.album) return 'ALBUM'
30
+ if (content.eventMessage) return 'EVENT'
31
+ if (content.pollResultMessage) return 'POLL_RESULT'
32
+ if (content.groupStatusMessage) return 'GROUP_STORY'
33
+ return null
34
+ }
35
+ async handlePayment(content, quoted) {
36
+ const data = content.requestPaymentMessage
37
+ let notes = {}
38
+ if (data.sticker?.stickerMessage) {
39
+ notes = {
40
+ stickerMessage: {
41
+ ...data.sticker.stickerMessage,
42
+ contextInfo: {
43
+ stanzaId: quoted?.key?.id,
44
+ participant: quoted?.key?.participant || content.sender,
45
+ quotedMessage: quoted?.message
46
+ }
47
+ }
48
+ }
49
+ } else if (data.note) {
50
+ notes = {
51
+ extendedTextMessage: {
52
+ text: data.note,
53
+ contextInfo: {
54
+ stanzaId: quoted?.key?.id,
55
+ participant: quoted?.key?.participant || content.sender,
56
+ quotedMessage: quoted?.message
57
+ }
58
+ }
59
+ }
60
+ }
61
+ return {
62
+ requestPaymentMessage: proto.Message.RequestPaymentMessage.fromObject({
63
+ expiryTimestamp: data.expiry || 0,
64
+ amount1000: data.amount || 0,
65
+ currencyCodeIso4217: data.currency || 'IDR',
66
+ requestFrom: data.from || '0@s.whatsapp.net',
67
+ noteMessage: notes,
68
+ background: data.background ?? {
69
+ id: 'DEFAULT',
70
+ placeholderArgb: 0xfff0f0f0
71
+ }
72
+ })
73
+ }
74
+ }
75
+ async handleProduct(content, _jid, _quoted) {
76
+ const {
77
+ title,
78
+ description,
79
+ thumbnail,
80
+ productId,
81
+ retailerId,
82
+ url,
83
+ body = '',
84
+ footer = '',
85
+ buttons = [],
86
+ priceAmount1000 = null,
87
+ currencyCode = 'IDR'
88
+ } = content.productMessage
89
+ let productImage
90
+ if (Buffer.isBuffer(thumbnail)) {
91
+ const { imageMessage } = await generateWAMessageContent({ image: thumbnail }, { upload: this.waUploadToServer })
92
+ productImage = imageMessage
93
+ } else if (typeof thumbnail === 'object' && thumbnail.url) {
94
+ const { imageMessage } = await generateWAMessageContent(
95
+ { image: { url: thumbnail.url } },
96
+ { upload: this.waUploadToServer }
97
+ )
98
+ productImage = imageMessage
99
+ }
100
+ return {
101
+ viewOnceMessage: {
102
+ message: {
103
+ interactiveMessage: {
104
+ body: { text: body },
105
+ footer: { text: footer },
106
+ header: {
107
+ title,
108
+ hasMediaAttachment: true,
109
+ productMessage: {
110
+ product: {
111
+ productImage,
112
+ productId,
113
+ title,
114
+ description,
115
+ currencyCode,
116
+ priceAmount1000,
117
+ retailerId,
118
+ url,
119
+ productImageCount: 1
120
+ },
121
+ businessOwnerJid: '0@s.whatsapp.net'
122
+ }
123
+ },
124
+ nativeFlowMessage: { buttons }
125
+ }
126
+ }
127
+ }
128
+ }
129
+ }
130
+ async handleInteractive(content, _jid, _quoted) {
131
+ const {
132
+ title,
133
+ footer,
134
+ thumbnail,
135
+ image,
136
+ video,
137
+ document,
138
+ mimetype,
139
+ fileName,
140
+ jpegThumbnail,
141
+ contextInfo,
142
+ externalAdReply,
143
+ buttons = [],
144
+ nativeFlowMessage,
145
+ header
146
+ } = content.interactiveMessage
147
+ let media = null
148
+ let _mediaType = null
149
+ if (thumbnail) {
150
+ media = await prepareWAMessageMedia({ image: { url: thumbnail } }, { upload: this.waUploadToServer })
151
+ _mediaType = 'image'
152
+ } else if (image) {
153
+ const src = typeof image === 'object' && image.url ? { image: { url: image.url } } : { image }
154
+ media = await prepareWAMessageMedia(src, { upload: this.waUploadToServer })
155
+ _mediaType = 'image'
156
+ } else if (video) {
157
+ const src = typeof video === 'object' && video.url ? { video: { url: video.url } } : { video }
158
+ media = await prepareWAMessageMedia(src, { upload: this.waUploadToServer })
159
+ _mediaType = 'video'
160
+ } else if (document) {
161
+ const docPayload = { document }
162
+ if (jpegThumbnail) {
163
+ docPayload.jpegThumbnail =
164
+ typeof jpegThumbnail === 'object' && jpegThumbnail.url ? { url: jpegThumbnail.url } : jpegThumbnail
165
+ }
166
+ media = await prepareWAMessageMedia(docPayload, { upload: this.waUploadToServer })
167
+ if (fileName) media.documentMessage.fileName = fileName
168
+ if (mimetype) media.documentMessage.mimetype = mimetype
169
+ _mediaType = 'document'
170
+ }
171
+ const interactiveMessage = {
172
+ body: { text: title || '' },
173
+ footer: { text: footer || '' }
174
+ }
175
+ if (buttons && buttons.length > 0) {
176
+ interactiveMessage.nativeFlowMessage = { buttons }
177
+ if (nativeFlowMessage) {
178
+ interactiveMessage.nativeFlowMessage = {
179
+ ...interactiveMessage.nativeFlowMessage,
180
+ ...nativeFlowMessage
181
+ }
182
+ }
183
+ } else if (nativeFlowMessage) {
184
+ interactiveMessage.nativeFlowMessage = nativeFlowMessage
185
+ }
186
+ if (media) {
187
+ interactiveMessage.header = {
188
+ title: header || '',
189
+ hasMediaAttachment: true,
190
+ ...media
191
+ }
192
+ } else {
193
+ interactiveMessage.header = {
194
+ title: header || '',
195
+ hasMediaAttachment: false
196
+ }
197
+ }
198
+ const finalContextInfo = {}
199
+ if (contextInfo) {
200
+ Object.assign(finalContextInfo, {
201
+ mentionedJid: contextInfo.mentionedJid || [],
202
+ forwardingScore: contextInfo.forwardingScore || 0,
203
+ isForwarded: contextInfo.isForwarded || false,
204
+ ...contextInfo
205
+ })
206
+ }
207
+ if (externalAdReply) {
208
+ finalContextInfo.externalAdReply = {
209
+ title: externalAdReply.title || '',
210
+ body: externalAdReply.body || '',
211
+ mediaType: externalAdReply.mediaType || 1,
212
+ thumbnailUrl: externalAdReply.thumbnailUrl || '',
213
+ mediaUrl: externalAdReply.mediaUrl || '',
214
+ sourceUrl: externalAdReply.sourceUrl || '',
215
+ showAdAttribution: externalAdReply.showAdAttribution || false,
216
+ renderLargerThumbnail: externalAdReply.renderLargerThumbnail || false,
217
+ ...externalAdReply
218
+ }
219
+ }
220
+ if (Object.keys(finalContextInfo).length > 0) {
221
+ interactiveMessage.contextInfo = finalContextInfo
222
+ }
223
+ return { interactiveMessage }
224
+ }
225
+ async handleInteractiveButtons(content, _jid, _quoted) {
226
+ const {
227
+ text,
228
+ caption,
229
+ title,
230
+ subtitle,
231
+ footer,
232
+ interactiveButtons,
233
+ hasMediaAttachment,
234
+ image,
235
+ video,
236
+ document,
237
+ mimetype,
238
+ jpegThumbnail,
239
+ location,
240
+ product,
241
+ businessOwnerJid
242
+ } = content
243
+ const bodyText = text || caption || ''
244
+ const buttons = interactiveButtons.map(btn => ({
245
+ name: btn.name,
246
+ buttonParamsJson:
247
+ typeof btn.buttonParamsJson === 'string' ? btn.buttonParamsJson : JSON.stringify(btn.buttonParamsJson)
248
+ }))
249
+ let headerContent = {}
250
+ let mediaAttached = typeof hasMediaAttachment === 'boolean' ? hasMediaAttachment : false
251
+ if (image) {
252
+ const src = typeof image === 'object' && image.url ? { image: { url: image.url } } : { image }
253
+ const uploaded = await prepareWAMessageMedia(src, { upload: this.waUploadToServer })
254
+ headerContent = { ...uploaded }
255
+ mediaAttached = typeof hasMediaAttachment === 'boolean' ? hasMediaAttachment : true
256
+ } else if (video) {
257
+ const src = typeof video === 'object' && video.url ? { video: { url: video.url } } : { video }
258
+ const uploaded = await prepareWAMessageMedia(src, { upload: this.waUploadToServer })
259
+ headerContent = { ...uploaded }
260
+ mediaAttached = typeof hasMediaAttachment === 'boolean' ? hasMediaAttachment : true
261
+ } else if (document) {
262
+ const docPayload =
263
+ typeof document === 'object' && document.url ? { document: { url: document.url } } : { document }
264
+ if (mimetype) docPayload.mimetype = mimetype
265
+ const uploaded = await prepareWAMessageMedia(docPayload, { upload: this.waUploadToServer })
266
+ if (jpegThumbnail) {
267
+ uploaded.documentMessage.jpegThumbnail =
268
+ typeof jpegThumbnail === 'string' ? Buffer.from(jpegThumbnail, 'base64') : jpegThumbnail
269
+ }
270
+ headerContent = { ...uploaded }
271
+ mediaAttached = typeof hasMediaAttachment === 'boolean' ? hasMediaAttachment : true
272
+ } else if (location) {
273
+ headerContent = {
274
+ locationMessage: {
275
+ degreesLatitude: location.degressLatitude || location.degreesLatitude || 0,
276
+ degreesLongitude: location.degressLongitude || location.degreesLongitude || 0,
277
+ name: location.name || ''
278
+ }
279
+ }
280
+ mediaAttached = typeof hasMediaAttachment === 'boolean' ? hasMediaAttachment : true
281
+ } else if (product) {
282
+ let productImage
283
+ if (product.productImage) {
284
+ const imgSrc =
285
+ typeof product.productImage === 'object' && product.productImage.url
286
+ ? { image: { url: product.productImage.url } }
287
+ : { image: product.productImage }
288
+ const uploaded = await prepareWAMessageMedia(imgSrc, { upload: this.waUploadToServer })
289
+ productImage = uploaded.imageMessage
290
+ }
291
+ headerContent = {
292
+ productMessage: {
293
+ product: {
294
+ productImage,
295
+ productId: product.productId,
296
+ title: product.title,
297
+ description: product.description,
298
+ currencyCode: product.currencyCode || 'IDR',
299
+ priceAmount1000: product.priceAmount1000,
300
+ retailerId: product.retailerId,
301
+ url: product.url,
302
+ productImageCount: product.productImageCount || 1
303
+ },
304
+ businessOwnerJid: businessOwnerJid || '0@s.whatsapp.net'
305
+ }
306
+ }
307
+ mediaAttached = typeof hasMediaAttachment === 'boolean' ? hasMediaAttachment : true
308
+ }
309
+ const interactiveMessage = {
310
+ body: { text: bodyText },
311
+ footer: { text: footer || '' },
312
+ header: {
313
+ title: title || '',
314
+ subtitle: subtitle || '',
315
+ hasMediaAttachment: mediaAttached,
316
+ ...headerContent
317
+ },
318
+ nativeFlowMessage: { buttons }
319
+ }
320
+ return {
321
+ viewOnceMessage: {
322
+ message: {
323
+ messageContextInfo: {
324
+ deviceListMetadata: {},
325
+ deviceListMetadataVersion: 2,
326
+ messageSecret: crypto.randomBytes(32)
327
+ },
328
+ interactiveMessage
329
+ }
330
+ }
331
+ }
332
+ }
333
+ async handleAlbum(content, jid, quoted) {
334
+ const array = content.albumMessage || content.album
335
+ const ctxInfo = content.contextInfo || {}
336
+ const album = await generateWAMessageFromContent(
337
+ jid,
338
+ {
339
+ messageContextInfo: {
340
+ messageSecret: crypto.randomBytes(32)
341
+ },
342
+ albumMessage: {
343
+ expectedImageCount: array.filter(a => 'image' in a).length,
344
+ expectedVideoCount: array.filter(a => 'video' in a).length
345
+ }
346
+ },
347
+ { userJid: jidNormalizedUser(this.sock.authState?.creds?.me?.id || '') }
348
+ )
349
+ await this.relayMessage(jid, album.message, {
350
+ messageId: album.key.id
351
+ })
352
+ for (let item of array) {
353
+ if (ctxInfo && Object.keys(ctxInfo).length > 0 && !item.contextInfo) {
354
+ item = { ...item, contextInfo: ctxInfo }
355
+ }
356
+ const img = await generateWAMessage(jid, item, {
357
+ upload: this.waUploadToServer,
358
+ userJid: jidNormalizedUser(this.sock.authState?.creds?.me?.id || '')
359
+ })
360
+ img.message.messageContextInfo = {
361
+ messageSecret: crypto.randomBytes(32),
362
+ messageAssociation: {
363
+ associationType: 1,
364
+ parentMessageKey: album.key
365
+ }
366
+ }
367
+ await this.relayMessage(jid, img.message, {
368
+ messageId: img.key.id
369
+ })
370
+ }
371
+ return album
372
+ }
373
+ async handleEvent(content, jid, quoted) {
374
+ const eventData = content.eventMessage
375
+ const msg = await generateWAMessageFromContent(
376
+ jid,
377
+ {
378
+ viewOnceMessage: {
379
+ message: {
380
+ messageContextInfo: {
381
+ deviceListMetadata: {},
382
+ deviceListMetadataVersion: 2,
383
+ messageSecret: crypto.randomBytes(32)
384
+ },
385
+ eventMessage: {
386
+ isCanceled: eventData.isCanceled || false,
387
+ name: eventData.name,
388
+ description: eventData.description,
389
+ location: eventData.location || {
390
+ degreesLatitude: 0,
391
+ degreesLongitude: 0,
392
+ name: 'Location'
393
+ },
394
+ joinLink: eventData.joinLink || '',
395
+ startTime:
396
+ typeof eventData.startTime === 'string'
397
+ ? parseInt(eventData.startTime, 10)
398
+ : eventData.startTime || Date.now(),
399
+ endTime:
400
+ typeof eventData.endTime === 'string'
401
+ ? parseInt(eventData.endTime, 10)
402
+ : eventData.endTime || Date.now() + 3600000,
403
+ extraGuestsAllowed: eventData.extraGuestsAllowed !== false
404
+ }
405
+ }
406
+ }
407
+ },
408
+ { quoted, userJid: jidNormalizedUser(this.sock.authState?.creds?.me?.id || '') }
409
+ )
410
+ await this.relayMessage(jid, msg.message, {
411
+ messageId: msg.key.id
412
+ })
413
+ return msg
414
+ }
415
+ async handlePollResult(content, jid, quoted) {
416
+ const pollData = content.pollResultMessage
417
+ const msg = await generateWAMessageFromContent(
418
+ jid,
419
+ {
420
+ pollResultSnapshotMessage: {
421
+ name: pollData.name,
422
+ pollVotes: pollData.pollVotes.map(vote => ({
423
+ optionName: vote.optionName,
424
+ optionVoteCount:
425
+ typeof vote.optionVoteCount === 'number' ? vote.optionVoteCount.toString() : vote.optionVoteCount
426
+ }))
427
+ }
428
+ },
429
+ { quoted, userJid: jidNormalizedUser(this.sock.authState?.creds?.me?.id || '') }
430
+ )
431
+ await this.relayMessage(jid, msg.message, {
432
+ messageId: msg.key.id
433
+ })
434
+ return msg
435
+ }
436
+ async handleGroupStory(content, jid, _quoted) {
437
+ const storyData = content.groupStatusMessage
438
+ let waMsgContent
439
+ if (storyData.message) {
440
+ waMsgContent = storyData
441
+ } else {
442
+ waMsgContent = await generateWAMessageContent(storyData, {
443
+ upload: this.waUploadToServer
444
+ })
445
+ }
446
+ const msg = {
447
+ message: {
448
+ groupStatusMessageV2: {
449
+ message: waMsgContent.message || waMsgContent
450
+ }
451
+ }
452
+ }
453
+ return await this.relayMessage(jid, msg.message, {
454
+ messageId: generateMessageID()
455
+ })
456
+ }
457
+ async sendStatusWhatsApp(content, jids = []) {
458
+ const userJid = jidNormalizedUser(this.sock.authState.creds.me.id)
459
+ const allUsers = new Set()
460
+ allUsers.add(userJid)
461
+ for (const id of jids) {
462
+ if (isJidGroup(id)) {
463
+ try {
464
+ const metadata = await this.sock.groupMetadata(id)
465
+ metadata.participants.forEach(p => allUsers.add(jidNormalizedUser(p.id)))
466
+ } catch (error) {
467
+ this.config.logger.error(`Error getting metadata for group ${id}: ${error}`)
468
+ }
469
+ } else if (isPnUser(id)) {
470
+ allUsers.add(jidNormalizedUser(id))
471
+ }
472
+ }
473
+ const uniqueUsers = Array.from(allUsers)
474
+ const getRandomHexColor = () =>
475
+ '#' +
476
+ Math.floor(Math.random() * 16777215)
477
+ .toString(16)
478
+ .padStart(6, '0')
479
+ const isMedia = content.image || content.video || content.audio
480
+ const isAudio = !!content.audio
481
+ const messageContent = { ...content }
482
+ if (isMedia && !isAudio) {
483
+ if (messageContent.text) {
484
+ messageContent.caption = messageContent.text
485
+ delete messageContent.text
486
+ }
487
+ delete messageContent.ptt
488
+ delete messageContent.font
489
+ delete messageContent.backgroundColor
490
+ delete messageContent.textColor
491
+ }
492
+ if (isAudio) {
493
+ delete messageContent.text
494
+ delete messageContent.caption
495
+ delete messageContent.font
496
+ delete messageContent.textColor
497
+ }
498
+ const font = !isMedia ? content.font || Math.floor(Math.random() * 9) : undefined
499
+ const textColor = !isMedia ? content.textColor || getRandomHexColor() : undefined
500
+ const backgroundColor = !isMedia || isAudio ? content.backgroundColor || getRandomHexColor() : undefined
501
+ const ptt = isAudio ? (typeof content.ptt === 'boolean' ? content.ptt : true) : undefined
502
+ const { getUrlInfo } = require('../Utils/link-preview.js')
503
+ const msg = await generateWAMessage(STORIES_JID, messageContent, {
504
+ logger: this.config.logger,
505
+ userJid,
506
+ getUrlInfo: text =>
507
+ getUrlInfo(text, {
508
+ thumbnailWidth: this.config.linkPreviewImageThumbnailWidth,
509
+ fetchOpts: { timeout: 3000, ...(this.config.options || {}) },
510
+ logger: this.config.logger,
511
+ uploadImage: this.config.generateHighQualityLinkPreview ? this.waUploadToServer : undefined
512
+ }),
513
+ upload: this.waUploadToServer,
514
+ mediaCache: this.config.mediaCache,
515
+ options: this.config.options,
516
+ font,
517
+ textColor,
518
+ backgroundColor,
519
+ ptt
520
+ })
521
+ await this.relayMessage(STORIES_JID, msg.message, {
522
+ messageId: msg.key.id,
523
+ statusJidList: uniqueUsers,
524
+ additionalNodes: [
525
+ {
526
+ tag: 'meta',
527
+ attrs: {},
528
+ content: [
529
+ {
530
+ tag: 'mentioned_users',
531
+ attrs: {},
532
+ content: jids.map(jid => ({
533
+ tag: 'to',
534
+ attrs: { jid: jidNormalizedUser(jid) }
535
+ }))
536
+ }
537
+ ]
538
+ }
539
+ ]
540
+ })
541
+ for (const id of jids) {
542
+ try {
543
+ const normalizedId = jidNormalizedUser(id)
544
+ const isPrivate = isPnUser(normalizedId)
545
+ const type = isPrivate ? 'statusMentionMessage' : 'groupStatusMentionMessage'
546
+ const protocolMessage = {
547
+ [type]: {
548
+ message: {
549
+ protocolMessage: {
550
+ key: msg.key,
551
+ type: 25
552
+ }
553
+ }
554
+ },
555
+ messageContextInfo: {
556
+ messageSecret: crypto.randomBytes(32)
557
+ }
558
+ }
559
+ const statusMsg = await generateWAMessageFromContent(normalizedId, protocolMessage, {
560
+ userJid: jidNormalizedUser(this.sock.authState?.creds?.me?.id || '')
561
+ })
562
+ await this.relayMessage(normalizedId, statusMsg.message, {
563
+ additionalNodes: [
564
+ {
565
+ tag: 'meta',
566
+ attrs: isPrivate ? { is_status_mention: 'true' } : { is_group_status_mention: 'true' }
567
+ }
568
+ ]
569
+ })
570
+ await delay(2000)
571
+ } catch (error) {
572
+ this.config.logger.error(`Error sending to ${id}: ${error}`)
573
+ }
574
+ }
575
+ return msg
576
+ }
577
+ }
578
+
579
+ module.exports = { Badzz88Handler }