@zervonexx/baileys 5.0.0 → 5.0.1

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 (125) hide show
  1. package/LICENSE +22 -22
  2. package/README.md +4112 -4112
  3. package/WAProto/WAProtoCompile.js +322 -322
  4. package/WAProto/fetcher.js +67 -67
  5. package/WAProto/index.js +95 -95
  6. package/WAProto/parser.js +333 -333
  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 +248 -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 +14 -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 +194 -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 +2 -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 +942 -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 +191 -192
@@ -1,84 +1,84 @@
1
- import WebSocket from 'ws'
2
- import { DEFAULT_ORIGIN } from '../../Defaults/index.js'
3
- import { AbstractSocketClient } from './types.js'
4
-
5
- const MAX_CONNECT_RETRIES = 5
6
- const BASE_RETRY_DELAY = 1000
7
-
8
- export class WebSocketClient extends AbstractSocketClient {
9
- constructor() {
10
- super(...arguments)
11
- this.socket = null
12
- this._connectRetries = 0
13
- this._connectTimer = null
14
- this._dead = false
15
- }
16
-
17
- get isOpen() { return this.socket?.readyState === WebSocket.OPEN }
18
- get isClosed() { return this.socket === null || this.socket?.readyState === WebSocket.CLOSED }
19
- get isClosing() { return this.socket === null || this.socket?.readyState === WebSocket.CLOSING }
20
- get isConnecting() { return this.socket?.readyState === WebSocket.CONNECTING }
21
-
22
- connect() {
23
- if (this.socket || this._dead) return
24
- this._tryConnect()
25
- }
26
-
27
- _tryConnect() {
28
- if (this._dead) return
29
- let didOpen = false
30
- this.socket = new WebSocket(this.url, {
31
- origin: DEFAULT_ORIGIN,
32
- headers: this.config.options?.headers,
33
- handshakeTimeout: this.config.connectTimeoutMs,
34
- timeout: this.config.connectTimeoutMs,
35
- agent: this.config.agent,
36
- family: 4,
37
- })
38
- this.socket.setMaxListeners(0)
39
-
40
- for (const event of ['close', 'upgrade', 'message', 'open', 'ping', 'pong', 'unexpected-response']) {
41
- this.socket.on(event, (...args) => this.emit(event, ...args))
42
- }
43
-
44
- this.socket.once('open', () => {
45
- didOpen = true
46
- this._connectRetries = 0
47
- })
48
-
49
- this.socket.on('error', (err) => {
50
- const isConnectError = !didOpen && ['ETIMEDOUT', 'ECONNREFUSED', 'ENETUNREACH', 'ENOTFOUND', 'EAI_AGAIN'].includes(err.code)
51
- if (isConnectError && this._connectRetries < MAX_CONNECT_RETRIES) {
52
- this._connectRetries++
53
- const delay = Math.min(BASE_RETRY_DELAY * Math.pow(2, this._connectRetries - 1), 30000)
54
- this.socket = null
55
- this._connectTimer = setTimeout(() => this._tryConnect(), delay)
56
- } else {
57
- this.emit('error', err)
58
- }
59
- })
60
- }
61
-
62
- async close() {
63
- this._dead = true
64
- clearTimeout(this._connectTimer)
65
- if (!this.socket) return
66
- const closePromise = new Promise(resolve => { this.socket?.once('close', resolve) })
67
- this.socket.close()
68
- await closePromise
69
- this.socket = null
70
- }
71
-
72
- reset() {
73
- this._dead = false
74
- this._connectRetries = 0
75
- clearTimeout(this._connectTimer)
76
- }
77
-
78
- send(str, cb) {
79
- this.socket?.send(str, cb)
80
- return Boolean(this.socket)
81
- }
82
- }
83
-
1
+ import WebSocket from 'ws'
2
+ import { DEFAULT_ORIGIN } from '../../Defaults/index.js'
3
+ import { AbstractSocketClient } from './types.js'
4
+
5
+ const MAX_CONNECT_RETRIES = 5
6
+ const BASE_RETRY_DELAY = 1000
7
+
8
+ export class WebSocketClient extends AbstractSocketClient {
9
+ constructor() {
10
+ super(...arguments)
11
+ this.socket = null
12
+ this._connectRetries = 0
13
+ this._connectTimer = null
14
+ this._dead = false
15
+ }
16
+
17
+ get isOpen() { return this.socket?.readyState === WebSocket.OPEN }
18
+ get isClosed() { return this.socket === null || this.socket?.readyState === WebSocket.CLOSED }
19
+ get isClosing() { return this.socket === null || this.socket?.readyState === WebSocket.CLOSING }
20
+ get isConnecting() { return this.socket?.readyState === WebSocket.CONNECTING }
21
+
22
+ connect() {
23
+ if (this.socket || this._dead) return
24
+ this._tryConnect()
25
+ }
26
+
27
+ _tryConnect() {
28
+ if (this._dead) return
29
+ let didOpen = false
30
+ this.socket = new WebSocket(this.url, {
31
+ origin: DEFAULT_ORIGIN,
32
+ headers: this.config.options?.headers,
33
+ handshakeTimeout: this.config.connectTimeoutMs,
34
+ timeout: this.config.connectTimeoutMs,
35
+ agent: this.config.agent,
36
+ family: 4,
37
+ })
38
+ this.socket.setMaxListeners(0)
39
+
40
+ for (const event of ['close', 'upgrade', 'message', 'open', 'ping', 'pong', 'unexpected-response']) {
41
+ this.socket.on(event, (...args) => this.emit(event, ...args))
42
+ }
43
+
44
+ this.socket.once('open', () => {
45
+ didOpen = true
46
+ this._connectRetries = 0
47
+ })
48
+
49
+ this.socket.on('error', (err) => {
50
+ const isConnectError = !didOpen && ['ETIMEDOUT', 'ECONNREFUSED', 'ENETUNREACH', 'ENOTFOUND', 'EAI_AGAIN'].includes(err.code)
51
+ if (isConnectError && this._connectRetries < MAX_CONNECT_RETRIES) {
52
+ this._connectRetries++
53
+ const delay = Math.min(BASE_RETRY_DELAY * Math.pow(2, this._connectRetries - 1), 30000)
54
+ this.socket = null
55
+ this._connectTimer = setTimeout(() => this._tryConnect(), delay)
56
+ } else {
57
+ this.emit('error', err)
58
+ }
59
+ })
60
+ }
61
+
62
+ async close() {
63
+ this._dead = true
64
+ clearTimeout(this._connectTimer)
65
+ if (!this.socket) return
66
+ const closePromise = new Promise(resolve => { this.socket?.once('close', resolve) })
67
+ this.socket.close()
68
+ await closePromise
69
+ this.socket = null
70
+ }
71
+
72
+ reset() {
73
+ this._dead = false
74
+ this._connectRetries = 0
75
+ clearTimeout(this._connectTimer)
76
+ }
77
+
78
+ send(str, cb) {
79
+ this.socket?.send(str, cb)
80
+ return Boolean(this.socket)
81
+ }
82
+ }
83
+
84
84
  export default WebSocketClient
