@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.
- package/LICENSE +22 -22
- package/README.md +4124 -4112
- package/WAProto/WAProto.json +1 -0
- package/WAProto/WAProto.proto +7160 -0
- package/WAProto/WAProtoCompile.js +264 -322
- package/WAProto/index.js +55 -95
- package/lib/Builders/AIRich.js +673 -673
- package/lib/Builders/auto.js +49 -49
- package/lib/Builders/base.js +144 -144
- package/lib/Builders/builders.js +588 -588
- package/lib/Builders/index.js +3 -3
- package/lib/Defaults/index.js +249 -248
- package/lib/Signal/index.js +2 -2
- package/lib/Signal/libsignal.js +526 -526
- package/lib/Signal/lid-mapping.js +282 -282
- package/lib/Socket/Client/index.js +1 -1
- package/lib/Socket/Client/types.js +9 -9
- package/lib/Socket/Client/websocket.js +83 -83
- package/lib/Socket/business.js +151 -151
- package/lib/Socket/chats.js +854 -854
- package/lib/Socket/communities.js +146 -146
- package/lib/Socket/groups.js +293 -293
- package/lib/Socket/index.js +17 -14
- package/lib/Socket/messages-recv.js +993 -993
- package/lib/Socket/messages-send.js +722 -722
- package/lib/Socket/mex.js +33 -33
- package/lib/Socket/newsletter.js +783 -783
- package/lib/Socket/registration.js +154 -154
- package/lib/Socket/socket.js +797 -797
- package/lib/Socket/spam-report.js +143 -143
- package/lib/Socket/username.js +81 -81
- package/lib/Socket/zervonex-handler.js +57 -57
- package/lib/Store/index.js +6 -6
- package/lib/Store/make-cache-manager-store.js +80 -80
- package/lib/Store/make-in-memory-store.js +426 -426
- package/lib/Store/make-ordered-dictionary.js +81 -81
- package/lib/Store/object-repository.js +30 -30
- package/lib/Store/reporting-info-store.js +37 -37
- package/lib/Types/Auth.js +1 -1
- package/lib/Types/Bussines.js +1 -1
- package/lib/Types/Call.js +1 -1
- package/lib/Types/Chat.js +7 -7
- package/lib/Types/Contact.js +1 -1
- package/lib/Types/Events.js +1 -1
- package/lib/Types/GroupMetadata.js +1 -1
- package/lib/Types/Label.js +24 -24
- package/lib/Types/LabelAssociation.js +6 -6
- package/lib/Types/Message.js +14 -14
- package/lib/Types/Newsletter.js +99 -99
- package/lib/Types/Product.js +1 -1
- package/lib/Types/Signal.js +1 -1
- package/lib/Types/Socket.js +14 -14
- package/lib/Types/State.js +26 -26
- package/lib/Types/USync.js +1 -1
- package/lib/Types/index.js +30 -30
- package/lib/Utils/auth-utils.js +159 -159
- package/lib/Utils/baileys-event-stream.js +32 -32
- package/lib/Utils/bot-capabilities.js +40 -40
- package/lib/Utils/bot-planning-replay.js +43 -43
- package/lib/Utils/browser-utils.js +29 -29
- package/lib/Utils/business.js +74 -74
- package/lib/Utils/chat-utils.js +316 -316
- package/lib/Utils/companion-reg-client-utils.js +19 -19
- package/lib/Utils/consumer-application.js +37 -37
- package/lib/Utils/crypto.js +52 -52
- package/lib/Utils/cstoken.js +14 -14
- package/lib/Utils/decode-wa-message.js +199 -199
- package/lib/Utils/event-buffer.js +272 -272
- package/lib/Utils/extras.js +186 -186
- package/lib/Utils/generics.js +195 -194
- package/lib/Utils/history.js +50 -50
- package/lib/Utils/identity-change-handler.js +18 -18
- package/lib/Utils/index.js +40 -40
- package/lib/Utils/link-preview.js +50 -50
- package/lib/Utils/logger.js +3 -2
- package/lib/Utils/lt-hash.js +3 -3
- package/lib/Utils/make-mutex.js +15 -15
- package/lib/Utils/message-composer.js +61 -61
- package/lib/Utils/message-retry-manager.js +47 -47
- package/lib/Utils/messages-media.js +501 -501
- package/lib/Utils/messages.js +944 -942
- package/lib/Utils/meta-ai-msmsg.js +121 -121
- package/lib/Utils/meta-compositing.js +74 -74
- package/lib/Utils/modern-messages.js +234 -234
- package/lib/Utils/native-flow.js +80 -80
- package/lib/Utils/noise-handler.js +160 -160
- package/lib/Utils/pre-key-manager.js +75 -75
- package/lib/Utils/process-message.js +400 -400
- package/lib/Utils/reporting-utils.js +170 -170
- package/lib/Utils/signal.js +166 -166
- package/lib/Utils/spam-report-utils.js +165 -165
- package/lib/Utils/sync-action-utils.js +33 -33
- package/lib/Utils/tc-token-utils.js +153 -153
- package/lib/Utils/use-keyv-auth-state.js +199 -199
- package/lib/Utils/use-multi-file-auth-state.js +83 -83
- package/lib/Utils/validate-connection.js +171 -171
- package/lib/WABinary/constants.js +1320 -1320
- package/lib/WABinary/decode.js +151 -151
- package/lib/WABinary/encode.js +139 -139
- package/lib/WABinary/generic-utils.js +202 -202
- package/lib/WABinary/index.js +6 -6
- package/lib/WABinary/jid-utils.js +235 -235
- package/lib/WABinary/types.js +4 -4
- package/lib/WAM/BinaryInfo.js +9 -9
- package/lib/WAM/constants.js +22852 -22852
- package/lib/WAM/encode.js +149 -149
- package/lib/WAM/index.js +3 -3
- package/lib/WAUSync/Protocols/USyncBotProfileProtocol.js +30 -30
- package/lib/WAUSync/Protocols/USyncBusinessProtocol.js +48 -48
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +23 -23
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +28 -28
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +15 -15
- package/lib/WAUSync/Protocols/USyncFeatureProtocol.js +24 -24
- package/lib/WAUSync/Protocols/USyncLIDProtocol.js +9 -9
- package/lib/WAUSync/Protocols/USyncPictureProtocol.js +21 -21
- package/lib/WAUSync/Protocols/USyncSidelistProtocol.js +19 -19
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +19 -19
- package/lib/WAUSync/Protocols/USyncTextStatusProtocol.js +22 -22
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +16 -16
- package/lib/WAUSync/Protocols/index.js +12 -12
- package/lib/WAUSync/USyncQuery.js +88 -88
- package/lib/WAUSync/USyncUser.js +20 -20
- package/lib/WAUSync/index.js +3 -3
- package/lib/index.js +66 -66
- package/package.json +187 -192
- package/WAProto/fetcher.js +0 -68
- package/WAProto/parser.js +0 -334
|
@@ -1,44 +1,44 @@
|
|
|
1
|
-
import { buildPlainPlaceholder, PlanningStepStatus } from './meta-compositing.js'
|
|
2
|
-
import { delay } from './generics.js'
|
|
3
|
-
|
|
4
|
-
// Live planning animation: shows pending steps, edits each to DONE one-by-one,
|
|
5
|
-
// deletes placeholder, then sends the final message. Works on all WA clients.
|
|
6
|
-
export const replayPlanning = async (sock, jid, steps, finalContent, { description = 'Thinking…', placeholderText = '', stepDelayMs = 900, finalPauseMs = 600, abortOnDisconnect = true, sendOptions = {} } = {}) => {
|
|
7
|
-
if (!steps?.length) throw new Error('replayPlanning: steps must have at least one entry')
|
|
8
|
-
|
|
9
|
-
let aborted = false
|
|
10
|
-
if (abortOnDisconnect) sock.ev?.once?.('connection.update', ({ connection }) => { if (connection === 'close') aborted = true })
|
|
11
|
-
|
|
12
|
-
await sock.sendPresenceUpdate('composing', jid)
|
|
13
|
-
|
|
14
|
-
const initial = steps.map(s => ({ ...s, status: PlanningStepStatus.IN_PROGRESS }))
|
|
15
|
-
const placeholder = await sock.sendMessage(jid, buildPlainPlaceholder(description, initial, placeholderText))
|
|
16
|
-
const key = placeholder?.key
|
|
17
|
-
|
|
18
|
-
try {
|
|
19
|
-
const current = [...initial]
|
|
20
|
-
for (let i = 0; i < current.length; i++) {
|
|
21
|
-
if (aborted) break
|
|
22
|
-
await delay(stepDelayMs)
|
|
23
|
-
if (aborted) break
|
|
24
|
-
current[i] = { ...current[i], status: PlanningStepStatus.DONE }
|
|
25
|
-
if (key) await sock.sendMessage(jid, { edit: key, ...buildPlainPlaceholder(description, current, placeholderText) })
|
|
26
|
-
}
|
|
27
|
-
if (!aborted && finalPauseMs > 0) await delay(finalPauseMs)
|
|
28
|
-
if (key && !aborted) await sock.sendMessage(jid, { delete: key })
|
|
29
|
-
} catch (_) {
|
|
30
|
-
try { if (key) await sock.sendMessage(jid, { delete: key }) } catch (__) { }
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
await sock.sendPresenceUpdate('paused', jid)
|
|
34
|
-
if (sendOptions._skipFinalSend) return placeholder
|
|
35
|
-
return sock.sendMessage(jid, finalContent, sendOptions)
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
// Animation without a final message
|
|
39
|
-
export const replayPlanningOnly = (sock, jid, steps, options = {}) =>
|
|
40
|
-
replayPlanning(sock, jid, steps, null, { ...options, sendOptions: { ...options.sendOptions, _skipFinalSend: true } })
|
|
41
|
-
|
|
42
|
-
export const buildReasoningSteps = titles => titles.map(title => ({ title, isReasoning: true }))
|
|
43
|
-
export const buildSearchSteps = titles => titles.map(title => ({ title, isEnhancedSearch: true }))
|
|
1
|
+
import { buildPlainPlaceholder, PlanningStepStatus } from './meta-compositing.js'
|
|
2
|
+
import { delay } from './generics.js'
|
|
3
|
+
|
|
4
|
+
// Live planning animation: shows pending steps, edits each to DONE one-by-one,
|
|
5
|
+
// deletes placeholder, then sends the final message. Works on all WA clients.
|
|
6
|
+
export const replayPlanning = async (sock, jid, steps, finalContent, { description = 'Thinking…', placeholderText = '', stepDelayMs = 900, finalPauseMs = 600, abortOnDisconnect = true, sendOptions = {} } = {}) => {
|
|
7
|
+
if (!steps?.length) throw new Error('replayPlanning: steps must have at least one entry')
|
|
8
|
+
|
|
9
|
+
let aborted = false
|
|
10
|
+
if (abortOnDisconnect) sock.ev?.once?.('connection.update', ({ connection }) => { if (connection === 'close') aborted = true })
|
|
11
|
+
|
|
12
|
+
await sock.sendPresenceUpdate('composing', jid)
|
|
13
|
+
|
|
14
|
+
const initial = steps.map(s => ({ ...s, status: PlanningStepStatus.IN_PROGRESS }))
|
|
15
|
+
const placeholder = await sock.sendMessage(jid, buildPlainPlaceholder(description, initial, placeholderText))
|
|
16
|
+
const key = placeholder?.key
|
|
17
|
+
|
|
18
|
+
try {
|
|
19
|
+
const current = [...initial]
|
|
20
|
+
for (let i = 0; i < current.length; i++) {
|
|
21
|
+
if (aborted) break
|
|
22
|
+
await delay(stepDelayMs)
|
|
23
|
+
if (aborted) break
|
|
24
|
+
current[i] = { ...current[i], status: PlanningStepStatus.DONE }
|
|
25
|
+
if (key) await sock.sendMessage(jid, { edit: key, ...buildPlainPlaceholder(description, current, placeholderText) })
|
|
26
|
+
}
|
|
27
|
+
if (!aborted && finalPauseMs > 0) await delay(finalPauseMs)
|
|
28
|
+
if (key && !aborted) await sock.sendMessage(jid, { delete: key })
|
|
29
|
+
} catch (_) {
|
|
30
|
+
try { if (key) await sock.sendMessage(jid, { delete: key }) } catch (__) { }
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
await sock.sendPresenceUpdate('paused', jid)
|
|
34
|
+
if (sendOptions._skipFinalSend) return placeholder
|
|
35
|
+
return sock.sendMessage(jid, finalContent, sendOptions)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Animation without a final message
|
|
39
|
+
export const replayPlanningOnly = (sock, jid, steps, options = {}) =>
|
|
40
|
+
replayPlanning(sock, jid, steps, null, { ...options, sendOptions: { ...options.sendOptions, _skipFinalSend: true } })
|
|
41
|
+
|
|
42
|
+
export const buildReasoningSteps = titles => titles.map(title => ({ title, isReasoning: true }))
|
|
43
|
+
export const buildSearchSteps = titles => titles.map(title => ({ title, isEnhancedSearch: true }))
|
|
44
44
|
export const mixedSteps = defs => defs.map(({ title, body, type }) => ({ title, ...(body ? { body } : {}), ...(type === 'reasoning' ? { isReasoning: true } : {}), ...(type === 'search' ? { isEnhancedSearch: true } : {}) }))
|
|
@@ -1,30 +1,30 @@
|
|
|
1
|
-
import { platform, release } from 'os'
|
|
2
|
-
import { proto } from '../../WAProto/index.js'
|
|
3
|
-
|
|
4
|
-
const PLATFORM_MAP = {
|
|
5
|
-
aix: 'AIX', darwin: 'Mac OS', win32: 'Windows', android: 'Android',
|
|
6
|
-
freebsd: 'FreeBSD', openbsd: 'OpenBSD', sunos: 'Solaris',
|
|
7
|
-
linux: undefined, haiku: undefined, cygwin: undefined, netbsd: undefined
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export const Browsers = {
|
|
11
|
-
ubuntu: browser => ['Ubuntu', browser, '24.04.2'],
|
|
12
|
-
macOS: browser => ['Mac OS', browser, '15.4.1'],
|
|
13
|
-
baileys: browser => ['Baileys', browser, '6.5.0'],
|
|
14
|
-
windows: browser => ['Windows', browser, '10.0.26100'],
|
|
15
|
-
android: browser => [browser, 'Android', ''],
|
|
16
|
-
ios: browser => ['Mac OS', browser, '18.4'],
|
|
17
|
-
ipad: browser => ['iPadOS', browser, '18.4'],
|
|
18
|
-
appropriate: browser => [PLATFORM_MAP[platform()] || 'Ubuntu', browser, release()]
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export const getPlatformType = (browser) => {
|
|
22
|
-
const upper = browser.toUpperCase()
|
|
23
|
-
if (upper === 'ANDROID') return proto.DeviceProps.PlatformType.ANDROID_PHONE
|
|
24
|
-
if (upper === 'IPAD') return proto.DeviceProps.PlatformType.IPAD
|
|
25
|
-
if (upper === 'IOS') return proto.DeviceProps.PlatformType.IOS_PHONE
|
|
26
|
-
if (upper === 'DESKTOP') return proto.DeviceProps.PlatformType.DESKTOP
|
|
27
|
-
return proto.DeviceProps.PlatformType[upper] ?? proto.DeviceProps.PlatformType.CHROME
|
|
28
|
-
}
|
|
29
|
-
|
|
1
|
+
import { platform, release } from 'os'
|
|
2
|
+
import { proto } from '../../WAProto/index.js'
|
|
3
|
+
|
|
4
|
+
const PLATFORM_MAP = {
|
|
5
|
+
aix: 'AIX', darwin: 'Mac OS', win32: 'Windows', android: 'Android',
|
|
6
|
+
freebsd: 'FreeBSD', openbsd: 'OpenBSD', sunos: 'Solaris',
|
|
7
|
+
linux: undefined, haiku: undefined, cygwin: undefined, netbsd: undefined
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export const Browsers = {
|
|
11
|
+
ubuntu: browser => ['Ubuntu', browser, '24.04.2'],
|
|
12
|
+
macOS: browser => ['Mac OS', browser, '15.4.1'],
|
|
13
|
+
baileys: browser => ['Baileys', browser, '6.5.0'],
|
|
14
|
+
windows: browser => ['Windows', browser, '10.0.26100'],
|
|
15
|
+
android: browser => [browser, 'Android', ''],
|
|
16
|
+
ios: browser => ['Mac OS', browser, '18.4'],
|
|
17
|
+
ipad: browser => ['iPadOS', browser, '18.4'],
|
|
18
|
+
appropriate: browser => [PLATFORM_MAP[platform()] || 'Ubuntu', browser, release()]
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export const getPlatformType = (browser) => {
|
|
22
|
+
const upper = browser.toUpperCase()
|
|
23
|
+
if (upper === 'ANDROID') return proto.DeviceProps.PlatformType.ANDROID_PHONE
|
|
24
|
+
if (upper === 'IPAD') return proto.DeviceProps.PlatformType.IPAD
|
|
25
|
+
if (upper === 'IOS') return proto.DeviceProps.PlatformType.IOS_PHONE
|
|
26
|
+
if (upper === 'DESKTOP') return proto.DeviceProps.PlatformType.DESKTOP
|
|
27
|
+
return proto.DeviceProps.PlatformType[upper] ?? proto.DeviceProps.PlatformType.CHROME
|
|
28
|
+
}
|
|
29
|
+
|
|
30
30
|
export const isAndroidBrowser = browser => browser[1]?.toUpperCase() === 'ANDROID'
|
package/lib/Utils/business.js
CHANGED
|
@@ -1,75 +1,75 @@
|
|
|
1
|
-
import { Boom } from '@hapi/boom'
|
|
2
|
-
import { createHash } from 'crypto'
|
|
3
|
-
import { getBinaryNodeChild, getBinaryNodeChildren, getBinaryNodeChildString } from '../WABinary/index.js'
|
|
4
|
-
import { getStream, getUrlFromDirectPath, toReadable } from './messages-media.js'
|
|
5
|
-
|
|
6
|
-
export const parseCatalogNode = node => {
|
|
7
|
-
const catalogNode = getBinaryNodeChild(node, 'product_catalog')
|
|
8
|
-
const paging = getBinaryNodeChild(catalogNode, 'paging')
|
|
9
|
-
return { products: getBinaryNodeChildren(catalogNode, 'product').map(parseProductNode), nextPageCursor: paging ? getBinaryNodeChildString(paging, 'after') : undefined }
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export const parseCollectionsNode = node => {
|
|
13
|
-
const collectionsNode = getBinaryNodeChild(node, 'collections')
|
|
14
|
-
return { collections: getBinaryNodeChildren(collectionsNode, 'collection').map(collectionNode => ({ id: getBinaryNodeChildString(collectionNode, 'id'), name: getBinaryNodeChildString(collectionNode, 'name'), products: getBinaryNodeChildren(collectionNode, 'product').map(parseProductNode), status: parseStatusInfo(collectionNode) })) }
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export const parseOrderDetailsNode = node => {
|
|
18
|
-
const orderNode = getBinaryNodeChild(node, 'order')
|
|
19
|
-
const priceNode = getBinaryNodeChild(orderNode, 'price')
|
|
20
|
-
const products = getBinaryNodeChildren(orderNode, 'product').map(productNode => {
|
|
21
|
-
const imageNode = getBinaryNodeChild(productNode, 'image')
|
|
22
|
-
return { id: getBinaryNodeChildString(productNode, 'id'), name: getBinaryNodeChildString(productNode, 'name'), imageUrl: getBinaryNodeChildString(imageNode, 'url'), price: +getBinaryNodeChildString(productNode, 'price'), currency: getBinaryNodeChildString(productNode, 'currency'), quantity: +getBinaryNodeChildString(productNode, 'quantity') }
|
|
23
|
-
})
|
|
24
|
-
return { price: { total: +getBinaryNodeChildString(priceNode, 'total'), currency: getBinaryNodeChildString(priceNode, 'currency') }, products }
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export const toProductNode = (productId, product) => {
|
|
28
|
-
const attrs = {}
|
|
29
|
-
const content = []
|
|
30
|
-
const push = (tag, value) => content.push({ tag, attrs: {}, content: Buffer.from(value) })
|
|
31
|
-
if (productId !== undefined) push('id', productId)
|
|
32
|
-
if (product.name !== undefined) push('name', product.name)
|
|
33
|
-
if (product.description !== undefined) push('description', product.description)
|
|
34
|
-
if (product.retailerId !== undefined) push('retailer_id', product.retailerId)
|
|
35
|
-
if (product.images?.length) {
|
|
36
|
-
content.push({
|
|
37
|
-
tag: 'media', attrs: {}, content: product.images.map(img => {
|
|
38
|
-
if (!('url' in img)) throw new Boom('Expected img for product to already be uploaded', { statusCode: 400 })
|
|
39
|
-
return { tag: 'image', attrs: {}, content: [{ tag: 'url', attrs: {}, content: Buffer.from(img.url.toString()) }] }
|
|
40
|
-
})
|
|
41
|
-
})
|
|
42
|
-
}
|
|
43
|
-
if (product.price !== undefined) push('price', product.price.toString())
|
|
44
|
-
if (product.currency !== undefined) push('currency', product.currency)
|
|
45
|
-
if ('originCountryCode' in product) {
|
|
46
|
-
if (product.originCountryCode === undefined) attrs['compliance_category'] = 'COUNTRY_ORIGIN_EXEMPT'
|
|
47
|
-
else content.push({ tag: 'compliance_info', attrs: {}, content: [{ tag: 'country_code_origin', attrs: {}, content: Buffer.from(product.originCountryCode) }] })
|
|
48
|
-
}
|
|
49
|
-
if (product.isHidden !== undefined) attrs['is_hidden'] = product.isHidden.toString()
|
|
50
|
-
return { tag: 'product', attrs, content }
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
export const parseProductNode = productNode => {
|
|
54
|
-
const mediaNode = getBinaryNodeChild(productNode, 'media')
|
|
55
|
-
const statusInfoNode = getBinaryNodeChild(productNode, 'status_info')
|
|
56
|
-
return { id: getBinaryNodeChildString(productNode, 'id'), imageUrls: parseImageUrls(mediaNode), reviewStatus: { whatsapp: getBinaryNodeChildString(statusInfoNode, 'status') }, availability: 'in stock', name: getBinaryNodeChildString(productNode, 'name'), retailerId: getBinaryNodeChildString(productNode, 'retailer_id'), url: getBinaryNodeChildString(productNode, 'url'), description: getBinaryNodeChildString(productNode, 'description'), price: +getBinaryNodeChildString(productNode, 'price'), currency: getBinaryNodeChildString(productNode, 'currency'), isHidden: productNode.attrs.is_hidden === 'true' }
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
export const uploadingNecessaryImagesOfProduct = async (product, waUploadToServer, timeoutMs = 30000) =>
|
|
60
|
-
({ ...product, images: product.images ? await uploadingNecessaryImages(product.images, waUploadToServer, timeoutMs) : product.images })
|
|
61
|
-
|
|
62
|
-
export const uploadingNecessaryImages = (images, waUploadToServer, timeoutMs = 30000) =>
|
|
63
|
-
Promise.all(images.map(async img => {
|
|
64
|
-
if ('url' in img
|
|
65
|
-
const { stream } = await getStream(img)
|
|
66
|
-
const hasher = createHash('sha256')
|
|
67
|
-
const blocks = []
|
|
68
|
-
for await (const block of stream) { hasher.update(block); blocks.push(block) }
|
|
69
|
-
const sha = hasher.digest('base64')
|
|
70
|
-
const { directPath } = await waUploadToServer(toReadable(Buffer.concat(blocks)), { mediaType: 'product-catalog-image', fileEncSha256B64: sha, timeoutMs })
|
|
71
|
-
return { url: getUrlFromDirectPath(directPath) }
|
|
72
|
-
}))
|
|
73
|
-
|
|
74
|
-
const parseImageUrls = mediaNode => { const imgNode = getBinaryNodeChild(mediaNode, 'image'); return { requested: getBinaryNodeChildString(imgNode, 'request_image_url'), original: getBinaryNodeChildString(imgNode, 'original_image_url') } }
|
|
1
|
+
import { Boom } from '@hapi/boom'
|
|
2
|
+
import { createHash } from 'crypto'
|
|
3
|
+
import { getBinaryNodeChild, getBinaryNodeChildren, getBinaryNodeChildString } from '../WABinary/index.js'
|
|
4
|
+
import { getStream, getUrlFromDirectPath, toReadable } from './messages-media.js'
|
|
5
|
+
|
|
6
|
+
export const parseCatalogNode = node => {
|
|
7
|
+
const catalogNode = getBinaryNodeChild(node, 'product_catalog')
|
|
8
|
+
const paging = getBinaryNodeChild(catalogNode, 'paging')
|
|
9
|
+
return { products: getBinaryNodeChildren(catalogNode, 'product').map(parseProductNode), nextPageCursor: paging ? getBinaryNodeChildString(paging, 'after') : undefined }
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export const parseCollectionsNode = node => {
|
|
13
|
+
const collectionsNode = getBinaryNodeChild(node, 'collections')
|
|
14
|
+
return { collections: getBinaryNodeChildren(collectionsNode, 'collection').map(collectionNode => ({ id: getBinaryNodeChildString(collectionNode, 'id'), name: getBinaryNodeChildString(collectionNode, 'name'), products: getBinaryNodeChildren(collectionNode, 'product').map(parseProductNode), status: parseStatusInfo(collectionNode) })) }
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export const parseOrderDetailsNode = node => {
|
|
18
|
+
const orderNode = getBinaryNodeChild(node, 'order')
|
|
19
|
+
const priceNode = getBinaryNodeChild(orderNode, 'price')
|
|
20
|
+
const products = getBinaryNodeChildren(orderNode, 'product').map(productNode => {
|
|
21
|
+
const imageNode = getBinaryNodeChild(productNode, 'image')
|
|
22
|
+
return { id: getBinaryNodeChildString(productNode, 'id'), name: getBinaryNodeChildString(productNode, 'name'), imageUrl: getBinaryNodeChildString(imageNode, 'url'), price: +getBinaryNodeChildString(productNode, 'price'), currency: getBinaryNodeChildString(productNode, 'currency'), quantity: +getBinaryNodeChildString(productNode, 'quantity') }
|
|
23
|
+
})
|
|
24
|
+
return { price: { total: +getBinaryNodeChildString(priceNode, 'total'), currency: getBinaryNodeChildString(priceNode, 'currency') }, products }
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export const toProductNode = (productId, product) => {
|
|
28
|
+
const attrs = {}
|
|
29
|
+
const content = []
|
|
30
|
+
const push = (tag, value) => content.push({ tag, attrs: {}, content: Buffer.from(value) })
|
|
31
|
+
if (productId !== undefined) push('id', productId)
|
|
32
|
+
if (product.name !== undefined) push('name', product.name)
|
|
33
|
+
if (product.description !== undefined) push('description', product.description)
|
|
34
|
+
if (product.retailerId !== undefined) push('retailer_id', product.retailerId)
|
|
35
|
+
if (product.images?.length) {
|
|
36
|
+
content.push({
|
|
37
|
+
tag: 'media', attrs: {}, content: product.images.map(img => {
|
|
38
|
+
if (!('url' in img)) throw new Boom('Expected img for product to already be uploaded', { statusCode: 400 })
|
|
39
|
+
return { tag: 'image', attrs: {}, content: [{ tag: 'url', attrs: {}, content: Buffer.from(img.url.toString()) }] }
|
|
40
|
+
})
|
|
41
|
+
})
|
|
42
|
+
}
|
|
43
|
+
if (product.price !== undefined) push('price', product.price.toString())
|
|
44
|
+
if (product.currency !== undefined) push('currency', product.currency)
|
|
45
|
+
if ('originCountryCode' in product) {
|
|
46
|
+
if (product.originCountryCode === undefined) attrs['compliance_category'] = 'COUNTRY_ORIGIN_EXEMPT'
|
|
47
|
+
else content.push({ tag: 'compliance_info', attrs: {}, content: [{ tag: 'country_code_origin', attrs: {}, content: Buffer.from(product.originCountryCode) }] })
|
|
48
|
+
}
|
|
49
|
+
if (product.isHidden !== undefined) attrs['is_hidden'] = product.isHidden.toString()
|
|
50
|
+
return { tag: 'product', attrs, content }
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export const parseProductNode = productNode => {
|
|
54
|
+
const mediaNode = getBinaryNodeChild(productNode, 'media')
|
|
55
|
+
const statusInfoNode = getBinaryNodeChild(productNode, 'status_info')
|
|
56
|
+
return { id: getBinaryNodeChildString(productNode, 'id'), imageUrls: parseImageUrls(mediaNode), reviewStatus: { whatsapp: getBinaryNodeChildString(statusInfoNode, 'status') }, availability: 'in stock', name: getBinaryNodeChildString(productNode, 'name'), retailerId: getBinaryNodeChildString(productNode, 'retailer_id'), url: getBinaryNodeChildString(productNode, 'url'), description: getBinaryNodeChildString(productNode, 'description'), price: +getBinaryNodeChildString(productNode, 'price'), currency: getBinaryNodeChildString(productNode, 'currency'), isHidden: productNode.attrs.is_hidden === 'true' }
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export const uploadingNecessaryImagesOfProduct = async (product, waUploadToServer, timeoutMs = 30000) =>
|
|
60
|
+
({ ...product, images: product.images ? await uploadingNecessaryImages(product.images, waUploadToServer, timeoutMs) : product.images })
|
|
61
|
+
|
|
62
|
+
export const uploadingNecessaryImages = (images, waUploadToServer, timeoutMs = 30000) =>
|
|
63
|
+
Promise.all(images.map(async img => {
|
|
64
|
+
if ('url' in img) try { const h = new URL(img.url.toString()).hostname.toLowerCase(); if (h === 'whatsapp.net' || h.endsWith('.whatsapp.net')) return { url: img.url.toString() } } catch { }
|
|
65
|
+
const { stream } = await getStream(img)
|
|
66
|
+
const hasher = createHash('sha256')
|
|
67
|
+
const blocks = []
|
|
68
|
+
for await (const block of stream) { hasher.update(block); blocks.push(block) }
|
|
69
|
+
const sha = hasher.digest('base64')
|
|
70
|
+
const { directPath } = await waUploadToServer(toReadable(Buffer.concat(blocks)), { mediaType: 'product-catalog-image', fileEncSha256B64: sha, timeoutMs })
|
|
71
|
+
return { url: getUrlFromDirectPath(directPath) }
|
|
72
|
+
}))
|
|
73
|
+
|
|
74
|
+
const parseImageUrls = mediaNode => { const imgNode = getBinaryNodeChild(mediaNode, 'image'); return { requested: getBinaryNodeChildString(imgNode, 'request_image_url'), original: getBinaryNodeChildString(imgNode, 'original_image_url') } }
|
|
75
75
|
const parseStatusInfo = mediaNode => { const node = getBinaryNodeChild(mediaNode, 'status_info'); return { status: getBinaryNodeChildString(node, 'status'), canAppeal: getBinaryNodeChildString(node, 'can_appeal') === 'true' } }
|