xspacesocket 0.0.0-stage → 1.3.0
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 +32 -0
- package/README.md +143 -2
- package/WAProto/GenerateStatics.sh +3 -0
- package/WAProto/WAProto.proto +5488 -0
- package/WAProto/fix-imports.js +85 -0
- package/WAProto/index.d.ts +14043 -0
- package/WAProto/index.js +107198 -0
- package/engine-requirements.js +10 -0
- package/lib/Defaults/index.d.ts +81 -0
- package/lib/Defaults/index.d.ts.map +1 -0
- package/lib/Defaults/index.js +146 -0
- package/lib/Defaults/index.js.map +1 -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 +431 -0
- package/lib/Signal/libsignal.js.map +1 -0
- package/lib/Signal/lid-mapping.d.ts +23 -0
- package/lib/Signal/lid-mapping.d.ts.map +1 -0
- package/lib/Signal/lid-mapping.js +277 -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 +387 -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/channelPollVote.d.ts +44 -0
- package/lib/Socket/channelPollVote.d.ts.map +1 -0
- package/lib/Socket/channelPollVote.js +65 -0
- package/lib/Socket/channelPollVote.js.map +1 -0
- package/lib/Socket/channelSchedule.d.ts +229 -0
- package/lib/Socket/channelSchedule.d.ts.map +1 -0
- package/lib/Socket/channelSchedule.js +465 -0
- package/lib/Socket/channelSchedule.js.map +1 -0
- package/lib/Socket/channelStatus.d.ts +317 -0
- package/lib/Socket/channelStatus.d.ts.map +1 -0
- package/lib/Socket/channelStatus.js +479 -0
- package/lib/Socket/channelStatus.js.map +1 -0
- package/lib/Socket/chats.d.ts +150 -0
- package/lib/Socket/chats.d.ts.map +1 -0
- package/lib/Socket/chats.js +1313 -0
- package/lib/Socket/chats.js.map +1 -0
- package/lib/Socket/communities.d.ts +444 -0
- package/lib/Socket/communities.d.ts.map +1 -0
- package/lib/Socket/communities.js +447 -0
- package/lib/Socket/communities.js.map +1 -0
- package/lib/Socket/groupHistory.d.ts +38 -0
- package/lib/Socket/groupHistory.d.ts.map +1 -0
- package/lib/Socket/groupHistory.js +88 -0
- package/lib/Socket/groupHistory.js.map +1 -0
- package/lib/Socket/groupStatus.d.ts +28 -0
- package/lib/Socket/groupStatus.d.ts.map +1 -0
- package/lib/Socket/groupStatus.js +47 -0
- package/lib/Socket/groupStatus.js.map +1 -0
- package/lib/Socket/groups.d.ts +167 -0
- package/lib/Socket/groups.d.ts.map +1 -0
- package/lib/Socket/groups.js +354 -0
- package/lib/Socket/groups.js.map +1 -0
- package/lib/Socket/index.d.ts +430 -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 +373 -0
- package/lib/Socket/messages-recv.d.ts.map +1 -0
- package/lib/Socket/messages-recv.js +1897 -0
- package/lib/Socket/messages-recv.js.map +1 -0
- package/lib/Socket/messages-send.d.ts +404 -0
- package/lib/Socket/messages-send.d.ts.map +1 -0
- package/lib/Socket/messages-send.js +1528 -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 +176 -0
- package/lib/Socket/newsletter.d.ts.map +1 -0
- package/lib/Socket/newsletter.js +181 -0
- package/lib/Socket/newsletter.js.map +1 -0
- package/lib/Socket/newsletterDirectory.d.ts +201 -0
- package/lib/Socket/newsletterDirectory.d.ts.map +1 -0
- package/lib/Socket/newsletterDirectory.js +291 -0
- package/lib/Socket/newsletterDirectory.js.map +1 -0
- package/lib/Socket/payment.d.ts +37 -0
- package/lib/Socket/payment.d.ts.map +1 -0
- package/lib/Socket/payment.js +124 -0
- package/lib/Socket/payment.js.map +1 -0
- package/lib/Socket/report.d.ts +70 -0
- package/lib/Socket/report.d.ts.map +1 -0
- package/lib/Socket/report.js +265 -0
- package/lib/Socket/report.js.map +1 -0
- package/lib/Socket/socket.d.ts +59 -0
- package/lib/Socket/socket.d.ts.map +1 -0
- package/lib/Socket/socket.js +982 -0
- package/lib/Socket/socket.js.map +1 -0
- package/lib/Socket/stickerPack.d.ts +29 -0
- package/lib/Socket/stickerPack.d.ts.map +1 -0
- package/lib/Socket/stickerPack.js +130 -0
- package/lib/Socket/stickerPack.js.map +1 -0
- package/lib/Socket/username.d.ts +55 -0
- package/lib/Socket/username.d.ts.map +1 -0
- package/lib/Socket/username.js +114 -0
- package/lib/Socket/username.js.map +1 -0
- package/lib/Types/Auth.d.ts +117 -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/ChannelStatus.d.ts +34 -0
- package/lib/Types/ChannelStatus.d.ts.map +1 -0
- package/lib/Types/ChannelStatus.js +2 -0
- package/lib/Types/ChannelStatus.js.map +1 -0
- package/lib/Types/Chat.d.ts +141 -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 +26 -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 +260 -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 +73 -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 +466 -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/Mex.d.ts +141 -0
- package/lib/Types/Mex.d.ts.map +1 -0
- package/lib/Types/Mex.js +37 -0
- package/lib/Types/Mex.js.map +1 -0
- package/lib/Types/Payment.d.ts +47 -0
- package/lib/Types/Payment.d.ts.map +1 -0
- package/lib/Types/Payment.js +2 -0
- package/lib/Types/Payment.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/Report.d.ts +219 -0
- package/lib/Types/Report.d.ts.map +1 -0
- package/lib/Types/Report.js +166 -0
- package/lib/Types/Report.js.map +1 -0
- package/lib/Types/Signal.d.ts +87 -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 +136 -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 +97 -0
- package/lib/Types/State.d.ts.map +1 -0
- package/lib/Types/State.js +56 -0
- package/lib/Types/State.js.map +1 -0
- package/lib/Types/StickerPack.d.ts +52 -0
- package/lib/Types/StickerPack.d.ts.map +1 -0
- package/lib/Types/StickerPack.js +2 -0
- package/lib/Types/StickerPack.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/Username.d.ts +19 -0
- package/lib/Types/Username.d.ts.map +1 -0
- package/lib/Types/Username.js +2 -0
- package/lib/Types/Username.js.map +1 -0
- package/lib/Types/index.d.ts +71 -0
- package/lib/Types/index.d.ts.map +1 -0
- package/lib/Types/index.js +31 -0
- package/lib/Types/index.js.map +1 -0
- package/lib/Utils/auth-utils.d.ts +24 -0
- package/lib/Utils/auth-utils.d.ts.map +1 -0
- package/lib/Utils/auth-utils.js +302 -0
- package/lib/Utils/auth-utils.js.map +1 -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 +29 -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 +100 -0
- package/lib/Utils/chat-utils.d.ts.map +1 -0
- package/lib/Utils/chat-utils.js +890 -0
- package/lib/Utils/chat-utils.js.map +1 -0
- package/lib/Utils/companion-reg-client-utils.d.ts +17 -0
- package/lib/Utils/companion-reg-client-utils.d.ts.map +1 -0
- package/lib/Utils/companion-reg-client-utils.js +35 -0
- package/lib/Utils/companion-reg-client-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 +66 -0
- package/lib/Utils/decode-wa-message.d.ts.map +1 -0
- package/lib/Utils/decode-wa-message.js +317 -0
- package/lib/Utils/decode-wa-message.js.map +1 -0
- package/lib/Utils/event-buffer.d.ts +36 -0
- package/lib/Utils/event-buffer.d.ts.map +1 -0
- package/lib/Utils/event-buffer.js +642 -0
- package/lib/Utils/event-buffer.js.map +1 -0
- package/lib/Utils/generics.d.ts +92 -0
- package/lib/Utils/generics.d.ts.map +1 -0
- package/lib/Utils/generics.js +393 -0
- package/lib/Utils/generics.js.map +1 -0
- package/lib/Utils/group-history.d.ts +37 -0
- package/lib/Utils/group-history.d.ts.map +1 -0
- package/lib/Utils/group-history.js +55 -0
- package/lib/Utils/group-history.js.map +1 -0
- package/lib/Utils/history.d.ts +24 -0
- package/lib/Utils/history.d.ts.map +1 -0
- package/lib/Utils/history.js +134 -0
- package/lib/Utils/history.js.map +1 -0
- package/lib/Utils/identity-change-handler.d.ts +44 -0
- package/lib/Utils/identity-change-handler.d.ts.map +1 -0
- package/lib/Utils/identity-change-handler.js +50 -0
- package/lib/Utils/identity-change-handler.js.map +1 -0
- package/lib/Utils/index.d.ts +26 -0
- package/lib/Utils/index.d.ts.map +1 -0
- package/lib/Utils/index.js +26 -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 +99 -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 +119 -0
- package/lib/Utils/message-retry-manager.d.ts.map +1 -0
- package/lib/Utils/message-retry-manager.js +276 -0
- package/lib/Utils/message-retry-manager.js.map +1 -0
- package/lib/Utils/messages-media.d.ts +149 -0
- package/lib/Utils/messages-media.d.ts.map +1 -0
- package/lib/Utils/messages-media.js +875 -0
- package/lib/Utils/messages-media.js.map +1 -0
- package/lib/Utils/messages.d.ts +178 -0
- package/lib/Utils/messages.d.ts.map +1 -0
- package/lib/Utils/messages.js +1325 -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/offline-node-processor.d.ts +17 -0
- package/lib/Utils/offline-node-processor.d.ts.map +1 -0
- package/lib/Utils/offline-node-processor.js +40 -0
- package/lib/Utils/offline-node-processor.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 +85 -0
- package/lib/Utils/process-message.d.ts.map +1 -0
- package/lib/Utils/process-message.js +688 -0
- package/lib/Utils/process-message.js.map +1 -0
- package/lib/Utils/reporting-utils.d.ts +18 -0
- package/lib/Utils/reporting-utils.d.ts.map +1 -0
- package/lib/Utils/reporting-utils.js +265 -0
- package/lib/Utils/reporting-utils.js.map +1 -0
- package/lib/Utils/rich-response.d.ts +12 -0
- package/lib/Utils/rich-response.d.ts.map +1 -0
- package/lib/Utils/rich-response.js +97 -0
- package/lib/Utils/rich-response.js.map +1 -0
- package/lib/Utils/signal.d.ts +47 -0
- package/lib/Utils/signal.d.ts.map +1 -0
- package/lib/Utils/signal.js +201 -0
- package/lib/Utils/signal.js.map +1 -0
- package/lib/Utils/stanza-ack.d.ts +11 -0
- package/lib/Utils/stanza-ack.d.ts.map +1 -0
- package/lib/Utils/stanza-ack.js +38 -0
- package/lib/Utils/stanza-ack.js.map +1 -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 +49 -0
- package/lib/Utils/sync-action-utils.js.map +1 -0
- package/lib/Utils/sync-key-auto-resolve.d.ts +10 -0
- package/lib/Utils/sync-key-auto-resolve.d.ts.map +1 -0
- package/lib/Utils/sync-key-auto-resolve.js +33 -0
- package/lib/Utils/sync-key-auto-resolve.js.map +1 -0
- package/lib/Utils/tc-token-utils.d.ts +37 -0
- package/lib/Utils/tc-token-utils.d.ts.map +1 -0
- package/lib/Utils/tc-token-utils.js +164 -0
- package/lib/Utils/tc-token-utils.js.map +1 -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 +153 -0
- package/lib/Utils/use-multi-file-auth-state.js.map +1 -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 +211 -0
- package/lib/Utils/validate-connection.js.map +1 -0
- package/lib/Utils/zip.d.ts +11 -0
- package/lib/Utils/zip.d.ts.map +1 -0
- package/lib/Utils/zip.js +88 -0
- package/lib/Utils/zip.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 +22 -0
- package/lib/WABinary/generic-utils.d.ts.map +1 -0
- package/lib/WABinary/generic-utils.js +201 -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 +6 -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 +96 -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 +52 -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/USyncUsernameProtocol.d.ts +10 -0
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.d.ts.map +1 -0
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +25 -0
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.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 +6 -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 +30 -0
- package/lib/WAUSync/USyncQuery.d.ts.map +1 -0
- package/lib/WAUSync/USyncQuery.js +98 -0
- package/lib/WAUSync/USyncQuery.js.map +1 -0
- package/lib/WAUSync/USyncUser.d.ts +17 -0
- package/lib/WAUSync/USyncUser.d.ts.map +1 -0
- package/lib/WAUSync/USyncUser.js +31 -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 +11 -0
- package/lib/index.js.map +1 -0
- package/package.json +139 -4
|
@@ -0,0 +1,1325 @@
|
|
|
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, generateThumbnail, getAudioDuration, getAudioWaveform, getRawMediaUploadData } from './messages-media.js';
|
|
12
|
+
import { shouldIncludeReportingToken } from './reporting-utils.js';
|
|
13
|
+
import { markdownToAIRichResponse } from './rich-response.js';
|
|
14
|
+
const MIMETYPE_MAP = {
|
|
15
|
+
image: 'image/jpeg',
|
|
16
|
+
video: 'video/mp4',
|
|
17
|
+
document: 'application/pdf',
|
|
18
|
+
audio: 'audio/ogg; codecs=opus',
|
|
19
|
+
sticker: 'image/webp',
|
|
20
|
+
'product-catalog-image': 'image/jpeg'
|
|
21
|
+
};
|
|
22
|
+
const MessageTypeProto = {
|
|
23
|
+
image: WAProto.Message.ImageMessage,
|
|
24
|
+
video: WAProto.Message.VideoMessage,
|
|
25
|
+
audio: WAProto.Message.AudioMessage,
|
|
26
|
+
sticker: WAProto.Message.StickerMessage,
|
|
27
|
+
document: WAProto.Message.DocumentMessage
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Uses a regex to test whether the string contains a URL, and returns the URL if it does.
|
|
31
|
+
* @param text eg. hello https://google.com
|
|
32
|
+
* @returns the URL, eg. https://google.com
|
|
33
|
+
*/
|
|
34
|
+
export const extractUrlFromText = (text) => text.match(URL_REGEX)?.[0];
|
|
35
|
+
export const generateLinkPreviewIfRequired = async (text, getUrlInfo, logger) => {
|
|
36
|
+
const url = extractUrlFromText(text);
|
|
37
|
+
if (!!getUrlInfo && url) {
|
|
38
|
+
try {
|
|
39
|
+
const urlInfo = await getUrlInfo(url);
|
|
40
|
+
return urlInfo;
|
|
41
|
+
}
|
|
42
|
+
catch (error) {
|
|
43
|
+
// ignore if fails
|
|
44
|
+
logger?.warn({ trace: error.stack }, 'url generation failed');
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
const applyStickerFlags = (message, uploadData) => {
|
|
49
|
+
const sticker = message.stickerMessage;
|
|
50
|
+
if (!sticker)
|
|
51
|
+
return message;
|
|
52
|
+
if (typeof uploadData.isAiSticker === 'boolean')
|
|
53
|
+
sticker.isAiSticker = uploadData.isAiSticker;
|
|
54
|
+
if (typeof uploadData.isAvatar === 'boolean')
|
|
55
|
+
sticker.isAvatar = uploadData.isAvatar;
|
|
56
|
+
if (typeof uploadData.isLottie === 'boolean')
|
|
57
|
+
sticker.isLottie = uploadData.isLottie;
|
|
58
|
+
if (typeof uploadData.accessibilityLabel === 'string')
|
|
59
|
+
sticker.accessibilityLabel = uploadData.accessibilityLabel;
|
|
60
|
+
sticker.stickerSentTs || (sticker.stickerSentTs = Date.now());
|
|
61
|
+
return message;
|
|
62
|
+
};
|
|
63
|
+
const assertColor = async (color) => {
|
|
64
|
+
let assertedColor;
|
|
65
|
+
if (typeof color === 'number') {
|
|
66
|
+
assertedColor = color > 0 ? color : 0xffffffff + Number(color) + 1;
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
let hex = color.trim().replace('#', '');
|
|
70
|
+
if (hex.length <= 6) {
|
|
71
|
+
hex = 'FF' + hex.padStart(6, '0');
|
|
72
|
+
}
|
|
73
|
+
assertedColor = parseInt(hex, 16);
|
|
74
|
+
return assertedColor;
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
export const prepareWAMessageMedia = async (message, options) => {
|
|
78
|
+
const logger = options.logger;
|
|
79
|
+
let mediaType;
|
|
80
|
+
for (const key of MEDIA_KEYS) {
|
|
81
|
+
if (key in message) {
|
|
82
|
+
mediaType = key;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
if (!mediaType) {
|
|
86
|
+
throw new Boom('Invalid media type', { statusCode: 400 });
|
|
87
|
+
}
|
|
88
|
+
const uploadData = {
|
|
89
|
+
...message,
|
|
90
|
+
media: message[mediaType]
|
|
91
|
+
};
|
|
92
|
+
delete uploadData[mediaType];
|
|
93
|
+
// check if cacheable + generate cache key
|
|
94
|
+
const cacheableKey = typeof uploadData.media === 'object' &&
|
|
95
|
+
'url' in uploadData.media &&
|
|
96
|
+
!!uploadData.media.url &&
|
|
97
|
+
!!options.mediaCache &&
|
|
98
|
+
mediaType + ':' + uploadData.media.url.toString();
|
|
99
|
+
if (mediaType === 'document' && !uploadData.fileName) {
|
|
100
|
+
uploadData.fileName = 'file';
|
|
101
|
+
}
|
|
102
|
+
if (!uploadData.mimetype) {
|
|
103
|
+
uploadData.mimetype = MIMETYPE_MAP[mediaType];
|
|
104
|
+
}
|
|
105
|
+
if (cacheableKey) {
|
|
106
|
+
const mediaBuff = await options.mediaCache.get(cacheableKey);
|
|
107
|
+
if (mediaBuff) {
|
|
108
|
+
logger?.debug({ cacheableKey }, 'got media cache hit');
|
|
109
|
+
const obj = proto.Message.decode(mediaBuff);
|
|
110
|
+
const key = `${mediaType}Message`;
|
|
111
|
+
Object.assign(obj[key], { ...uploadData, media: undefined });
|
|
112
|
+
return applyStickerFlags(obj, uploadData);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
const isNewsletter = !!options.jid && isJidNewsletter(options.jid);
|
|
116
|
+
if (isNewsletter) {
|
|
117
|
+
logger?.info({ key: cacheableKey }, 'Preparing raw media for newsletter');
|
|
118
|
+
const { filePath, fileSha256, fileLength } = await getRawMediaUploadData(uploadData.media, options.mediaTypeOverride || mediaType, logger);
|
|
119
|
+
const fileSha256B64 = fileSha256.toString('base64');
|
|
120
|
+
const { directPath, thumbnailDirectPath, thumbnailSha256 } = await options.upload(filePath, {
|
|
121
|
+
fileEncSha256B64: fileSha256B64,
|
|
122
|
+
mediaType: mediaType,
|
|
123
|
+
timeoutMs: options.mediaUploadTimeoutMs,
|
|
124
|
+
newsletter: true
|
|
125
|
+
});
|
|
126
|
+
await fs.unlink(filePath);
|
|
127
|
+
const obj = WAProto.Message.fromObject({
|
|
128
|
+
// todo: add more support here
|
|
129
|
+
[`${mediaType}Message`]: MessageTypeProto[mediaType].fromObject({
|
|
130
|
+
// url intentionally omitted — newsletters use directPath only
|
|
131
|
+
directPath,
|
|
132
|
+
fileSha256,
|
|
133
|
+
fileLength,
|
|
134
|
+
thumbnailDirectPath,
|
|
135
|
+
thumbnailSha256: thumbnailSha256 ? Buffer.from(thumbnailSha256, 'base64') : undefined,
|
|
136
|
+
...uploadData,
|
|
137
|
+
media: undefined
|
|
138
|
+
})
|
|
139
|
+
});
|
|
140
|
+
if (uploadData.ptv) {
|
|
141
|
+
obj.ptvMessage = obj.videoMessage;
|
|
142
|
+
delete obj.videoMessage;
|
|
143
|
+
}
|
|
144
|
+
if (obj.stickerMessage) {
|
|
145
|
+
obj.stickerMessage.stickerSentTs = Date.now();
|
|
146
|
+
}
|
|
147
|
+
if (cacheableKey) {
|
|
148
|
+
logger?.debug({ cacheableKey }, 'set cache');
|
|
149
|
+
await options.mediaCache.set(cacheableKey, WAProto.Message.encode(obj).finish());
|
|
150
|
+
}
|
|
151
|
+
return applyStickerFlags(obj, uploadData);
|
|
152
|
+
}
|
|
153
|
+
const requiresDurationComputation = mediaType === 'audio' && typeof uploadData.seconds === 'undefined';
|
|
154
|
+
const requiresThumbnailComputation = (mediaType === 'image' || mediaType === 'video') && typeof uploadData['jpegThumbnail'] === 'undefined';
|
|
155
|
+
const requiresWaveformProcessing = mediaType === 'audio' && uploadData.ptt === true && typeof uploadData.waveform === 'undefined';
|
|
156
|
+
const requiresAudioBackground = options.backgroundColor && mediaType === 'audio' && uploadData.ptt === true;
|
|
157
|
+
const requiresOriginalForSomeProcessing = requiresDurationComputation || requiresThumbnailComputation;
|
|
158
|
+
const { mediaKey, encFilePath, originalFilePath, fileEncSha256, fileSha256, fileLength } = await encryptedStream(uploadData.media, options.mediaTypeOverride || mediaType, {
|
|
159
|
+
logger,
|
|
160
|
+
saveOriginalFileIfRequired: requiresOriginalForSomeProcessing,
|
|
161
|
+
opts: options.options
|
|
162
|
+
});
|
|
163
|
+
const fileEncSha256B64 = fileEncSha256.toString('base64');
|
|
164
|
+
const [{ mediaUrl, directPath }] = await Promise.all([
|
|
165
|
+
(async () => {
|
|
166
|
+
const result = await options.upload(encFilePath, {
|
|
167
|
+
fileEncSha256B64,
|
|
168
|
+
mediaType,
|
|
169
|
+
timeoutMs: options.mediaUploadTimeoutMs
|
|
170
|
+
});
|
|
171
|
+
logger?.debug({ mediaType, cacheableKey }, 'uploaded media');
|
|
172
|
+
return result;
|
|
173
|
+
})(),
|
|
174
|
+
(async () => {
|
|
175
|
+
try {
|
|
176
|
+
if (requiresThumbnailComputation) {
|
|
177
|
+
const { thumbnail, originalImageDimensions } = await generateThumbnail(originalFilePath, mediaType, options);
|
|
178
|
+
uploadData.jpegThumbnail = thumbnail;
|
|
179
|
+
if (!uploadData.width && originalImageDimensions) {
|
|
180
|
+
uploadData.width = originalImageDimensions.width;
|
|
181
|
+
uploadData.height = originalImageDimensions.height;
|
|
182
|
+
logger?.debug('set dimensions');
|
|
183
|
+
}
|
|
184
|
+
logger?.debug('generated thumbnail');
|
|
185
|
+
}
|
|
186
|
+
if (requiresDurationComputation) {
|
|
187
|
+
uploadData.seconds = await getAudioDuration(originalFilePath);
|
|
188
|
+
logger?.debug('computed audio duration');
|
|
189
|
+
}
|
|
190
|
+
if (requiresWaveformProcessing) {
|
|
191
|
+
uploadData.waveform = await getAudioWaveform(originalFilePath, logger);
|
|
192
|
+
logger?.debug('processed waveform');
|
|
193
|
+
}
|
|
194
|
+
if (requiresAudioBackground) {
|
|
195
|
+
uploadData.backgroundArgb = await assertColor(options.backgroundColor);
|
|
196
|
+
logger?.debug('computed backgroundColor audio status');
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
catch (error) {
|
|
200
|
+
logger?.warn({ trace: error.stack }, 'failed to obtain extra info');
|
|
201
|
+
}
|
|
202
|
+
})()
|
|
203
|
+
]).finally(async () => {
|
|
204
|
+
try {
|
|
205
|
+
await fs.unlink(encFilePath);
|
|
206
|
+
if (originalFilePath) {
|
|
207
|
+
await fs.unlink(originalFilePath);
|
|
208
|
+
}
|
|
209
|
+
logger?.debug('removed tmp files');
|
|
210
|
+
}
|
|
211
|
+
catch (error) {
|
|
212
|
+
logger?.warn('failed to remove tmp file');
|
|
213
|
+
}
|
|
214
|
+
});
|
|
215
|
+
const obj = WAProto.Message.fromObject({
|
|
216
|
+
[`${mediaType}Message`]: MessageTypeProto[mediaType].fromObject({
|
|
217
|
+
url: mediaUrl,
|
|
218
|
+
directPath,
|
|
219
|
+
mediaKey,
|
|
220
|
+
fileEncSha256,
|
|
221
|
+
fileSha256,
|
|
222
|
+
fileLength,
|
|
223
|
+
mediaKeyTimestamp: unixTimestampSeconds(),
|
|
224
|
+
...uploadData,
|
|
225
|
+
media: undefined
|
|
226
|
+
})
|
|
227
|
+
});
|
|
228
|
+
if (uploadData.ptv) {
|
|
229
|
+
obj.ptvMessage = obj.videoMessage;
|
|
230
|
+
delete obj.videoMessage;
|
|
231
|
+
}
|
|
232
|
+
if (cacheableKey) {
|
|
233
|
+
logger?.debug({ cacheableKey }, 'set cache');
|
|
234
|
+
await options.mediaCache.set(cacheableKey, WAProto.Message.encode(obj).finish());
|
|
235
|
+
}
|
|
236
|
+
return applyStickerFlags(obj, uploadData);
|
|
237
|
+
};
|
|
238
|
+
export const prepareDisappearingMessageSettingContent = (ephemeralExpiration) => {
|
|
239
|
+
ephemeralExpiration = ephemeralExpiration || 0;
|
|
240
|
+
const content = {
|
|
241
|
+
ephemeralMessage: {
|
|
242
|
+
message: {
|
|
243
|
+
protocolMessage: {
|
|
244
|
+
type: WAProto.Message.ProtocolMessage.Type.EPHEMERAL_SETTING,
|
|
245
|
+
ephemeralExpiration
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
};
|
|
250
|
+
return WAProto.Message.fromObject(content);
|
|
251
|
+
};
|
|
252
|
+
/**
|
|
253
|
+
* Generate forwarded message content like WA does
|
|
254
|
+
* @param message the message to forward
|
|
255
|
+
* @param options.forceForward will show the message as forwarded even if it is from you
|
|
256
|
+
*/
|
|
257
|
+
export const generateForwardMessageContent = (message, forceForward) => {
|
|
258
|
+
let content = message.message;
|
|
259
|
+
if (!content) {
|
|
260
|
+
throw new Boom('no content in message', { statusCode: 400 });
|
|
261
|
+
}
|
|
262
|
+
// hacky copy
|
|
263
|
+
content = normalizeMessageContent(content);
|
|
264
|
+
content = proto.Message.decode(proto.Message.encode(content).finish());
|
|
265
|
+
let key = Object.keys(content)[0];
|
|
266
|
+
let score = content?.[key]?.contextInfo?.forwardingScore || 0;
|
|
267
|
+
score += message.key.fromMe && !forceForward ? 0 : 1;
|
|
268
|
+
if (key === 'conversation') {
|
|
269
|
+
content.extendedTextMessage = { text: content[key] };
|
|
270
|
+
delete content.conversation;
|
|
271
|
+
key = 'extendedTextMessage';
|
|
272
|
+
}
|
|
273
|
+
const key_ = content?.[key];
|
|
274
|
+
if (score > 0) {
|
|
275
|
+
key_.contextInfo = { forwardingScore: score, isForwarded: true };
|
|
276
|
+
}
|
|
277
|
+
else {
|
|
278
|
+
key_.contextInfo = {};
|
|
279
|
+
}
|
|
280
|
+
return content;
|
|
281
|
+
};
|
|
282
|
+
export const hasNonNullishProperty = (message, key) => {
|
|
283
|
+
return (typeof message === 'object' &&
|
|
284
|
+
message !== null &&
|
|
285
|
+
key in message &&
|
|
286
|
+
message[key] !== null &&
|
|
287
|
+
message[key] !== undefined);
|
|
288
|
+
};
|
|
289
|
+
function hasOptionalProperty(obj, key) {
|
|
290
|
+
return typeof obj === 'object' && obj !== null && key in obj && obj[key] !== null;
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* Build the native-flow button array for an interactiveMessage.
|
|
294
|
+
*
|
|
295
|
+
* Each entry of `nativeFlow` is either a shorthand button ({ id }, { copy },
|
|
296
|
+
* { url }, { call }, { sections }) or an already-shaped native-flow row
|
|
297
|
+
* which is passed through untouched.
|
|
298
|
+
*
|
|
299
|
+
* Optional top-level extras:
|
|
300
|
+
* - offerText (+ offerUrl, offerCode, offerExpiration) -> limited_time_offer
|
|
301
|
+
* - optionText (+ optionTitle) -> bottom_sheet
|
|
302
|
+
*/
|
|
303
|
+
export const prepareNativeFlowButtons = (message) => {
|
|
304
|
+
const buttons = message.nativeFlow;
|
|
305
|
+
const isButtonsFieldArray = Array.isArray(buttons);
|
|
306
|
+
const correctedField = isButtonsFieldArray ? buttons : buttons.buttons;
|
|
307
|
+
const messageParamsJson = {};
|
|
308
|
+
if (hasOptionalProperty(message, 'offerText') && !!message.offerText) {
|
|
309
|
+
Object.assign(messageParamsJson, {
|
|
310
|
+
limited_time_offer: {
|
|
311
|
+
text: message.offerText,
|
|
312
|
+
url: message.offerUrl,
|
|
313
|
+
copy_code: message.offerCode,
|
|
314
|
+
expiration_time: message.offerExpiration
|
|
315
|
+
}
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
if (hasOptionalProperty(message, 'optionText') && !!message.optionText) {
|
|
319
|
+
Object.assign(messageParamsJson, {
|
|
320
|
+
bottom_sheet: {
|
|
321
|
+
in_thread_buttons_limit: 1,
|
|
322
|
+
divider_indices: Array.from({ length: correctedField.length }, (_, index) => index),
|
|
323
|
+
list_title: message.optionTitle || '📄 Select Options',
|
|
324
|
+
button_title: message.optionText
|
|
325
|
+
}
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
return {
|
|
329
|
+
buttons: correctedField.map(button => {
|
|
330
|
+
const shorthand = button;
|
|
331
|
+
const buttonText = shorthand.text || shorthand.buttonText;
|
|
332
|
+
const buttonIcon = shorthand.icon?.toUpperCase();
|
|
333
|
+
if (hasOptionalProperty(button, 'id') && !!button.id) {
|
|
334
|
+
return {
|
|
335
|
+
name: 'quick_reply',
|
|
336
|
+
buttonParamsJson: JSON.stringify({
|
|
337
|
+
display_text: buttonText || '👉🏻 Click',
|
|
338
|
+
id: button.id,
|
|
339
|
+
icon: buttonIcon
|
|
340
|
+
})
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
else if (hasOptionalProperty(button, 'copy') && !!button.copy) {
|
|
344
|
+
return {
|
|
345
|
+
name: 'cta_copy',
|
|
346
|
+
buttonParamsJson: JSON.stringify({
|
|
347
|
+
display_text: buttonText || '📋 Copy',
|
|
348
|
+
copy_code: button.copy,
|
|
349
|
+
icon: buttonIcon
|
|
350
|
+
})
|
|
351
|
+
};
|
|
352
|
+
}
|
|
353
|
+
else if (hasOptionalProperty(button, 'url') && !!button.url) {
|
|
354
|
+
return {
|
|
355
|
+
name: 'cta_url',
|
|
356
|
+
buttonParamsJson: JSON.stringify({
|
|
357
|
+
display_text: buttonText || '🌐 Visit',
|
|
358
|
+
url: button.url,
|
|
359
|
+
merchant_url: button.url,
|
|
360
|
+
webview_interaction: button.useWebview,
|
|
361
|
+
icon: buttonIcon
|
|
362
|
+
})
|
|
363
|
+
};
|
|
364
|
+
}
|
|
365
|
+
else if (hasOptionalProperty(button, 'call') && !!button.call) {
|
|
366
|
+
return {
|
|
367
|
+
name: 'cta_call',
|
|
368
|
+
buttonParamsJson: JSON.stringify({
|
|
369
|
+
display_text: buttonText || '📞 Call',
|
|
370
|
+
phone_number: button.call,
|
|
371
|
+
icon: buttonIcon
|
|
372
|
+
})
|
|
373
|
+
};
|
|
374
|
+
}
|
|
375
|
+
else if (hasOptionalProperty(button, 'sections') && !!button.sections) {
|
|
376
|
+
return {
|
|
377
|
+
name: 'single_select',
|
|
378
|
+
buttonParamsJson: JSON.stringify({
|
|
379
|
+
title: buttonText || '📋 Select',
|
|
380
|
+
sections: button.sections,
|
|
381
|
+
icon: buttonIcon
|
|
382
|
+
})
|
|
383
|
+
};
|
|
384
|
+
}
|
|
385
|
+
return button;
|
|
386
|
+
}),
|
|
387
|
+
messageParamsJson: JSON.stringify(messageParamsJson)
|
|
388
|
+
};
|
|
389
|
+
};
|
|
390
|
+
/** Media types that are valid at the top of an interactiveMessage header. */
|
|
391
|
+
export const hasValidInteractiveHeader = (message) => {
|
|
392
|
+
return !!(message.imageMessage ||
|
|
393
|
+
message.videoMessage ||
|
|
394
|
+
message.documentMessage ||
|
|
395
|
+
message.productMessage ||
|
|
396
|
+
message.locationMessage);
|
|
397
|
+
};
|
|
398
|
+
/**
|
|
399
|
+
* Determines whether a message needs a biz binary node on the outgoing stanza.
|
|
400
|
+
* Interactive shapes (buttons/list/template/native-flow) are rejected or
|
|
401
|
+
* rendered stripped without it.
|
|
402
|
+
*/
|
|
403
|
+
export const shouldIncludeBizBinaryNode = (message) => {
|
|
404
|
+
return !!(message.buttonsMessage ||
|
|
405
|
+
message.listMessage ||
|
|
406
|
+
message.templateMessage ||
|
|
407
|
+
(message.interactiveMessage && message.interactiveMessage.nativeFlowMessage));
|
|
408
|
+
};
|
|
409
|
+
export const generateWAMessageContent = async (message, options) => {
|
|
410
|
+
let m = {};
|
|
411
|
+
if (hasNonNullishProperty(message, 'text')) {
|
|
412
|
+
const extContent = { text: message.text };
|
|
413
|
+
let urlInfo = message.linkPreview;
|
|
414
|
+
if (typeof urlInfo === 'undefined') {
|
|
415
|
+
urlInfo = await generateLinkPreviewIfRequired(message.text, options.getUrlInfo, options.logger);
|
|
416
|
+
}
|
|
417
|
+
if (urlInfo) {
|
|
418
|
+
extContent.matchedText = urlInfo['matched-text'];
|
|
419
|
+
extContent.jpegThumbnail = urlInfo.jpegThumbnail;
|
|
420
|
+
extContent.description = urlInfo.description;
|
|
421
|
+
extContent.title = urlInfo.title;
|
|
422
|
+
extContent.previewType = 0;
|
|
423
|
+
const img = urlInfo.highQualityThumbnail;
|
|
424
|
+
if (img) {
|
|
425
|
+
extContent.thumbnailDirectPath = img.directPath;
|
|
426
|
+
extContent.mediaKey = img.mediaKey;
|
|
427
|
+
extContent.mediaKeyTimestamp = img.mediaKeyTimestamp;
|
|
428
|
+
extContent.thumbnailWidth = img.width;
|
|
429
|
+
extContent.thumbnailHeight = img.height;
|
|
430
|
+
extContent.thumbnailSha256 = img.fileSha256;
|
|
431
|
+
extContent.thumbnailEncSha256 = img.fileEncSha256;
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
if (options.backgroundColor) {
|
|
435
|
+
extContent.backgroundArgb = await assertColor(options.backgroundColor);
|
|
436
|
+
}
|
|
437
|
+
if (options.font) {
|
|
438
|
+
extContent.font = options.font;
|
|
439
|
+
}
|
|
440
|
+
m.extendedTextMessage = extContent;
|
|
441
|
+
}
|
|
442
|
+
else if (hasNonNullishProperty(message, 'markdownTable')) {
|
|
443
|
+
const richResponse = markdownToAIRichResponse(message.markdownTable, { title: message.tableTitle });
|
|
444
|
+
richResponse.contextInfo = { ...richResponse.contextInfo, ...message.contextInfo };
|
|
445
|
+
m.botForwardedMessage = { message: { richResponseMessage: richResponse } };
|
|
446
|
+
}
|
|
447
|
+
else if (hasNonNullishProperty(message, 'contacts')) {
|
|
448
|
+
const contactLen = message.contacts.contacts.length;
|
|
449
|
+
if (!contactLen) {
|
|
450
|
+
throw new Boom('require atleast 1 contact', { statusCode: 400 });
|
|
451
|
+
}
|
|
452
|
+
if (contactLen === 1) {
|
|
453
|
+
m.contactMessage = WAProto.Message.ContactMessage.create(message.contacts.contacts[0]);
|
|
454
|
+
}
|
|
455
|
+
else {
|
|
456
|
+
m.contactsArrayMessage = WAProto.Message.ContactsArrayMessage.create(message.contacts);
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
else if (hasNonNullishProperty(message, 'location')) {
|
|
460
|
+
m.locationMessage = WAProto.Message.LocationMessage.create(message.location);
|
|
461
|
+
}
|
|
462
|
+
else if (hasNonNullishProperty(message, 'react')) {
|
|
463
|
+
if (!message.react.senderTimestampMs) {
|
|
464
|
+
message.react.senderTimestampMs = Date.now();
|
|
465
|
+
}
|
|
466
|
+
m.reactionMessage = WAProto.Message.ReactionMessage.create(message.react);
|
|
467
|
+
}
|
|
468
|
+
else if (hasNonNullishProperty(message, 'delete')) {
|
|
469
|
+
m.protocolMessage = {
|
|
470
|
+
key: message.delete,
|
|
471
|
+
type: WAProto.Message.ProtocolMessage.Type.REVOKE
|
|
472
|
+
};
|
|
473
|
+
}
|
|
474
|
+
else if (hasNonNullishProperty(message, 'forward')) {
|
|
475
|
+
m = generateForwardMessageContent(message.forward, message.force);
|
|
476
|
+
}
|
|
477
|
+
else if (hasNonNullishProperty(message, 'disappearingMessagesInChat')) {
|
|
478
|
+
const exp = typeof message.disappearingMessagesInChat === 'boolean'
|
|
479
|
+
? message.disappearingMessagesInChat
|
|
480
|
+
? WA_DEFAULT_EPHEMERAL
|
|
481
|
+
: 0
|
|
482
|
+
: message.disappearingMessagesInChat;
|
|
483
|
+
m = prepareDisappearingMessageSettingContent(exp);
|
|
484
|
+
}
|
|
485
|
+
else if (hasNonNullishProperty(message, 'groupInvite')) {
|
|
486
|
+
m.groupInviteMessage = {};
|
|
487
|
+
m.groupInviteMessage.inviteCode = message.groupInvite.inviteCode;
|
|
488
|
+
m.groupInviteMessage.inviteExpiration = message.groupInvite.inviteExpiration;
|
|
489
|
+
m.groupInviteMessage.caption = message.groupInvite.text;
|
|
490
|
+
m.groupInviteMessage.groupJid = message.groupInvite.jid;
|
|
491
|
+
m.groupInviteMessage.groupName = message.groupInvite.subject;
|
|
492
|
+
//TODO: use built-in interface and get disappearing mode info etc.
|
|
493
|
+
//TODO: cache / use store!?
|
|
494
|
+
if (options.getProfilePicUrl) {
|
|
495
|
+
const pfpUrl = await options.getProfilePicUrl(message.groupInvite.jid, 'preview');
|
|
496
|
+
if (pfpUrl) {
|
|
497
|
+
const resp = await fetch(pfpUrl, { method: 'GET', dispatcher: options?.options?.dispatcher });
|
|
498
|
+
if (resp.ok) {
|
|
499
|
+
const buf = Buffer.from(await resp.arrayBuffer());
|
|
500
|
+
m.groupInviteMessage.jpegThumbnail = buf;
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
else if (hasNonNullishProperty(message, 'pin')) {
|
|
506
|
+
m.pinInChatMessage = {};
|
|
507
|
+
m.messageContextInfo = {};
|
|
508
|
+
m.pinInChatMessage.key = message.pin;
|
|
509
|
+
m.pinInChatMessage.type = message.type;
|
|
510
|
+
m.pinInChatMessage.senderTimestampMs = Date.now();
|
|
511
|
+
m.messageContextInfo.messageAddOnDurationInSecs = message.type === 1 ? message.time || 86400 : 0;
|
|
512
|
+
}
|
|
513
|
+
else if (hasNonNullishProperty(message, 'buttonReply')) {
|
|
514
|
+
switch (message.type) {
|
|
515
|
+
case 'template':
|
|
516
|
+
m.templateButtonReplyMessage = {
|
|
517
|
+
selectedDisplayText: message.buttonReply.displayText,
|
|
518
|
+
selectedId: message.buttonReply.id,
|
|
519
|
+
selectedIndex: message.buttonReply.index
|
|
520
|
+
};
|
|
521
|
+
break;
|
|
522
|
+
case 'plain':
|
|
523
|
+
m.buttonsResponseMessage = {
|
|
524
|
+
selectedButtonId: message.buttonReply.id,
|
|
525
|
+
selectedDisplayText: message.buttonReply.displayText,
|
|
526
|
+
type: proto.Message.ButtonsResponseMessage.Type.DISPLAY_TEXT
|
|
527
|
+
};
|
|
528
|
+
break;
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
else if (hasOptionalProperty(message, 'ptv') && message.ptv) {
|
|
532
|
+
const { videoMessage } = await prepareWAMessageMedia({ video: message.video }, options);
|
|
533
|
+
m.ptvMessage = videoMessage;
|
|
534
|
+
}
|
|
535
|
+
else if (hasNonNullishProperty(message, 'product')) {
|
|
536
|
+
const { imageMessage } = await prepareWAMessageMedia({ image: message.product.productImage }, options);
|
|
537
|
+
m.productMessage = WAProto.Message.ProductMessage.create({
|
|
538
|
+
...message,
|
|
539
|
+
product: {
|
|
540
|
+
...message.product,
|
|
541
|
+
productImage: imageMessage
|
|
542
|
+
}
|
|
543
|
+
});
|
|
544
|
+
}
|
|
545
|
+
else if (hasNonNullishProperty(message, 'listReply')) {
|
|
546
|
+
m.listResponseMessage = { ...message.listReply };
|
|
547
|
+
}
|
|
548
|
+
else if (hasNonNullishProperty(message, 'event')) {
|
|
549
|
+
m.eventMessage = {};
|
|
550
|
+
const startTime = Math.floor(message.event.startDate.getTime() / 1000);
|
|
551
|
+
if (message.event.call && options.getCallLink) {
|
|
552
|
+
const token = await options.getCallLink(message.event.call, { startTime });
|
|
553
|
+
m.eventMessage.joinLink = (message.event.call === 'audio' ? CALL_AUDIO_PREFIX : CALL_VIDEO_PREFIX) + token;
|
|
554
|
+
}
|
|
555
|
+
m.messageContextInfo = {
|
|
556
|
+
// encKey
|
|
557
|
+
messageSecret: message.event.messageSecret || randomBytes(32)
|
|
558
|
+
};
|
|
559
|
+
m.eventMessage.name = message.event.name;
|
|
560
|
+
m.eventMessage.description = message.event.description;
|
|
561
|
+
m.eventMessage.startTime = startTime;
|
|
562
|
+
m.eventMessage.endTime = message.event.endDate ? message.event.endDate.getTime() / 1000 : undefined;
|
|
563
|
+
m.eventMessage.isCanceled = message.event.isCancelled ?? false;
|
|
564
|
+
m.eventMessage.extraGuestsAllowed = message.event.extraGuestsAllowed;
|
|
565
|
+
m.eventMessage.isScheduleCall = message.event.isScheduleCall ?? false;
|
|
566
|
+
m.eventMessage.location = message.event.location;
|
|
567
|
+
}
|
|
568
|
+
else if (hasNonNullishProperty(message, 'poll')) {
|
|
569
|
+
// `selectableOptionsCount` is the proto field name; `selectableCount` is the
|
|
570
|
+
// historic Baileys name. Accept both, with the proto name winning.
|
|
571
|
+
const selectableCount = message.poll.selectableOptionsCount ?? message.poll.selectableCount ?? 0;
|
|
572
|
+
const toAnnouncementGroup = message.poll.toAnnouncementGroup ?? false;
|
|
573
|
+
const pollType = message.poll.pollType ?? proto.Message.PollType.POLL;
|
|
574
|
+
if (!Array.isArray(message.poll.values)) {
|
|
575
|
+
throw new Boom('Invalid poll values', { statusCode: 400 });
|
|
576
|
+
}
|
|
577
|
+
if (selectableCount < 0 || selectableCount > message.poll.values.length) {
|
|
578
|
+
throw new Boom(`poll.selectableCount in poll should be >= 0 and <= ${message.poll.values.length}`, {
|
|
579
|
+
statusCode: 400
|
|
580
|
+
});
|
|
581
|
+
}
|
|
582
|
+
if (pollType === proto.Message.PollType.QUIZ) {
|
|
583
|
+
if (selectableCount !== 1) {
|
|
584
|
+
throw new Boom('quiz polls must allow exactly one selection', { statusCode: 400 });
|
|
585
|
+
}
|
|
586
|
+
if (!message.poll.correctAnswer) {
|
|
587
|
+
throw new Boom('quiz polls need a correctAnswer', { statusCode: 400 });
|
|
588
|
+
}
|
|
589
|
+
if (!message.poll.values.includes(message.poll.correctAnswer)) {
|
|
590
|
+
throw new Boom('quiz correctAnswer must be one of the poll values', { statusCode: 400 });
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
if (message.poll.endTime !== undefined && message.poll.endTime <= Date.now() / 1000) {
|
|
594
|
+
throw new Boom('poll endTime must be in the future', { statusCode: 400 });
|
|
595
|
+
}
|
|
596
|
+
m.messageContextInfo = {
|
|
597
|
+
// encKey
|
|
598
|
+
messageSecret: message.poll.messageSecret || randomBytes(32)
|
|
599
|
+
};
|
|
600
|
+
const pollCreationMessage = {
|
|
601
|
+
name: message.poll.name,
|
|
602
|
+
selectableOptionsCount: selectableCount,
|
|
603
|
+
options: message.poll.values.map(optionName => ({ optionName })),
|
|
604
|
+
pollType,
|
|
605
|
+
correctAnswer: message.poll.correctAnswer ? { optionName: message.poll.correctAnswer } : undefined,
|
|
606
|
+
endTime: message.poll.endTime,
|
|
607
|
+
hideParticipantName: message.poll.hideParticipantName,
|
|
608
|
+
allowAddOption: message.poll.allowAddOption
|
|
609
|
+
};
|
|
610
|
+
if (toAnnouncementGroup) {
|
|
611
|
+
// poll v2 is for community announcement groups (single select and multiple)
|
|
612
|
+
m.pollCreationMessageV2 = pollCreationMessage;
|
|
613
|
+
}
|
|
614
|
+
else {
|
|
615
|
+
if (selectableCount === 1) {
|
|
616
|
+
//poll v3 is for single select polls
|
|
617
|
+
m.pollCreationMessageV3 = pollCreationMessage;
|
|
618
|
+
}
|
|
619
|
+
else {
|
|
620
|
+
// poll for multiple choice polls
|
|
621
|
+
m.pollCreationMessage = pollCreationMessage;
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
else if (hasNonNullishProperty(message, 'pollAddOption')) {
|
|
626
|
+
// Add one option to an existing poll. The option hash is the same SHA-256
|
|
627
|
+
// of the option name the vote aggregation uses to match votes to options.
|
|
628
|
+
if (!message.pollAddOption.option) {
|
|
629
|
+
throw new Boom('pollAddOption needs an option', { statusCode: 400 });
|
|
630
|
+
}
|
|
631
|
+
m.pollAddOptionMessage = {
|
|
632
|
+
pollCreationMessageKey: message.pollAddOption.pollCreationMessageKey,
|
|
633
|
+
addOption: {
|
|
634
|
+
optionName: message.pollAddOption.option,
|
|
635
|
+
optionHash: sha256(Buffer.from(message.pollAddOption.option)).toString()
|
|
636
|
+
}
|
|
637
|
+
};
|
|
638
|
+
}
|
|
639
|
+
else if (hasNonNullishProperty(message, 'pollUpdateMessage')) {
|
|
640
|
+
// Manual poll vote update, with a pre-encrypted vote. Voting is usually
|
|
641
|
+
// handled internally; this is the raw passthrough for callers that
|
|
642
|
+
// encrypted the vote themselves.
|
|
643
|
+
m.pollUpdateMessage = message.pollUpdateMessage;
|
|
644
|
+
}
|
|
645
|
+
else if (hasNonNullishProperty(message, 'pollAddOptionMessage')) {
|
|
646
|
+
// Raw poll add-option message, with a pre-built option.
|
|
647
|
+
m.pollAddOptionMessage = message.pollAddOptionMessage;
|
|
648
|
+
}
|
|
649
|
+
else if (hasNonNullishProperty(message, 'album')) {
|
|
650
|
+
m.albumMessage = {
|
|
651
|
+
expectedImageCount: message.album.expectedImageCount,
|
|
652
|
+
expectedVideoCount: message.album.expectedVideoCount
|
|
653
|
+
};
|
|
654
|
+
}
|
|
655
|
+
else if (hasNonNullishProperty(message, 'sharePhoneNumber')) {
|
|
656
|
+
m.protocolMessage = {
|
|
657
|
+
type: proto.Message.ProtocolMessage.Type.SHARE_PHONE_NUMBER
|
|
658
|
+
};
|
|
659
|
+
}
|
|
660
|
+
else if (hasNonNullishProperty(message, 'requestPhoneNumber')) {
|
|
661
|
+
m.requestPhoneNumberMessage = {};
|
|
662
|
+
}
|
|
663
|
+
else if (hasNonNullishProperty(message, 'limitSharing')) {
|
|
664
|
+
m.protocolMessage = {
|
|
665
|
+
type: proto.Message.ProtocolMessage.Type.LIMIT_SHARING,
|
|
666
|
+
limitSharing: {
|
|
667
|
+
sharingLimited: message.limitSharing === true,
|
|
668
|
+
trigger: 1,
|
|
669
|
+
limitSharingSettingTimestamp: Date.now(),
|
|
670
|
+
initiatedByMe: true
|
|
671
|
+
}
|
|
672
|
+
};
|
|
673
|
+
}
|
|
674
|
+
else {
|
|
675
|
+
m = await prepareWAMessageMedia(message, options);
|
|
676
|
+
}
|
|
677
|
+
// Interactive messages: native-flow buttons wrap the content built above
|
|
678
|
+
// (a text body, or a prepared media header). `interactiveButtons` is the
|
|
679
|
+
// gifted-btns-style alias for `nativeFlow`. This is a second stage, not
|
|
680
|
+
// another branch of the chain above: `{ text, nativeFlow }` must reach
|
|
681
|
+
// here, and the text branch above would swallow it first.
|
|
682
|
+
if ('nativeFlow' in message || 'interactiveButtons' in message) {
|
|
683
|
+
const flowButtons = message.nativeFlow ?? message.interactiveButtons;
|
|
684
|
+
if (!flowButtons) {
|
|
685
|
+
throw new Boom('nativeFlow buttons are required', { statusCode: 400 });
|
|
686
|
+
}
|
|
687
|
+
const flowMessage = { ...message, nativeFlow: flowButtons };
|
|
688
|
+
const interactiveMessage = {
|
|
689
|
+
nativeFlowMessage: prepareNativeFlowButtons(flowMessage)
|
|
690
|
+
};
|
|
691
|
+
if (hasOptionalProperty(message, 'bizJid')) {
|
|
692
|
+
interactiveMessage.collectionMessage = {
|
|
693
|
+
bizJid: message.bizJid,
|
|
694
|
+
id: message.id,
|
|
695
|
+
messageVersion: 1
|
|
696
|
+
};
|
|
697
|
+
}
|
|
698
|
+
else if (hasOptionalProperty(message, 'shopSurface')) {
|
|
699
|
+
interactiveMessage.shopStorefrontMessage = {
|
|
700
|
+
surface: message.shopSurface,
|
|
701
|
+
id: message.id,
|
|
702
|
+
messageVersion: 1
|
|
703
|
+
};
|
|
704
|
+
}
|
|
705
|
+
if (hasOptionalProperty(message, 'text')) {
|
|
706
|
+
interactiveMessage.body = { text: message.text };
|
|
707
|
+
}
|
|
708
|
+
else {
|
|
709
|
+
if (hasOptionalProperty(message, 'caption')) {
|
|
710
|
+
const isValidHeader = hasValidInteractiveHeader(m);
|
|
711
|
+
if (!isValidHeader) {
|
|
712
|
+
throw new Boom('Invalid media type for interactive message header', { statusCode: 400 });
|
|
713
|
+
}
|
|
714
|
+
interactiveMessage.header = {
|
|
715
|
+
title: message.title || '',
|
|
716
|
+
subtitle: message.subtitle || '',
|
|
717
|
+
hasMediaAttachment: isValidHeader
|
|
718
|
+
};
|
|
719
|
+
interactiveMessage.body = { text: message.caption };
|
|
720
|
+
}
|
|
721
|
+
// NOTE: the reference also sets interactiveMessage.jpegThumbnail
|
|
722
|
+
// from message.thumbnail, but neither proto defines that field on
|
|
723
|
+
// InteractiveMessage, so it is dropped on encode — omitted here.
|
|
724
|
+
if (interactiveMessage.header) {
|
|
725
|
+
Object.assign(interactiveMessage.header, m);
|
|
726
|
+
}
|
|
727
|
+
}
|
|
728
|
+
if (hasOptionalProperty(message, 'audioFooter')) {
|
|
729
|
+
const { audioMessage } = await prepareWAMessageMedia({ audio: message.audioFooter }, options);
|
|
730
|
+
interactiveMessage.footer = {
|
|
731
|
+
audioMessage,
|
|
732
|
+
hasMediaAttachment: true
|
|
733
|
+
};
|
|
734
|
+
}
|
|
735
|
+
else if (hasOptionalProperty(message, 'footer')) {
|
|
736
|
+
interactiveMessage.footer = { text: message.footer };
|
|
737
|
+
}
|
|
738
|
+
m = { interactiveMessage };
|
|
739
|
+
}
|
|
740
|
+
// View-once text (and media) in the V2 envelope: the official client sends
|
|
741
|
+
// viewOnceMessageV2 — or the voice-note-capable viewOnceMessageV2Extension —
|
|
742
|
+
// with the viewOnce flag on the inner message itself. The old v1 envelope is
|
|
743
|
+
// kept for plain `viewOnce`, which predates view-once text.
|
|
744
|
+
if ((hasOptionalProperty(message, 'viewOnceV2') && !!message.viewOnceV2) ||
|
|
745
|
+
(hasOptionalProperty(message, 'viewOnceV2Extension') && !!message.viewOnceV2Extension)) {
|
|
746
|
+
if (m.extendedTextMessage) {
|
|
747
|
+
m.extendedTextMessage.viewOnce = true;
|
|
748
|
+
}
|
|
749
|
+
else if (m.imageMessage) {
|
|
750
|
+
m.imageMessage.viewOnce = true;
|
|
751
|
+
}
|
|
752
|
+
else if (m.videoMessage) {
|
|
753
|
+
m.videoMessage.viewOnce = true;
|
|
754
|
+
}
|
|
755
|
+
else if (m.ptvMessage) {
|
|
756
|
+
m.ptvMessage.viewOnce = true;
|
|
757
|
+
}
|
|
758
|
+
else if (m.audioMessage) {
|
|
759
|
+
m.audioMessage.viewOnce = true;
|
|
760
|
+
}
|
|
761
|
+
if (hasOptionalProperty(message, 'viewOnceV2') && !!message.viewOnceV2) {
|
|
762
|
+
m = { viewOnceMessageV2: { message: m } };
|
|
763
|
+
}
|
|
764
|
+
else {
|
|
765
|
+
m = { viewOnceMessageV2Extension: { message: m } };
|
|
766
|
+
}
|
|
767
|
+
}
|
|
768
|
+
else if (hasOptionalProperty(message, 'viewOnce') && !!message.viewOnce) {
|
|
769
|
+
m = { viewOnceMessage: { message: m } };
|
|
770
|
+
}
|
|
771
|
+
// The content object that carries contextInfo (mentions, quoted message,
|
|
772
|
+
// ...). For a view-once envelope the contextInfo lives on the INNER
|
|
773
|
+
// message - the wrapper (viewOnceMessage / V2 / V2Extension) has no
|
|
774
|
+
// contextInfo field in the proto, so anything set on it is dropped on
|
|
775
|
+
// encode and mentions never render.
|
|
776
|
+
const contextInfoTarget = () => {
|
|
777
|
+
const wrapper = m.viewOnceMessage || m.viewOnceMessageV2 || m.viewOnceMessageV2Extension;
|
|
778
|
+
if (wrapper?.message) {
|
|
779
|
+
const innerType = Object.keys(wrapper.message)[0];
|
|
780
|
+
return wrapper.message[innerType];
|
|
781
|
+
}
|
|
782
|
+
const messageType = Object.keys(m)[0];
|
|
783
|
+
return m[messageType];
|
|
784
|
+
};
|
|
785
|
+
if ((hasOptionalProperty(message, 'mentions') && message.mentions?.length) ||
|
|
786
|
+
(hasOptionalProperty(message, 'mentionAll') && message.mentionAll)) {
|
|
787
|
+
const key = contextInfoTarget();
|
|
788
|
+
if (key && 'contextInfo' in key) {
|
|
789
|
+
key.contextInfo = key.contextInfo || {};
|
|
790
|
+
if (message.mentions?.length) {
|
|
791
|
+
key.contextInfo.mentionedJid = message.mentions;
|
|
792
|
+
}
|
|
793
|
+
if (message.mentionAll) {
|
|
794
|
+
key.contextInfo.nonJidMentions = 1;
|
|
795
|
+
}
|
|
796
|
+
}
|
|
797
|
+
else if (key) {
|
|
798
|
+
key.contextInfo = {
|
|
799
|
+
mentionedJid: message.mentions,
|
|
800
|
+
nonJidMentions: message.mentionAll ? 1 : 0
|
|
801
|
+
};
|
|
802
|
+
}
|
|
803
|
+
}
|
|
804
|
+
if (hasOptionalProperty(message, 'edit')) {
|
|
805
|
+
m = {
|
|
806
|
+
protocolMessage: {
|
|
807
|
+
key: message.edit,
|
|
808
|
+
editedMessage: m,
|
|
809
|
+
timestampMs: Date.now(),
|
|
810
|
+
type: WAProto.Message.ProtocolMessage.Type.MESSAGE_EDIT
|
|
811
|
+
}
|
|
812
|
+
};
|
|
813
|
+
}
|
|
814
|
+
if (hasOptionalProperty(message, 'contextInfo') && !!message.contextInfo) {
|
|
815
|
+
const key = contextInfoTarget();
|
|
816
|
+
if ('contextInfo' in key && !!key.contextInfo) {
|
|
817
|
+
key.contextInfo = { ...key.contextInfo, ...message.contextInfo };
|
|
818
|
+
}
|
|
819
|
+
else if (key) {
|
|
820
|
+
key.contextInfo = message.contextInfo;
|
|
821
|
+
}
|
|
822
|
+
}
|
|
823
|
+
if (hasOptionalProperty(message, 'albumParentKey') && !!message.albumParentKey) {
|
|
824
|
+
m.messageContextInfo = {
|
|
825
|
+
...m.messageContextInfo,
|
|
826
|
+
messageAssociation: {
|
|
827
|
+
associationType: WAProto.MessageAssociation.AssociationType.MEDIA_ALBUM,
|
|
828
|
+
parentMessageKey: message.albumParentKey
|
|
829
|
+
}
|
|
830
|
+
};
|
|
831
|
+
}
|
|
832
|
+
if (shouldIncludeReportingToken(m)) {
|
|
833
|
+
m.messageContextInfo = m.messageContextInfo || {};
|
|
834
|
+
if (!m.messageContextInfo.messageSecret) {
|
|
835
|
+
m.messageContextInfo.messageSecret = randomBytes(32);
|
|
836
|
+
}
|
|
837
|
+
}
|
|
838
|
+
return WAProto.Message.create(m);
|
|
839
|
+
};
|
|
840
|
+
export const generateWAMessageFromContent = (jid, message, options) => {
|
|
841
|
+
// set timestamp to now
|
|
842
|
+
// if not specified
|
|
843
|
+
if (!options.timestamp) {
|
|
844
|
+
options.timestamp = new Date();
|
|
845
|
+
}
|
|
846
|
+
const innerMessage = normalizeMessageContent(message);
|
|
847
|
+
const key = getContentType(innerMessage);
|
|
848
|
+
const timestamp = unixTimestampSeconds(options.timestamp);
|
|
849
|
+
const { quoted, userJid } = options;
|
|
850
|
+
if (quoted && !isJidNewsletter(jid)) {
|
|
851
|
+
const participant = quoted.key.fromMe
|
|
852
|
+
? userJid // TODO: Add support for LIDs
|
|
853
|
+
: quoted.participant || quoted.key.participant || quoted.key.remoteJid;
|
|
854
|
+
let quotedMsg = normalizeMessageContent(quoted.message);
|
|
855
|
+
const msgType = getContentType(quotedMsg);
|
|
856
|
+
// strip any redundant properties
|
|
857
|
+
quotedMsg = proto.Message.create({ [msgType]: quotedMsg[msgType] });
|
|
858
|
+
const quotedContent = quotedMsg[msgType];
|
|
859
|
+
if (typeof quotedContent === 'object' && quotedContent && 'contextInfo' in quotedContent) {
|
|
860
|
+
delete quotedContent.contextInfo;
|
|
861
|
+
}
|
|
862
|
+
const contextInfo = ('contextInfo' in innerMessage[key] && innerMessage[key]?.contextInfo) || {};
|
|
863
|
+
contextInfo.participant = jidNormalizedUser(participant);
|
|
864
|
+
contextInfo.stanzaId = quoted.key.id;
|
|
865
|
+
contextInfo.quotedMessage = quotedMsg;
|
|
866
|
+
// if a participant is quoted, then it must be a group
|
|
867
|
+
// hence, remoteJid of group must also be entered
|
|
868
|
+
if (jid !== quoted.key.remoteJid) {
|
|
869
|
+
contextInfo.remoteJid = quoted.key.remoteJid;
|
|
870
|
+
}
|
|
871
|
+
if (contextInfo && innerMessage[key]) {
|
|
872
|
+
/* @ts-ignore */
|
|
873
|
+
innerMessage[key].contextInfo = contextInfo;
|
|
874
|
+
}
|
|
875
|
+
}
|
|
876
|
+
if (
|
|
877
|
+
// if we want to send a disappearing message
|
|
878
|
+
!!options?.ephemeralExpiration &&
|
|
879
|
+
// and it's not a protocol message -- delete, toggle disappear message
|
|
880
|
+
key !== 'protocolMessage' &&
|
|
881
|
+
// already not converted to disappearing message
|
|
882
|
+
key !== 'ephemeralMessage' &&
|
|
883
|
+
// newsletters don't support ephemeral messages
|
|
884
|
+
!isJidNewsletter(jid)) {
|
|
885
|
+
/* @ts-ignore */
|
|
886
|
+
innerMessage[key].contextInfo = {
|
|
887
|
+
...(innerMessage[key].contextInfo || {}),
|
|
888
|
+
expiration: options.ephemeralExpiration || WA_DEFAULT_EPHEMERAL
|
|
889
|
+
//ephemeralSettingTimestamp: options.ephemeralOptions.eph_setting_ts?.toString()
|
|
890
|
+
};
|
|
891
|
+
}
|
|
892
|
+
message = WAProto.Message.create(message);
|
|
893
|
+
const messageJSON = {
|
|
894
|
+
key: {
|
|
895
|
+
remoteJid: jid,
|
|
896
|
+
fromMe: true,
|
|
897
|
+
id: options?.messageId || generateMessageIDV2()
|
|
898
|
+
},
|
|
899
|
+
message: message,
|
|
900
|
+
messageTimestamp: timestamp,
|
|
901
|
+
messageStubParameters: [],
|
|
902
|
+
participant: isJidGroup(jid) || isJidStatusBroadcast(jid) ? userJid : undefined, // TODO: Add support for LIDs
|
|
903
|
+
status: WAMessageStatus.PENDING
|
|
904
|
+
};
|
|
905
|
+
return WAProto.WebMessageInfo.fromObject(messageJSON);
|
|
906
|
+
};
|
|
907
|
+
export const generateWAMessage = async (jid, content, options) => {
|
|
908
|
+
// ensure msg ID is with every log
|
|
909
|
+
options.logger = options?.logger?.child({ msgId: options.messageId });
|
|
910
|
+
// Pass jid in the options to generateWAMessageContent
|
|
911
|
+
return generateWAMessageFromContent(jid, await generateWAMessageContent(content, { ...options, jid }), options);
|
|
912
|
+
};
|
|
913
|
+
/** Get the key to access the true type of content */
|
|
914
|
+
export const getContentType = (content) => {
|
|
915
|
+
if (content) {
|
|
916
|
+
const keys = Object.keys(content);
|
|
917
|
+
const key = keys.find(k => (k === 'conversation' || k.includes('Message')) && k !== 'senderKeyDistributionMessage');
|
|
918
|
+
return key;
|
|
919
|
+
}
|
|
920
|
+
};
|
|
921
|
+
/**
|
|
922
|
+
* Normalizes ephemeral, view once messages to regular message content
|
|
923
|
+
* Eg. image messages in ephemeral messages, in view once messages etc.
|
|
924
|
+
* @param content
|
|
925
|
+
* @returns
|
|
926
|
+
*/
|
|
927
|
+
export const normalizeMessageContent = (content) => {
|
|
928
|
+
if (!content) {
|
|
929
|
+
return undefined;
|
|
930
|
+
}
|
|
931
|
+
// set max iterations to prevent an infinite loop
|
|
932
|
+
for (let i = 0; i < 5; i++) {
|
|
933
|
+
const inner = getFutureProofMessage(content);
|
|
934
|
+
if (!inner) {
|
|
935
|
+
break;
|
|
936
|
+
}
|
|
937
|
+
content = inner.message;
|
|
938
|
+
}
|
|
939
|
+
return content;
|
|
940
|
+
function getFutureProofMessage(message) {
|
|
941
|
+
return (message?.ephemeralMessage ||
|
|
942
|
+
message?.viewOnceMessage ||
|
|
943
|
+
message?.documentWithCaptionMessage ||
|
|
944
|
+
message?.viewOnceMessageV2 ||
|
|
945
|
+
message?.viewOnceMessageV2Extension ||
|
|
946
|
+
message?.editedMessage ||
|
|
947
|
+
message?.associatedChildMessage ||
|
|
948
|
+
message?.groupStatusMessage ||
|
|
949
|
+
message?.groupStatusMessageV2);
|
|
950
|
+
}
|
|
951
|
+
};
|
|
952
|
+
/**
|
|
953
|
+
* Whether the outgoing content is view once media (image, video, round video or audio).
|
|
954
|
+
* WA Web tags these sends with a `<meta view_once="true"/>` stanza node.
|
|
955
|
+
* Looks through a disappearing message wrapper. View once wrappers around
|
|
956
|
+
* anything else (e.g. buttons wrapped so they render) don't count.
|
|
957
|
+
*/
|
|
958
|
+
export const isViewOnceMessage = (content) => {
|
|
959
|
+
const inner = content?.ephemeralMessage?.message ?? content;
|
|
960
|
+
const wrapped = (inner?.viewOnceMessage || inner?.viewOnceMessageV2 || inner?.viewOnceMessageV2Extension)?.message;
|
|
961
|
+
return !!(wrapped?.imageMessage || wrapped?.videoMessage || wrapped?.ptvMessage || wrapped?.audioMessage);
|
|
962
|
+
};
|
|
963
|
+
const VIEW_ONCE_MEDIA = {
|
|
964
|
+
imageMessage: 'image',
|
|
965
|
+
videoMessage: 'video',
|
|
966
|
+
audioMessage: 'audio',
|
|
967
|
+
documentMessage: 'document',
|
|
968
|
+
ptvMessage: 'ptv'
|
|
969
|
+
};
|
|
970
|
+
/**
|
|
971
|
+
* Unwrap a message down to view once media, if it carries any.
|
|
972
|
+
*
|
|
973
|
+
* Peels disappearing-message wrappers and view once wrappers (V1, V2 and
|
|
974
|
+
* V2Extension, nested up to 5 deep) plus documentWithCaptionMessage, then
|
|
975
|
+
* returns the inner media message. Unlike `isViewOnceMessage` — which only
|
|
976
|
+
* looks one disappearing layer deep and only counts image/video/round-video/
|
|
977
|
+
* audio, because it drives the `<meta view_once>` tag on sends — this also
|
|
978
|
+
* counts document view once, and a bare media message carrying the `viewOnce`
|
|
979
|
+
* flag itself (e.g. a copy restored from a store, where the wrapper was
|
|
980
|
+
* already stripped).
|
|
981
|
+
*
|
|
982
|
+
* Returns undefined when the content holds no media at all.
|
|
983
|
+
*/
|
|
984
|
+
export const extractViewOnceMedia = (content) => {
|
|
985
|
+
if (!content) {
|
|
986
|
+
return undefined;
|
|
987
|
+
}
|
|
988
|
+
// did the content arrive inside a view once wrapper at any depth?
|
|
989
|
+
const seenWrapper = (node, depth = 0) => {
|
|
990
|
+
if (!node || depth > 5) {
|
|
991
|
+
return false;
|
|
992
|
+
}
|
|
993
|
+
if (node.viewOnceMessage?.message || node.viewOnceMessageV2?.message || node.viewOnceMessageV2Extension?.message) {
|
|
994
|
+
return true;
|
|
995
|
+
}
|
|
996
|
+
return seenWrapper(node.ephemeralMessage?.message, depth + 1);
|
|
997
|
+
};
|
|
998
|
+
const wrapped = seenWrapper(content);
|
|
999
|
+
const inner = normalizeMessageContent(content);
|
|
1000
|
+
if (!inner) {
|
|
1001
|
+
return undefined;
|
|
1002
|
+
}
|
|
1003
|
+
const innerType = getContentType(inner);
|
|
1004
|
+
if (!innerType) {
|
|
1005
|
+
return undefined;
|
|
1006
|
+
}
|
|
1007
|
+
const mediaType = VIEW_ONCE_MEDIA[innerType];
|
|
1008
|
+
if (!mediaType) {
|
|
1009
|
+
return undefined;
|
|
1010
|
+
}
|
|
1011
|
+
const media = inner[innerType];
|
|
1012
|
+
const isViewOnce = wrapped || media?.viewOnce === true;
|
|
1013
|
+
return { media, mediaType, isViewOnce };
|
|
1014
|
+
};
|
|
1015
|
+
/**
|
|
1016
|
+
* Extract the true message content from a message
|
|
1017
|
+
* Eg. extracts the inner message from a disappearing message/view once message
|
|
1018
|
+
*/
|
|
1019
|
+
export const extractMessageContent = (content) => {
|
|
1020
|
+
const extractFromTemplateMessage = (msg) => {
|
|
1021
|
+
if (msg.imageMessage) {
|
|
1022
|
+
return { imageMessage: msg.imageMessage };
|
|
1023
|
+
}
|
|
1024
|
+
else if (msg.documentMessage) {
|
|
1025
|
+
return { documentMessage: msg.documentMessage };
|
|
1026
|
+
}
|
|
1027
|
+
else if (msg.videoMessage) {
|
|
1028
|
+
return { videoMessage: msg.videoMessage };
|
|
1029
|
+
}
|
|
1030
|
+
else if (msg.locationMessage) {
|
|
1031
|
+
return { locationMessage: msg.locationMessage };
|
|
1032
|
+
}
|
|
1033
|
+
else {
|
|
1034
|
+
return {
|
|
1035
|
+
conversation: 'contentText' in msg ? msg.contentText : 'hydratedContentText' in msg ? msg.hydratedContentText : ''
|
|
1036
|
+
};
|
|
1037
|
+
}
|
|
1038
|
+
};
|
|
1039
|
+
content = normalizeMessageContent(content);
|
|
1040
|
+
if (content?.buttonsMessage) {
|
|
1041
|
+
return extractFromTemplateMessage(content.buttonsMessage);
|
|
1042
|
+
}
|
|
1043
|
+
if (content?.templateMessage?.hydratedFourRowTemplate) {
|
|
1044
|
+
return extractFromTemplateMessage(content?.templateMessage?.hydratedFourRowTemplate);
|
|
1045
|
+
}
|
|
1046
|
+
if (content?.templateMessage?.hydratedTemplate) {
|
|
1047
|
+
return extractFromTemplateMessage(content?.templateMessage?.hydratedTemplate);
|
|
1048
|
+
}
|
|
1049
|
+
if (content?.templateMessage?.fourRowTemplate) {
|
|
1050
|
+
return extractFromTemplateMessage(content?.templateMessage?.fourRowTemplate);
|
|
1051
|
+
}
|
|
1052
|
+
return content;
|
|
1053
|
+
};
|
|
1054
|
+
/**
|
|
1055
|
+
* Returns the device predicted by message ID
|
|
1056
|
+
*/
|
|
1057
|
+
export const getDevice = (id) => /^3A.{18}$/.test(id)
|
|
1058
|
+
? 'ios'
|
|
1059
|
+
: /^3E.{20}$/.test(id)
|
|
1060
|
+
? 'web'
|
|
1061
|
+
: /^(.{21}|.{32})$/.test(id)
|
|
1062
|
+
? 'android'
|
|
1063
|
+
: /^(3F|.{18}$)/.test(id)
|
|
1064
|
+
? 'desktop'
|
|
1065
|
+
: 'unknown';
|
|
1066
|
+
/** Upserts a receipt in the message */
|
|
1067
|
+
export const updateMessageWithReceipt = (msg, receipt) => {
|
|
1068
|
+
msg.userReceipt = msg.userReceipt || [];
|
|
1069
|
+
const recp = msg.userReceipt.find(m => m.userJid === receipt.userJid);
|
|
1070
|
+
if (recp) {
|
|
1071
|
+
Object.assign(recp, receipt);
|
|
1072
|
+
}
|
|
1073
|
+
else {
|
|
1074
|
+
msg.userReceipt.push(receipt);
|
|
1075
|
+
}
|
|
1076
|
+
};
|
|
1077
|
+
/** Update the message with a new reaction */
|
|
1078
|
+
export const updateMessageWithReaction = (msg, reaction) => {
|
|
1079
|
+
const authorID = getKeyAuthor(reaction.key);
|
|
1080
|
+
const reactions = (msg.reactions || []).filter(r => getKeyAuthor(r.key) !== authorID);
|
|
1081
|
+
reaction.text = reaction.text || '';
|
|
1082
|
+
reactions.push(reaction);
|
|
1083
|
+
msg.reactions = reactions;
|
|
1084
|
+
};
|
|
1085
|
+
/** Update the message with a new poll update */
|
|
1086
|
+
export const updateMessageWithPollUpdate = (msg, update) => {
|
|
1087
|
+
const authorID = getKeyAuthor(update.pollUpdateMessageKey);
|
|
1088
|
+
const reactions = (msg.pollUpdates || []).filter(r => getKeyAuthor(r.pollUpdateMessageKey) !== authorID);
|
|
1089
|
+
if (update.vote?.selectedOptions?.length) {
|
|
1090
|
+
reactions.push(update);
|
|
1091
|
+
}
|
|
1092
|
+
msg.pollUpdates = reactions;
|
|
1093
|
+
};
|
|
1094
|
+
/** Update the message with a new event response */
|
|
1095
|
+
export const updateMessageWithEventResponse = (msg, update) => {
|
|
1096
|
+
const authorID = getKeyAuthor(update.eventResponseMessageKey);
|
|
1097
|
+
const responses = (msg.eventResponses || []).filter(r => getKeyAuthor(r.eventResponseMessageKey) !== authorID);
|
|
1098
|
+
responses.push(update);
|
|
1099
|
+
msg.eventResponses = responses;
|
|
1100
|
+
};
|
|
1101
|
+
/**
|
|
1102
|
+
* Aggregates all poll updates in a poll.
|
|
1103
|
+
* @param msg the poll creation message
|
|
1104
|
+
* @param meId your jid
|
|
1105
|
+
* @returns A list of options & their voters
|
|
1106
|
+
*/
|
|
1107
|
+
export function getAggregateVotesInPollMessage({ message, pollUpdates }, meId) {
|
|
1108
|
+
const opts = message?.pollCreationMessage?.options ||
|
|
1109
|
+
message?.pollCreationMessageV2?.options ||
|
|
1110
|
+
message?.pollCreationMessageV3?.options ||
|
|
1111
|
+
[];
|
|
1112
|
+
const voteHashMap = opts.reduce((acc, opt) => {
|
|
1113
|
+
const hash = sha256(Buffer.from(opt.optionName || '')).toString();
|
|
1114
|
+
acc[hash] = {
|
|
1115
|
+
name: opt.optionName || '',
|
|
1116
|
+
voters: []
|
|
1117
|
+
};
|
|
1118
|
+
return acc;
|
|
1119
|
+
}, {});
|
|
1120
|
+
for (const update of pollUpdates || []) {
|
|
1121
|
+
const { vote } = update;
|
|
1122
|
+
if (!vote) {
|
|
1123
|
+
continue;
|
|
1124
|
+
}
|
|
1125
|
+
for (const option of vote.selectedOptions || []) {
|
|
1126
|
+
const hash = option.toString();
|
|
1127
|
+
let data = voteHashMap[hash];
|
|
1128
|
+
if (!data) {
|
|
1129
|
+
voteHashMap[hash] = {
|
|
1130
|
+
name: 'Unknown',
|
|
1131
|
+
voters: []
|
|
1132
|
+
};
|
|
1133
|
+
data = voteHashMap[hash];
|
|
1134
|
+
}
|
|
1135
|
+
voteHashMap[hash].voters.push(getKeyAuthor(update.pollUpdateMessageKey, meId));
|
|
1136
|
+
}
|
|
1137
|
+
}
|
|
1138
|
+
return Object.values(voteHashMap);
|
|
1139
|
+
}
|
|
1140
|
+
/**
|
|
1141
|
+
* Aggregates all event responses in an event message.
|
|
1142
|
+
* @param msg the event creation message
|
|
1143
|
+
* @param meId your jid
|
|
1144
|
+
* @returns A list of response types & their responders
|
|
1145
|
+
*/
|
|
1146
|
+
export function getAggregateResponsesInEventMessage({ eventResponses }, meId) {
|
|
1147
|
+
const responseTypes = ['GOING', 'NOT_GOING', 'MAYBE'];
|
|
1148
|
+
const responseMap = {};
|
|
1149
|
+
for (const type of responseTypes) {
|
|
1150
|
+
responseMap[type] = {
|
|
1151
|
+
response: type,
|
|
1152
|
+
responders: []
|
|
1153
|
+
};
|
|
1154
|
+
}
|
|
1155
|
+
for (const update of eventResponses || []) {
|
|
1156
|
+
const responseType = update.eventResponse || 'UNKNOWN';
|
|
1157
|
+
if (responseType !== 'UNKNOWN' && responseMap[responseType]) {
|
|
1158
|
+
responseMap[responseType].responders.push(getKeyAuthor(update.eventResponseMessageKey, meId));
|
|
1159
|
+
}
|
|
1160
|
+
}
|
|
1161
|
+
return Object.values(responseMap);
|
|
1162
|
+
}
|
|
1163
|
+
/** Given a list of message keys, aggregates them by chat & sender. Useful for sending read receipts in bulk */
|
|
1164
|
+
export const aggregateMessageKeysNotFromMe = (keys) => {
|
|
1165
|
+
const keyMap = {};
|
|
1166
|
+
for (const { remoteJid, id, participant, fromMe } of keys) {
|
|
1167
|
+
if (!fromMe) {
|
|
1168
|
+
const uqKey = `${remoteJid}:${participant || ''}`;
|
|
1169
|
+
if (!keyMap[uqKey]) {
|
|
1170
|
+
keyMap[uqKey] = {
|
|
1171
|
+
jid: remoteJid,
|
|
1172
|
+
participant: participant,
|
|
1173
|
+
messageIds: []
|
|
1174
|
+
};
|
|
1175
|
+
}
|
|
1176
|
+
keyMap[uqKey].messageIds.push(id);
|
|
1177
|
+
}
|
|
1178
|
+
}
|
|
1179
|
+
return Object.values(keyMap);
|
|
1180
|
+
};
|
|
1181
|
+
const REUPLOAD_REQUIRED_STATUS = [410, 404];
|
|
1182
|
+
/**
|
|
1183
|
+
* When a media link's signature expires, in ms: its `oe` query parameter, in hex unix seconds.
|
|
1184
|
+
* Undefined unless the query carries exactly one `oe` and it is plain hex.
|
|
1185
|
+
*/
|
|
1186
|
+
const mediaLinkExpiry = (link) => {
|
|
1187
|
+
let values;
|
|
1188
|
+
try {
|
|
1189
|
+
// a directPath has no host; the base only makes it parseable
|
|
1190
|
+
values = new URL(link, 'https://mmg.whatsapp.net').searchParams.getAll('oe');
|
|
1191
|
+
}
|
|
1192
|
+
catch {
|
|
1193
|
+
return undefined;
|
|
1194
|
+
}
|
|
1195
|
+
const [oe] = values;
|
|
1196
|
+
if (values.length !== 1 || !oe || !/^[0-9a-f]+$/i.test(oe)) {
|
|
1197
|
+
return undefined;
|
|
1198
|
+
}
|
|
1199
|
+
const expiry = parseInt(oe, 16) * 1000;
|
|
1200
|
+
return Number.isSafeInteger(expiry) ? expiry : undefined;
|
|
1201
|
+
};
|
|
1202
|
+
/**
|
|
1203
|
+
* Whether a 403 is worth a reupload: the link that failed carries an `oe` that has passed.
|
|
1204
|
+
* A conservative heuristic, not a documented contract: in a sample of 84 history-sync
|
|
1205
|
+
* downloads the media CDN answered 403 for all 34 links whose `oe` had passed and for none
|
|
1206
|
+
* of the 50 whose `oe` had not. It reads the local clock, so a clock that is off moves the line.
|
|
1207
|
+
* The link is the url getHttpStream reports on its error, or else the one
|
|
1208
|
+
* downloadContentFromMessage requests: the directPath when there is one, else the url.
|
|
1209
|
+
*/
|
|
1210
|
+
const isExpiredLinkError = (error, message) => {
|
|
1211
|
+
let link = error?.data?.url;
|
|
1212
|
+
if (typeof link !== 'string' && !(link instanceof URL)) {
|
|
1213
|
+
const content = extractMessageContent(message.message);
|
|
1214
|
+
const contentType = content ? getContentType(content) : undefined;
|
|
1215
|
+
const media = (contentType && content?.[contentType]);
|
|
1216
|
+
link = media?.directPath || media?.url;
|
|
1217
|
+
}
|
|
1218
|
+
if (!link) {
|
|
1219
|
+
return false;
|
|
1220
|
+
}
|
|
1221
|
+
const expiry = mediaLinkExpiry(link.toString());
|
|
1222
|
+
return expiry !== undefined && expiry <= Date.now();
|
|
1223
|
+
};
|
|
1224
|
+
/**
|
|
1225
|
+
* Downloads the given message. Throws an error if it's not a media message
|
|
1226
|
+
*/
|
|
1227
|
+
export const downloadMediaMessage = async (message, type, options, ctx) => {
|
|
1228
|
+
const result = await downloadMsg().catch(async (error) => {
|
|
1229
|
+
// the media download throws a Boom, which carries the HTTP status on output.statusCode, not on status
|
|
1230
|
+
const status = error?.output?.statusCode ?? error?.status;
|
|
1231
|
+
const expired = typeof status === 'number' &&
|
|
1232
|
+
(REUPLOAD_REQUIRED_STATUS.includes(status) || (status === 403 && isExpiredLinkError(error, message)));
|
|
1233
|
+
if (ctx && expired) {
|
|
1234
|
+
ctx.logger.info({ key: message.key }, 'sending reupload media request...');
|
|
1235
|
+
// request reupload
|
|
1236
|
+
message = await ctx.reuploadRequest(message);
|
|
1237
|
+
const result = await downloadMsg();
|
|
1238
|
+
return result;
|
|
1239
|
+
}
|
|
1240
|
+
throw error;
|
|
1241
|
+
});
|
|
1242
|
+
return result;
|
|
1243
|
+
async function downloadMsg() {
|
|
1244
|
+
const mContent = extractMessageContent(message.message);
|
|
1245
|
+
if (!mContent) {
|
|
1246
|
+
throw new Boom('No message present', { statusCode: 400, data: message });
|
|
1247
|
+
}
|
|
1248
|
+
const contentType = getContentType(mContent);
|
|
1249
|
+
let mediaType = contentType?.replace('Message', '');
|
|
1250
|
+
const media = mContent[contentType];
|
|
1251
|
+
if (!media || typeof media !== 'object' || (!('url' in media) && !('thumbnailDirectPath' in media))) {
|
|
1252
|
+
throw new Boom(`"${contentType}" message is not a media message`);
|
|
1253
|
+
}
|
|
1254
|
+
let download;
|
|
1255
|
+
if ('thumbnailDirectPath' in media && !('url' in media)) {
|
|
1256
|
+
download = {
|
|
1257
|
+
directPath: media.thumbnailDirectPath,
|
|
1258
|
+
mediaKey: media.mediaKey
|
|
1259
|
+
};
|
|
1260
|
+
mediaType = 'thumbnail-link';
|
|
1261
|
+
}
|
|
1262
|
+
else {
|
|
1263
|
+
download = media;
|
|
1264
|
+
}
|
|
1265
|
+
const stream = await downloadContentFromMessage(download, mediaType, options);
|
|
1266
|
+
if (type === 'buffer') {
|
|
1267
|
+
const bufferArray = [];
|
|
1268
|
+
for await (const chunk of stream) {
|
|
1269
|
+
bufferArray.push(chunk);
|
|
1270
|
+
}
|
|
1271
|
+
return Buffer.concat(bufferArray);
|
|
1272
|
+
}
|
|
1273
|
+
return stream;
|
|
1274
|
+
}
|
|
1275
|
+
};
|
|
1276
|
+
/**
|
|
1277
|
+
* Download a view once photo/video/voice note/document/round video.
|
|
1278
|
+
*
|
|
1279
|
+
* This is the one-call path bots need for view once recovery: it unwraps the
|
|
1280
|
+
* message, checks it really is view once media, and downloads it. It closes
|
|
1281
|
+
* two gaps over `downloadMediaMessage` alone:
|
|
1282
|
+
*
|
|
1283
|
+
* - a view once that arrived with a media key but no URL/directPath (the
|
|
1284
|
+
* sender's device has not served the file yet) is re-requested first via
|
|
1285
|
+
* `ctx.reuploadRequest` — `downloadMediaMessage` throws before it can
|
|
1286
|
+
* reupload when there is no URL at all, so without this the media is
|
|
1287
|
+
* unrecoverable even though the sender could still serve it.
|
|
1288
|
+
* - the view once check itself: `isViewOnceMessage` is the send-path check
|
|
1289
|
+
* (one disappearing layer, no documents), so recovery uses the wider
|
|
1290
|
+
* `extractViewOnceMedia` instead.
|
|
1291
|
+
*
|
|
1292
|
+
* Throws a Boom (400) when the message is not view once media, or when it
|
|
1293
|
+
* has no download URL and no `reuploadRequest` was supplied to ask the
|
|
1294
|
+
* sender's device to re-serve it. Expired links are still handled by
|
|
1295
|
+
* `downloadMediaMessage` itself, which reuploads and retries.
|
|
1296
|
+
*/
|
|
1297
|
+
export const downloadViewOnceMessage = async (message, type, options, ctx) => {
|
|
1298
|
+
const extracted = extractViewOnceMedia(message.message);
|
|
1299
|
+
if (!extracted?.isViewOnce) {
|
|
1300
|
+
throw new Boom('message is not a view once media message', { statusCode: 400, data: message });
|
|
1301
|
+
}
|
|
1302
|
+
const { media } = extracted;
|
|
1303
|
+
if (!media.url && !media.directPath) {
|
|
1304
|
+
if (!media.mediaKey || !ctx?.reuploadRequest) {
|
|
1305
|
+
throw new Boom('view once media has no download URL; pass reuploadRequest (sock.updateMediaMessage) to ask the sender to re-serve it', { statusCode: 400, data: message });
|
|
1306
|
+
}
|
|
1307
|
+
ctx.logger.info({ key: message.key }, 'view once media has no URL, requesting re-upload...');
|
|
1308
|
+
message = await ctx.reuploadRequest(message);
|
|
1309
|
+
}
|
|
1310
|
+
return downloadMediaMessage(message, type, options, ctx);
|
|
1311
|
+
};
|
|
1312
|
+
/** Checks whether the given message is a media message; if it is returns the inner content */
|
|
1313
|
+
export const assertMediaContent = (content) => {
|
|
1314
|
+
content = extractMessageContent(content);
|
|
1315
|
+
const mediaContent = content?.documentMessage ||
|
|
1316
|
+
content?.imageMessage ||
|
|
1317
|
+
content?.videoMessage ||
|
|
1318
|
+
content?.audioMessage ||
|
|
1319
|
+
content?.stickerMessage;
|
|
1320
|
+
if (!mediaContent) {
|
|
1321
|
+
throw new Boom('given message is not a media message', { statusCode: 400, data: content });
|
|
1322
|
+
}
|
|
1323
|
+
return mediaContent;
|
|
1324
|
+
};
|
|
1325
|
+
//# sourceMappingURL=messages.js.map
|