@zervonexx/baileys 5.0.0 → 5.0.2

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 (127) hide show
  1. package/LICENSE +22 -22
  2. package/README.md +4124 -4112
  3. package/WAProto/WAProto.json +1 -0
  4. package/WAProto/WAProto.proto +7160 -0
  5. package/WAProto/WAProtoCompile.js +264 -322
  6. package/WAProto/index.js +55 -95
  7. package/lib/Builders/AIRich.js +673 -673
  8. package/lib/Builders/auto.js +49 -49
  9. package/lib/Builders/base.js +144 -144
  10. package/lib/Builders/builders.js +588 -588
  11. package/lib/Builders/index.js +3 -3
  12. package/lib/Defaults/index.js +249 -248
  13. package/lib/Signal/index.js +2 -2
  14. package/lib/Signal/libsignal.js +526 -526
  15. package/lib/Signal/lid-mapping.js +282 -282
  16. package/lib/Socket/Client/index.js +1 -1
  17. package/lib/Socket/Client/types.js +9 -9
  18. package/lib/Socket/Client/websocket.js +83 -83
  19. package/lib/Socket/business.js +151 -151
  20. package/lib/Socket/chats.js +854 -854
  21. package/lib/Socket/communities.js +146 -146
  22. package/lib/Socket/groups.js +293 -293
  23. package/lib/Socket/index.js +17 -14
  24. package/lib/Socket/messages-recv.js +993 -993
  25. package/lib/Socket/messages-send.js +722 -722
  26. package/lib/Socket/mex.js +33 -33
  27. package/lib/Socket/newsletter.js +783 -783
  28. package/lib/Socket/registration.js +154 -154
  29. package/lib/Socket/socket.js +797 -797
  30. package/lib/Socket/spam-report.js +143 -143
  31. package/lib/Socket/username.js +81 -81
  32. package/lib/Socket/zervonex-handler.js +57 -57
  33. package/lib/Store/index.js +6 -6
  34. package/lib/Store/make-cache-manager-store.js +80 -80
  35. package/lib/Store/make-in-memory-store.js +426 -426
  36. package/lib/Store/make-ordered-dictionary.js +81 -81
  37. package/lib/Store/object-repository.js +30 -30
  38. package/lib/Store/reporting-info-store.js +37 -37
  39. package/lib/Types/Auth.js +1 -1
  40. package/lib/Types/Bussines.js +1 -1
  41. package/lib/Types/Call.js +1 -1
  42. package/lib/Types/Chat.js +7 -7
  43. package/lib/Types/Contact.js +1 -1
  44. package/lib/Types/Events.js +1 -1
  45. package/lib/Types/GroupMetadata.js +1 -1
  46. package/lib/Types/Label.js +24 -24
  47. package/lib/Types/LabelAssociation.js +6 -6
  48. package/lib/Types/Message.js +14 -14
  49. package/lib/Types/Newsletter.js +99 -99
  50. package/lib/Types/Product.js +1 -1
  51. package/lib/Types/Signal.js +1 -1
  52. package/lib/Types/Socket.js +14 -14
  53. package/lib/Types/State.js +26 -26
  54. package/lib/Types/USync.js +1 -1
  55. package/lib/Types/index.js +30 -30
  56. package/lib/Utils/auth-utils.js +159 -159
  57. package/lib/Utils/baileys-event-stream.js +32 -32
  58. package/lib/Utils/bot-capabilities.js +40 -40
  59. package/lib/Utils/bot-planning-replay.js +43 -43
  60. package/lib/Utils/browser-utils.js +29 -29
  61. package/lib/Utils/business.js +74 -74
  62. package/lib/Utils/chat-utils.js +316 -316
  63. package/lib/Utils/companion-reg-client-utils.js +19 -19
  64. package/lib/Utils/consumer-application.js +37 -37
  65. package/lib/Utils/crypto.js +52 -52
  66. package/lib/Utils/cstoken.js +14 -14
  67. package/lib/Utils/decode-wa-message.js +199 -199
  68. package/lib/Utils/event-buffer.js +272 -272
  69. package/lib/Utils/extras.js +186 -186
  70. package/lib/Utils/generics.js +195 -194
  71. package/lib/Utils/history.js +50 -50
  72. package/lib/Utils/identity-change-handler.js +18 -18
  73. package/lib/Utils/index.js +40 -40
  74. package/lib/Utils/link-preview.js +50 -50
  75. package/lib/Utils/logger.js +3 -2
  76. package/lib/Utils/lt-hash.js +3 -3
  77. package/lib/Utils/make-mutex.js +15 -15
  78. package/lib/Utils/message-composer.js +61 -61
  79. package/lib/Utils/message-retry-manager.js +47 -47
  80. package/lib/Utils/messages-media.js +501 -501
  81. package/lib/Utils/messages.js +944 -942
  82. package/lib/Utils/meta-ai-msmsg.js +121 -121
  83. package/lib/Utils/meta-compositing.js +74 -74
  84. package/lib/Utils/modern-messages.js +234 -234
  85. package/lib/Utils/native-flow.js +80 -80
  86. package/lib/Utils/noise-handler.js +160 -160
  87. package/lib/Utils/pre-key-manager.js +75 -75
  88. package/lib/Utils/process-message.js +400 -400
  89. package/lib/Utils/reporting-utils.js +170 -170
  90. package/lib/Utils/signal.js +166 -166
  91. package/lib/Utils/spam-report-utils.js +165 -165
  92. package/lib/Utils/sync-action-utils.js +33 -33
  93. package/lib/Utils/tc-token-utils.js +153 -153
  94. package/lib/Utils/use-keyv-auth-state.js +199 -199
  95. package/lib/Utils/use-multi-file-auth-state.js +83 -83
  96. package/lib/Utils/validate-connection.js +171 -171
  97. package/lib/WABinary/constants.js +1320 -1320
  98. package/lib/WABinary/decode.js +151 -151
  99. package/lib/WABinary/encode.js +139 -139
  100. package/lib/WABinary/generic-utils.js +202 -202
  101. package/lib/WABinary/index.js +6 -6
  102. package/lib/WABinary/jid-utils.js +235 -235
  103. package/lib/WABinary/types.js +4 -4
  104. package/lib/WAM/BinaryInfo.js +9 -9
  105. package/lib/WAM/constants.js +22852 -22852
  106. package/lib/WAM/encode.js +149 -149
  107. package/lib/WAM/index.js +3 -3
  108. package/lib/WAUSync/Protocols/USyncBotProfileProtocol.js +30 -30
  109. package/lib/WAUSync/Protocols/USyncBusinessProtocol.js +48 -48
  110. package/lib/WAUSync/Protocols/USyncContactProtocol.js +23 -23
  111. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +28 -28
  112. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +15 -15
  113. package/lib/WAUSync/Protocols/USyncFeatureProtocol.js +24 -24
  114. package/lib/WAUSync/Protocols/USyncLIDProtocol.js +9 -9
  115. package/lib/WAUSync/Protocols/USyncPictureProtocol.js +21 -21
  116. package/lib/WAUSync/Protocols/USyncSidelistProtocol.js +19 -19
  117. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +19 -19
  118. package/lib/WAUSync/Protocols/USyncTextStatusProtocol.js +22 -22
  119. package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +16 -16
  120. package/lib/WAUSync/Protocols/index.js +12 -12
  121. package/lib/WAUSync/USyncQuery.js +88 -88
  122. package/lib/WAUSync/USyncUser.js +20 -20
  123. package/lib/WAUSync/index.js +3 -3
  124. package/lib/index.js +66 -66
  125. package/package.json +187 -192
  126. package/WAProto/fetcher.js +0 -68
  127. package/WAProto/parser.js +0 -334