@@ -1,152 +1,152 @@
1
- import { parseCatalogNode, getRawMediaUploadData, parseCollectionsNode, parseOrderDetailsNode, parseProductNode, toProductNode, uploadingNecessaryImagesOfProduct } from '../Utils/index.js'
2
- import { jidNormalizedUser, S_WHATSAPP_NET, getBinaryNodeChild } from '../WABinary/index.js'
3
- import { makeMessagesRecvSocket } from './messages-recv.js'
4
-
5
- export const makeBusinessSocket = (config) => {
6
- const sock = makeMessagesRecvSocket(config)
7
- const { authState, query, waUploadToServer } = sock
8
-
9
- const updateBussinesProfile = async (args) => {
10
- const node = []
11
- for (const key of ['address', 'email', 'description']) {
12
- if (args[key]) node.push({ tag: key, attrs: {}, content: args[key] })
13
- }
14
- if (args.websites) {
15
- node.push(...args.websites.map(website => ({ tag: 'website', attrs: {}, content: website })))
16
- }
17
- if (args.hours) {
18
- node.push({
19
- tag: 'business_hours',
20
- attrs: { timezone: args.hours.timezone },
21
- content: args.hours.days.map(cfg => {
22
- const base = { tag: 'business_hours_config', attrs: { day_of_week: cfg.day, mode: cfg.mode } }
23
- if (cfg.mode === 'specific_hours') {
24
- return { ...base, attrs: { ...base.attrs, open_time: cfg.openTimeInMinutes, close_time: cfg.closeTimeInMinutes } }
25
- }
26
- return base
27
- })
28
- })
29
- }
30
- return query({
31
- tag: 'iq',
32
- attrs: { to: S_WHATSAPP_NET, type: 'set', xmlns: 'w:biz' },
33
- content: [{ tag: 'business_profile', attrs: { v: '3', mutation_type: 'delta' }, content: node }]
34
- })
35
- }
36
-
37
- const updateCoverPhoto = async (photo) => {
38
- const { fileSha256, filePath } = await getRawMediaUploadData(photo, 'biz-cover-photo')
39
- const { meta_hmac, fbid, ts } = await waUploadToServer(filePath, {
40
- fileEncSha256B64: fileSha256.toString('base64'),
41
- mediaType: 'biz-cover-photo'
42
- })
43
- await query({
44
- tag: 'iq',
45
- attrs: { to: S_WHATSAPP_NET, type: 'set', xmlns: 'w:biz' },
46
- content: [{ tag: 'business_profile', attrs: { v: '3', mutation_type: 'delta' }, content: [{ tag: 'cover_photo', attrs: { id: String(fbid), op: 'update', token: meta_hmac, ts: String(ts) } }] }]
47
- })
48
- return fbid
49
- }
50
-
51
- const removeCoverPhoto = async (id) =>
52
- query({
53
- tag: 'iq',
54
- attrs: { to: S_WHATSAPP_NET, type: 'set', xmlns: 'w:biz' },
55
- content: [{ tag: 'business_profile', attrs: { v: '3', mutation_type: 'delta' }, content: [{ tag: 'cover_photo', attrs: { op: 'delete', id } }] }]
56
- })
57
-
58
- const getCatalog = async ({ jid, limit, cursor } = {}) => {
59
- jid = jidNormalizedUser(jid || authState.creds.me?.id)
60
- const queryParamNodes = [
61
- { tag: 'limit', attrs: {}, content: Buffer.from((limit || 10).toString()) },
62
- { tag: 'width', attrs: {}, content: Buffer.from('100') },
63
- { tag: 'height', attrs: {}, content: Buffer.from('100') }
64
- ]
65
- if (cursor) queryParamNodes.push({ tag: 'after', attrs: {}, content: cursor })
66
- const result = await query({
67
- tag: 'iq',
68
- attrs: { to: S_WHATSAPP_NET, type: 'get', xmlns: 'w:biz:catalog' },
69
- content: [{ tag: 'product_catalog', attrs: { jid, allow_shop_source: 'true' }, content: queryParamNodes }]
70
- })
71
- return parseCatalogNode(result)
72
- }
73
-
74
- const getCollections = async (jid, limit = 51) => {
75
- jid = jidNormalizedUser(jid || authState.creds.me?.id)
76
- const result = await query({
77
- tag: 'iq',
78
- attrs: { to: S_WHATSAPP_NET, type: 'get', xmlns: 'w:biz:catalog', smax_id: '35' },
79
- content: [{
80
- tag: 'collections',
81
- attrs: { biz_jid: jid },
82
- content: [
83
- { tag: 'collection_limit', attrs: {}, content: Buffer.from(limit.toString()) },
84
- { tag: 'item_limit', attrs: {}, content: Buffer.from(limit.toString()) },
85
- { tag: 'width', attrs: {}, content: Buffer.from('100') },
86
- { tag: 'height', attrs: {}, content: Buffer.from('100') }
87
- ]
88
- }]
89
- })
90
- return parseCollectionsNode(result)
91
- }
92
-
93
- const getOrderDetails = async (orderId, tokenBase64) => {
94
- const result = await query({
95
- tag: 'iq',
96
- attrs: { to: S_WHATSAPP_NET, type: 'get', xmlns: 'fb:thrift_iq', smax_id: '5' },
97
- content: [{
98
- tag: 'order',
99
- attrs: { op: 'get', id: orderId },
100
- content: [
101
- { tag: 'image_dimensions', attrs: {}, content: [{ tag: 'width', attrs: {}, content: Buffer.from('100') }, { tag: 'height', attrs: {}, content: Buffer.from('100') }] },
102
- { tag: 'token', attrs: {}, content: Buffer.from(tokenBase64) }
103
- ]
104
- }]
105
- })
106
- return parseOrderDetailsNode(result)
107
- }
108
-
109
- const productUpdate = async (productId, update) => {
110
- update = await uploadingNecessaryImagesOfProduct(update, waUploadToServer)
111
- const result = await query({
112
- tag: 'iq',
113
- attrs: { to: S_WHATSAPP_NET, type: 'set', xmlns: 'w:biz:catalog' },
114
- content: [{ tag: 'product_catalog_edit', attrs: { v: '1' }, content: [toProductNode(productId, update), { tag: 'width', attrs: {}, content: '100' }, { tag: 'height', attrs: {}, content: '100' }] }]
115
- })
116
- return parseProductNode(getBinaryNodeChild(getBinaryNodeChild(result, 'product_catalog_edit'), 'product'))
117
- }
118
-
119
- const productCreate = async (create) => {
120
- create.isHidden = !!create.isHidden
121
- create = await uploadingNecessaryImagesOfProduct(create, waUploadToServer)
122
- const result = await query({
123
- tag: 'iq',
124
- attrs: { to: S_WHATSAPP_NET, type: 'set', xmlns: 'w:biz:catalog' },
125
- content: [{ tag: 'product_catalog_add', attrs: { v: '1' }, content: [toProductNode(undefined, create), { tag: 'width', attrs: {}, content: '100' }, { tag: 'height', attrs: {}, content: '100' }] }]
126
- })
127
- return parseProductNode(getBinaryNodeChild(getBinaryNodeChild(result, 'product_catalog_add'), 'product'))
128
- }
129
-
130
- const productDelete = async (productIds) => {
131
- const result = await query({
132
- tag: 'iq',
133
- attrs: { to: S_WHATSAPP_NET, type: 'set', xmlns: 'w:biz:catalog' },
134
- content: [{ tag: 'product_catalog_delete', attrs: { v: '1' }, content: productIds.map(id => ({ tag: 'product', attrs: {}, content: [{ tag: 'id', attrs: {}, content: Buffer.from(id) }] })) }]
135
- })
136
- return { deleted: +(getBinaryNodeChild(result, 'product_catalog_delete')?.attrs.deleted_count || 0) }
137
- }
138
-
139
- return {
140
- ...sock,
141
- logger: config.logger,
142
- getOrderDetails,
143
- getCatalog,
144
- getCollections,
145
- productCreate,
146
- productDelete,
147
- productUpdate,
148
- updateBussinesProfile,
149
- updateCoverPhoto,
150
- removeCoverPhoto
151
- }
1
+ import { parseCatalogNode, getRawMediaUploadData, parseCollectionsNode, parseOrderDetailsNode, parseProductNode, toProductNode, uploadingNecessaryImagesOfProduct } from '../Utils/index.js'
2
+ import { jidNormalizedUser, S_WHATSAPP_NET, getBinaryNodeChild } from '../WABinary/index.js'
3
+ import { makeMessagesRecvSocket } from './messages-recv.js'
4
+
5
+ export const makeBusinessSocket = (config) => {
6
+ const sock = makeMessagesRecvSocket(config)
7
+ const { authState, query, waUploadToServer } = sock
8
+
9
+ const updateBussinesProfile = async (args) => {
10
+ const node = []
11
+ for (const key of ['address', 'email', 'description']) {
12
+ if (args[key]) node.push({ tag: key, attrs: {}, content: args[key] })
13
+ }
14
+ if (args.websites) {
15
+ node.push(...args.websites.map(website => ({ tag: 'website', attrs: {}, content: website })))
16
+ }
17
+ if (args.hours) {
18
+ node.push({
19
+ tag: 'business_hours',
20
+ attrs: { timezone: args.hours.timezone },
21
+ content: args.hours.days.map(cfg => {
22
+ const base = { tag: 'business_hours_config', attrs: { day_of_week: cfg.day, mode: cfg.mode } }
23
+ if (cfg.mode === 'specific_hours') {
24
+ return { ...base, attrs: { ...base.attrs, open_time: cfg.openTimeInMinutes, close_time: cfg.closeTimeInMinutes } }
25
+ }
26
+ return base
27
+ })
28
+ })
29
+ }
30
+ return query({
31
+ tag: 'iq',
32
+ attrs: { to: S_WHATSAPP_NET, type: 'set', xmlns: 'w:biz' },
33
+ content: [{ tag: 'business_profile', attrs: { v: '3', mutation_type: 'delta' }, content: node }]
34
+ })
35
+ }
36
+
37
+ const updateCoverPhoto = async (photo) => {
38
+ const { fileSha256, filePath } = await getRawMediaUploadData(photo, 'biz-cover-photo')
39
+ const { meta_hmac, fbid, ts } = await waUploadToServer(filePath, {
40
+ fileEncSha256B64: fileSha256.toString('base64'),
41
+ mediaType: 'biz-cover-photo'
42
+ })
43
+ await query({
44
+ tag: 'iq',
45
+ attrs: { to: S_WHATSAPP_NET, type: 'set', xmlns: 'w:biz' },
46
+ content: [{ tag: 'business_profile', attrs: { v: '3', mutation_type: 'delta' }, content: [{ tag: 'cover_photo', attrs: { id: String(fbid), op: 'update', token: meta_hmac, ts: String(ts) } }] }]
47
+ })
48
+ return fbid
49
+ }
50
+
51
+ const removeCoverPhoto = async (id) =>
52
+ query({
53
+ tag: 'iq',
54
+ attrs: { to: S_WHATSAPP_NET, type: 'set', xmlns: 'w:biz' },
55
+ content: [{ tag: 'business_profile', attrs: { v: '3', mutation_type: 'delta' }, content: [{ tag: 'cover_photo', attrs: { op: 'delete', id } }] }]
56
+ })
57
+
58
+ const getCatalog = async ({ jid, limit, cursor } = {}) => {
59
+ jid = jidNormalizedUser(jid || authState.creds.me?.id)
60
+ const queryParamNodes = [
61
+ { tag: 'limit', attrs: {}, content: Buffer.from((limit || 10).toString()) },
62
+ { tag: 'width', attrs: {}, content: Buffer.from('100') },
63
+ { tag: 'height', attrs: {}, content: Buffer.from('100') }
64
+ ]
65
+ if (cursor) queryParamNodes.push({ tag: 'after', attrs: {}, content: cursor })
66
+ const result = await query({
67
+ tag: 'iq',
68
+ attrs: { to: S_WHATSAPP_NET, type: 'get', xmlns: 'w:biz:catalog' },
69
+ content: [{ tag: 'product_catalog', attrs: { jid, allow_shop_source: 'true' }, content: queryParamNodes }]
70
+ })
71
+ return parseCatalogNode(result)
72
+ }
73
+
74
+ const getCollections = async (jid, limit = 51) => {
75
+ jid = jidNormalizedUser(jid || authState.creds.me?.id)
76
+ const result = await query({
77
+ tag: 'iq',
78
+ attrs: { to: S_WHATSAPP_NET, type: 'get', xmlns: 'w:biz:catalog', smax_id: '35' },
79
+ content: [{
80
+ tag: 'collections',
81
+ attrs: { biz_jid: jid },
82
+ content: [
83
+ { tag: 'collection_limit', attrs: {}, content: Buffer.from(limit.toString()) },
84
+ { tag: 'item_limit', attrs: {}, content: Buffer.from(limit.toString()) },
85
+ { tag: 'width', attrs: {}, content: Buffer.from('100') },
86
+ { tag: 'height', attrs: {}, content: Buffer.from('100') }
87
+ ]
88
+ }]
89
+ })
90
+ return parseCollectionsNode(result)
91
+ }
92
+
93
+ const getOrderDetails = async (orderId, tokenBase64) => {
94
+ const result = await query({
95
+ tag: 'iq',
96
+ attrs: { to: S_WHATSAPP_NET, type: 'get', xmlns: 'fb:thrift_iq', smax_id: '5' },
97
+ content: [{
98
+ tag: 'order',
99
+ attrs: { op: 'get', id: orderId },
100
+ content: [
101
+ { tag: 'image_dimensions', attrs: {}, content: [{ tag: 'width', attrs: {}, content: Buffer.from('100') }, { tag: 'height', attrs: {}, content: Buffer.from('100') }] },
102
+ { tag: 'token', attrs: {}, content: Buffer.from(tokenBase64) }
103
+ ]
104
+ }]
105
+ })
106
+ return parseOrderDetailsNode(result)
107
+ }
108
+
109
+ const productUpdate = async (productId, update) => {
110
+ update = await uploadingNecessaryImagesOfProduct(update, waUploadToServer)
111
+ const result = await query({
112
+ tag: 'iq',
113
+ attrs: { to: S_WHATSAPP_NET, type: 'set', xmlns: 'w:biz:catalog' },
114
+ content: [{ tag: 'product_catalog_edit', attrs: { v: '1' }, content: [toProductNode(productId, update), { tag: 'width', attrs: {}, content: '100' }, { tag: 'height', attrs: {}, content: '100' }] }]
115
+ })
116
+ return parseProductNode(getBinaryNodeChild(getBinaryNodeChild(result, 'product_catalog_edit'), 'product'))
117
+ }
118
+
119
+ const productCreate = async (create) => {
120
+ create.isHidden = !!create.isHidden
121
+ create = await uploadingNecessaryImagesOfProduct(create, waUploadToServer)
122
+ const result = await query({
123
+ tag: 'iq',
124
+ attrs: { to: S_WHATSAPP_NET, type: 'set', xmlns: 'w:biz:catalog' },
125
+ content: [{ tag: 'product_catalog_add', attrs: { v: '1' }, content: [toProductNode(undefined, create), { tag: 'width', attrs: {}, content: '100' }, { tag: 'height', attrs: {}, content: '100' }] }]
126
+ })
127
+ return parseProductNode(getBinaryNodeChild(getBinaryNodeChild(result, 'product_catalog_add'), 'product'))
128
+ }
129
+
130
+ const productDelete = async (productIds) => {
131
+ const result = await query({
132
+ tag: 'iq',
133
+ attrs: { to: S_WHATSAPP_NET, type: 'set', xmlns: 'w:biz:catalog' },
134
+ content: [{ tag: 'product_catalog_delete', attrs: { v: '1' }, content: productIds.map(id => ({ tag: 'product', attrs: {}, content: [{ tag: 'id', attrs: {}, content: Buffer.from(id) }] })) }]
135
+ })
136
+ return { deleted: +(getBinaryNodeChild(result, 'product_catalog_delete')?.attrs.deleted_count || 0) }
137
+ }
138
+
139
+ return {
140
+ ...sock,
141
+ logger: config.logger,
142
+ getOrderDetails,
143
+ getCatalog,
144
+ getCollections,
145
+ productCreate,
146
+ productDelete,
147
+ productUpdate,
148
+ updateBussinesProfile,
149
+ updateCoverPhoto,
150
+ removeCoverPhoto
151
+ }
152
152
  }