@rakku-kun/baileys 2.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 +21 -0
- package/README.md +647 -0
- package/WAProto/GenerateStatics.sh +3 -0
- package/WAProto/WAProto.proto +5479 -0
- package/WAProto/fix-imports.js +81 -0
- package/WAProto/index.d.ts +14017 -0
- package/WAProto/index.js +97687 -0
- package/engine-requirements.js +10 -0
- package/index.cjs +196 -0
- package/index.d.ts +30 -0
- package/lib/Defaults/index.d.ts +74 -0
- package/lib/Defaults/index.d.ts.map +1 -0
- package/lib/Defaults/index.js +135 -0
- package/lib/Defaults/index.js.map +1 -0
- package/lib/Defaults/phonenumber-mcc.json +223 -0
- package/lib/Signal/Group/ciphertext-message.d.ts +10 -0
- package/lib/Signal/Group/ciphertext-message.d.ts.map +1 -0
- package/lib/Signal/Group/ciphertext-message.js +12 -0
- package/lib/Signal/Group/ciphertext-message.js.map +1 -0
- package/lib/Signal/Group/group-session-builder.d.ts +15 -0
- package/lib/Signal/Group/group-session-builder.d.ts.map +1 -0
- package/lib/Signal/Group/group-session-builder.js +30 -0
- package/lib/Signal/Group/group-session-builder.js.map +1 -0
- package/lib/Signal/Group/group_cipher.d.ts +17 -0
- package/lib/Signal/Group/group_cipher.d.ts.map +1 -0
- package/lib/Signal/Group/group_cipher.js +82 -0
- package/lib/Signal/Group/group_cipher.js.map +1 -0
- package/lib/Signal/Group/index.d.ts +12 -0
- package/lib/Signal/Group/index.d.ts.map +1 -0
- package/lib/Signal/Group/index.js +12 -0
- package/lib/Signal/Group/index.js.map +1 -0
- package/lib/Signal/Group/keyhelper.d.ts +11 -0
- package/lib/Signal/Group/keyhelper.d.ts.map +1 -0
- package/lib/Signal/Group/keyhelper.js +18 -0
- package/lib/Signal/Group/keyhelper.js.map +1 -0
- package/lib/Signal/Group/sender-chain-key.d.ts +14 -0
- package/lib/Signal/Group/sender-chain-key.d.ts.map +1 -0
- package/lib/Signal/Group/sender-chain-key.js +26 -0
- package/lib/Signal/Group/sender-chain-key.js.map +1 -0
- package/lib/Signal/Group/sender-key-distribution-message.d.ts +17 -0
- package/lib/Signal/Group/sender-key-distribution-message.d.ts.map +1 -0
- package/lib/Signal/Group/sender-key-distribution-message.js +63 -0
- package/lib/Signal/Group/sender-key-distribution-message.js.map +1 -0
- package/lib/Signal/Group/sender-key-message.d.ts +19 -0
- package/lib/Signal/Group/sender-key-message.d.ts.map +1 -0
- package/lib/Signal/Group/sender-key-message.js +66 -0
- package/lib/Signal/Group/sender-key-message.js.map +1 -0
- package/lib/Signal/Group/sender-key-name.d.ts +18 -0
- package/lib/Signal/Group/sender-key-name.d.ts.map +1 -0
- package/lib/Signal/Group/sender-key-name.js +48 -0
- package/lib/Signal/Group/sender-key-name.js.map +1 -0
- package/lib/Signal/Group/sender-key-record.d.ts +31 -0
- package/lib/Signal/Group/sender-key-record.d.ts.map +1 -0
- package/lib/Signal/Group/sender-key-record.js +41 -0
- package/lib/Signal/Group/sender-key-record.js.map +1 -0
- package/lib/Signal/Group/sender-key-state.d.ts +39 -0
- package/lib/Signal/Group/sender-key-state.d.ts.map +1 -0
- package/lib/Signal/Group/sender-key-state.js +84 -0
- package/lib/Signal/Group/sender-key-state.js.map +1 -0
- package/lib/Signal/Group/sender-message-key.d.ts +12 -0
- package/lib/Signal/Group/sender-message-key.d.ts.map +1 -0
- package/lib/Signal/Group/sender-message-key.js +26 -0
- package/lib/Signal/Group/sender-message-key.js.map +1 -0
- package/lib/Signal/libsignal.d.ts +5 -0
- package/lib/Signal/libsignal.d.ts.map +1 -0
- package/lib/Signal/libsignal.js +438 -0
- package/lib/Signal/libsignal.js.map +1 -0
- package/lib/Signal/lid-mapping.d.ts +19 -0
- package/lib/Signal/lid-mapping.d.ts.map +1 -0
- package/lib/Signal/lid-mapping.js +271 -0
- package/lib/Signal/lid-mapping.js.map +1 -0
- package/lib/Socket/Client/index.d.ts +3 -0
- package/lib/Socket/Client/index.d.ts.map +1 -0
- package/lib/Socket/Client/index.js +3 -0
- package/lib/Socket/Client/index.js.map +1 -0
- package/lib/Socket/Client/types.d.ts +16 -0
- package/lib/Socket/Client/types.d.ts.map +1 -0
- package/lib/Socket/Client/types.js +11 -0
- package/lib/Socket/Client/types.js.map +1 -0
- package/lib/Socket/Client/websocket.d.ts +13 -0
- package/lib/Socket/Client/websocket.d.ts.map +1 -0
- package/lib/Socket/Client/websocket.js +54 -0
- package/lib/Socket/Client/websocket.js.map +1 -0
- package/lib/Socket/business.d.ts +202 -0
- package/lib/Socket/business.d.ts.map +1 -0
- package/lib/Socket/business.js +379 -0
- package/lib/Socket/business.js.map +1 -0
- package/lib/Socket/chats.d.ts +111 -0
- package/lib/Socket/chats.d.ts.map +1 -0
- package/lib/Socket/chats.js +1133 -0
- package/lib/Socket/chats.js.map +1 -0
- package/lib/Socket/communities.d.ts +258 -0
- package/lib/Socket/communities.d.ts.map +1 -0
- package/lib/Socket/communities.js +431 -0
- package/lib/Socket/communities.js.map +1 -0
- package/lib/Socket/community.js +361 -0
- package/lib/Socket/groups.d.ts +150 -0
- package/lib/Socket/groups.d.ts.map +1 -0
- package/lib/Socket/groups.js +459 -0
- package/lib/Socket/groups.js.map +1 -0
- package/lib/Socket/index.d.ts +245 -0
- package/lib/Socket/index.d.ts.map +1 -0
- package/lib/Socket/index.js +12 -0
- package/lib/Socket/index.js.map +1 -0
- package/lib/Socket/messages-recv.d.ts +187 -0
- package/lib/Socket/messages-recv.d.ts.map +1 -0
- package/lib/Socket/messages-recv.js +1681 -0
- package/lib/Socket/messages-recv.js.map +1 -0
- package/lib/Socket/messages-send.d.ts +183 -0
- package/lib/Socket/messages-send.d.ts.map +1 -0
- package/lib/Socket/messages-send.js +1635 -0
- package/lib/Socket/messages-send.js.map +1 -0
- package/lib/Socket/mex.d.ts +3 -0
- package/lib/Socket/mex.d.ts.map +1 -0
- package/lib/Socket/mex.js +42 -0
- package/lib/Socket/mex.js.map +1 -0
- package/lib/Socket/newsletter.d.ts +160 -0
- package/lib/Socket/newsletter.d.ts.map +1 -0
- package/lib/Socket/newsletter.js +270 -0
- package/lib/Socket/newsletter.js.map +1 -0
- package/lib/Socket/socket.d.ts +55 -0
- package/lib/Socket/socket.d.ts.map +1 -0
- package/lib/Socket/socket.js +944 -0
- package/lib/Socket/socket.js.map +1 -0
- package/lib/Socket/usync.js +76 -0
- package/lib/Store/index.js +4 -0
- package/lib/Store/make-cache-manager-store.js +77 -0
- package/lib/Store/make-in-memory-store.js +400 -0
- package/lib/Store/make-ordered-dictionary.js +78 -0
- package/lib/Store/object-repository.js +23 -0
- package/lib/Types/Auth.d.ts +116 -0
- package/lib/Types/Auth.d.ts.map +1 -0
- package/lib/Types/Auth.js +2 -0
- package/lib/Types/Auth.js.map +1 -0
- package/lib/Types/Bussines.d.ts +25 -0
- package/lib/Types/Bussines.d.ts.map +1 -0
- package/lib/Types/Bussines.js +2 -0
- package/lib/Types/Bussines.js.map +1 -0
- package/lib/Types/Call.d.ts +15 -0
- package/lib/Types/Call.d.ts.map +1 -0
- package/lib/Types/Call.js +2 -0
- package/lib/Types/Call.js.map +1 -0
- package/lib/Types/Chat.d.ts +123 -0
- package/lib/Types/Chat.d.ts.map +1 -0
- package/lib/Types/Chat.js +8 -0
- package/lib/Types/Chat.js.map +1 -0
- package/lib/Types/Contact.d.ts +24 -0
- package/lib/Types/Contact.d.ts.map +1 -0
- package/lib/Types/Contact.js +2 -0
- package/lib/Types/Contact.js.map +1 -0
- package/lib/Types/Events.d.ts +237 -0
- package/lib/Types/Events.d.ts.map +1 -0
- package/lib/Types/Events.js +2 -0
- package/lib/Types/Events.js.map +1 -0
- package/lib/Types/GroupMetadata.d.ts +67 -0
- package/lib/Types/GroupMetadata.d.ts.map +1 -0
- package/lib/Types/GroupMetadata.js +2 -0
- package/lib/Types/GroupMetadata.js.map +1 -0
- package/lib/Types/Label.d.ts +47 -0
- package/lib/Types/Label.d.ts.map +1 -0
- package/lib/Types/Label.js +25 -0
- package/lib/Types/Label.js.map +1 -0
- package/lib/Types/LabelAssociation.d.ts +30 -0
- package/lib/Types/LabelAssociation.d.ts.map +1 -0
- package/lib/Types/LabelAssociation.js +7 -0
- package/lib/Types/LabelAssociation.js.map +1 -0
- package/lib/Types/Message.d.ts +495 -0
- package/lib/Types/Message.d.ts.map +1 -0
- package/lib/Types/Message.js +11 -0
- package/lib/Types/Message.js.map +1 -0
- package/lib/Types/MexUpdates.js +9 -0
- package/lib/Types/Newsletter.d.ts +135 -0
- package/lib/Types/Newsletter.d.ts.map +1 -0
- package/lib/Types/Newsletter.js +43 -0
- package/lib/Types/Newsletter.js.map +1 -0
- package/lib/Types/Product.d.ts +79 -0
- package/lib/Types/Product.d.ts.map +1 -0
- package/lib/Types/Product.js +2 -0
- package/lib/Types/Product.js.map +1 -0
- package/lib/Types/Signal.d.ts +76 -0
- package/lib/Types/Signal.d.ts.map +1 -0
- package/lib/Types/Signal.js +2 -0
- package/lib/Types/Signal.js.map +1 -0
- package/lib/Types/Socket.d.ts +133 -0
- package/lib/Types/Socket.d.ts.map +1 -0
- package/lib/Types/Socket.js +3 -0
- package/lib/Types/Socket.js.map +1 -0
- package/lib/Types/State.d.ts +39 -0
- package/lib/Types/State.d.ts.map +1 -0
- package/lib/Types/State.js +13 -0
- package/lib/Types/State.js.map +1 -0
- package/lib/Types/USync.d.ts +26 -0
- package/lib/Types/USync.d.ts.map +1 -0
- package/lib/Types/USync.js +2 -0
- package/lib/Types/USync.js.map +1 -0
- package/lib/Types/index.d.ts +65 -0
- package/lib/Types/index.d.ts.map +1 -0
- package/lib/Types/index.js +27 -0
- package/lib/Types/index.js.map +1 -0
- package/lib/Utils/audioToBuffer.js +31 -0
- package/lib/Utils/auth-utils.d.ts +19 -0
- package/lib/Utils/auth-utils.d.ts.map +1 -0
- package/lib/Utils/auth-utils.js +290 -0
- package/lib/Utils/auth-utils.js.map +1 -0
- package/lib/Utils/baileys-event-stream.js +54 -0
- package/lib/Utils/browser-utils.d.ts +4 -0
- package/lib/Utils/browser-utils.d.ts.map +1 -0
- package/lib/Utils/browser-utils.js +41 -0
- package/lib/Utils/browser-utils.js.map +1 -0
- package/lib/Utils/business.d.ts +23 -0
- package/lib/Utils/business.d.ts.map +1 -0
- package/lib/Utils/business.js +231 -0
- package/lib/Utils/business.js.map +1 -0
- package/lib/Utils/chat-utils.d.ts +70 -0
- package/lib/Utils/chat-utils.d.ts.map +1 -0
- package/lib/Utils/chat-utils.js +812 -0
- package/lib/Utils/chat-utils.js.map +1 -0
- package/lib/Utils/crypto.d.ts +37 -0
- package/lib/Utils/crypto.d.ts.map +1 -0
- package/lib/Utils/crypto.js +118 -0
- package/lib/Utils/crypto.js.map +1 -0
- package/lib/Utils/decode-wa-message.d.ts +48 -0
- package/lib/Utils/decode-wa-message.d.ts.map +1 -0
- package/lib/Utils/decode-wa-message.js +283 -0
- package/lib/Utils/decode-wa-message.js.map +1 -0
- package/lib/Utils/event-buffer.d.ts +34 -0
- package/lib/Utils/event-buffer.d.ts.map +1 -0
- package/lib/Utils/event-buffer.js +574 -0
- package/lib/Utils/event-buffer.js.map +1 -0
- package/lib/Utils/generics.d.ts +91 -0
- package/lib/Utils/generics.d.ts.map +1 -0
- package/lib/Utils/generics.js +411 -0
- package/lib/Utils/generics.js.map +1 -0
- package/lib/Utils/history.d.ts +22 -0
- package/lib/Utils/history.d.ts.map +1 -0
- package/lib/Utils/history.js +131 -0
- package/lib/Utils/history.js.map +1 -0
- package/lib/Utils/identity-change-handler.d.ts +37 -0
- package/lib/Utils/identity-change-handler.d.ts.map +1 -0
- package/lib/Utils/identity-change-handler.js +49 -0
- package/lib/Utils/identity-change-handler.js.map +1 -0
- package/lib/Utils/index.d.ts +22 -0
- package/lib/Utils/index.d.ts.map +1 -0
- package/lib/Utils/index.js +27 -0
- package/lib/Utils/index.js.map +1 -0
- package/lib/Utils/link-preview.d.ts +21 -0
- package/lib/Utils/link-preview.d.ts.map +1 -0
- package/lib/Utils/link-preview.js +85 -0
- package/lib/Utils/link-preview.js.map +1 -0
- package/lib/Utils/logger.d.ts +13 -0
- package/lib/Utils/logger.d.ts.map +1 -0
- package/lib/Utils/logger.js +3 -0
- package/lib/Utils/logger.js.map +1 -0
- package/lib/Utils/lt-hash.d.ts +8 -0
- package/lib/Utils/lt-hash.d.ts.map +1 -0
- package/lib/Utils/lt-hash.js +8 -0
- package/lib/Utils/lt-hash.js.map +1 -0
- package/lib/Utils/make-mutex.d.ts +9 -0
- package/lib/Utils/make-mutex.d.ts.map +1 -0
- package/lib/Utils/make-mutex.js +33 -0
- package/lib/Utils/make-mutex.js.map +1 -0
- package/lib/Utils/message-retry-manager.d.ts +110 -0
- package/lib/Utils/message-retry-manager.d.ts.map +1 -0
- package/lib/Utils/message-retry-manager.js +225 -0
- package/lib/Utils/message-retry-manager.js.map +1 -0
- package/lib/Utils/messages-media.d.ts +130 -0
- package/lib/Utils/messages-media.d.ts.map +1 -0
- package/lib/Utils/messages-media.js +830 -0
- package/lib/Utils/messages-media.js.map +1 -0
- package/lib/Utils/messages-newsletter.d.ts +84 -0
- package/lib/Utils/messages-newsletter.js +316 -0
- package/lib/Utils/messages.d.ts +92 -0
- package/lib/Utils/messages.d.ts.map +1 -0
- package/lib/Utils/messages.js +1699 -0
- package/lib/Utils/messages.js.map +1 -0
- package/lib/Utils/noise-handler.d.ts +20 -0
- package/lib/Utils/noise-handler.d.ts.map +1 -0
- package/lib/Utils/noise-handler.js +201 -0
- package/lib/Utils/noise-handler.js.map +1 -0
- package/lib/Utils/pre-key-manager.d.ts +28 -0
- package/lib/Utils/pre-key-manager.d.ts.map +1 -0
- package/lib/Utils/pre-key-manager.js +106 -0
- package/lib/Utils/pre-key-manager.js.map +1 -0
- package/lib/Utils/process-message.d.ts +60 -0
- package/lib/Utils/process-message.d.ts.map +1 -0
- package/lib/Utils/process-message.js +528 -0
- package/lib/Utils/process-message.js.map +1 -0
- package/lib/Utils/reporting-utils.d.ts +11 -0
- package/lib/Utils/reporting-utils.d.ts.map +1 -0
- package/lib/Utils/reporting-utils.js +258 -0
- package/lib/Utils/reporting-utils.js.map +1 -0
- package/lib/Utils/resolve-jid.d.ts +43 -0
- package/lib/Utils/resolve-jid.js +101 -0
- package/lib/Utils/signal.d.ts +34 -0
- package/lib/Utils/signal.d.ts.map +1 -0
- package/lib/Utils/signal.js +159 -0
- package/lib/Utils/signal.js.map +1 -0
- package/lib/Utils/streamToBuffer.js +17 -0
- package/lib/Utils/sync-action-utils.d.ts +19 -0
- package/lib/Utils/sync-action-utils.d.ts.map +1 -0
- package/lib/Utils/sync-action-utils.js +48 -0
- package/lib/Utils/sync-action-utils.js.map +1 -0
- package/lib/Utils/tc-token-utils.d.ts +12 -0
- package/lib/Utils/tc-token-utils.d.ts.map +1 -0
- package/lib/Utils/tc-token-utils.js +18 -0
- package/lib/Utils/tc-token-utils.js.map +1 -0
- package/lib/Utils/use-mongo-file-auth-state.js +77 -0
- package/lib/Utils/use-multi-file-auth-state.d.ts +13 -0
- package/lib/Utils/use-multi-file-auth-state.d.ts.map +1 -0
- package/lib/Utils/use-multi-file-auth-state.js +121 -0
- package/lib/Utils/use-multi-file-auth-state.js.map +1 -0
- package/lib/Utils/use-single-file-auth-state.js +74 -0
- package/lib/Utils/validate-connection.d.ts +11 -0
- package/lib/Utils/validate-connection.d.ts.map +1 -0
- package/lib/Utils/validate-connection.js +200 -0
- package/lib/Utils/validate-connection.js.map +1 -0
- package/lib/WABinary/constants.d.ts +28 -0
- package/lib/WABinary/constants.d.ts.map +1 -0
- package/lib/WABinary/constants.js +1301 -0
- package/lib/WABinary/constants.js.map +1 -0
- package/lib/WABinary/decode.d.ts +7 -0
- package/lib/WABinary/decode.d.ts.map +1 -0
- package/lib/WABinary/decode.js +262 -0
- package/lib/WABinary/decode.js.map +1 -0
- package/lib/WABinary/encode.d.ts +3 -0
- package/lib/WABinary/encode.d.ts.map +1 -0
- package/lib/WABinary/encode.js +220 -0
- package/lib/WABinary/encode.js.map +1 -0
- package/lib/WABinary/generic-utils.d.ts +15 -0
- package/lib/WABinary/generic-utils.d.ts.map +1 -0
- package/lib/WABinary/generic-utils.js +128 -0
- package/lib/WABinary/generic-utils.js.map +1 -0
- package/lib/WABinary/index.d.ts +6 -0
- package/lib/WABinary/index.d.ts.map +1 -0
- package/lib/WABinary/index.js +7 -0
- package/lib/WABinary/index.js.map +1 -0
- package/lib/WABinary/jid-utils.d.ts +48 -0
- package/lib/WABinary/jid-utils.d.ts.map +1 -0
- package/lib/WABinary/jid-utils.js +98 -0
- package/lib/WABinary/jid-utils.js.map +1 -0
- package/lib/WABinary/types.d.ts +19 -0
- package/lib/WABinary/types.d.ts.map +1 -0
- package/lib/WABinary/types.js +2 -0
- package/lib/WABinary/types.js.map +1 -0
- package/lib/WAM/BinaryInfo.d.ts +9 -0
- package/lib/WAM/BinaryInfo.d.ts.map +1 -0
- package/lib/WAM/BinaryInfo.js +10 -0
- package/lib/WAM/BinaryInfo.js.map +1 -0
- package/lib/WAM/constants.d.ts +40 -0
- package/lib/WAM/constants.d.ts.map +1 -0
- package/lib/WAM/constants.js +22853 -0
- package/lib/WAM/constants.js.map +1 -0
- package/lib/WAM/encode.d.ts +3 -0
- package/lib/WAM/encode.d.ts.map +1 -0
- package/lib/WAM/encode.js +150 -0
- package/lib/WAM/encode.js.map +1 -0
- package/lib/WAM/index.d.ts +4 -0
- package/lib/WAM/index.d.ts.map +1 -0
- package/lib/WAM/index.js +4 -0
- package/lib/WAM/index.js.map +1 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +10 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts.map +1 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +29 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js.map +1 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +23 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts.map +1 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +54 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js.map +1 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +13 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts.map +1 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +27 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js.map +1 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +13 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts.map +1 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +38 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js.map +1 -0
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +26 -0
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts.map +1 -0
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +51 -0
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js.map +1 -0
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +10 -0
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts.map +1 -0
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +29 -0
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js.map +1 -0
- package/lib/WAUSync/Protocols/index.d.ts +5 -0
- package/lib/WAUSync/Protocols/index.d.ts.map +1 -0
- package/lib/WAUSync/Protocols/index.js +6 -0
- package/lib/WAUSync/Protocols/index.js.map +1 -0
- package/lib/WAUSync/USyncQuery.d.ts +29 -0
- package/lib/WAUSync/USyncQuery.d.ts.map +1 -0
- package/lib/WAUSync/USyncQuery.js +94 -0
- package/lib/WAUSync/USyncQuery.js.map +1 -0
- package/lib/WAUSync/USyncUser.d.ts +13 -0
- package/lib/WAUSync/USyncUser.d.ts.map +1 -0
- package/lib/WAUSync/USyncUser.js +23 -0
- package/lib/WAUSync/USyncUser.js.map +1 -0
- package/lib/WAUSync/index.d.ts +4 -0
- package/lib/WAUSync/index.d.ts.map +1 -0
- package/lib/WAUSync/index.js +4 -0
- package/lib/WAUSync/index.js.map +1 -0
- package/lib/index.d.ts +12 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +31 -0
- package/lib/index.js.map +1 -0
- package/package.json +94 -0
|
@@ -0,0 +1,1699 @@
|
|
|
1
|
+
import { Boom } from '@hapi/boom';
|
|
2
|
+
import { randomBytes } from 'crypto';
|
|
3
|
+
import { promises as fs } from 'fs';
|
|
4
|
+
import {} from 'stream';
|
|
5
|
+
import { proto } from '../../WAProto/index.js';
|
|
6
|
+
import { CALL_AUDIO_PREFIX, CALL_VIDEO_PREFIX, MEDIA_KEYS, URL_REGEX, WA_DEFAULT_EPHEMERAL } from '../Defaults/index.js';
|
|
7
|
+
import { WAMessageStatus, WAProto } from '../Types/index.js';
|
|
8
|
+
import { isJidGroup, isJidNewsletter, isJidStatusBroadcast, jidNormalizedUser } from '../WABinary/index.js';
|
|
9
|
+
import { sha256 } from './crypto.js';
|
|
10
|
+
import { generateMessageIDV2, getKeyAuthor, unixTimestampSeconds } from './generics.js';
|
|
11
|
+
import { downloadContentFromMessage, encryptedStream, prepareStream, generateThumbnail, getAudioDuration, getAudioWaveform, getStream, toBuffer } from './messages-media.js';
|
|
12
|
+
import { createRequire } from 'module';
|
|
13
|
+
const _require = createRequire(import.meta.url);
|
|
14
|
+
import { shouldIncludeReportingToken } from './reporting-utils.js';
|
|
15
|
+
const MIMETYPE_MAP = {
|
|
16
|
+
image: 'image/jpeg',
|
|
17
|
+
video: 'video/mp4',
|
|
18
|
+
document: 'application/pdf',
|
|
19
|
+
audio: 'audio/ogg; codecs=opus',
|
|
20
|
+
sticker: 'image/webp',
|
|
21
|
+
'product-catalog-image': 'image/jpeg'
|
|
22
|
+
};
|
|
23
|
+
/** Map ekstensi audio ke mimetype */
|
|
24
|
+
const AUDIO_MIMETYPE_MAP = {
|
|
25
|
+
ogg: 'audio/ogg; codecs=opus',
|
|
26
|
+
oga: 'audio/ogg; codecs=opus',
|
|
27
|
+
opus: 'audio/ogg; codecs=opus',
|
|
28
|
+
mp3: 'audio/mpeg',
|
|
29
|
+
mpeg: 'audio/mpeg',
|
|
30
|
+
mp4: 'audio/mp4',
|
|
31
|
+
m4a: 'audio/mp4',
|
|
32
|
+
aac: 'audio/aac',
|
|
33
|
+
wav: 'audio/wav',
|
|
34
|
+
wave: 'audio/wav',
|
|
35
|
+
flac: 'audio/flac',
|
|
36
|
+
webm: 'audio/webm',
|
|
37
|
+
amr: 'audio/amr',
|
|
38
|
+
'3gp': 'audio/3gpp',
|
|
39
|
+
'3gpp': 'audio/3gpp',
|
|
40
|
+
wma: 'audio/x-ms-wma',
|
|
41
|
+
caf: 'audio/x-caf',
|
|
42
|
+
aiff: 'audio/aiff',
|
|
43
|
+
aif: 'audio/aiff',
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* Deteksi mimetype audio dari magic bytes buffer.
|
|
47
|
+
* Return null jika tidak dikenali.
|
|
48
|
+
*/
|
|
49
|
+
const detectAudioMimetypeFromBuffer = (buf) => {
|
|
50
|
+
if (!buf || buf.length < 12) return null;
|
|
51
|
+
// OGG
|
|
52
|
+
if (buf[0] === 0x4F && buf[1] === 0x67 && buf[2] === 0x67 && buf[3] === 0x53)
|
|
53
|
+
return 'audio/ogg; codecs=opus';
|
|
54
|
+
// MP3 (ID3 tag atau sync bits)
|
|
55
|
+
if ((buf[0] === 0x49 && buf[1] === 0x44 && buf[2] === 0x33) ||
|
|
56
|
+
(buf[0] === 0xFF && (buf[1] & 0xE0) === 0xE0))
|
|
57
|
+
return 'audio/mpeg';
|
|
58
|
+
// MP4/M4A (ftyp box)
|
|
59
|
+
if (buf[4] === 0x66 && buf[5] === 0x74 && buf[6] === 0x79 && buf[7] === 0x70)
|
|
60
|
+
return 'audio/mp4';
|
|
61
|
+
// RIFF/WAV
|
|
62
|
+
if (buf[0] === 0x52 && buf[1] === 0x49 && buf[2] === 0x46 && buf[3] === 0x46 &&
|
|
63
|
+
buf[8] === 0x57 && buf[9] === 0x41 && buf[10] === 0x56 && buf[11] === 0x45)
|
|
64
|
+
return 'audio/wav';
|
|
65
|
+
// FLAC
|
|
66
|
+
if (buf[0] === 0x66 && buf[1] === 0x4C && buf[2] === 0x61 && buf[3] === 0x43)
|
|
67
|
+
return 'audio/flac';
|
|
68
|
+
// WEBM/MKV
|
|
69
|
+
if (buf[0] === 0x1A && buf[1] === 0x45 && buf[2] === 0xDF && buf[3] === 0xA3)
|
|
70
|
+
return 'audio/webm';
|
|
71
|
+
// AMR
|
|
72
|
+
if (buf[0] === 0x23 && buf[1] === 0x21 && buf[2] === 0x41 && buf[3] === 0x4D &&
|
|
73
|
+
buf[4] === 0x52)
|
|
74
|
+
return 'audio/amr';
|
|
75
|
+
return null;
|
|
76
|
+
};
|
|
77
|
+
/**
|
|
78
|
+
* Deteksi mimetype audio secara otomatis dari media input.
|
|
79
|
+
* Cek: 1) ekstensi URL/path, 2) magic bytes buffer, 3) fallback ke ogg/opus.
|
|
80
|
+
*/
|
|
81
|
+
const detectAudioMimetype = async (media) => {
|
|
82
|
+
// Cek ekstensi dari URL atau path string
|
|
83
|
+
if (typeof media === 'string' || (media && typeof media === 'object' && 'url' in media)) {
|
|
84
|
+
const urlStr = typeof media === 'string' ? media : media.url?.toString?.() ?? '';
|
|
85
|
+
// Ambil path tanpa query string, lalu cari semua ekstensi
|
|
86
|
+
const pathOnly = urlStr.split('?')[0];
|
|
87
|
+
// Cek ekstensi terakhir (.m4a, .mp3, dst)
|
|
88
|
+
const extMatch = pathOnly.match(/\.([a-zA-Z0-9]{2,5})(?:[^/]*)?$/);
|
|
89
|
+
if (extMatch) {
|
|
90
|
+
const ext = extMatch[1].toLowerCase();
|
|
91
|
+
if (AUDIO_MIMETYPE_MAP[ext]) return AUDIO_MIMETYPE_MAP[ext];
|
|
92
|
+
}
|
|
93
|
+
// Fallback: scan semua segmen path untuk ekstensi audio yang dikenal
|
|
94
|
+
// Contoh: ".plus.aac.ep.m4a" → cek tiap segment dari belakang
|
|
95
|
+
const segments = pathOnly.split('.');
|
|
96
|
+
for (let i = segments.length - 1; i >= 0; i--) {
|
|
97
|
+
const seg = segments[i].toLowerCase().split('/')[0].split('?')[0];
|
|
98
|
+
if (AUDIO_MIMETYPE_MAP[seg]) return AUDIO_MIMETYPE_MAP[seg];
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
// Cek magic bytes jika Buffer
|
|
102
|
+
if (Buffer.isBuffer(media)) {
|
|
103
|
+
const detected = detectAudioMimetypeFromBuffer(media);
|
|
104
|
+
if (detected) return detected;
|
|
105
|
+
}
|
|
106
|
+
// Fallback: default ogg/opus
|
|
107
|
+
return MIMETYPE_MAP.audio;
|
|
108
|
+
};
|
|
109
|
+
const MessageTypeProto = {
|
|
110
|
+
image: WAProto.Message.ImageMessage,
|
|
111
|
+
video: WAProto.Message.VideoMessage,
|
|
112
|
+
audio: WAProto.Message.AudioMessage,
|
|
113
|
+
sticker: WAProto.Message.StickerMessage,
|
|
114
|
+
document: WAProto.Message.DocumentMessage
|
|
115
|
+
};
|
|
116
|
+
/**
|
|
117
|
+
* Uses a regex to test whether the string contains a URL, and returns the URL if it does.
|
|
118
|
+
* @param text eg. hello https://google.com
|
|
119
|
+
* @returns the URL, eg. https://google.com
|
|
120
|
+
*/
|
|
121
|
+
export const extractUrlFromText = (text) => text.match(URL_REGEX)?.[0];
|
|
122
|
+
export const generateLinkPreviewIfRequired = async (text, getUrlInfo, logger) => {
|
|
123
|
+
const url = extractUrlFromText(text);
|
|
124
|
+
if (!!getUrlInfo && url) {
|
|
125
|
+
try {
|
|
126
|
+
const urlInfo = await getUrlInfo(url);
|
|
127
|
+
return urlInfo;
|
|
128
|
+
}
|
|
129
|
+
catch (error) {
|
|
130
|
+
// ignore if fails
|
|
131
|
+
logger?.warn({ trace: error.stack }, 'url generation failed');
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
const assertColor = async (color) => {
|
|
136
|
+
let assertedColor;
|
|
137
|
+
if (typeof color === 'number') {
|
|
138
|
+
assertedColor = color > 0 ? color : 0xffffffff + Number(color) + 1;
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
let hex = color.trim().replace('#', '');
|
|
142
|
+
if (hex.length <= 6) {
|
|
143
|
+
hex = 'FF' + hex.padStart(6, '0');
|
|
144
|
+
}
|
|
145
|
+
assertedColor = parseInt(hex, 16);
|
|
146
|
+
return assertedColor;
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
export const prepareWAMessageMedia = async (message, options) => {
|
|
150
|
+
const logger = options.logger;
|
|
151
|
+
let mediaType;
|
|
152
|
+
for (const key of MEDIA_KEYS) {
|
|
153
|
+
if (key in message) {
|
|
154
|
+
mediaType = key;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
if (!mediaType) {
|
|
158
|
+
throw new Boom('Invalid media type', { statusCode: 400 });
|
|
159
|
+
}
|
|
160
|
+
const uploadData = {
|
|
161
|
+
...message,
|
|
162
|
+
media: message[mediaType]
|
|
163
|
+
};
|
|
164
|
+
delete uploadData[mediaType];
|
|
165
|
+
// check if cacheable + generate cache key
|
|
166
|
+
const cacheableKey = typeof uploadData.media === 'object' &&
|
|
167
|
+
'url' in uploadData.media &&
|
|
168
|
+
!!uploadData.media.url &&
|
|
169
|
+
!!options.mediaCache &&
|
|
170
|
+
mediaType + ':' + uploadData.media.url.toString();
|
|
171
|
+
if (mediaType === 'document' && !uploadData.fileName) {
|
|
172
|
+
uploadData.fileName = 'file';
|
|
173
|
+
}
|
|
174
|
+
if (!uploadData.mimetype) {
|
|
175
|
+
uploadData.mimetype = MIMETYPE_MAP[mediaType];
|
|
176
|
+
}
|
|
177
|
+
if (cacheableKey) {
|
|
178
|
+
const mediaBuff = await options.mediaCache.get(cacheableKey);
|
|
179
|
+
if (mediaBuff) {
|
|
180
|
+
logger?.debug({ cacheableKey }, 'got media cache hit');
|
|
181
|
+
const obj = proto.Message.decode(mediaBuff);
|
|
182
|
+
const key = `${mediaType}Message`;
|
|
183
|
+
Object.assign(obj[key], { ...uploadData, media: undefined });
|
|
184
|
+
return obj;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
const isNewsletter = !!options.jid && isJidNewsletter(options.jid);
|
|
188
|
+
if (isNewsletter) options.newsletter = true;
|
|
189
|
+
const requiresDurationComputation = mediaType === 'audio' && typeof uploadData.seconds === 'undefined';
|
|
190
|
+
const requiresThumbnailComputation = (mediaType === 'image' || mediaType === 'video') && typeof uploadData['jpegThumbnail'] === 'undefined';
|
|
191
|
+
const requiresWaveformProcessing = mediaType === 'audio' && (uploadData.ptt === true || !!options.backgroundColor);
|
|
192
|
+
const requiresAudioBackground = options.backgroundColor && mediaType === 'audio';
|
|
193
|
+
const requiresOriginalForSomeProcessing = requiresDurationComputation || requiresThumbnailComputation || requiresWaveformProcessing;
|
|
194
|
+
let streamResult;
|
|
195
|
+
try {
|
|
196
|
+
streamResult = await (options.newsletter ? prepareStream : encryptedStream)(
|
|
197
|
+
uploadData.media,
|
|
198
|
+
options.mediaTypeOverride || mediaType,
|
|
199
|
+
{
|
|
200
|
+
logger,
|
|
201
|
+
saveOriginalFileIfRequired: requiresOriginalForSomeProcessing,
|
|
202
|
+
opts: options.options,
|
|
203
|
+
isPtt: uploadData.ptt,
|
|
204
|
+
}
|
|
205
|
+
);
|
|
206
|
+
} catch (streamErr) {
|
|
207
|
+
throw streamErr;
|
|
208
|
+
}
|
|
209
|
+
const { mediaKey, encWriteStream, bodyPath, fileEncSha256, fileSha256, fileLength, didSaveToTmpPath } = streamResult;
|
|
210
|
+
|
|
211
|
+
const fileEncSha256B64 = (options.newsletter ? fileSha256 : fileEncSha256 ?? fileSha256).toString('base64');
|
|
212
|
+
|
|
213
|
+
const [{ mediaUrl, directPath, handle: uploadHandle }] = await Promise.all([
|
|
214
|
+
(async () => {
|
|
215
|
+
const result = await options.upload(encWriteStream, {
|
|
216
|
+
fileEncSha256B64,
|
|
217
|
+
mediaType,
|
|
218
|
+
timeoutMs: options.mediaUploadTimeoutMs,
|
|
219
|
+
newsletter: !!options.newsletter
|
|
220
|
+
});
|
|
221
|
+
return result;
|
|
222
|
+
})(),
|
|
223
|
+
(async () => {
|
|
224
|
+
try {
|
|
225
|
+
if (requiresThumbnailComputation) {
|
|
226
|
+
const { thumbnail, originalImageDimensions } = await generateThumbnail(bodyPath, mediaType, options);
|
|
227
|
+
uploadData.jpegThumbnail = thumbnail;
|
|
228
|
+
if (!uploadData.width && originalImageDimensions) {
|
|
229
|
+
uploadData.width = originalImageDimensions.width;
|
|
230
|
+
uploadData.height = originalImageDimensions.height;
|
|
231
|
+
logger?.debug('set dimensions');
|
|
232
|
+
}
|
|
233
|
+
logger?.debug('generated thumbnail');
|
|
234
|
+
}
|
|
235
|
+
if (requiresDurationComputation) {
|
|
236
|
+
try {
|
|
237
|
+
if (bodyPath) {
|
|
238
|
+
uploadData.seconds = await getAudioDuration(bodyPath, uploadData.mimetype);
|
|
239
|
+
}
|
|
240
|
+
} catch (err) {
|
|
241
|
+
uploadData.seconds = 0;
|
|
242
|
+
}
|
|
243
|
+
// Pastikan seconds valid — NaN/undefined bikin WhatsApp tampilkan Loading...
|
|
244
|
+
if (typeof uploadData.seconds !== 'number' || isNaN(uploadData.seconds)) {
|
|
245
|
+
uploadData.seconds = 0;
|
|
246
|
+
}
|
|
247
|
+
logger?.debug('computed audio duration');
|
|
248
|
+
}
|
|
249
|
+
if (requiresWaveformProcessing) {
|
|
250
|
+
try {
|
|
251
|
+
uploadData.waveform = await getAudioWaveform(bodyPath || encWriteStream, logger);
|
|
252
|
+
} catch (err) {
|
|
253
|
+
}
|
|
254
|
+
if (!uploadData.waveform) {
|
|
255
|
+
uploadData.waveform = new Uint8Array([0,99,0,99,0,99,0,99,88,99,0,99,0,55,0,99,0,99,0,99,0,99,0,99,88,99,0,99,0,55,0,99]);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
if (requiresAudioBackground) {
|
|
259
|
+
uploadData.backgroundArgb = await assertColor(options.backgroundColor);
|
|
260
|
+
logger?.debug('computed backgroundColor audio status');
|
|
261
|
+
}
|
|
262
|
+
} catch (error) {
|
|
263
|
+
logger?.warn({ trace: error.stack }, 'failed to obtain extra info');
|
|
264
|
+
}
|
|
265
|
+
})()
|
|
266
|
+
]).finally(async () => {
|
|
267
|
+
try {
|
|
268
|
+
if (!Buffer.isBuffer(encWriteStream)) {
|
|
269
|
+
encWriteStream.destroy?.();
|
|
270
|
+
}
|
|
271
|
+
if (didSaveToTmpPath && bodyPath) {
|
|
272
|
+
await fs.unlink(bodyPath).catch(() => {});
|
|
273
|
+
}
|
|
274
|
+
} catch (error) {
|
|
275
|
+
logger?.warn('failed to remove tmp file');
|
|
276
|
+
}
|
|
277
|
+
});
|
|
278
|
+
const obj = WAProto.Message.fromObject({
|
|
279
|
+
[`${mediaType}Message`]: MessageTypeProto[mediaType].fromObject({
|
|
280
|
+
url: uploadHandle ? undefined : mediaUrl,
|
|
281
|
+
directPath,
|
|
282
|
+
mediaKey: mediaKey,
|
|
283
|
+
fileEncSha256: fileEncSha256,
|
|
284
|
+
fileSha256,
|
|
285
|
+
fileLength,
|
|
286
|
+
mediaKeyTimestamp: uploadHandle ? undefined : unixTimestampSeconds(),
|
|
287
|
+
...uploadData,
|
|
288
|
+
media: undefined,
|
|
289
|
+
...(options?.contextInfo ? { contextInfo: options.contextInfo } : {})
|
|
290
|
+
})
|
|
291
|
+
});
|
|
292
|
+
if (uploadData.ptv) {
|
|
293
|
+
obj.ptvMessage = obj.videoMessage;
|
|
294
|
+
delete obj.videoMessage;
|
|
295
|
+
}
|
|
296
|
+
// Attach uploadHandle so sendMessage can use it as media_id
|
|
297
|
+
if (uploadHandle) {
|
|
298
|
+
obj._uploadHandle = uploadHandle;
|
|
299
|
+
}
|
|
300
|
+
if (mediaType === 'audio') {
|
|
301
|
+
}
|
|
302
|
+
if (cacheableKey) {
|
|
303
|
+
logger?.debug({ cacheableKey }, 'set cache');
|
|
304
|
+
await options.mediaCache.set(cacheableKey, WAProto.Message.encode(obj).finish());
|
|
305
|
+
}
|
|
306
|
+
return obj;
|
|
307
|
+
};
|
|
308
|
+
export const prepareDisappearingMessageSettingContent = (ephemeralExpiration) => {
|
|
309
|
+
ephemeralExpiration = ephemeralExpiration || 0;
|
|
310
|
+
const content = {
|
|
311
|
+
ephemeralMessage: {
|
|
312
|
+
message: {
|
|
313
|
+
protocolMessage: {
|
|
314
|
+
type: WAProto.Message.ProtocolMessage.Type.EPHEMERAL_SETTING,
|
|
315
|
+
ephemeralExpiration
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
};
|
|
320
|
+
return WAProto.Message.fromObject(content);
|
|
321
|
+
};
|
|
322
|
+
/**
|
|
323
|
+
* Generate forwarded message content like WA does
|
|
324
|
+
* @param message the message to forward
|
|
325
|
+
* @param options.forceForward will show the message as forwarded even if it is from you
|
|
326
|
+
*/
|
|
327
|
+
export const generateForwardMessageContent = (message, forceForward) => {
|
|
328
|
+
let content = message.message;
|
|
329
|
+
if (!content) {
|
|
330
|
+
throw new Boom('no content in message', { statusCode: 400 });
|
|
331
|
+
}
|
|
332
|
+
// hacky copy
|
|
333
|
+
content = normalizeMessageContent(content);
|
|
334
|
+
content = proto.Message.decode(proto.Message.encode(content).finish());
|
|
335
|
+
let key = Object.keys(content)[0];
|
|
336
|
+
let score = content?.[key]?.contextInfo?.forwardingScore || 0;
|
|
337
|
+
score += message.key.fromMe && !forceForward ? 0 : 1;
|
|
338
|
+
if (key === 'conversation') {
|
|
339
|
+
content.extendedTextMessage = { text: content[key] };
|
|
340
|
+
delete content.conversation;
|
|
341
|
+
key = 'extendedTextMessage';
|
|
342
|
+
}
|
|
343
|
+
const key_ = content?.[key];
|
|
344
|
+
if (score > 0) {
|
|
345
|
+
key_.contextInfo = { forwardingScore: score, isForwarded: true };
|
|
346
|
+
}
|
|
347
|
+
else {
|
|
348
|
+
key_.contextInfo = {};
|
|
349
|
+
}
|
|
350
|
+
return content;
|
|
351
|
+
};
|
|
352
|
+
export const hasNonNullishProperty = (message, key) => {
|
|
353
|
+
return (typeof message === 'object' &&
|
|
354
|
+
message !== null &&
|
|
355
|
+
key in message &&
|
|
356
|
+
message[key] !== null &&
|
|
357
|
+
message[key] !== undefined);
|
|
358
|
+
};
|
|
359
|
+
function hasOptionalProperty(obj, key) {
|
|
360
|
+
return typeof obj === 'object' && obj !== null && key in obj && obj[key] !== null;
|
|
361
|
+
}
|
|
362
|
+
export const generateWAMessageContent = async (message, options) => {
|
|
363
|
+
var _a, _b;
|
|
364
|
+
let m = {};
|
|
365
|
+
if (hasNonNullishProperty(message, 'text')) {
|
|
366
|
+
const extContent = { text: message.text };
|
|
367
|
+
let urlInfo = message.linkPreview;
|
|
368
|
+
if (typeof urlInfo === 'undefined') {
|
|
369
|
+
urlInfo = await generateLinkPreviewIfRequired(message.text, options.getUrlInfo, options.logger);
|
|
370
|
+
}
|
|
371
|
+
if (urlInfo) {
|
|
372
|
+
extContent.matchedText = urlInfo['matched-text'];
|
|
373
|
+
extContent.jpegThumbnail = urlInfo.jpegThumbnail;
|
|
374
|
+
extContent.description = urlInfo.description;
|
|
375
|
+
extContent.title = urlInfo.title;
|
|
376
|
+
extContent.previewType = 0;
|
|
377
|
+
const img = urlInfo.highQualityThumbnail;
|
|
378
|
+
if (img) {
|
|
379
|
+
extContent.thumbnailDirectPath = img.directPath;
|
|
380
|
+
extContent.mediaKey = img.mediaKey;
|
|
381
|
+
extContent.mediaKeyTimestamp = img.mediaKeyTimestamp;
|
|
382
|
+
extContent.thumbnailWidth = img.width;
|
|
383
|
+
extContent.thumbnailHeight = img.height;
|
|
384
|
+
extContent.thumbnailSha256 = img.fileSha256;
|
|
385
|
+
extContent.thumbnailEncSha256 = img.fileEncSha256;
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
if (options.backgroundColor) {
|
|
389
|
+
extContent.backgroundArgb = await assertColor(options.backgroundColor);
|
|
390
|
+
}
|
|
391
|
+
if (options.font) {
|
|
392
|
+
extContent.font = options.font;
|
|
393
|
+
}
|
|
394
|
+
m.extendedTextMessage = extContent;
|
|
395
|
+
}
|
|
396
|
+
else if (hasNonNullishProperty(message, 'contacts')) {
|
|
397
|
+
const contactLen = message.contacts.contacts.length;
|
|
398
|
+
if (!contactLen) {
|
|
399
|
+
throw new Boom('require atleast 1 contact', { statusCode: 400 });
|
|
400
|
+
}
|
|
401
|
+
if (contactLen === 1) {
|
|
402
|
+
m.contactMessage = WAProto.Message.ContactMessage.create(message.contacts.contacts[0]);
|
|
403
|
+
}
|
|
404
|
+
else {
|
|
405
|
+
m.contactsArrayMessage = WAProto.Message.ContactsArrayMessage.create(message.contacts);
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
else if (hasNonNullishProperty(message, 'location')) {
|
|
409
|
+
if (message.live) {
|
|
410
|
+
m.liveLocationMessage = WAProto.Message.LiveLocationMessage.create(message.location);
|
|
411
|
+
} else {
|
|
412
|
+
m.locationMessage = WAProto.Message.LocationMessage.create(message.location);
|
|
413
|
+
}
|
|
414
|
+
const locType = message.live ? 'liveLocationMessage' : 'locationMessage';
|
|
415
|
+
if (m[locType]) {
|
|
416
|
+
m[locType].contextInfo = {
|
|
417
|
+
...(message.contextInfo || {}),
|
|
418
|
+
...(message.mentions ? { mentionedJid: message.mentions } : {})
|
|
419
|
+
};
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
else if (hasNonNullishProperty(message, 'react')) {
|
|
423
|
+
if (!message.react.senderTimestampMs) {
|
|
424
|
+
message.react.senderTimestampMs = Date.now();
|
|
425
|
+
}
|
|
426
|
+
m.reactionMessage = WAProto.Message.ReactionMessage.create(message.react);
|
|
427
|
+
}
|
|
428
|
+
else if (hasNonNullishProperty(message, 'delete')) {
|
|
429
|
+
m.protocolMessage = {
|
|
430
|
+
key: message.delete,
|
|
431
|
+
type: WAProto.Message.ProtocolMessage.Type.REVOKE
|
|
432
|
+
};
|
|
433
|
+
}
|
|
434
|
+
else if (hasNonNullishProperty(message, 'forward')) {
|
|
435
|
+
m = generateForwardMessageContent(message.forward, message.force);
|
|
436
|
+
}
|
|
437
|
+
else if (hasNonNullishProperty(message, 'disappearingMessagesInChat')) {
|
|
438
|
+
const exp = typeof message.disappearingMessagesInChat === 'boolean'
|
|
439
|
+
? message.disappearingMessagesInChat
|
|
440
|
+
? WA_DEFAULT_EPHEMERAL
|
|
441
|
+
: 0
|
|
442
|
+
: message.disappearingMessagesInChat;
|
|
443
|
+
m = prepareDisappearingMessageSettingContent(exp);
|
|
444
|
+
}
|
|
445
|
+
else if (hasNonNullishProperty(message, 'groupInvite')) {
|
|
446
|
+
m.groupInviteMessage = {};
|
|
447
|
+
m.groupInviteMessage.inviteCode = message.groupInvite.inviteCode;
|
|
448
|
+
m.groupInviteMessage.inviteExpiration = message.groupInvite.inviteExpiration;
|
|
449
|
+
m.groupInviteMessage.caption = message.groupInvite.text;
|
|
450
|
+
m.groupInviteMessage.groupJid = message.groupInvite.jid;
|
|
451
|
+
m.groupInviteMessage.groupName = message.groupInvite.subject;
|
|
452
|
+
//TODO: use built-in interface and get disappearing mode info etc.
|
|
453
|
+
//TODO: cache / use store!?
|
|
454
|
+
if (options.getProfilePicUrl) {
|
|
455
|
+
const pfpUrl = await options.getProfilePicUrl(message.groupInvite.jid, 'preview');
|
|
456
|
+
if (pfpUrl) {
|
|
457
|
+
const resp = await fetch(pfpUrl, { method: 'GET', dispatcher: options?.options?.dispatcher });
|
|
458
|
+
if (resp.ok) {
|
|
459
|
+
const buf = Buffer.from(await resp.arrayBuffer());
|
|
460
|
+
m.groupInviteMessage.jpegThumbnail = buf;
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
else if (hasNonNullishProperty(message, 'pin')) {
|
|
466
|
+
m.pinInChatMessage = {};
|
|
467
|
+
m.messageContextInfo = {};
|
|
468
|
+
m.pinInChatMessage.key = message.pin.key;
|
|
469
|
+
m.pinInChatMessage.type = message.pin?.type || 1;
|
|
470
|
+
m.pinInChatMessage.senderTimestampMs = message.pin?.time || Date.now();
|
|
471
|
+
m.messageContextInfo.messageAddOnDurationInSecs = message.pin.type === 1 ? message.pin.time || 86400 : 0;
|
|
472
|
+
m.messageContextInfo.messageAddOnExpiryType = proto.MessageContextInfo.MessageAddonExpiryType.STATIC;
|
|
473
|
+
}
|
|
474
|
+
else if (hasNonNullishProperty(message, 'keep')) {
|
|
475
|
+
m.keepInChatMessage = {};
|
|
476
|
+
m.keepInChatMessage.key = message.keep.key;
|
|
477
|
+
m.keepInChatMessage.keepType = message.keep?.type || 1;
|
|
478
|
+
m.keepInChatMessage.timestampMs = message.keep?.time || Date.now();
|
|
479
|
+
}
|
|
480
|
+
else if (hasNonNullishProperty(message, 'call')) {
|
|
481
|
+
m.scheduledCallCreationMessage = {};
|
|
482
|
+
m.scheduledCallCreationMessage.scheduledTimestampMs = message.call?.time || Date.now();
|
|
483
|
+
m.scheduledCallCreationMessage.callType = message.call?.type || 1;
|
|
484
|
+
m.scheduledCallCreationMessage.title = message.call?.name || 'Call Creation';
|
|
485
|
+
m.scheduledCallCreationMessage.contextInfo = {
|
|
486
|
+
...(message.contextInfo || {}),
|
|
487
|
+
...(message.mentions ? { mentionedJid: message.mentions } : {})
|
|
488
|
+
};
|
|
489
|
+
}
|
|
490
|
+
else if (hasNonNullishProperty(message, 'paymentInvite')) {
|
|
491
|
+
m.messageContextInfo = {};
|
|
492
|
+
m.paymentInviteMessage = {};
|
|
493
|
+
m.paymentInviteMessage.expiryTimestamp = message.paymentInvite?.expiry || 0;
|
|
494
|
+
m.paymentInviteMessage.serviceType = message.paymentInvite?.type || 2;
|
|
495
|
+
m.paymentInviteMessage.contextInfo = {
|
|
496
|
+
...(message.contextInfo || {}),
|
|
497
|
+
...(message.mentions ? { mentionedJid: message.mentions } : {})
|
|
498
|
+
};
|
|
499
|
+
}
|
|
500
|
+
else if (hasNonNullishProperty(message, 'buttonReply')) {
|
|
501
|
+
switch (message.type) {
|
|
502
|
+
case 'list':
|
|
503
|
+
m.listResponseMessage = {
|
|
504
|
+
title: message.buttonReply.title,
|
|
505
|
+
description: message.buttonReply.description,
|
|
506
|
+
singleSelectReply: {
|
|
507
|
+
selectedRowId: message.buttonReply.rowId
|
|
508
|
+
},
|
|
509
|
+
listType: proto.Message.ListResponseMessage.ListType.SINGLE_SELECT
|
|
510
|
+
};
|
|
511
|
+
break;
|
|
512
|
+
case 'template':
|
|
513
|
+
m.templateButtonReplyMessage = {
|
|
514
|
+
selectedDisplayText: message.buttonReply.displayText,
|
|
515
|
+
selectedId: message.buttonReply.id,
|
|
516
|
+
selectedIndex: message.buttonReply.index
|
|
517
|
+
};
|
|
518
|
+
break;
|
|
519
|
+
case 'plain':
|
|
520
|
+
m.buttonsResponseMessage = {
|
|
521
|
+
selectedButtonId: message.buttonReply.id,
|
|
522
|
+
selectedDisplayText: message.buttonReply.displayText,
|
|
523
|
+
type: proto.Message.ButtonsResponseMessage.Type.DISPLAY_TEXT
|
|
524
|
+
};
|
|
525
|
+
break;
|
|
526
|
+
case 'interactive':
|
|
527
|
+
m.interactiveResponseMessage = {
|
|
528
|
+
body: {
|
|
529
|
+
text: message.buttonReply.displayText,
|
|
530
|
+
format: proto.Message.InteractiveResponseMessage.Body.Format.EXTENSIONS_1
|
|
531
|
+
},
|
|
532
|
+
nativeFlowResponseMessage: {
|
|
533
|
+
name: message.buttonReply.nativeFlows.name,
|
|
534
|
+
paramsJson: message.buttonReply.nativeFlows.paramsJson,
|
|
535
|
+
version: message.buttonReply.nativeFlows.version
|
|
536
|
+
}
|
|
537
|
+
};
|
|
538
|
+
break;
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
else if (hasNonNullishProperty(message, 'album')) {
|
|
542
|
+
const imageMessages = message.album.filter(item => 'image' in item);
|
|
543
|
+
const videoMessages = message.album.filter(item => 'video' in item);
|
|
544
|
+
m.albumMessage = WAProto.Message.AlbumMessage.fromObject({
|
|
545
|
+
expectedImageCount: imageMessages.length,
|
|
546
|
+
expectedVideoCount: videoMessages.length
|
|
547
|
+
});
|
|
548
|
+
}
|
|
549
|
+
else if (hasNonNullishProperty(message, 'order')) {
|
|
550
|
+
m.orderMessage = WAProto.Message.OrderMessage.fromObject(message.order);
|
|
551
|
+
m.orderMessage.contextInfo = {
|
|
552
|
+
...(message.contextInfo || {}),
|
|
553
|
+
...(message.mentions ? { mentionedJid: message.mentions } : {})
|
|
554
|
+
};
|
|
555
|
+
}
|
|
556
|
+
else if (hasNonNullishProperty(message, 'payment')) {
|
|
557
|
+
const requestPaymentMessage = {
|
|
558
|
+
amount: {
|
|
559
|
+
currencyCode: message.payment?.currency || 'IDR',
|
|
560
|
+
offset: message.payment?.offset || 0,
|
|
561
|
+
value: message.payment?.amount || 999999999
|
|
562
|
+
},
|
|
563
|
+
expiryTimestamp: message.payment?.expiry || 0,
|
|
564
|
+
amount1000: (message.payment?.amount || 999999999) * 1000,
|
|
565
|
+
currencyCodeIso4217: message.payment?.currency || 'IDR',
|
|
566
|
+
requestFrom: message.payment?.from || '0@s.whatsapp.net',
|
|
567
|
+
noteMessage: {
|
|
568
|
+
extendedTextMessage: {
|
|
569
|
+
text: message.payment?.note || 'Notes'
|
|
570
|
+
}
|
|
571
|
+
},
|
|
572
|
+
background: {
|
|
573
|
+
placeholderArgb: message.payment?.image?.placeholderArgb || 4278190080,
|
|
574
|
+
textArgb: message.payment?.image?.textArgb || 4294967295,
|
|
575
|
+
subtextArgb: message.payment?.image?.subtextArgb || 4294967295,
|
|
576
|
+
type: 1
|
|
577
|
+
}
|
|
578
|
+
};
|
|
579
|
+
requestPaymentMessage.noteMessage.extendedTextMessage.contextInfo = {
|
|
580
|
+
...(message.contextInfo || {}),
|
|
581
|
+
...(message.mentions ? { mentionedJid: message.mentions } : {})
|
|
582
|
+
};
|
|
583
|
+
m.requestPaymentMessage = requestPaymentMessage;
|
|
584
|
+
}
|
|
585
|
+
else if (hasNonNullishProperty(message, 'pollResult')) {
|
|
586
|
+
if (!Array.isArray(message.pollResult.values)) {
|
|
587
|
+
throw new Boom('Invalid pollResult values', { statusCode: 400 });
|
|
588
|
+
}
|
|
589
|
+
const pollResultSnapshotMessage = {
|
|
590
|
+
name: message.pollResult.name,
|
|
591
|
+
pollVotes: message.pollResult.values.map(([optionName, optionVoteCount]) => ({
|
|
592
|
+
optionName,
|
|
593
|
+
optionVoteCount
|
|
594
|
+
}))
|
|
595
|
+
};
|
|
596
|
+
pollResultSnapshotMessage.contextInfo = {
|
|
597
|
+
...(message.contextInfo || {}),
|
|
598
|
+
...(message.mentions ? { mentionedJid: message.mentions } : {})
|
|
599
|
+
};
|
|
600
|
+
m.pollResultSnapshotMessage = pollResultSnapshotMessage;
|
|
601
|
+
}
|
|
602
|
+
else if (hasOptionalProperty(message, 'ptv') && message.ptv) {
|
|
603
|
+
const { videoMessage } = await prepareWAMessageMedia({ video: message.video }, options);
|
|
604
|
+
m.ptvMessage = videoMessage;
|
|
605
|
+
}
|
|
606
|
+
else if (hasNonNullishProperty(message, 'product')) {
|
|
607
|
+
const { imageMessage } = await prepareWAMessageMedia({ image: message.product.productImage }, options);
|
|
608
|
+
m.productMessage = WAProto.Message.ProductMessage.create({
|
|
609
|
+
...message,
|
|
610
|
+
product: {
|
|
611
|
+
...message.product,
|
|
612
|
+
productImage: imageMessage
|
|
613
|
+
}
|
|
614
|
+
});
|
|
615
|
+
}
|
|
616
|
+
else if (hasNonNullishProperty(message, 'listReply')) {
|
|
617
|
+
m.listResponseMessage = { ...message.listReply };
|
|
618
|
+
}
|
|
619
|
+
else if (hasNonNullishProperty(message, 'event')) {
|
|
620
|
+
m.eventMessage = {};
|
|
621
|
+
const startTime = Math.floor(message.event.startDate.getTime() / 1000);
|
|
622
|
+
if (message.event.call && options.getCallLink) {
|
|
623
|
+
const token = await options.getCallLink(message.event.call, { startTime });
|
|
624
|
+
m.eventMessage.joinLink = (message.event.call === 'audio' ? CALL_AUDIO_PREFIX : CALL_VIDEO_PREFIX) + token;
|
|
625
|
+
}
|
|
626
|
+
m.messageContextInfo = {
|
|
627
|
+
// encKey
|
|
628
|
+
messageSecret: message.event.messageSecret || randomBytes(32)
|
|
629
|
+
};
|
|
630
|
+
m.eventMessage.name = message.event.name;
|
|
631
|
+
m.eventMessage.description = message.event.description;
|
|
632
|
+
m.eventMessage.startTime = startTime;
|
|
633
|
+
m.eventMessage.endTime = message.event.endDate ? message.event.endDate.getTime() / 1000 : undefined;
|
|
634
|
+
m.eventMessage.isCanceled = message.event.isCancelled ?? false;
|
|
635
|
+
m.eventMessage.extraGuestsAllowed = message.event.extraGuestsAllowed;
|
|
636
|
+
m.eventMessage.isScheduleCall = message.event.isScheduleCall ?? false;
|
|
637
|
+
m.eventMessage.location = message.event.location;
|
|
638
|
+
}
|
|
639
|
+
else if (hasNonNullishProperty(message, 'poll')) {
|
|
640
|
+
(_a = message.poll).selectableCount || (_a.selectableCount = 0);
|
|
641
|
+
(_b = message.poll).toAnnouncementGroup || (_b.toAnnouncementGroup = false);
|
|
642
|
+
if (!Array.isArray(message.poll.values)) {
|
|
643
|
+
throw new Boom('Invalid poll values', { statusCode: 400 });
|
|
644
|
+
}
|
|
645
|
+
if (message.poll.selectableCount < 0 || message.poll.selectableCount > message.poll.values.length) {
|
|
646
|
+
throw new Boom(`poll.selectableCount in poll should be >= 0 and <= ${message.poll.values.length}`, {
|
|
647
|
+
statusCode: 400
|
|
648
|
+
});
|
|
649
|
+
}
|
|
650
|
+
m.messageContextInfo = {
|
|
651
|
+
// encKey
|
|
652
|
+
messageSecret: message.poll.messageSecret || randomBytes(32)
|
|
653
|
+
};
|
|
654
|
+
const pollCreationMessage = {
|
|
655
|
+
name: message.poll.name,
|
|
656
|
+
selectableOptionsCount: message.poll.selectableCount,
|
|
657
|
+
options: message.poll.values.map(optionName => ({ optionName }))
|
|
658
|
+
};
|
|
659
|
+
if (message.poll.toAnnouncementGroup) {
|
|
660
|
+
// poll v2 is for community announcement groups (single select and multiple)
|
|
661
|
+
m.pollCreationMessageV2 = pollCreationMessage;
|
|
662
|
+
}
|
|
663
|
+
else {
|
|
664
|
+
if (message.poll.selectableCount === 1) {
|
|
665
|
+
//poll v3 is for single select polls
|
|
666
|
+
m.pollCreationMessageV3 = pollCreationMessage;
|
|
667
|
+
}
|
|
668
|
+
else {
|
|
669
|
+
// poll for multiple choice polls
|
|
670
|
+
m.pollCreationMessage = pollCreationMessage;
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
else if (hasNonNullishProperty(message, 'stickerPack')) {
|
|
675
|
+
const { zip } = _require('fflate');
|
|
676
|
+
const { stickers, cover, name, publisher, packId, description } = message.stickerPack;
|
|
677
|
+
|
|
678
|
+
// ── Validasi jumlah sticker ───────────────────────────────────────────
|
|
679
|
+
if (stickers.length > 60) {
|
|
680
|
+
throw new Boom('Sticker pack exceeds the maximum limit of 60 stickers', { statusCode: 400 });
|
|
681
|
+
}
|
|
682
|
+
if (stickers.length === 0) {
|
|
683
|
+
throw new Boom('Sticker pack must contain at least one sticker', { statusCode: 400 });
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
const stickerPackId = packId || generateMessageIDV2();
|
|
687
|
+
const [_sharp, _jimp] = await Promise.all([import('sharp').catch(() => null), import('jimp').catch(() => null)]);
|
|
688
|
+
const lib = _sharp ? { sharp: _sharp } : _jimp ? { jimp: _jimp } : null;
|
|
689
|
+
if (!lib) throw new Boom('No image processing library available (install sharp or jimp)');
|
|
690
|
+
|
|
691
|
+
// ── Helper: deteksi WebP dari magic bytes ─────────────────────────────
|
|
692
|
+
const isWebPBuffer = (buf) => (
|
|
693
|
+
buf.length >= 12 &&
|
|
694
|
+
buf[0] === 0x52 && buf[1] === 0x49 && buf[2] === 0x46 && buf[3] === 0x46 &&
|
|
695
|
+
buf[8] === 0x57 && buf[9] === 0x45 && buf[10] === 0x42 && buf[11] === 0x50
|
|
696
|
+
);
|
|
697
|
+
|
|
698
|
+
// ── Helper: deteksi animasi WebP (VP8X/ANIM/ANMF chunks) ─────────────
|
|
699
|
+
const isAnimatedWebP = (buf) => {
|
|
700
|
+
if (!isWebPBuffer(buf)) return false;
|
|
701
|
+
let offset = 12;
|
|
702
|
+
while (offset < buf.length - 8) {
|
|
703
|
+
const fourCC = buf.toString('ascii', offset, offset + 4);
|
|
704
|
+
const chunkSize = buf.readUInt32LE(offset + 4);
|
|
705
|
+
if (fourCC === 'VP8X') {
|
|
706
|
+
const flagsOffset = offset + 8;
|
|
707
|
+
if (flagsOffset < buf.length && (buf[flagsOffset] & 0x02)) return true;
|
|
708
|
+
} else if (fourCC === 'ANIM' || fourCC === 'ANMF') {
|
|
709
|
+
return true;
|
|
710
|
+
}
|
|
711
|
+
offset += 8 + chunkSize + (chunkSize % 2);
|
|
712
|
+
}
|
|
713
|
+
return false;
|
|
714
|
+
};
|
|
715
|
+
|
|
716
|
+
// ── Step 1: proses & zip semua sticker ────────────────────────────────
|
|
717
|
+
const stickerData = {};
|
|
718
|
+
const stickerPromises = stickers.map(async (s, i) => {
|
|
719
|
+
const { stream } = await getStream(s.data || s.sticker);
|
|
720
|
+
const buffer = await toBuffer(stream);
|
|
721
|
+
|
|
722
|
+
let webpBuffer;
|
|
723
|
+
let isAnimated = false;
|
|
724
|
+
if (isWebPBuffer(buffer)) {
|
|
725
|
+
webpBuffer = buffer;
|
|
726
|
+
isAnimated = isAnimatedWebP(buffer);
|
|
727
|
+
} else if ('sharp' in lib && lib.sharp) {
|
|
728
|
+
webpBuffer = await lib.sharp.default(buffer).webp().toBuffer();
|
|
729
|
+
} else {
|
|
730
|
+
throw new Boom(
|
|
731
|
+
'No image processing library (sharp) available for converting sticker to WebP. ' +
|
|
732
|
+
'Either install sharp or provide stickers in WebP format.'
|
|
733
|
+
);
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
if (webpBuffer.length > 1024 * 1024) {
|
|
737
|
+
throw new Boom(`Sticker at index ${i} exceeds the 1MB size limit`, { statusCode: 400 });
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
const hash = sha256(webpBuffer).toString('base64').replace(/\//g, '-');
|
|
741
|
+
const fileName = `${hash}.webp`;
|
|
742
|
+
stickerData[fileName] = [new Uint8Array(webpBuffer), { level: 0 }];
|
|
743
|
+
return {
|
|
744
|
+
fileName,
|
|
745
|
+
mimetype: 'image/webp',
|
|
746
|
+
isAnimated,
|
|
747
|
+
emojis: s.emojis || [],
|
|
748
|
+
accessibilityLabel: s.accessibilityLabel || ''
|
|
749
|
+
};
|
|
750
|
+
});
|
|
751
|
+
const stickerMetadata = await Promise.all(stickerPromises);
|
|
752
|
+
|
|
753
|
+
// ── Step 2: proses cover & masukkan ke dalam ZIP ──────────────────────
|
|
754
|
+
const trayIconFileName = `${stickerPackId}.webp`;
|
|
755
|
+
const coverBuffer = await toBuffer((await getStream(cover)).stream);
|
|
756
|
+
|
|
757
|
+
let coverWebpBuffer;
|
|
758
|
+
if (isWebPBuffer(coverBuffer)) {
|
|
759
|
+
coverWebpBuffer = coverBuffer;
|
|
760
|
+
} else if ('sharp' in lib && lib.sharp) {
|
|
761
|
+
coverWebpBuffer = await lib.sharp.default(coverBuffer).webp().toBuffer();
|
|
762
|
+
} else {
|
|
763
|
+
throw new Boom(
|
|
764
|
+
'No image processing library (sharp) available for converting cover to WebP. ' +
|
|
765
|
+
'Either install sharp or provide cover in WebP format.'
|
|
766
|
+
);
|
|
767
|
+
}
|
|
768
|
+
stickerData[trayIconFileName] = [new Uint8Array(coverWebpBuffer), { level: 0 }];
|
|
769
|
+
|
|
770
|
+
// ── Step 3: buat ZIP buffer ───────────────────────────────────────────
|
|
771
|
+
const zipBuffer = await new Promise((resolve, reject) => {
|
|
772
|
+
zip(stickerData, (err, data) => {
|
|
773
|
+
if (err) reject(err);
|
|
774
|
+
else resolve(Buffer.from(data));
|
|
775
|
+
});
|
|
776
|
+
});
|
|
777
|
+
|
|
778
|
+
// ── Step 4: encrypt ZIP (generate random mediaKey) ────────────────────
|
|
779
|
+
const stickerPackUpload = await encryptedStream(zipBuffer, 'sticker-pack', {
|
|
780
|
+
logger: options.logger,
|
|
781
|
+
opts: options.options
|
|
782
|
+
});
|
|
783
|
+
|
|
784
|
+
// ── Step 5: upload ZIP ────────────────────────────────────────────────
|
|
785
|
+
const stickerPackUploadResult = await options.upload(stickerPackUpload.encWriteStream, {
|
|
786
|
+
fileEncSha256B64: stickerPackUpload.fileEncSha256.toString('base64'),
|
|
787
|
+
mediaType: 'sticker-pack',
|
|
788
|
+
timeoutMs: options.mediaUploadTimeoutMs
|
|
789
|
+
});
|
|
790
|
+
|
|
791
|
+
// ── Step 6: build stickerPackMessage ──────────────────────────────────
|
|
792
|
+
m.stickerPackMessage = {
|
|
793
|
+
name,
|
|
794
|
+
publisher,
|
|
795
|
+
stickerPackId,
|
|
796
|
+
packDescription: description,
|
|
797
|
+
stickerPackOrigin: WAProto.Message.StickerPackMessage.StickerPackOrigin.THIRD_PARTY,
|
|
798
|
+
stickerPackSize: zipBuffer.length,
|
|
799
|
+
stickers: stickerMetadata,
|
|
800
|
+
fileSha256: stickerPackUpload.fileSha256,
|
|
801
|
+
fileEncSha256: stickerPackUpload.fileEncSha256,
|
|
802
|
+
mediaKey: stickerPackUpload.mediaKey,
|
|
803
|
+
directPath: stickerPackUploadResult.directPath,
|
|
804
|
+
fileLength: stickerPackUpload.fileLength,
|
|
805
|
+
mediaKeyTimestamp: unixTimestampSeconds(),
|
|
806
|
+
trayIconFileName
|
|
807
|
+
};
|
|
808
|
+
|
|
809
|
+
// ── Step 7: generate & upload thumbnail (pakai mediaKey yang sama) ────
|
|
810
|
+
try {
|
|
811
|
+
let thumbnailBuffer;
|
|
812
|
+
if ('sharp' in lib && lib.sharp) {
|
|
813
|
+
thumbnailBuffer = await lib.sharp.default(coverBuffer).resize(252, 252).jpeg().toBuffer();
|
|
814
|
+
} else if ('jimp' in lib && lib.jimp) {
|
|
815
|
+
const jimpImage = await (lib.jimp.Jimp || lib.jimp.default).read(coverBuffer);
|
|
816
|
+
thumbnailBuffer = await jimpImage.resize({ w: 252, h: 252 }).getBuffer('image/jpeg');
|
|
817
|
+
} else {
|
|
818
|
+
throw new Error('No image processing library available for thumbnail generation');
|
|
819
|
+
}
|
|
820
|
+
|
|
821
|
+
if (!thumbnailBuffer || thumbnailBuffer.length === 0) {
|
|
822
|
+
throw new Error('Failed to generate thumbnail buffer');
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
const thumbUpload = await encryptedStream(thumbnailBuffer, 'thumbnail-sticker-pack', {
|
|
826
|
+
logger: options.logger,
|
|
827
|
+
opts: options.options,
|
|
828
|
+
mediaKey: stickerPackUpload.mediaKey
|
|
829
|
+
});
|
|
830
|
+
|
|
831
|
+
const thumbUploadResult = await options.upload(thumbUpload.encWriteStream, {
|
|
832
|
+
fileEncSha256B64: thumbUpload.fileEncSha256.toString('base64'),
|
|
833
|
+
mediaType: 'thumbnail-sticker-pack',
|
|
834
|
+
timeoutMs: options.mediaUploadTimeoutMs
|
|
835
|
+
});
|
|
836
|
+
|
|
837
|
+
Object.assign(m.stickerPackMessage, {
|
|
838
|
+
thumbnailDirectPath: thumbUploadResult.directPath,
|
|
839
|
+
thumbnailSha256: thumbUpload.fileSha256,
|
|
840
|
+
thumbnailEncSha256: thumbUpload.fileEncSha256,
|
|
841
|
+
thumbnailHeight: 252,
|
|
842
|
+
thumbnailWidth: 252,
|
|
843
|
+
imageDataHash: sha256(thumbnailBuffer).toString('base64')
|
|
844
|
+
});
|
|
845
|
+
} catch (e) {
|
|
846
|
+
options.logger?.warn?.(`Thumbnail generation failed: ${e}`);
|
|
847
|
+
}
|
|
848
|
+
|
|
849
|
+
m.stickerPackMessage.contextInfo = {
|
|
850
|
+
...(message.contextInfo || {}),
|
|
851
|
+
...(message.mentions ? { mentionedJid: message.mentions } : {})
|
|
852
|
+
};
|
|
853
|
+
}
|
|
854
|
+
else if (hasNonNullishProperty(message, 'adminInvite')) {
|
|
855
|
+
m.newsletterAdminInviteMessage = {};
|
|
856
|
+
m.newsletterAdminInviteMessage.newsletterJid = message.adminInvite.jid;
|
|
857
|
+
m.newsletterAdminInviteMessage.newsletterName = message.adminInvite.name;
|
|
858
|
+
m.newsletterAdminInviteMessage.caption = message.adminInvite.caption;
|
|
859
|
+
m.newsletterAdminInviteMessage.inviteExpiration = message.adminInvite.expiration;
|
|
860
|
+
if (message.adminInvite.jpegThumbnail) {
|
|
861
|
+
m.newsletterAdminInviteMessage.jpegThumbnail = message.adminInvite.jpegThumbnail;
|
|
862
|
+
} else if (options.getProfilePicUrl) {
|
|
863
|
+
try {
|
|
864
|
+
const pfpUrl = await options.getProfilePicUrl(message.adminInvite.jid);
|
|
865
|
+
if (pfpUrl) {
|
|
866
|
+
const { thumbnail } = await generateThumbnail(pfpUrl, 'image');
|
|
867
|
+
m.newsletterAdminInviteMessage.jpegThumbnail = thumbnail;
|
|
868
|
+
}
|
|
869
|
+
} catch (_) {}
|
|
870
|
+
}
|
|
871
|
+
m.newsletterAdminInviteMessage.contextInfo = {
|
|
872
|
+
...(message.contextInfo || {}),
|
|
873
|
+
...(message.mentions ? { mentionedJid: message.mentions } : {})
|
|
874
|
+
};
|
|
875
|
+
}
|
|
876
|
+
else if (hasNonNullishProperty(message, 'sharePhoneNumber')) {
|
|
877
|
+
m.protocolMessage = {
|
|
878
|
+
type: proto.Message.ProtocolMessage.Type.SHARE_PHONE_NUMBER
|
|
879
|
+
};
|
|
880
|
+
}
|
|
881
|
+
else if (hasNonNullishProperty(message, 'requestPhoneNumber')) {
|
|
882
|
+
m.requestPhoneNumberMessage = {};
|
|
883
|
+
}
|
|
884
|
+
else if (hasNonNullishProperty(message, 'limitSharing')) {
|
|
885
|
+
m.protocolMessage = {
|
|
886
|
+
type: proto.Message.ProtocolMessage.Type.LIMIT_SHARING,
|
|
887
|
+
limitSharing: {
|
|
888
|
+
sharingLimited: message.limitSharing === true,
|
|
889
|
+
trigger: 1,
|
|
890
|
+
limitSharingSettingTimestamp: Date.now(),
|
|
891
|
+
initiatedByMe: true
|
|
892
|
+
}
|
|
893
|
+
};
|
|
894
|
+
}
|
|
895
|
+
else if ('interactiveMessage' in message && !!message.interactiveMessage) {
|
|
896
|
+
// ── Passthrough interactiveMessage raw object ──────────────────────
|
|
897
|
+
// Must be BEFORE the else block to avoid hitting prepareWAMessageMedia
|
|
898
|
+
// which throws 'Invalid media type' for interactiveMessage keys.
|
|
899
|
+
m = { interactiveMessage: message.interactiveMessage };
|
|
900
|
+
}
|
|
901
|
+
else {
|
|
902
|
+
m = await prepareWAMessageMedia(message, options);
|
|
903
|
+
}
|
|
904
|
+
if ('sections' in message && !!message.sections) {
|
|
905
|
+
const listMessage = {
|
|
906
|
+
title: message.title,
|
|
907
|
+
buttonText: message.buttonText,
|
|
908
|
+
footerText: message.footer,
|
|
909
|
+
description: message.text,
|
|
910
|
+
sections: message.sections,
|
|
911
|
+
listType: proto.Message.ListMessage.ListType.SINGLE_SELECT
|
|
912
|
+
};
|
|
913
|
+
listMessage.contextInfo = {
|
|
914
|
+
...(message.contextInfo || {}),
|
|
915
|
+
...(message.mentions ? { mentionedJid: message.mentions } : {})
|
|
916
|
+
};
|
|
917
|
+
m = { listMessage };
|
|
918
|
+
}
|
|
919
|
+
else if ('productList' in message && !!message.productList) {
|
|
920
|
+
const thumbnail = message.thumbnail ? await generateThumbnail(message.thumbnail, 'image') : null;
|
|
921
|
+
const listMessage = {
|
|
922
|
+
title: message.title,
|
|
923
|
+
buttonText: message.buttonText,
|
|
924
|
+
footerText: message.footer,
|
|
925
|
+
description: message.text,
|
|
926
|
+
productListInfo: {
|
|
927
|
+
productSections: message.productList,
|
|
928
|
+
headerImage: {
|
|
929
|
+
productId: message.productList[0].products[0].productId,
|
|
930
|
+
jpegThumbnail: thumbnail?.thumbnail || null
|
|
931
|
+
},
|
|
932
|
+
businessOwnerJid: message.businessOwnerJid
|
|
933
|
+
},
|
|
934
|
+
listType: proto.Message.ListMessage.ListType.PRODUCT_LIST
|
|
935
|
+
};
|
|
936
|
+
listMessage.contextInfo = {
|
|
937
|
+
...(message.contextInfo || {}),
|
|
938
|
+
...(message.mentions ? { mentionedJid: message.mentions } : {})
|
|
939
|
+
};
|
|
940
|
+
m = { listMessage };
|
|
941
|
+
}
|
|
942
|
+
else if ('buttons' in message && !!message.buttons) {
|
|
943
|
+
const buttonsMessage = {
|
|
944
|
+
buttons: message.buttons.map(b => ({ ...b, type: proto.Message.ButtonsMessage.Button.Type.RESPONSE }))
|
|
945
|
+
};
|
|
946
|
+
if ('text' in message) {
|
|
947
|
+
buttonsMessage.contentText = message.text;
|
|
948
|
+
buttonsMessage.headerType = proto.Message.ButtonsMessage.HeaderType.EMPTY;
|
|
949
|
+
}
|
|
950
|
+
else {
|
|
951
|
+
if ('caption' in message) {
|
|
952
|
+
buttonsMessage.contentText = message.caption;
|
|
953
|
+
}
|
|
954
|
+
const type = Object.keys(m)[0].replace('Message', '').toUpperCase();
|
|
955
|
+
buttonsMessage.headerType = proto.Message.ButtonsMessage.HeaderType[type];
|
|
956
|
+
Object.assign(buttonsMessage, m);
|
|
957
|
+
}
|
|
958
|
+
if ('footer' in message && !!message.footer) {
|
|
959
|
+
buttonsMessage.footerText = message.footer;
|
|
960
|
+
}
|
|
961
|
+
if ('title' in message && !!message.title) {
|
|
962
|
+
buttonsMessage.text = message.title;
|
|
963
|
+
buttonsMessage.headerType = proto.Message.ButtonsMessage.HeaderType.TEXT;
|
|
964
|
+
}
|
|
965
|
+
buttonsMessage.contextInfo = {
|
|
966
|
+
...(message.contextInfo || {}),
|
|
967
|
+
...(message.mentions ? { mentionedJid: message.mentions } : {})
|
|
968
|
+
};
|
|
969
|
+
m = { buttonsMessage };
|
|
970
|
+
}
|
|
971
|
+
else if ('templateButtons' in message && !!message.templateButtons) {
|
|
972
|
+
const hydratedTemplate = {
|
|
973
|
+
hydratedButtons: message.templateButtons
|
|
974
|
+
};
|
|
975
|
+
if ('text' in message) {
|
|
976
|
+
hydratedTemplate.hydratedContentText = message.text;
|
|
977
|
+
}
|
|
978
|
+
else {
|
|
979
|
+
if ('caption' in message) {
|
|
980
|
+
hydratedTemplate.hydratedContentText = message.caption;
|
|
981
|
+
}
|
|
982
|
+
Object.assign(hydratedTemplate, m);
|
|
983
|
+
}
|
|
984
|
+
if ('footer' in message && !!message.footer) {
|
|
985
|
+
hydratedTemplate.hydratedFooterText = message.footer;
|
|
986
|
+
}
|
|
987
|
+
hydratedTemplate.contextInfo = {
|
|
988
|
+
...(message.contextInfo || {}),
|
|
989
|
+
...(message.mentions ? { mentionedJid: message.mentions } : {})
|
|
990
|
+
};
|
|
991
|
+
m = { templateMessage: { fourRowTemplate: hydratedTemplate, hydratedTemplate } };
|
|
992
|
+
}
|
|
993
|
+
else if ('interactiveButtons' in message && !!message.interactiveButtons) {
|
|
994
|
+
const interactiveMessage = {
|
|
995
|
+
nativeFlowMessage: proto.Message.InteractiveMessage.NativeFlowMessage.fromObject({
|
|
996
|
+
buttons: message.interactiveButtons,
|
|
997
|
+
})
|
|
998
|
+
};
|
|
999
|
+
if ('text' in message) {
|
|
1000
|
+
interactiveMessage.body = { text: message.text };
|
|
1001
|
+
interactiveMessage.header = {
|
|
1002
|
+
title: message.title,
|
|
1003
|
+
subtitle: message.subtitle,
|
|
1004
|
+
hasMediaAttachment: message?.media ?? false
|
|
1005
|
+
};
|
|
1006
|
+
}
|
|
1007
|
+
else if ('caption' in message) {
|
|
1008
|
+
interactiveMessage.body = { text: message.caption };
|
|
1009
|
+
interactiveMessage.header = {
|
|
1010
|
+
title: message.title,
|
|
1011
|
+
subtitle: message.subtitle,
|
|
1012
|
+
hasMediaAttachment: message?.media ?? false
|
|
1013
|
+
};
|
|
1014
|
+
Object.assign(interactiveMessage.header, m);
|
|
1015
|
+
}
|
|
1016
|
+
if ('footer' in message && !!message.footer) {
|
|
1017
|
+
interactiveMessage.footer = { text: message.footer };
|
|
1018
|
+
}
|
|
1019
|
+
if ('contextInfo' in message && !!message.contextInfo) {
|
|
1020
|
+
interactiveMessage.contextInfo = message.contextInfo;
|
|
1021
|
+
}
|
|
1022
|
+
if ('mentions' in message && !!message.mentions) {
|
|
1023
|
+
interactiveMessage.contextInfo = { ...interactiveMessage.contextInfo, mentionedJid: message.mentions };
|
|
1024
|
+
}
|
|
1025
|
+
m = { interactiveMessage };
|
|
1026
|
+
}
|
|
1027
|
+
else if ('shop' in message && !!message.shop) {
|
|
1028
|
+
const interactiveMessage = {
|
|
1029
|
+
shopStorefrontMessage: proto.Message.InteractiveMessage.ShopMessage.fromObject({
|
|
1030
|
+
surface: message.shop.surface,
|
|
1031
|
+
id: message.shop.id
|
|
1032
|
+
})
|
|
1033
|
+
};
|
|
1034
|
+
if ('text' in message) {
|
|
1035
|
+
interactiveMessage.body = { text: message.text };
|
|
1036
|
+
interactiveMessage.header = {
|
|
1037
|
+
title: message.title,
|
|
1038
|
+
subtitle: message.subtitle,
|
|
1039
|
+
hasMediaAttachment: false
|
|
1040
|
+
};
|
|
1041
|
+
}
|
|
1042
|
+
else if ('caption' in message) {
|
|
1043
|
+
interactiveMessage.body = { text: message.caption };
|
|
1044
|
+
interactiveMessage.header = {
|
|
1045
|
+
title: message.title,
|
|
1046
|
+
subtitle: message.subtitle,
|
|
1047
|
+
hasMediaAttachment: message?.media ?? false,
|
|
1048
|
+
};
|
|
1049
|
+
Object.assign(interactiveMessage.header, m);
|
|
1050
|
+
}
|
|
1051
|
+
if ('footer' in message && !!message.footer) {
|
|
1052
|
+
interactiveMessage.footer = { text: message.footer };
|
|
1053
|
+
}
|
|
1054
|
+
if ('title' in message && !!message.title && !interactiveMessage.header) {
|
|
1055
|
+
interactiveMessage.header = {
|
|
1056
|
+
title: message.title,
|
|
1057
|
+
subtitle: message.subtitle,
|
|
1058
|
+
hasMediaAttachment: message?.media ?? false
|
|
1059
|
+
};
|
|
1060
|
+
}
|
|
1061
|
+
if ('contextInfo' in message && !!message.contextInfo) {
|
|
1062
|
+
interactiveMessage.contextInfo = message.contextInfo;
|
|
1063
|
+
}
|
|
1064
|
+
if ('mentions' in message && !!message.mentions) {
|
|
1065
|
+
interactiveMessage.contextInfo = { mentionedJid: message.mentions };
|
|
1066
|
+
}
|
|
1067
|
+
m = { interactiveMessage };
|
|
1068
|
+
}
|
|
1069
|
+
else if ('collection' in message && !!message.collection) {
|
|
1070
|
+
const interactiveMessage = {
|
|
1071
|
+
collectionMessage: {
|
|
1072
|
+
bizJid: message.collection.bizJid,
|
|
1073
|
+
id: message.collection.id,
|
|
1074
|
+
messageVersion: message?.collection?.version
|
|
1075
|
+
}
|
|
1076
|
+
};
|
|
1077
|
+
if ('text' in message) {
|
|
1078
|
+
interactiveMessage.body = { text: message.text };
|
|
1079
|
+
interactiveMessage.header = {
|
|
1080
|
+
title: message.title,
|
|
1081
|
+
subtitle: message.subtitle,
|
|
1082
|
+
hasMediaAttachment: false
|
|
1083
|
+
};
|
|
1084
|
+
}
|
|
1085
|
+
else {
|
|
1086
|
+
if ('caption' in message) {
|
|
1087
|
+
interactiveMessage.body = { text: message.caption };
|
|
1088
|
+
interactiveMessage.header = {
|
|
1089
|
+
title: message.title,
|
|
1090
|
+
subtitle: message.subtitle,
|
|
1091
|
+
hasMediaAttachment: message.hasMediaAttachment ? message.hasMediaAttachment : false,
|
|
1092
|
+
...Object.assign(interactiveMessage, m)
|
|
1093
|
+
};
|
|
1094
|
+
}
|
|
1095
|
+
}
|
|
1096
|
+
if ('footer' in message && !!message.footer) {
|
|
1097
|
+
interactiveMessage.footer = { text: message.footer };
|
|
1098
|
+
}
|
|
1099
|
+
interactiveMessage.contextInfo = {
|
|
1100
|
+
...(message.contextInfo || {}),
|
|
1101
|
+
...(message.mentions ? { mentionedJid: message.mentions } : {})
|
|
1102
|
+
};
|
|
1103
|
+
m = { interactiveMessage };
|
|
1104
|
+
}
|
|
1105
|
+
else if ('cards' in message && !!message.cards) {
|
|
1106
|
+
const slides = await Promise.all(message.cards.map(async (slide) => {
|
|
1107
|
+
const { image, video, product, title, body, footer, buttons } = slide;
|
|
1108
|
+
let header;
|
|
1109
|
+
if (product) {
|
|
1110
|
+
const { imageMessage } = await prepareWAMessageMedia({ image: product.productImage, ...options }, options);
|
|
1111
|
+
header = {
|
|
1112
|
+
productMessage: {
|
|
1113
|
+
product: {
|
|
1114
|
+
...product,
|
|
1115
|
+
productImage: imageMessage,
|
|
1116
|
+
},
|
|
1117
|
+
...slide
|
|
1118
|
+
}
|
|
1119
|
+
};
|
|
1120
|
+
}
|
|
1121
|
+
else if (image) {
|
|
1122
|
+
header = await prepareWAMessageMedia({ image: image, ...options }, options);
|
|
1123
|
+
}
|
|
1124
|
+
else if (video) {
|
|
1125
|
+
header = await prepareWAMessageMedia({ video: video, ...options }, options);
|
|
1126
|
+
}
|
|
1127
|
+
return {
|
|
1128
|
+
header: {
|
|
1129
|
+
title,
|
|
1130
|
+
hasMediaAttachment: true,
|
|
1131
|
+
...header
|
|
1132
|
+
},
|
|
1133
|
+
body: { text: body },
|
|
1134
|
+
footer: { text: footer },
|
|
1135
|
+
nativeFlowMessage: { buttons }
|
|
1136
|
+
};
|
|
1137
|
+
}));
|
|
1138
|
+
const interactiveMessage = {
|
|
1139
|
+
carouselMessage: { cards: slides }
|
|
1140
|
+
};
|
|
1141
|
+
if ('text' in message) {
|
|
1142
|
+
interactiveMessage.body = { text: message.text };
|
|
1143
|
+
interactiveMessage.header = {
|
|
1144
|
+
title: message.title,
|
|
1145
|
+
subtitle: message.subtitle,
|
|
1146
|
+
hasMediaAttachment: false
|
|
1147
|
+
};
|
|
1148
|
+
}
|
|
1149
|
+
if ('footer' in message && !!message.footer) {
|
|
1150
|
+
interactiveMessage.footer = { text: message.footer };
|
|
1151
|
+
}
|
|
1152
|
+
interactiveMessage.contextInfo = {
|
|
1153
|
+
...(message.contextInfo || {}),
|
|
1154
|
+
...(message.mentions ? { mentionedJid: message.mentions } : {})
|
|
1155
|
+
};
|
|
1156
|
+
m = { interactiveMessage };
|
|
1157
|
+
}
|
|
1158
|
+
if (hasOptionalProperty(message, 'ephemeral') && !!message.ephemeral) {
|
|
1159
|
+
m = { ephemeralMessage: { message: m } };
|
|
1160
|
+
}
|
|
1161
|
+
if (hasOptionalProperty(message, 'viewOnce') && !!message.viewOnce) {
|
|
1162
|
+
m = { viewOnceMessageV2: { message: m } };
|
|
1163
|
+
}
|
|
1164
|
+
if (hasOptionalProperty(message, 'viewOnceExt') && !!message.viewOnceExt) {
|
|
1165
|
+
m = { viewOnceMessageV2Extension: { message: m } };
|
|
1166
|
+
}
|
|
1167
|
+
if (hasOptionalProperty(message, 'mentions') && message.mentions?.length) {
|
|
1168
|
+
const messageType = Object.keys(m)[0];
|
|
1169
|
+
const key = m[messageType];
|
|
1170
|
+
if ('contextInfo' in key && !!key.contextInfo) {
|
|
1171
|
+
key.contextInfo.mentionedJid = message.mentions;
|
|
1172
|
+
}
|
|
1173
|
+
else if (key) {
|
|
1174
|
+
key.contextInfo = {
|
|
1175
|
+
mentionedJid: message.mentions
|
|
1176
|
+
};
|
|
1177
|
+
}
|
|
1178
|
+
}
|
|
1179
|
+
if (hasOptionalProperty(message, 'edit')) {
|
|
1180
|
+
m = {
|
|
1181
|
+
protocolMessage: {
|
|
1182
|
+
key: message.edit,
|
|
1183
|
+
editedMessage: m,
|
|
1184
|
+
timestampMs: Date.now(),
|
|
1185
|
+
type: WAProto.Message.ProtocolMessage.Type.MESSAGE_EDIT
|
|
1186
|
+
}
|
|
1187
|
+
};
|
|
1188
|
+
}
|
|
1189
|
+
if (hasOptionalProperty(message, 'contextInfo') && !!message.contextInfo) {
|
|
1190
|
+
const messageType = Object.keys(m)[0];
|
|
1191
|
+
const key = m[messageType];
|
|
1192
|
+
if ('contextInfo' in key && !!key.contextInfo) {
|
|
1193
|
+
key.contextInfo = { ...key.contextInfo, ...message.contextInfo };
|
|
1194
|
+
}
|
|
1195
|
+
else if (key) {
|
|
1196
|
+
key.contextInfo = message.contextInfo;
|
|
1197
|
+
}
|
|
1198
|
+
}
|
|
1199
|
+
if (shouldIncludeReportingToken(m) && !options.newsletter) {
|
|
1200
|
+
m.messageContextInfo = m.messageContextInfo || {};
|
|
1201
|
+
if (!m.messageContextInfo.messageSecret) {
|
|
1202
|
+
m.messageContextInfo.messageSecret = randomBytes(32);
|
|
1203
|
+
}
|
|
1204
|
+
}
|
|
1205
|
+
return WAProto.Message.create(m);
|
|
1206
|
+
};
|
|
1207
|
+
export const generateWAMessageFromContent = (jid, message, options) => {
|
|
1208
|
+
// set timestamp to now
|
|
1209
|
+
// if not specified
|
|
1210
|
+
if (!options.timestamp) {
|
|
1211
|
+
options.timestamp = new Date();
|
|
1212
|
+
}
|
|
1213
|
+
const innerMessage = normalizeMessageContent(message);
|
|
1214
|
+
const key = getContentType(innerMessage);
|
|
1215
|
+
const timestamp = unixTimestampSeconds(options.timestamp);
|
|
1216
|
+
const { quoted, userJid } = options;
|
|
1217
|
+
if (quoted && !isJidNewsletter(jid)) {
|
|
1218
|
+
const participant = quoted.key.fromMe
|
|
1219
|
+
? userJid // TODO: Add support for LIDs
|
|
1220
|
+
: quoted.participant || quoted.key.participant || quoted.key.remoteJid;
|
|
1221
|
+
let quotedMsg = normalizeMessageContent(quoted.message);
|
|
1222
|
+
const msgType = getContentType(quotedMsg);
|
|
1223
|
+
// strip any redundant properties
|
|
1224
|
+
quotedMsg = proto.Message.create({ [msgType]: quotedMsg[msgType] });
|
|
1225
|
+
const quotedContent = quotedMsg[msgType];
|
|
1226
|
+
if (typeof quotedContent === 'object' && quotedContent && 'contextInfo' in quotedContent) {
|
|
1227
|
+
delete quotedContent.contextInfo;
|
|
1228
|
+
}
|
|
1229
|
+
const contextInfo = ('contextInfo' in innerMessage[key] && innerMessage[key]?.contextInfo) || {};
|
|
1230
|
+
contextInfo.participant = jidNormalizedUser(participant);
|
|
1231
|
+
contextInfo.stanzaId = quoted.key.id;
|
|
1232
|
+
contextInfo.quotedMessage = quotedMsg;
|
|
1233
|
+
// if a participant is quoted, then it must be a group
|
|
1234
|
+
// hence, remoteJid of group must also be entered
|
|
1235
|
+
if (jid !== quoted.key.remoteJid) {
|
|
1236
|
+
contextInfo.remoteJid = quoted.key.remoteJid;
|
|
1237
|
+
}
|
|
1238
|
+
if (contextInfo && innerMessage[key]) {
|
|
1239
|
+
/* @ts-ignore */
|
|
1240
|
+
innerMessage[key].contextInfo = contextInfo;
|
|
1241
|
+
}
|
|
1242
|
+
}
|
|
1243
|
+
if (
|
|
1244
|
+
// if we want to send a disappearing message
|
|
1245
|
+
!!options?.ephemeralExpiration &&
|
|
1246
|
+
// and it's not a protocol message -- delete, toggle disappear message
|
|
1247
|
+
key !== 'protocolMessage' &&
|
|
1248
|
+
// already not converted to disappearing message
|
|
1249
|
+
key !== 'ephemeralMessage' &&
|
|
1250
|
+
// newsletters don't support ephemeral messages
|
|
1251
|
+
!isJidNewsletter(jid)) {
|
|
1252
|
+
/* @ts-ignore */
|
|
1253
|
+
innerMessage[key].contextInfo = {
|
|
1254
|
+
...(innerMessage[key].contextInfo || {}),
|
|
1255
|
+
expiration: options.ephemeralExpiration || WA_DEFAULT_EPHEMERAL
|
|
1256
|
+
//ephemeralSettingTimestamp: options.ephemeralOptions.eph_setting_ts?.toString()
|
|
1257
|
+
};
|
|
1258
|
+
}
|
|
1259
|
+
message = WAProto.Message.create(message);
|
|
1260
|
+
const messageJSON = {
|
|
1261
|
+
key: {
|
|
1262
|
+
remoteJid: jid,
|
|
1263
|
+
fromMe: true,
|
|
1264
|
+
id: options?.messageId || generateMessageIDV2()
|
|
1265
|
+
},
|
|
1266
|
+
message: message,
|
|
1267
|
+
messageTimestamp: timestamp,
|
|
1268
|
+
messageStubParameters: [],
|
|
1269
|
+
participant: isJidGroup(jid) || isJidStatusBroadcast(jid) ? userJid : undefined, // TODO: Add support for LIDs
|
|
1270
|
+
status: WAMessageStatus.PENDING
|
|
1271
|
+
};
|
|
1272
|
+
return WAProto.WebMessageInfo.fromObject(messageJSON);
|
|
1273
|
+
};
|
|
1274
|
+
export const generateWAMessage = async (jid, content, options) => {
|
|
1275
|
+
// ensure msg ID is with every log
|
|
1276
|
+
options.logger = options?.logger?.child({ msgId: options.messageId });
|
|
1277
|
+
// Pass jid + newsletter flag to generateWAMessageContent (like wiley)
|
|
1278
|
+
const _isNewsletter = typeof jid === 'string' && jid.endsWith('@newsletter');
|
|
1279
|
+
return generateWAMessageFromContent(jid, await generateWAMessageContent(content, { newsletter: _isNewsletter, ...options, jid }), options);
|
|
1280
|
+
};
|
|
1281
|
+
/** Get the key to access the true type of content */
|
|
1282
|
+
export const getContentType = (content) => {
|
|
1283
|
+
if (content) {
|
|
1284
|
+
const keys = Object.keys(content);
|
|
1285
|
+
const key = keys.find(k => (k === 'conversation' || k.includes('Message')) && k !== 'senderKeyDistributionMessage');
|
|
1286
|
+
return key;
|
|
1287
|
+
}
|
|
1288
|
+
};
|
|
1289
|
+
/**
|
|
1290
|
+
* Maps a message content type key to its MediaType string.
|
|
1291
|
+
* Handles ptvMessage → 'ptv', audioMessage ptt → 'ptt', etc.
|
|
1292
|
+
*/
|
|
1293
|
+
export const getMediaTypeFromContentType = (contentType, content) => {
|
|
1294
|
+
if (!contentType)
|
|
1295
|
+
return undefined;
|
|
1296
|
+
if (contentType === 'ptvMessage')
|
|
1297
|
+
return 'ptv';
|
|
1298
|
+
if (contentType === 'audioMessage' && content?.[contentType]?.ptt)
|
|
1299
|
+
return 'ptt';
|
|
1300
|
+
if (contentType === 'videoMessage' && content?.[contentType]?.gifPlayback)
|
|
1301
|
+
return 'gif';
|
|
1302
|
+
return contentType.replace('Message', '');
|
|
1303
|
+
};
|
|
1304
|
+
/**
|
|
1305
|
+
* Normalizes ephemeral, view once messages to regular message content
|
|
1306
|
+
* Eg. image messages in ephemeral messages, in view once messages etc.
|
|
1307
|
+
* @param content
|
|
1308
|
+
* @returns
|
|
1309
|
+
*/
|
|
1310
|
+
export const normalizeMessageContent = (content) => {
|
|
1311
|
+
if (!content) {
|
|
1312
|
+
return undefined;
|
|
1313
|
+
}
|
|
1314
|
+
// set max iterations to prevent an infinite loop
|
|
1315
|
+
for (let i = 0; i < 5; i++) {
|
|
1316
|
+
const inner = getFutureProofMessage(content);
|
|
1317
|
+
if (!inner) {
|
|
1318
|
+
break;
|
|
1319
|
+
}
|
|
1320
|
+
content = inner.message;
|
|
1321
|
+
}
|
|
1322
|
+
return content;
|
|
1323
|
+
function getFutureProofMessage(message) {
|
|
1324
|
+
return (message?.ephemeralMessage ||
|
|
1325
|
+
message?.viewOnceMessage ||
|
|
1326
|
+
message?.documentWithCaptionMessage ||
|
|
1327
|
+
message?.viewOnceMessageV2 ||
|
|
1328
|
+
message?.viewOnceMessageV2Extension ||
|
|
1329
|
+
message?.editedMessage ||
|
|
1330
|
+
message?.associatedChildMessage ||
|
|
1331
|
+
message?.groupStatusMessage ||
|
|
1332
|
+
message?.groupStatusMessageV2);
|
|
1333
|
+
}
|
|
1334
|
+
};
|
|
1335
|
+
/**
|
|
1336
|
+
* Extract the true message content from a message
|
|
1337
|
+
* Eg. extracts the inner message from a disappearing message/view once message
|
|
1338
|
+
*/
|
|
1339
|
+
export const extractMessageContent = (content) => {
|
|
1340
|
+
const extractFromTemplateMessage = (msg) => {
|
|
1341
|
+
if (msg.imageMessage) {
|
|
1342
|
+
return { imageMessage: msg.imageMessage };
|
|
1343
|
+
}
|
|
1344
|
+
else if (msg.documentMessage) {
|
|
1345
|
+
return { documentMessage: msg.documentMessage };
|
|
1346
|
+
}
|
|
1347
|
+
else if (msg.videoMessage) {
|
|
1348
|
+
return { videoMessage: msg.videoMessage };
|
|
1349
|
+
}
|
|
1350
|
+
else if (msg.locationMessage) {
|
|
1351
|
+
return { locationMessage: msg.locationMessage };
|
|
1352
|
+
}
|
|
1353
|
+
else {
|
|
1354
|
+
return {
|
|
1355
|
+
conversation: 'contentText' in msg ? msg.contentText : 'hydratedContentText' in msg ? msg.hydratedContentText : ''
|
|
1356
|
+
};
|
|
1357
|
+
}
|
|
1358
|
+
};
|
|
1359
|
+
content = normalizeMessageContent(content);
|
|
1360
|
+
if (content?.buttonsMessage) {
|
|
1361
|
+
return extractFromTemplateMessage(content.buttonsMessage);
|
|
1362
|
+
}
|
|
1363
|
+
if (content?.templateMessage?.hydratedFourRowTemplate) {
|
|
1364
|
+
return extractFromTemplateMessage(content?.templateMessage?.hydratedFourRowTemplate);
|
|
1365
|
+
}
|
|
1366
|
+
if (content?.templateMessage?.hydratedTemplate) {
|
|
1367
|
+
return extractFromTemplateMessage(content?.templateMessage?.hydratedTemplate);
|
|
1368
|
+
}
|
|
1369
|
+
if (content?.templateMessage?.fourRowTemplate) {
|
|
1370
|
+
return extractFromTemplateMessage(content?.templateMessage?.fourRowTemplate);
|
|
1371
|
+
}
|
|
1372
|
+
return content;
|
|
1373
|
+
};
|
|
1374
|
+
/**
|
|
1375
|
+
* Returns the device predicted by message ID
|
|
1376
|
+
*/
|
|
1377
|
+
export const getDevice = (id) => /^3A.{18}$/.test(id)
|
|
1378
|
+
? 'ios'
|
|
1379
|
+
: /^3E.{20}$/.test(id)
|
|
1380
|
+
? 'web'
|
|
1381
|
+
: /^(.{21}|.{32})$/.test(id)
|
|
1382
|
+
? 'android'
|
|
1383
|
+
: /^(3F|.{18}$)/.test(id)
|
|
1384
|
+
? 'desktop'
|
|
1385
|
+
: 'unknown';
|
|
1386
|
+
/** Upserts a receipt in the message */
|
|
1387
|
+
export const updateMessageWithReceipt = (msg, receipt) => {
|
|
1388
|
+
msg.userReceipt = msg.userReceipt || [];
|
|
1389
|
+
const recp = msg.userReceipt.find(m => m.userJid === receipt.userJid);
|
|
1390
|
+
if (recp) {
|
|
1391
|
+
Object.assign(recp, receipt);
|
|
1392
|
+
}
|
|
1393
|
+
else {
|
|
1394
|
+
msg.userReceipt.push(receipt);
|
|
1395
|
+
}
|
|
1396
|
+
};
|
|
1397
|
+
/** Update the message with a new reaction */
|
|
1398
|
+
export const updateMessageWithReaction = (msg, reaction) => {
|
|
1399
|
+
const authorID = getKeyAuthor(reaction.key);
|
|
1400
|
+
const reactions = (msg.reactions || []).filter(r => getKeyAuthor(r.key) !== authorID);
|
|
1401
|
+
reaction.text = reaction.text || '';
|
|
1402
|
+
reactions.push(reaction);
|
|
1403
|
+
msg.reactions = reactions;
|
|
1404
|
+
};
|
|
1405
|
+
/** Update the message with a new poll update */
|
|
1406
|
+
export const updateMessageWithPollUpdate = (msg, update) => {
|
|
1407
|
+
const authorID = getKeyAuthor(update.pollUpdateMessageKey);
|
|
1408
|
+
const reactions = (msg.pollUpdates || []).filter(r => getKeyAuthor(r.pollUpdateMessageKey) !== authorID);
|
|
1409
|
+
if (update.vote?.selectedOptions?.length) {
|
|
1410
|
+
reactions.push(update);
|
|
1411
|
+
}
|
|
1412
|
+
msg.pollUpdates = reactions;
|
|
1413
|
+
};
|
|
1414
|
+
/** Update the message with a new event response */
|
|
1415
|
+
export const updateMessageWithEventResponse = (msg, update) => {
|
|
1416
|
+
const authorID = getKeyAuthor(update.eventResponseMessageKey);
|
|
1417
|
+
const responses = (msg.eventResponses || []).filter(r => getKeyAuthor(r.eventResponseMessageKey) !== authorID);
|
|
1418
|
+
responses.push(update);
|
|
1419
|
+
msg.eventResponses = responses;
|
|
1420
|
+
};
|
|
1421
|
+
/**
|
|
1422
|
+
* Aggregates all poll updates in a poll.
|
|
1423
|
+
* @param msg the poll creation message
|
|
1424
|
+
* @param meId your jid
|
|
1425
|
+
* @returns A list of options & their voters
|
|
1426
|
+
*/
|
|
1427
|
+
export function getAggregateVotesInPollMessage({ message, pollUpdates }, meId) {
|
|
1428
|
+
const opts = message?.pollCreationMessage?.options ||
|
|
1429
|
+
message?.pollCreationMessageV2?.options ||
|
|
1430
|
+
message?.pollCreationMessageV3?.options ||
|
|
1431
|
+
[];
|
|
1432
|
+
const voteHashMap = opts.reduce((acc, opt) => {
|
|
1433
|
+
const hash = sha256(Buffer.from(opt.optionName || '')).toString();
|
|
1434
|
+
acc[hash] = {
|
|
1435
|
+
name: opt.optionName || '',
|
|
1436
|
+
voters: []
|
|
1437
|
+
};
|
|
1438
|
+
return acc;
|
|
1439
|
+
}, {});
|
|
1440
|
+
for (const update of pollUpdates || []) {
|
|
1441
|
+
const { vote } = update;
|
|
1442
|
+
if (!vote) {
|
|
1443
|
+
continue;
|
|
1444
|
+
}
|
|
1445
|
+
for (const option of vote.selectedOptions || []) {
|
|
1446
|
+
const hash = option.toString();
|
|
1447
|
+
let data = voteHashMap[hash];
|
|
1448
|
+
if (!data) {
|
|
1449
|
+
voteHashMap[hash] = {
|
|
1450
|
+
name: 'Unknown',
|
|
1451
|
+
voters: []
|
|
1452
|
+
};
|
|
1453
|
+
data = voteHashMap[hash];
|
|
1454
|
+
}
|
|
1455
|
+
voteHashMap[hash].voters.push(getKeyAuthor(update.pollUpdateMessageKey, meId));
|
|
1456
|
+
}
|
|
1457
|
+
}
|
|
1458
|
+
return Object.values(voteHashMap);
|
|
1459
|
+
}
|
|
1460
|
+
/**
|
|
1461
|
+
* Aggregates all event responses in an event message.
|
|
1462
|
+
* @param msg the event creation message
|
|
1463
|
+
* @param meId your jid
|
|
1464
|
+
* @returns A list of response types & their responders
|
|
1465
|
+
*/
|
|
1466
|
+
export function getAggregateResponsesInEventMessage({ eventResponses }, meId) {
|
|
1467
|
+
const responseTypes = ['GOING', 'NOT_GOING', 'MAYBE'];
|
|
1468
|
+
const responseMap = {};
|
|
1469
|
+
for (const type of responseTypes) {
|
|
1470
|
+
responseMap[type] = {
|
|
1471
|
+
response: type,
|
|
1472
|
+
responders: []
|
|
1473
|
+
};
|
|
1474
|
+
}
|
|
1475
|
+
for (const update of eventResponses || []) {
|
|
1476
|
+
const responseType = update.eventResponse || 'UNKNOWN';
|
|
1477
|
+
if (responseType !== 'UNKNOWN' && responseMap[responseType]) {
|
|
1478
|
+
responseMap[responseType].responders.push(getKeyAuthor(update.eventResponseMessageKey, meId));
|
|
1479
|
+
}
|
|
1480
|
+
}
|
|
1481
|
+
return Object.values(responseMap);
|
|
1482
|
+
}
|
|
1483
|
+
/** Given a list of message keys, aggregates them by chat & sender. Useful for sending read receipts in bulk */
|
|
1484
|
+
export const aggregateMessageKeysNotFromMe = (keys) => {
|
|
1485
|
+
const keyMap = {};
|
|
1486
|
+
for (const { remoteJid, id, participant, fromMe } of keys) {
|
|
1487
|
+
if (!fromMe) {
|
|
1488
|
+
const uqKey = `${remoteJid}:${participant || ''}`;
|
|
1489
|
+
if (!keyMap[uqKey]) {
|
|
1490
|
+
keyMap[uqKey] = {
|
|
1491
|
+
jid: remoteJid,
|
|
1492
|
+
participant: participant,
|
|
1493
|
+
messageIds: []
|
|
1494
|
+
};
|
|
1495
|
+
}
|
|
1496
|
+
keyMap[uqKey].messageIds.push(id);
|
|
1497
|
+
}
|
|
1498
|
+
}
|
|
1499
|
+
return Object.values(keyMap);
|
|
1500
|
+
};
|
|
1501
|
+
const REUPLOAD_REQUIRED_STATUS = [410, 404];
|
|
1502
|
+
/**
|
|
1503
|
+
* Downloads the given message. Throws an error if it's not a media message
|
|
1504
|
+
*/
|
|
1505
|
+
export const downloadMediaMessage = async (message, type, options, ctx) => {
|
|
1506
|
+
const result = await downloadMsg().catch(async (error) => {
|
|
1507
|
+
if (ctx &&
|
|
1508
|
+
typeof error?.status === 'number' && // treat errors with status as HTTP failures requiring reupload
|
|
1509
|
+
REUPLOAD_REQUIRED_STATUS.includes(error.status)) {
|
|
1510
|
+
ctx.logger.info({ key: message.key }, 'sending reupload media request...');
|
|
1511
|
+
// request reupload
|
|
1512
|
+
message = await ctx.reuploadRequest(message);
|
|
1513
|
+
const result = await downloadMsg();
|
|
1514
|
+
return result;
|
|
1515
|
+
}
|
|
1516
|
+
throw error;
|
|
1517
|
+
});
|
|
1518
|
+
return result;
|
|
1519
|
+
async function downloadMsg() {
|
|
1520
|
+
const mContent = extractMessageContent(message.message);
|
|
1521
|
+
if (!mContent) {
|
|
1522
|
+
throw new Boom('No message present', { statusCode: 400, data: message });
|
|
1523
|
+
}
|
|
1524
|
+
const contentType = getContentType(mContent);
|
|
1525
|
+
let mediaType = getMediaTypeFromContentType(contentType, mContent);
|
|
1526
|
+
const media = mContent[contentType];
|
|
1527
|
+
if (!media || typeof media !== 'object' || (!('url' in media) && !('thumbnailDirectPath' in media))) {
|
|
1528
|
+
throw new Boom(`"${contentType}" message is not a media message`);
|
|
1529
|
+
}
|
|
1530
|
+
let download;
|
|
1531
|
+
if ('thumbnailDirectPath' in media && !('url' in media)) {
|
|
1532
|
+
download = {
|
|
1533
|
+
directPath: media.thumbnailDirectPath,
|
|
1534
|
+
mediaKey: media.mediaKey
|
|
1535
|
+
};
|
|
1536
|
+
mediaType = 'thumbnail-link';
|
|
1537
|
+
}
|
|
1538
|
+
else {
|
|
1539
|
+
download = media;
|
|
1540
|
+
}
|
|
1541
|
+
const stream = await downloadContentFromMessage(download, mediaType, options);
|
|
1542
|
+
if (type === 'buffer') {
|
|
1543
|
+
const bufferArray = [];
|
|
1544
|
+
for await (const chunk of stream) {
|
|
1545
|
+
bufferArray.push(chunk);
|
|
1546
|
+
}
|
|
1547
|
+
return Buffer.concat(bufferArray);
|
|
1548
|
+
}
|
|
1549
|
+
return stream;
|
|
1550
|
+
}
|
|
1551
|
+
};
|
|
1552
|
+
/** Checks whether the given message is a media message; if it is returns the inner content */
|
|
1553
|
+
export const assertMediaContent = (content) => {
|
|
1554
|
+
content = extractMessageContent(content);
|
|
1555
|
+
const mediaContent = content?.documentMessage ||
|
|
1556
|
+
content?.imageMessage ||
|
|
1557
|
+
content?.videoMessage ||
|
|
1558
|
+
content?.audioMessage ||
|
|
1559
|
+
content?.stickerMessage;
|
|
1560
|
+
if (!mediaContent) {
|
|
1561
|
+
throw new Boom('given message is not a media message', { statusCode: 400, data: content });
|
|
1562
|
+
}
|
|
1563
|
+
return mediaContent;
|
|
1564
|
+
};
|
|
1565
|
+
|
|
1566
|
+
export const patchMessageForMdIfRequired = (message) => {
|
|
1567
|
+
if (message?.buttonsMessage ||
|
|
1568
|
+
message?.templateMessage ||
|
|
1569
|
+
message?.listMessage ||
|
|
1570
|
+
message?.interactiveMessage?.nativeFlowMessage
|
|
1571
|
+
) {
|
|
1572
|
+
message = {
|
|
1573
|
+
viewOnceMessageV2Extension: {
|
|
1574
|
+
message: {
|
|
1575
|
+
messageContextInfo: {
|
|
1576
|
+
deviceListMetadataVersion: 2,
|
|
1577
|
+
deviceListMetadata: {}
|
|
1578
|
+
},
|
|
1579
|
+
...message
|
|
1580
|
+
}
|
|
1581
|
+
}
|
|
1582
|
+
};
|
|
1583
|
+
}
|
|
1584
|
+
return message;
|
|
1585
|
+
};
|
|
1586
|
+
export const prepareAlbumMessageContent = async (jid, albums, options) => {
|
|
1587
|
+
let mediaHandle;
|
|
1588
|
+
let mediaMsg;
|
|
1589
|
+
const message = [];
|
|
1590
|
+
const albumMsg = generateWAMessageFromContent(jid, {
|
|
1591
|
+
albumMessage: {
|
|
1592
|
+
expectedImageCount: albums.filter(item => 'image' in item).length,
|
|
1593
|
+
expectedVideoCount: albums.filter(item => 'video' in item).length
|
|
1594
|
+
}
|
|
1595
|
+
}, options);
|
|
1596
|
+
await options.sock.relayMessage(jid, albumMsg.message, { messageId: albumMsg.key.id });
|
|
1597
|
+
for (const i in albums) {
|
|
1598
|
+
const media = albums[i];
|
|
1599
|
+
if ('image' in media) {
|
|
1600
|
+
mediaMsg = await generateWAMessage(jid, { image: media.image, ...media, ...options }, {
|
|
1601
|
+
userJid: options.userJid,
|
|
1602
|
+
upload: async (encFilePath, opts) => {
|
|
1603
|
+
const up = await options.sock.waUploadToServer(encFilePath, { ...opts, newsletter: isJidNewsletter(jid) });
|
|
1604
|
+
mediaHandle = up.handle;
|
|
1605
|
+
return up;
|
|
1606
|
+
},
|
|
1607
|
+
...options
|
|
1608
|
+
});
|
|
1609
|
+
} else if ('video' in media) {
|
|
1610
|
+
mediaMsg = await generateWAMessage(jid, { video: media.video, ...media, ...options }, {
|
|
1611
|
+
userJid: options.userJid,
|
|
1612
|
+
upload: async (encFilePath, opts) => {
|
|
1613
|
+
const up = await options.sock.waUploadToServer(encFilePath, { ...opts, newsletter: isJidNewsletter(jid) });
|
|
1614
|
+
mediaHandle = up.handle;
|
|
1615
|
+
return up;
|
|
1616
|
+
},
|
|
1617
|
+
...options
|
|
1618
|
+
});
|
|
1619
|
+
}
|
|
1620
|
+
if (mediaMsg) {
|
|
1621
|
+
mediaMsg.message.messageContextInfo = {
|
|
1622
|
+
messageSecret: randomBytes(32),
|
|
1623
|
+
messageAssociation: {
|
|
1624
|
+
associationType: 1,
|
|
1625
|
+
parentMessageKey: albumMsg.key
|
|
1626
|
+
}
|
|
1627
|
+
};
|
|
1628
|
+
}
|
|
1629
|
+
message.push(mediaMsg);
|
|
1630
|
+
}
|
|
1631
|
+
return message;
|
|
1632
|
+
};
|
|
1633
|
+
/**
|
|
1634
|
+
* Ekstrak quoted message dari contextInfo pesan.
|
|
1635
|
+
* Return object yang sudah dinormalisasi, termasuk debug info LID/PN.
|
|
1636
|
+
*
|
|
1637
|
+
* @param {object} msg - WAMessage object dari event messages.upsert
|
|
1638
|
+
* @param {object} [options]
|
|
1639
|
+
* @param {boolean} [options.debug] - jika true, log info ke console
|
|
1640
|
+
* @returns {object|null} quoted message info atau null jika tidak ada
|
|
1641
|
+
*/
|
|
1642
|
+
export const getQuotedMsg = (msg, options = {}) => {
|
|
1643
|
+
const { debug = false } = options;
|
|
1644
|
+
const msgContent = normalizeMessageContent(msg?.message);
|
|
1645
|
+
if (!msgContent) return null;
|
|
1646
|
+
const msgType = getContentType(msgContent);
|
|
1647
|
+
if (!msgType) return null;
|
|
1648
|
+
const innerMsg = msgContent[msgType];
|
|
1649
|
+
if (!innerMsg) return null;
|
|
1650
|
+
const contextInfo = innerMsg.contextInfo;
|
|
1651
|
+
if (!contextInfo?.stanzaId || !contextInfo?.quotedMessage) return null;
|
|
1652
|
+
|
|
1653
|
+
// Normalisasi participant (sender quoted message)
|
|
1654
|
+
const participant = contextInfo.participant || msg.key?.participant || msg.key?.remoteJid || '';
|
|
1655
|
+
|
|
1656
|
+
// Normalisasi mentionedJid di quoted message
|
|
1657
|
+
const quotedMsgContent = normalizeMessageContent(contextInfo.quotedMessage);
|
|
1658
|
+
const quotedMsgType = getContentType(quotedMsgContent);
|
|
1659
|
+
const quotedInnerMsg = quotedMsgType ? quotedMsgContent?.[quotedMsgType] : null;
|
|
1660
|
+
const quotedMentionedJid = quotedInnerMsg?.contextInfo?.mentionedJid || [];
|
|
1661
|
+
const quotedText = quotedInnerMsg?.text || quotedInnerMsg?.caption || quotedInnerMsg?.conversation || '';
|
|
1662
|
+
|
|
1663
|
+
const result = {
|
|
1664
|
+
key: {
|
|
1665
|
+
id: contextInfo.stanzaId,
|
|
1666
|
+
remoteJid: contextInfo.remoteJid || msg.key?.remoteJid || '',
|
|
1667
|
+
participant: participant,
|
|
1668
|
+
fromMe: false
|
|
1669
|
+
},
|
|
1670
|
+
message: contextInfo.quotedMessage,
|
|
1671
|
+
participant,
|
|
1672
|
+
sender: participant,
|
|
1673
|
+
text: quotedText,
|
|
1674
|
+
type: quotedMsgType || '',
|
|
1675
|
+
mentionedJid: quotedMentionedJid,
|
|
1676
|
+
// Debug info
|
|
1677
|
+
_rawContextInfo: debug ? contextInfo : undefined
|
|
1678
|
+
};
|
|
1679
|
+
|
|
1680
|
+
if (debug) {
|
|
1681
|
+
const hasLidInText = typeof quotedText === 'string' && /@\d{13,20}/.test(quotedText);
|
|
1682
|
+
const hasLidInMentioned = quotedMentionedJid.some(j => j?.endsWith('@lid'));
|
|
1683
|
+
const hasLidParticipant = participant?.endsWith('@lid');
|
|
1684
|
+
console.log('[getQuotedMsg DEBUG]', JSON.stringify({
|
|
1685
|
+
stanzaId: contextInfo.stanzaId,
|
|
1686
|
+
participant,
|
|
1687
|
+
participantIsLid: hasLidParticipant,
|
|
1688
|
+
quotedText,
|
|
1689
|
+
textHasLid: hasLidInText,
|
|
1690
|
+
mentionedJid: quotedMentionedJid,
|
|
1691
|
+
mentionedHasLid: hasLidInMentioned,
|
|
1692
|
+
quotedMsgType
|
|
1693
|
+
}, null, 2));
|
|
1694
|
+
}
|
|
1695
|
+
|
|
1696
|
+
return result;
|
|
1697
|
+
};
|
|
1698
|
+
|
|
1699
|
+
//# sourceMappingURL=messages.js.map
|