@@ -1,203 +1,203 @@
1
- import { Boom } from '@hapi/boom'
2
- import { randomBytes } from 'crypto'
3
- import { proto } from '../../WAProto/index.js'
4
-
5
- // ─── Node Traversal ───────────────────────────────────────────────────────────
6
-
7
- export const getBinaryNodeChildren = (node, childTag) =>
8
- Array.isArray(node?.content) ? node.content.filter(item => item.tag === childTag) : []
9
-
10
- export const getAllBinaryNodeChildren = ({ content }) =>
11
- Array.isArray(content) ? content : []
12
-
13
- export const getBinaryNodeChild = (node, childTag) =>
14
- Array.isArray(node?.content) ? node.content.find(item => item.tag === childTag) : undefined
15
-
16
- export const getBinaryNodeChildBuffer = (node, childTag) => {
17
- const child = getBinaryNodeChild(node, childTag)?.content
18
- return Buffer.isBuffer(child) || child instanceof Uint8Array ? child : undefined
19
- }
20
-
21
- export const getBinaryNodeChildString = (node, childTag) => {
22
- const child = getBinaryNodeChild(node, childTag)?.content
23
- if (Buffer.isBuffer(child) || child instanceof Uint8Array) return Buffer.from(child).toString('utf-8')
24
- if (typeof child === 'string') return child
25
- }
26
-
27
- export const getBinaryNodeChildUInt = (node, childTag, length) => {
28
- const buff = getBinaryNodeChildBuffer(node, childTag)
29
- return buff ? bufferToUInt(buff, length) : undefined
30
- }
31
-
32
- export const getBinaryFilteredBizBot = nodeContent => {
33
- if (!Array.isArray(nodeContent)) return false
34
- return nodeContent.some(b => b?.tag === 'bot' && b?.attrs?.biz_bot === '1')
35
- }
36
-
37
- function bufferToUInt(e, t) {
38
- let a = 0
39
- for (let i = 0; i < t; i++) a = 256 * a + e[i]
40
- return a
41
- }
42
-
43
- // ─── Node Validation ──────────────────────────────────────────────────────────
44
-
45
- export const assertNodeErrorFree = (node) => {
46
- const errNode = getBinaryNodeChild(node, 'error')
47
- if (errNode) {
48
- const errorCode = +errNode.attrs.code
49
- if (errorCode === 429) {
50
- const error = new Boom('Rate limit', { data: 429 })
51
- error.isRateLimit = true
52
- throw error
53
- }
54
- throw new Boom(errNode.attrs.text || 'Unknown error', { data: errorCode })
55
- }
56
- }
57
-
58
- export const reduceBinaryNodeToDictionary = (node, tag) => {
59
- const nodes = getBinaryNodeChildren(node, tag)
60
- return nodes.reduce((dict, { attrs }) => {
61
- if (typeof attrs.name === 'string') dict[attrs.name] = attrs.value || attrs.config_value
62
- else dict[attrs.config_code] = attrs.value || attrs.config_value
63
- return dict
64
- }, {})
65
- }
66
-
67
- export const getBinaryNodeMessages = ({ content }) => {
68
- const msgs = []
69
- if (Array.isArray(content)) {
70
- for (const item of content) {
71
- if (item.tag === 'message') msgs.push(proto.WebMessageInfo.decode(item.content).toJSON())
72
- }
73
- }
74
- return msgs
75
- }
76
-
77
- // ─── Button Type Detection ────────────────────────────────────────────────────
78
-
79
- export const getBinaryFilteredButtons = (nodeContent) => {
80
- if (!Array.isArray(nodeContent)) return false
81
- return nodeContent.some(a =>
82
- ['native_flow'].includes(a?.content?.[0]?.content?.[0]?.tag) ||
83
- ['interactive', 'buttons', 'list'].includes(a?.content?.[0]?.tag) ||
84
- ['hsm', 'biz'].includes(a?.tag)
85
- )
86
- }
87
-
88
- export const getButtonType = (message) => {
89
- if (message.listMessage) return 'list'
90
- if (message.buttonsMessage) return 'buttons'
91
- const interactiveMsg = message.interactiveMessage || message.interactive
92
- if (!interactiveMsg) return null
93
- const btn = interactiveMsg.nativeFlowMessage?.buttons?.[0]?.name
94
- if (['review_and_pay', 'review_order', 'payment_info', 'payment_status', 'payment_method'].includes(btn)) return btn
95
- // any interactiveMessage subtype (nativeFlow, carousel, shop, collection, bloksWidget, etc.)
96
- return 'interactive'
97
- }
98
-
99
- // ─── Biz Binary Node (canonical single implementation) ────────────────────────
100
-
101
- const FLOWS_MAP = new Set(['mpm', 'catalog_message', 'send_location', 'call_permission_request', 'wa_payment_transaction_details', 'automated_greeting_message_view_catalog', 'card_message', 'order_status', 'track_order', 'reorder', 'cancel_order', 'clear_chat', 'navigateToScreen', 'payment_status', 'payment_method', 'flow', 'flow_action', 'voice_call', 'video_call_button', 'otp_button', 'authentication_button', 'cta_reminder', 'cta_cancel_reminder'])
102
-
103
- const ORDER_RESPONSE_ALIAS = {
104
- review_and_pay: 'order_details', review_order: 'order_status',
105
- payment_info: 'payment_info', payment_status: 'payment_status',
106
- payment_method: 'payment_method', order_details: 'order_details',
107
- order_status: 'order_status', track_order: 'track_order',
108
- reorder: 'reorder', cancel_order: 'cancel_order'
109
- }
110
-
111
- const NATIVE_FLOW_ATTR = { type: 'native_flow', v: '1' }
112
- const MIXED_NATIVE_FLOW = { tag: 'interactive', attrs: NATIVE_FLOW_ATTR, content: [{ tag: 'native_flow', attrs: { v: '9', name: 'mixed' } }] }
113
- const LIST_TYPE_CONTENT = { tag: 'list', attrs: { v: '2', type: 'product_list' } }
114
- const DECISION_SOURCE = [{ tag: 'decision_source', attrs: { value: 'df' } }]
115
-
116
- export const getBizBinaryNode = (message) => {
117
-
118
- const firstButtonName = message.interactiveMessage?.nativeFlowMessage?.buttons?.[0]?.name
119
- const bizAttrs = { actual_actors: '2', host_storage: '2', privacy_mode_ts: `${Date.now() / 1000 | 0}` }
120
- const qualityContent = {
121
- tag: 'quality_control',
122
- attrs: { decision_id: randomBytes(20).toString('hex'), source_type: 'third_party' },
123
- content: DECISION_SOURCE
124
- }
125
-
126
- if (firstButtonName && ORDER_RESPONSE_ALIAS[firstButtonName]) {
127
- bizAttrs.native_flow_name = ORDER_RESPONSE_ALIAS[firstButtonName]
128
- return { tag: 'biz', attrs: bizAttrs, content: [qualityContent] }
129
- }
130
- if (firstButtonName && FLOWS_MAP.has(firstButtonName)) {
131
- return {
132
- tag: 'biz', attrs: bizAttrs,
133
- content: [
134
- { tag: 'interactive', attrs: NATIVE_FLOW_ATTR, content: [{ tag: 'native_flow', attrs: { v: '2', name: firstButtonName } }] },
135
- qualityContent
136
- ]
137
- }
138
- }
139
- // any interactiveMessage subtype (nativeFlowMessage, carouselMessage, shopStorefrontMessage,
140
- // collectionMessage, bloksWidget, or anything WA adds in future) gets MIXED_NATIVE_FLOW
141
- if (message.interactiveMessage || message.buttonsMessage || message.templateMessage) {
142
- return { tag: 'biz', attrs: bizAttrs, content: [MIXED_NATIVE_FLOW, qualityContent] }
143
- }
144
- if (message.listMessage) {
145
- return { tag: 'biz', attrs: bizAttrs, content: [LIST_TYPE_CONTENT, qualityContent] }
146
- }
147
- return { tag: 'biz', attrs: bizAttrs, content: [qualityContent] }
148
- }
149
-
150
- // Legacy alias — kept for back-compat, used by messages-send.js
151
- // Note: messages-send.js already calls normalizeMessageContent before passing in,
152
- // so the viewOnceMessage unwrap here is a safety net only.
153
- export const getButtonArgs = (message) => {
154
- const msgContent = message.viewOnceMessage?.message || message
155
- const interactiveMsg = msgContent.interactiveMessage || msgContent.interactive
156
- const firstButtonName = interactiveMsg?.nativeFlowMessage?.buttons?.[0]?.name
157
- const ts = Math.floor(Date.now() / 1000) - 77980457
158
- const bizAttrs = { actual_actors: '2', host_storage: '2', privacy_mode_ts: `${ts}` }
159
- const engagementNode = { tag: 'engagement', attrs: { customer_service_state: 'open', conversation_state: 'open' } }
160
-
161
- const order_response_name = {
162
- review_and_pay: 'order_details', review_order: 'order_status',
163
- payment_info: 'payment_info', payment_status: 'payment_status',
164
- payment_method: 'payment_method'
165
- }
166
- const flow_name = {
167
- cta_catalog: 'cta_catalog', mpm: 'mpm',
168
- call_permission_request: 'call_permission_request', call_request: 'call_permission_request',
169
- view_catalog: 'automated_greeting_message_view_catalog',
170
- automated_greeting_message_view_catalog: 'automated_greeting_message_view_catalog',
171
- wa_pay_detail: 'wa_payment_transaction_details',
172
- wa_payment_transaction_details: 'wa_payment_transaction_details',
173
- send_location: 'send_location', open_webview: 'open_webview', galaxy_message: 'galaxy_message'
174
- }
175
-
176
- if (firstButtonName && order_response_name[firstButtonName]) {
177
- return [{ tag: 'biz', attrs: { native_flow_name: order_response_name[firstButtonName] }, content: [] }]
178
- }
179
- // any interactiveMessage subtype or buttonsMessage gets an interactive native_flow node
180
- if (interactiveMsg || msgContent.buttonsMessage) {
181
- const name = (firstButtonName && flow_name[firstButtonName]) ? flow_name[firstButtonName] : 'mixed'
182
- return [{ tag: 'biz', attrs: bizAttrs, content: [engagementNode, { tag: 'interactive', attrs: { type: 'native_flow', v: '1' }, content: [{ tag: 'native_flow', attrs: { v: '9', name }, content: [] }] }] }]
183
- }
184
- if (msgContent.listMessage) {
185
- return [{ tag: 'biz', attrs: bizAttrs, content: [engagementNode] }]
186
- }
187
- return [{ tag: 'biz', attrs: bizAttrs, content: [engagementNode] }]
188
- }
189
-
190
- // ─── Debug Utilities ──────────────────────────────────────────────────────────
191
-
192
- const tabs = (n) => '\t'.repeat(n)
193
-
194
- export const binaryNodeToString = (node, i = 0) => {
195
- if (!node) return node
196
- if (typeof node === 'string') return tabs(i) + node
197
- if (node instanceof Uint8Array) return tabs(i) + Buffer.from(node).toString('hex')
198
- if (Array.isArray(node)) return node.map(x => tabs(i + 1) + binaryNodeToString(x, i + 1)).join('\n')
199
- const children = binaryNodeToString(node.content, i + 1)
200
- const tag = `<${node.tag} ${Object.entries(node.attrs || {}).filter(([, v]) => v !== undefined).map(([k, v]) => `${k}='${v}'`).join(' ')}`
201
- const content = children ? `>\n${children}\n${tabs(i)}</${node.tag}>` : '/>'
202
- return tag + content
1
+ import { Boom } from '@hapi/boom'
2
+ import { randomBytes } from 'crypto'
3
+ import { proto } from '../../WAProto/index.js'
4
+
5
+ // ─── Node Traversal ───────────────────────────────────────────────────────────
6
+
7
+ export const getBinaryNodeChildren = (node, childTag) =>
8
+ Array.isArray(node?.content) ? node.content.filter(item => item.tag === childTag) : []
9
+
10
+ export const getAllBinaryNodeChildren = ({ content }) =>
11
+ Array.isArray(content) ? content : []
12
+
13
+ export const getBinaryNodeChild = (node, childTag) =>
14
+ Array.isArray(node?.content) ? node.content.find(item => item.tag === childTag) : undefined
15
+
16
+ export const getBinaryNodeChildBuffer = (node, childTag) => {
17
+ const child = getBinaryNodeChild(node, childTag)?.content
18
+ return Buffer.isBuffer(child) || child instanceof Uint8Array ? child : undefined
19
+ }
20
+
21
+ export const getBinaryNodeChildString = (node, childTag) => {
22
+ const child = getBinaryNodeChild(node, childTag)?.content
23
+ if (Buffer.isBuffer(child) || child instanceof Uint8Array) return Buffer.from(child).toString('utf-8')
24
+ if (typeof child === 'string') return child
25
+ }
26
+
27
+ export const getBinaryNodeChildUInt = (node, childTag, length) => {
28
+ const buff = getBinaryNodeChildBuffer(node, childTag)
29
+ return buff ? bufferToUInt(buff, length) : undefined
30
+ }
31
+
32
+ export const getBinaryFilteredBizBot = nodeContent => {
33
+ if (!Array.isArray(nodeContent)) return false
34
+ return nodeContent.some(b => b?.tag === 'bot' && b?.attrs?.biz_bot === '1')
35
+ }
36
+
37
+ function bufferToUInt(e, t) {
38
+ let a = 0
39
+ for (let i = 0; i < t; i++) a = 256 * a + e[i]
40
+ return a
41
+ }
42
+
43
+ // ─── Node Validation ──────────────────────────────────────────────────────────
44
+
45
+ export const assertNodeErrorFree = (node) => {
46
+ const errNode = getBinaryNodeChild(node, 'error')
47
+ if (errNode) {
48
+ const errorCode = +errNode.attrs.code
49
+ if (errorCode === 429) {
50
+ const error = new Boom('Rate limit', { data: 429 })
51
+ error.isRateLimit = true
52
+ throw error
53
+ }
54
+ throw new Boom(errNode.attrs.text || 'Unknown error', { data: errorCode })
55
+ }
56
+ }
57
+
58
+ export const reduceBinaryNodeToDictionary = (node, tag) => {
59
+ const nodes = getBinaryNodeChildren(node, tag)
60
+ return nodes.reduce((dict, { attrs }) => {
61
+ if (typeof attrs.name === 'string') dict[attrs.name] = attrs.value || attrs.config_value
62
+ else dict[attrs.config_code] = attrs.value || attrs.config_value
63
+ return dict
64
+ }, {})
65
+ }
66
+
67
+ export const getBinaryNodeMessages = ({ content }) => {
68
+ const msgs = []
69
+ if (Array.isArray(content)) {
70
+ for (const item of content) {
71
+ if (item.tag === 'message') msgs.push(proto.WebMessageInfo.decode(item.content).toJSON())
72
+ }
73
+ }
74
+ return msgs
75
+ }
76
+
77
+ // ─── Button Type Detection ────────────────────────────────────────────────────
78
+
79
+ export const getBinaryFilteredButtons = (nodeContent) => {
80
+ if (!Array.isArray(nodeContent)) return false
81
+ return nodeContent.some(a =>
82
+ ['native_flow'].includes(a?.content?.[0]?.content?.[0]?.tag) ||
83
+ ['interactive', 'buttons', 'list'].includes(a?.content?.[0]?.tag) ||
84
+ ['hsm', 'biz'].includes(a?.tag)
85
+ )
86
+ }
87
+
88
+ export const getButtonType = (message) => {
89
+ if (message.listMessage) return 'list'
90
+ if (message.buttonsMessage) return 'buttons'
91
+ const interactiveMsg = message.interactiveMessage || message.interactive
92
+ if (!interactiveMsg) return null
93
+ const btn = interactiveMsg.nativeFlowMessage?.buttons?.[0]?.name
94
+ if (['review_and_pay', 'review_order', 'payment_info', 'payment_status', 'payment_method'].includes(btn)) return btn
95
+ // any interactiveMessage subtype (nativeFlow, carousel, shop, collection, bloksWidget, etc.)
96
+ return 'interactive'
97
+ }
98
+
99
+ // ─── Biz Binary Node (canonical single implementation) ────────────────────────
100
+
101
+ const FLOWS_MAP = new Set(['mpm', 'catalog_message', 'send_location', 'call_permission_request', 'wa_payment_transaction_details', 'automated_greeting_message_view_catalog', 'card_message', 'order_status', 'track_order', 'reorder', 'cancel_order', 'clear_chat', 'navigateToScreen', 'payment_status', 'payment_method', 'flow', 'flow_action', 'voice_call', 'video_call_button', 'otp_button', 'authentication_button', 'cta_reminder', 'cta_cancel_reminder'])
102
+
103
+ const ORDER_RESPONSE_ALIAS = {
104
+ review_and_pay: 'order_details', review_order: 'order_status',
105
+ payment_info: 'payment_info', payment_status: 'payment_status',
106
+ payment_method: 'payment_method', order_details: 'order_details',
107
+ order_status: 'order_status', track_order: 'track_order',
108
+ reorder: 'reorder', cancel_order: 'cancel_order'
109
+ }
110
+
111
+ const NATIVE_FLOW_ATTR = { type: 'native_flow', v: '1' }
112
+ const MIXED_NATIVE_FLOW = { tag: 'interactive', attrs: NATIVE_FLOW_ATTR, content: [{ tag: 'native_flow', attrs: { v: '9', name: 'mixed' } }] }
113
+ const LIST_TYPE_CONTENT = { tag: 'list', attrs: { v: '2', type: 'product_list' } }
114
+ const DECISION_SOURCE = [{ tag: 'decision_source', attrs: { value: 'df' } }]
115
+
116
+ export const getBizBinaryNode = (message) => {
117
+
118
+ const firstButtonName = message.interactiveMessage?.nativeFlowMessage?.buttons?.[0]?.name
119
+ const bizAttrs = { actual_actors: '2', host_storage: '2', privacy_mode_ts: `${Date.now() / 1000 | 0}` }
120
+ const qualityContent = {
121
+ tag: 'quality_control',
122
+ attrs: { decision_id: randomBytes(20).toString('hex'), source_type: 'third_party' },
123
+ content: DECISION_SOURCE
124
+ }
125
+
126
+ if (firstButtonName && ORDER_RESPONSE_ALIAS[firstButtonName]) {
127
+ bizAttrs.native_flow_name = ORDER_RESPONSE_ALIAS[firstButtonName]
128
+ return { tag: 'biz', attrs: bizAttrs, content: [qualityContent] }
129
+ }
130
+ if (firstButtonName && FLOWS_MAP.has(firstButtonName)) {
131
+ return {
132
+ tag: 'biz', attrs: bizAttrs,
133
+ content: [
134
+ { tag: 'interactive', attrs: NATIVE_FLOW_ATTR, content: [{ tag: 'native_flow', attrs: { v: '2', name: firstButtonName } }] },
135
+ qualityContent
136
+ ]
137
+ }
138
+ }
139
+ // any interactiveMessage subtype (nativeFlowMessage, carouselMessage, shopStorefrontMessage,
140
+ // collectionMessage, bloksWidget, or anything WA adds in future) gets MIXED_NATIVE_FLOW
141
+ if (message.interactiveMessage || message.buttonsMessage || message.templateMessage) {
142
+ return { tag: 'biz', attrs: bizAttrs, content: [MIXED_NATIVE_FLOW, qualityContent] }
143
+ }
144
+ if (message.listMessage) {
145
+ return { tag: 'biz', attrs: bizAttrs, content: [LIST_TYPE_CONTENT, qualityContent] }
146
+ }
147
+ return { tag: 'biz', attrs: bizAttrs, content: [qualityContent] }
148
+ }
149
+
150
+ // Legacy alias — kept for back-compat, used by messages-send.js
151
+ // Note: messages-send.js already calls normalizeMessageContent before passing in,
152
+ // so the viewOnceMessage unwrap here is a safety net only.
153
+ export const getButtonArgs = (message) => {
154
+ const msgContent = message.viewOnceMessage?.message || message
155
+ const interactiveMsg = msgContent.interactiveMessage || msgContent.interactive
156
+ const firstButtonName = interactiveMsg?.nativeFlowMessage?.buttons?.[0]?.name
157
+ const ts = Math.floor(Date.now() / 1000) - 77980457
158
+ const bizAttrs = { actual_actors: '2', host_storage: '2', privacy_mode_ts: `${ts}` }
159
+ const engagementNode = { tag: 'engagement', attrs: { customer_service_state: 'open', conversation_state: 'open' } }
160
+
161
+ const order_response_name = {
162
+ review_and_pay: 'order_details', review_order: 'order_status',
163
+ payment_info: 'payment_info', payment_status: 'payment_status',
164
+ payment_method: 'payment_method'
165
+ }
166
+ const flow_name = {
167
+ cta_catalog: 'cta_catalog', mpm: 'mpm',
168
+ call_permission_request: 'call_permission_request', call_request: 'call_permission_request',
169
+ view_catalog: 'automated_greeting_message_view_catalog',
170
+ automated_greeting_message_view_catalog: 'automated_greeting_message_view_catalog',
171
+ wa_pay_detail: 'wa_payment_transaction_details',
172
+ wa_payment_transaction_details: 'wa_payment_transaction_details',
173
+ send_location: 'send_location', open_webview: 'open_webview', galaxy_message: 'galaxy_message'
174
+ }
175
+
176
+ if (firstButtonName && order_response_name[firstButtonName]) {
177
+ return [{ tag: 'biz', attrs: { native_flow_name: order_response_name[firstButtonName] }, content: [] }]
178
+ }
179
+ // any interactiveMessage subtype or buttonsMessage gets an interactive native_flow node
180
+ if (interactiveMsg || msgContent.buttonsMessage) {
181
+ const name = (firstButtonName && flow_name[firstButtonName]) ? flow_name[firstButtonName] : 'mixed'
182
+ return [{ tag: 'biz', attrs: bizAttrs, content: [engagementNode, { tag: 'interactive', attrs: { type: 'native_flow', v: '1' }, content: [{ tag: 'native_flow', attrs: { v: '9', name }, content: [] }] }] }]
183
+ }
184
+ if (msgContent.listMessage) {
185
+ return [{ tag: 'biz', attrs: bizAttrs, content: [engagementNode] }]
186
+ }
187
+ return [{ tag: 'biz', attrs: bizAttrs, content: [engagementNode] }]
188
+ }
189
+
190
+ // ─── Debug Utilities ──────────────────────────────────────────────────────────
191
+
192
+ const tabs = (n) => '\t'.repeat(n)
193
+
194
+ export const binaryNodeToString = (node, i = 0) => {
195
+ if (!node) return node
196
+ if (typeof node === 'string') return tabs(i) + node
197
+ if (node instanceof Uint8Array) return tabs(i) + Buffer.from(node).toString('hex')
198
+ if (Array.isArray(node)) return node.map(x => tabs(i + 1) + binaryNodeToString(x, i + 1)).join('\n')
199
+ const children = binaryNodeToString(node.content, i + 1)
200
+ const tag = `<${node.tag} ${Object.entries(node.attrs || {}).filter(([, v]) => v !== undefined).map(([k, v]) => `${k}='${v}'`).join(' ')}`
201
+ const content = children ? `>\n${children}\n${tabs(i)}</${node.tag}>` : '/>'
202
+ return tag + content
203
203
  }
@@ -1,6 +1,6 @@
1
- export * from './constants.js'
2
- export * from './types.js'
3
- export * from './jid-utils.js'
4
- export * from './encode.js'
5
- export * from './decode.js'
6
- export * from './generic-utils.js'
1
+ export * from './constants.js'
2
+ export * from './types.js'
3
+ export * from './jid-utils.js'
4
+ export * from './encode.js'
5
+ export * from './decode.js'
6
+ export * from './generic-utils.js'