@japofc/baileys 2.1.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/NOTICE.md +111 -0
- package/README.md +1254 -0
- package/WAProto/index.d.ts +9 -0
- package/WAProto/index.js +142276 -0
- package/engine-requirements.js +13 -0
- package/lib/Builders/A2UI.d.ts +242 -0
- package/lib/Builders/A2UI.js +308 -0
- package/lib/Builders/AIRich.d.ts +105 -0
- package/lib/Builders/AIRich.js +2286 -0
- package/lib/Builders/Button.d.ts +103 -0
- package/lib/Builders/Button.js +883 -0
- package/lib/Builders/ButtonV2.d.ts +28 -0
- package/lib/Builders/ButtonV2.js +144 -0
- package/lib/Builders/ButtonV3.d.ts +25 -0
- package/lib/Builders/ButtonV3.js +184 -0
- package/lib/Builders/Carousel.d.ts +22 -0
- package/lib/Builders/Carousel.js +111 -0
- package/lib/Builders/JapBaileys.d.ts +34 -0
- package/lib/Builders/JapBaileys.js +108 -0
- package/lib/Builders/Poll.d.ts +25 -0
- package/lib/Builders/Poll.js +128 -0
- package/lib/Builders/index.d.ts +17 -0
- package/lib/Builders/index.js +11 -0
- package/lib/Builders/shared.d.ts +67 -0
- package/lib/Builders/shared.js +652 -0
- package/lib/Defaults/index.d.ts +154 -0
- package/lib/Defaults/index.js +178 -0
- package/lib/Framework/Bot.d.ts +61 -0
- package/lib/Framework/Bot.js +248 -0
- package/lib/Framework/Context.d.ts +21 -0
- package/lib/Framework/Context.js +89 -0
- package/lib/Framework/MediaManager.d.ts +17 -0
- package/lib/Framework/MediaManager.js +151 -0
- package/lib/Framework/SessionManager.d.ts +13 -0
- package/lib/Framework/SessionManager.js +33 -0
- package/lib/Framework/StatsManager.d.ts +26 -0
- package/lib/Framework/StatsManager.js +119 -0
- package/lib/Framework/Store/SQLiteStore.d.ts +11 -0
- package/lib/Framework/Store/SQLiteStore.js +73 -0
- package/lib/Framework/index.d.ts +9 -0
- package/lib/Framework/index.js +10 -0
- package/lib/Signal/Group/ciphertext-message.d.ts +9 -0
- package/lib/Signal/Group/ciphertext-message.js +11 -0
- package/lib/Signal/Group/group-session-builder.d.ts +7 -0
- package/lib/Signal/Group/group-session-builder.js +29 -0
- package/lib/Signal/Group/group_cipher.d.ts +10 -0
- package/lib/Signal/Group/group_cipher.js +81 -0
- package/lib/Signal/Group/index.d.ts +11 -0
- package/lib/Signal/Group/index.js +11 -0
- package/lib/Signal/Group/keyhelper.d.ts +6 -0
- package/lib/Signal/Group/keyhelper.js +17 -0
- package/lib/Signal/Group/sender-chain-key.d.ts +13 -0
- package/lib/Signal/Group/sender-chain-key.js +25 -0
- package/lib/Signal/Group/sender-key-distribution-message.d.ts +16 -0
- package/lib/Signal/Group/sender-key-distribution-message.js +62 -0
- package/lib/Signal/Group/sender-key-message.d.ts +18 -0
- package/lib/Signal/Group/sender-key-message.js +65 -0
- package/lib/Signal/Group/sender-key-name.d.ts +11 -0
- package/lib/Signal/Group/sender-key-name.js +47 -0
- package/lib/Signal/Group/sender-key-record.d.ts +12 -0
- package/lib/Signal/Group/sender-key-record.js +40 -0
- package/lib/Signal/Group/sender-key-state.d.ts +16 -0
- package/lib/Signal/Group/sender-key-state.js +83 -0
- package/lib/Signal/Group/sender-message-key.d.ts +11 -0
- package/lib/Signal/Group/sender-message-key.js +25 -0
- package/lib/Signal/libsignal.d.ts +54 -0
- package/lib/Signal/libsignal.js +430 -0
- package/lib/Signal/lid-mapping.d.ts +20 -0
- package/lib/Signal/lid-mapping.js +276 -0
- package/lib/Socket/Client/index.d.ts +2 -0
- package/lib/Socket/Client/index.js +2 -0
- package/lib/Socket/Client/types.d.ts +5 -0
- package/lib/Socket/Client/types.js +10 -0
- package/lib/Socket/Client/websocket.d.ts +12 -0
- package/lib/Socket/Client/websocket.js +53 -0
- package/lib/Socket/business.d.ts +395 -0
- package/lib/Socket/business.js +379 -0
- package/lib/Socket/chats.d.ts +136 -0
- package/lib/Socket/chats.js +1327 -0
- package/lib/Socket/communities.d.ts +562 -0
- package/lib/Socket/communities.js +439 -0
- package/lib/Socket/dugong.d.ts +117 -0
- package/lib/Socket/dugong.js +798 -0
- package/lib/Socket/groups.d.ts +285 -0
- package/lib/Socket/groups.js +354 -0
- package/lib/Socket/index.d.ts +585 -0
- package/lib/Socket/index.js +52 -0
- package/lib/Socket/messages-recv.d.ts +333 -0
- package/lib/Socket/messages-recv.js +1781 -0
- package/lib/Socket/messages-send.d.ts +353 -0
- package/lib/Socket/messages-send.js +1524 -0
- package/lib/Socket/mex.d.ts +1 -0
- package/lib/Socket/mex.js +41 -0
- package/lib/Socket/newsletter.d.ts +286 -0
- package/lib/Socket/newsletter.js +364 -0
- package/lib/Socket/socket.d.ts +60 -0
- package/lib/Socket/socket.js +1012 -0
- package/lib/Socket/username.d.ts +82 -0
- package/lib/Socket/username.js +159 -0
- package/lib/Store/index.d.ts +3 -0
- package/lib/Store/index.js +3 -0
- package/lib/Store/make-in-memory-store.d.ts +62 -0
- package/lib/Store/make-in-memory-store.js +428 -0
- package/lib/Store/make-ordered-dictionary.d.ts +12 -0
- package/lib/Store/make-ordered-dictionary.js +78 -0
- package/lib/Store/object-repository.d.ts +10 -0
- package/lib/Store/object-repository.js +23 -0
- package/lib/Types/Auth.d.ts +117 -0
- package/lib/Types/Auth.js +1 -0
- package/lib/Types/Bussines.d.ts +25 -0
- package/lib/Types/Call.d.ts +15 -0
- package/lib/Types/Call.js +1 -0
- package/lib/Types/Chat.d.ts +124 -0
- package/lib/Types/Chat.js +7 -0
- package/lib/Types/Contact.d.ts +26 -0
- package/lib/Types/Contact.js +1 -0
- package/lib/Types/Events.d.ts +256 -0
- package/lib/Types/Events.js +1 -0
- package/lib/Types/GroupMetadata.d.ts +71 -0
- package/lib/Types/GroupMetadata.js +1 -0
- package/lib/Types/Label.d.ts +5 -0
- package/lib/Types/Label.js +24 -0
- package/lib/Types/LabelAssociation.d.ts +6 -0
- package/lib/Types/LabelAssociation.js +6 -0
- package/lib/Types/Message.d.ts +62 -0
- package/lib/Types/Message.js +17 -0
- package/lib/Types/Mex.d.ts +2 -0
- package/lib/Types/Mex.js +38 -0
- package/lib/Types/Product.d.ts +79 -0
- package/lib/Types/Product.js +1 -0
- package/lib/Types/RichType.d.ts +2 -0
- package/lib/Types/RichType.js +22 -0
- package/lib/Types/Signal.d.ts +87 -0
- package/lib/Types/Signal.js +1 -0
- package/lib/Types/Socket.d.ts +138 -0
- package/lib/Types/Socket.js +1 -0
- package/lib/Types/State.d.ts +8 -0
- package/lib/Types/State.js +55 -0
- package/lib/Types/USync.d.ts +26 -0
- package/lib/Types/index.d.ts +68 -0
- package/lib/Types/index.js +27 -0
- package/lib/Utils/MessageBuilder.d.ts +2 -0
- package/lib/Utils/MessageBuilder.js +2 -0
- package/lib/Utils/PersistentStore.d.ts +60 -0
- package/lib/Utils/PersistentStore.js +592 -0
- package/lib/Utils/anti-delete.d.ts +74 -0
- package/lib/Utils/anti-delete.js +221 -0
- package/lib/Utils/auth-secure.d.ts +86 -0
- package/lib/Utils/auth-secure.js +764 -0
- package/lib/Utils/auth-utils.d.ts +57 -0
- package/lib/Utils/auth-utils.js +306 -0
- package/lib/Utils/auto-reply.d.ts +47 -0
- package/lib/Utils/auto-reply.js +157 -0
- package/lib/Utils/baileys-event-stream.d.ts +8 -0
- package/lib/Utils/baileys-event-stream.js +62 -0
- package/lib/Utils/browser-utils.d.ts +8 -0
- package/lib/Utils/browser-utils.js +27 -0
- package/lib/Utils/business.d.ts +49 -0
- package/lib/Utils/business.js +239 -0
- package/lib/Utils/button-helper-utils.d.ts +53 -0
- package/lib/Utils/button-helper-utils.js +312 -0
- package/lib/Utils/button-sender.d.ts +54 -0
- package/lib/Utils/button-sender.js +822 -0
- package/lib/Utils/chat-control.d.ts +159 -0
- package/lib/Utils/chat-control.js +230 -0
- package/lib/Utils/chat-history-helpers.d.ts +21 -0
- package/lib/Utils/chat-history-helpers.js +70 -0
- package/lib/Utils/chat-utils.d.ts +407 -0
- package/lib/Utils/chat-utils.js +885 -0
- package/lib/Utils/companion-reg-client-utils.d.ts +4 -0
- package/lib/Utils/companion-reg-client-utils.js +39 -0
- package/lib/Utils/crypto.d.ts +41 -0
- package/lib/Utils/crypto.js +147 -0
- package/lib/Utils/decode-wa-message.d.ts +83 -0
- package/lib/Utils/decode-wa-message.js +316 -0
- package/lib/Utils/event-buffer.d.ts +12 -0
- package/lib/Utils/event-buffer.js +635 -0
- package/lib/Utils/generics.d.ts +69 -0
- package/lib/Utils/generics.js +438 -0
- package/lib/Utils/history-share.d.ts +11 -0
- package/lib/Utils/history-share.js +70 -0
- package/lib/Utils/history.d.ts +90 -0
- package/lib/Utils/history.js +146 -0
- package/lib/Utils/humanizer.d.ts +12 -0
- package/lib/Utils/humanizer.js +73 -0
- package/lib/Utils/identity-change-handler.d.ts +13 -0
- package/lib/Utils/identity-change-handler.js +49 -0
- package/lib/Utils/index.d.ts +57 -0
- package/lib/Utils/index.js +56 -0
- package/lib/Utils/jid-tools.d.ts +6 -0
- package/lib/Utils/jid-tools.js +77 -0
- package/lib/Utils/link-preview.d.ts +12 -0
- package/lib/Utils/link-preview.js +84 -0
- package/lib/Utils/logger.d.ts +4 -0
- package/lib/Utils/logger.js +2 -0
- package/lib/Utils/lt-hash.d.ts +12 -0
- package/lib/Utils/lt-hash.js +26 -0
- package/lib/Utils/make-mutex.d.ts +6 -0
- package/lib/Utils/make-mutex.js +32 -0
- package/lib/Utils/media-messages.d.ts +18 -0
- package/lib/Utils/media-messages.js +70 -0
- package/lib/Utils/media-set.d.ts +13 -0
- package/lib/Utils/media-set.js +171 -0
- package/lib/Utils/message-kind.d.ts +32 -0
- package/lib/Utils/message-kind.js +136 -0
- package/lib/Utils/message-retry-manager.d.ts +79 -0
- package/lib/Utils/message-retry-manager.js +264 -0
- package/lib/Utils/message-search.d.ts +44 -0
- package/lib/Utils/message-search.js +188 -0
- package/lib/Utils/messages-media.d.ts +135 -0
- package/lib/Utils/messages-media.js +868 -0
- package/lib/Utils/messages.d.ts +44 -0
- package/lib/Utils/messages.js +2326 -0
- package/lib/Utils/meta-ai.d.ts +13 -0
- package/lib/Utils/meta-ai.js +74 -0
- package/lib/Utils/mini-app.d.ts +35 -0
- package/lib/Utils/mini-app.js +213 -0
- package/lib/Utils/noise-handler.d.ts +20 -0
- package/lib/Utils/noise-handler.js +200 -0
- package/lib/Utils/offline-node-processor.d.ts +9 -0
- package/lib/Utils/offline-node-processor.js +39 -0
- package/lib/Utils/past-participants.d.ts +14 -0
- package/lib/Utils/past-participants.js +40 -0
- package/lib/Utils/pre-key-manager.d.ts +25 -0
- package/lib/Utils/pre-key-manager.js +105 -0
- package/lib/Utils/process-message.d.ts +82 -0
- package/lib/Utils/process-message.js +808 -0
- package/lib/Utils/qr-render.d.ts +59 -0
- package/lib/Utils/qr-render.js +179 -0
- package/lib/Utils/qrcodegen.d.ts +79 -0
- package/lib/Utils/qrcodegen.js +848 -0
- package/lib/Utils/reporting-utils.d.ts +12 -0
- package/lib/Utils/reporting-utils.js +257 -0
- package/lib/Utils/rich-message-utils.d.ts +48 -0
- package/lib/Utils/rich-message-utils.js +444 -0
- package/lib/Utils/router.d.ts +37 -0
- package/lib/Utils/router.js +165 -0
- package/lib/Utils/scheduled-posts.d.ts +30 -0
- package/lib/Utils/scheduled-posts.js +110 -0
- package/lib/Utils/scheduling.d.ts +42 -0
- package/lib/Utils/scheduling.js +139 -0
- package/lib/Utils/signal.d.ts +82 -0
- package/lib/Utils/signal.js +200 -0
- package/lib/Utils/stanza-ack.d.ts +16 -0
- package/lib/Utils/stanza-ack.js +37 -0
- package/lib/Utils/status.d.ts +50 -0
- package/lib/Utils/status.js +107 -0
- package/lib/Utils/stickerpack.d.ts +51 -0
- package/lib/Utils/stickerpack.js +274 -0
- package/lib/Utils/sync-action-utils.d.ts +2 -0
- package/lib/Utils/sync-action-utils.js +48 -0
- package/lib/Utils/tag.d.ts +12 -0
- package/lib/Utils/tag.js +44 -0
- package/lib/Utils/tc-token-utils.d.ts +29 -0
- package/lib/Utils/tc-token-utils.js +162 -0
- package/lib/Utils/templates.d.ts +76 -0
- package/lib/Utils/templates.js +150 -0
- package/lib/Utils/transcribe.d.ts +12 -0
- package/lib/Utils/transcribe.js +107 -0
- package/lib/Utils/use-cache-manager-auth-state.d.ts +13 -0
- package/lib/Utils/use-cache-manager-auth-state.js +81 -0
- package/lib/Utils/use-multi-file-auth-state.d.ts +10 -0
- package/lib/Utils/use-multi-file-auth-state.js +166 -0
- package/lib/Utils/use-single-file-auth-state.d.ts +10 -0
- package/lib/Utils/use-single-file-auth-state.js +113 -0
- package/lib/Utils/use-sqlite-auth-state.d.ts +11 -0
- package/lib/Utils/use-sqlite-auth-state.js +169 -0
- package/lib/Utils/validate-connection.d.ts +42 -0
- package/lib/Utils/validate-connection.js +198 -0
- package/lib/Utils/vcard.d.ts +58 -0
- package/lib/Utils/vcard.js +104 -0
- package/lib/Utils/voice-note.d.ts +15 -0
- package/lib/Utils/voice-note.js +63 -0
- package/lib/VoIP/audio-feeder.d.ts +18 -0
- package/lib/VoIP/audio-feeder.js +160 -0
- package/lib/VoIP/call-recorder.d.ts +13 -0
- package/lib/VoIP/call-recorder.js +73 -0
- package/lib/VoIP/index.d.ts +86 -0
- package/lib/VoIP/index.js +611 -0
- package/lib/VoIP/relay-transport.d.ts +43 -0
- package/lib/VoIP/relay-transport.js +563 -0
- package/lib/VoIP/signaling.d.ts +26 -0
- package/lib/VoIP/signaling.js +662 -0
- package/lib/VoIP/types.d.ts +69 -0
- package/lib/VoIP/types.js +17 -0
- package/lib/VoIP/wasm-engine.d.ts +120 -0
- package/lib/VoIP/wasm-engine.js +1358 -0
- package/lib/VoIP/worker-bootstrap.d.ts +4 -0
- package/lib/VoIP/worker-bootstrap.js +1046 -0
- package/lib/WABinary/constants.d.ts +61 -0
- package/lib/WABinary/constants.js +1466 -0
- package/lib/WABinary/decode.d.ts +9 -0
- package/lib/WABinary/decode.js +261 -0
- package/lib/WABinary/encode.d.ts +2 -0
- package/lib/WABinary/encode.js +219 -0
- package/lib/WABinary/generic-utils.d.ts +74 -0
- package/lib/WABinary/generic-utils.js +276 -0
- package/lib/WABinary/index.d.ts +5 -0
- package/lib/WABinary/index.js +5 -0
- package/lib/WABinary/jid-utils.d.ts +28 -0
- package/lib/WABinary/jid-utils.js +95 -0
- package/lib/WABinary/types.d.ts +19 -0
- package/lib/WABinary/types.js +1 -0
- package/lib/WAM/BinaryInfo.d.ts +7 -0
- package/lib/WAM/BinaryInfo.js +9 -0
- package/lib/WAM/constants.d.ts +34926 -0
- package/lib/WAM/constants.js +22852 -0
- package/lib/WAM/encode.d.ts +1 -0
- package/lib/WAM/encode.js +149 -0
- package/lib/WAM/index.d.ts +3 -0
- package/lib/WAM/index.js +3 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +36 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +51 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +29 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +62 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +12 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +26 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +12 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +37 -0
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.d.ts +9 -0
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +24 -0
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +38 -0
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +50 -0
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +14 -0
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +28 -0
- package/lib/WAUSync/Protocols/index.d.ts +7 -0
- package/lib/WAUSync/Protocols/index.js +7 -0
- package/lib/WAUSync/USyncQuery.d.ts +20 -0
- package/lib/WAUSync/USyncQuery.js +121 -0
- package/lib/WAUSync/USyncUser.d.ts +16 -0
- package/lib/WAUSync/USyncUser.js +30 -0
- package/lib/WAUSync/index.d.ts +3 -0
- package/lib/WAUSync/index.js +3 -0
- package/lib/assets/wasm/loader.d.ts +5 -0
- package/lib/assets/wasm/loader.js +5 -0
- package/lib/assets/wasm/whatsapp.wasm +0 -0
- package/lib/assets/wasm/worker-modules.d.ts +5 -0
- package/lib/assets/wasm/worker-modules.js +273 -0
- package/lib/index.d.ts +18 -0
- package/lib/index.js +23 -0
- package/package.json +180 -0
- package/postinstall-banner.js +82 -0
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
export const UNAUTHORIZED_CODES: number[];
|
|
2
|
+
export const BIZ_BOT_SUPPORT_PAYLOAD: "{\"version\":1,\"is_ai_message\":true,\"should_upload_client_logs\":false,\"should_show_system_message\":false,\"ticket_id\":\"7004947587700716\",\"citation_items\":[],\"ticket_locale\":\"us\"}";
|
|
3
|
+
export const DEFAULT_ORIGIN: "https://web.whatsapp.com";
|
|
4
|
+
export const CALL_VIDEO_PREFIX: "https://call.whatsapp.com/video/";
|
|
5
|
+
export const CALL_AUDIO_PREFIX: "https://call.whatsapp.com/voice/";
|
|
6
|
+
export const DONATE_URL: "";
|
|
7
|
+
export const LIBRARY_NAME: "japofc/baileys";
|
|
8
|
+
export const COMPANION_DEVICE_VERSION: {
|
|
9
|
+
primary: number;
|
|
10
|
+
secondary: number;
|
|
11
|
+
tertiary: number;
|
|
12
|
+
};
|
|
13
|
+
export const DEF_CALLBACK_PREFIX: "CB:";
|
|
14
|
+
export const DEF_TAG_PREFIX: "TAG:";
|
|
15
|
+
export const PHONE_CONNECTION_CB: "CB:Pong";
|
|
16
|
+
export const WA_ADV_ACCOUNT_SIG_PREFIX: any;
|
|
17
|
+
export const WA_ADV_DEVICE_SIG_PREFIX: any;
|
|
18
|
+
export const WA_ADV_HOSTED_ACCOUNT_SIG_PREFIX: any;
|
|
19
|
+
export const WA_ADV_HOSTED_DEVICE_SIG_PREFIX: any;
|
|
20
|
+
export const WA_DEFAULT_EPHEMERAL: number;
|
|
21
|
+
/** Status messages older than 24 hours are considered expired */
|
|
22
|
+
export const STATUS_EXPIRY_SECONDS: number;
|
|
23
|
+
/** WA Web enforces a 14-day maximum age for placeholder resend requests */
|
|
24
|
+
export const PLACEHOLDER_MAX_AGE_SECONDS: number;
|
|
25
|
+
export const NOISE_MODE: "Noise_XX_25519_AESGCM_SHA256\0\0\0\0";
|
|
26
|
+
export const DICT_VERSION: 3;
|
|
27
|
+
export const KEY_BUNDLE_TYPE: any;
|
|
28
|
+
export const NOISE_WA_HEADER: any;
|
|
29
|
+
export const LEXER_REGEX: RegExp;
|
|
30
|
+
/** from: https://stackoverflow.com/questions/3809401/what-is-a-good-regular-expression-to-match-a-url */
|
|
31
|
+
export const URL_REGEX: RegExp;
|
|
32
|
+
export namespace WA_CERT_DETAILS {
|
|
33
|
+
let SERIAL: number;
|
|
34
|
+
let ISSUER: string;
|
|
35
|
+
let PUBLIC_KEY: any;
|
|
36
|
+
}
|
|
37
|
+
export const PROCESSABLE_HISTORY_TYPES: any[];
|
|
38
|
+
export namespace DEFAULT_CACHE_TTLS {
|
|
39
|
+
let SIGNAL_STORE: number;
|
|
40
|
+
let MSG_RETRY: number;
|
|
41
|
+
let CALL_OFFER: number;
|
|
42
|
+
let USER_DEVICES: number;
|
|
43
|
+
}
|
|
44
|
+
export namespace DEFAULT_CONNECTION_CONFIG {
|
|
45
|
+
export { version };
|
|
46
|
+
export let browser: any[];
|
|
47
|
+
export let waWebSocketUrl: string;
|
|
48
|
+
export let connectTimeoutMs: number;
|
|
49
|
+
export let keepAliveIntervalMs: number;
|
|
50
|
+
export let logger: any;
|
|
51
|
+
export let emitOwnEvents: boolean;
|
|
52
|
+
export let defaultQueryTimeoutMs: number;
|
|
53
|
+
export let customUploadHosts: never[];
|
|
54
|
+
export let retryRequestDelayMs: number;
|
|
55
|
+
export let maxMsgRetryCount: number;
|
|
56
|
+
export let fireInitQueries: boolean;
|
|
57
|
+
export let auth: undefined;
|
|
58
|
+
export let markOnlineOnConnect: boolean;
|
|
59
|
+
export let syncFullHistory: boolean;
|
|
60
|
+
export function patchMessageBeforeSending(msg: any): any;
|
|
61
|
+
export function shouldSyncHistoryMessage({ syncType }: {
|
|
62
|
+
syncType: any;
|
|
63
|
+
}): boolean;
|
|
64
|
+
export function shouldIgnoreJid(): boolean;
|
|
65
|
+
export let linkPreviewImageThumbnailWidth: number;
|
|
66
|
+
export namespace transactionOpts {
|
|
67
|
+
let maxCommitRetries: number;
|
|
68
|
+
let delayBetweenTriesMs: number;
|
|
69
|
+
}
|
|
70
|
+
export let eventBufferTimeoutMs: number;
|
|
71
|
+
export let statusBroadcastDelayMs: number;
|
|
72
|
+
export let albumDelayMs: number;
|
|
73
|
+
export let phashRetryEnabled: boolean;
|
|
74
|
+
export let generateHighQualityLinkPreview: boolean;
|
|
75
|
+
export let enableAutoSessionRecreation: boolean;
|
|
76
|
+
export let enableRecentMessageCache: boolean;
|
|
77
|
+
export let options: {};
|
|
78
|
+
export namespace appStateMacVerification {
|
|
79
|
+
let patch: boolean;
|
|
80
|
+
let snapshot: boolean;
|
|
81
|
+
}
|
|
82
|
+
export let countryCode: string;
|
|
83
|
+
export function getMessage(): Promise<undefined>;
|
|
84
|
+
export function cachedGroupMetadata(): Promise<undefined>;
|
|
85
|
+
export { makeLibSignalRepository as makeSignalRepository };
|
|
86
|
+
}
|
|
87
|
+
export const MEDIA_PATH_MAP: {
|
|
88
|
+
image: string;
|
|
89
|
+
video: string;
|
|
90
|
+
document: string;
|
|
91
|
+
audio: string;
|
|
92
|
+
sticker: string;
|
|
93
|
+
'sticker-pack': string;
|
|
94
|
+
'thumbnail-sticker-pack': string;
|
|
95
|
+
'thumbnail-link': string;
|
|
96
|
+
'thumbnail-image': string;
|
|
97
|
+
'thumbnail-video': string;
|
|
98
|
+
'thumbnail-document': string;
|
|
99
|
+
'product-catalog-image': string;
|
|
100
|
+
'md-app-state': string;
|
|
101
|
+
'md-msg-hist': string;
|
|
102
|
+
'biz-cover-photo': string;
|
|
103
|
+
};
|
|
104
|
+
export const NEWSLETTER_MEDIA_PATH_MAP: {
|
|
105
|
+
image: string;
|
|
106
|
+
video: string;
|
|
107
|
+
document: string;
|
|
108
|
+
audio: string;
|
|
109
|
+
sticker: string;
|
|
110
|
+
'thumbnail-link': string;
|
|
111
|
+
};
|
|
112
|
+
export const MEDIA_HKDF_KEY_MAPPING: {
|
|
113
|
+
audio: string;
|
|
114
|
+
document: string;
|
|
115
|
+
gif: string;
|
|
116
|
+
image: string;
|
|
117
|
+
ppic: string;
|
|
118
|
+
product: string;
|
|
119
|
+
ptt: string;
|
|
120
|
+
'sticker-pack': string;
|
|
121
|
+
'thumbnail-sticker-pack': string;
|
|
122
|
+
sticker: string;
|
|
123
|
+
video: string;
|
|
124
|
+
'thumbnail-document': string;
|
|
125
|
+
'thumbnail-image': string;
|
|
126
|
+
'thumbnail-video': string;
|
|
127
|
+
'thumbnail-link': string;
|
|
128
|
+
'md-msg-hist': string;
|
|
129
|
+
'md-app-state': string;
|
|
130
|
+
'product-catalog-image': string;
|
|
131
|
+
'payment-bg-image': string;
|
|
132
|
+
ptv: string;
|
|
133
|
+
'biz-cover-photo': string;
|
|
134
|
+
location: string;
|
|
135
|
+
contact: string;
|
|
136
|
+
'voip-token': string;
|
|
137
|
+
};
|
|
138
|
+
export const MEDIA_KEYS: string[];
|
|
139
|
+
/** 120s timeout for history sync stall detection, same as WA Web's handleChunkProgress / restartPausedTimer (g = 120) */
|
|
140
|
+
export const HISTORY_SYNC_PAUSED_TIMEOUT_MS: 120000;
|
|
141
|
+
export const MIN_PREKEY_COUNT: 5;
|
|
142
|
+
export const INITIAL_PREKEY_COUNT: 812;
|
|
143
|
+
export const UPLOAD_TIMEOUT: 30000;
|
|
144
|
+
export namespace TimeMs {
|
|
145
|
+
let Minute: number;
|
|
146
|
+
let Hour: number;
|
|
147
|
+
let Day: number;
|
|
148
|
+
let Week: number;
|
|
149
|
+
}
|
|
150
|
+
declare const version: number[];
|
|
151
|
+
import { makeLibSignalRepository } from '../Signal/libsignal.js';
|
|
152
|
+
export {};
|
|
153
|
+
/** Media direction/type tag used across media helpers (typed loosely). */
|
|
154
|
+
export type MediaType = any;
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import { proto } from '../../WAProto/index.js';
|
|
2
|
+
import { makeLibSignalRepository } from '../Signal/libsignal.js';
|
|
3
|
+
import { Browsers } from '../Utils/browser-utils.js';
|
|
4
|
+
import logger from '../Utils/logger.js';
|
|
5
|
+
// JAP@Fix: disamain sama baseline yang dipake pas diff WAProto (lihat
|
|
6
|
+
// WAProto/CHANGELOG-proto-upgrade.md — sempet lebih baru dari fallback
|
|
7
|
+
// lama, jadi socket ngaku versi lebih tua dari field yang udah didukung
|
|
8
|
+
// proto-nya sendiri). Ini cuma fallback kalau fetchLatestBaileysVersion()
|
|
9
|
+
// gagal total (mis. GitHub gak bisa diakses) — tetap worth di-update
|
|
10
|
+
// berkala, dan runtime tetap disaranin pake fetchLatestWaWebVersion()
|
|
11
|
+
// dulu (parse sw.js langsung) baru fallback ke fetchLatestBaileysVersion()
|
|
12
|
+
// / konstanta ini, karena fetchLatestBaileysVersion resmi Baileys ada
|
|
13
|
+
// history bug isLatest:true padahal versinya stale (upstream#2679).
|
|
14
|
+
const version = [2, 3000, 1046350168];
|
|
15
|
+
// JAP@Fix (bug 65): this was an untracked, uncommented magic number inline
|
|
16
|
+
// inside generateRegistrationNode() in validate-connection.js — a *second*,
|
|
17
|
+
// independent hardcoded version (WA's "companion/device-props" version sent
|
|
18
|
+
// during device pairing, distinct from `version` above which is the WA Web
|
|
19
|
+
// client version used for appVersion/buildHash). It can go stale the exact
|
|
20
|
+
// same way `version` can (same failure mode as upstream#2370/#2485),
|
|
21
|
+
// but unlike `version`, it isn't covered by fetchLatestWaWebVersion(),
|
|
22
|
+
// fetchLatestBaileysVersion(), or fetchBestWaVersion() — none of those touch
|
|
23
|
+
// it, so it was silently unmaintained. Named + documented here so it's at
|
|
24
|
+
// least visible and update-able; still worth bumping periodically by hand.
|
|
25
|
+
export const COMPANION_DEVICE_VERSION = { primary: 10, secondary: 15, tertiary: 7 };
|
|
26
|
+
export const UNAUTHORIZED_CODES = [401, 403, 419];
|
|
27
|
+
export const BIZ_BOT_SUPPORT_PAYLOAD = '{"version":1,"is_ai_message":true,"should_upload_client_logs":false,"should_show_system_message":false,"ticket_id":"7004947587700716","citation_items":[],"ticket_locale":"us"}';
|
|
28
|
+
export const DEFAULT_ORIGIN = 'https://web.whatsapp.com';
|
|
29
|
+
export const CALL_VIDEO_PREFIX = 'https://call.whatsapp.com/video/';
|
|
30
|
+
export const CALL_AUDIO_PREFIX = 'https://call.whatsapp.com/voice/';
|
|
31
|
+
export const DONATE_URL = ''; // JAP@Fix: cleared the donate URL fallback
|
|
32
|
+
export const LIBRARY_NAME = 'japofc/baileys';
|
|
33
|
+
export const DEF_CALLBACK_PREFIX = 'CB:';
|
|
34
|
+
export const DEF_TAG_PREFIX = 'TAG:';
|
|
35
|
+
export const PHONE_CONNECTION_CB = 'CB:Pong';
|
|
36
|
+
export const WA_ADV_ACCOUNT_SIG_PREFIX = Buffer.from([6, 0]);
|
|
37
|
+
export const WA_ADV_DEVICE_SIG_PREFIX = Buffer.from([6, 1]);
|
|
38
|
+
export const WA_ADV_HOSTED_ACCOUNT_SIG_PREFIX = Buffer.from([6, 5]);
|
|
39
|
+
export const WA_ADV_HOSTED_DEVICE_SIG_PREFIX = Buffer.from([6, 6]);
|
|
40
|
+
export const WA_DEFAULT_EPHEMERAL = 7 * 24 * 60 * 60;
|
|
41
|
+
/** Status messages older than 24 hours are considered expired */
|
|
42
|
+
export const STATUS_EXPIRY_SECONDS = 24 * 60 * 60;
|
|
43
|
+
/** WA Web enforces a 14-day maximum age for placeholder resend requests */
|
|
44
|
+
export const PLACEHOLDER_MAX_AGE_SECONDS = 14 * 24 * 60 * 60;
|
|
45
|
+
export const NOISE_MODE = 'Noise_XX_25519_AESGCM_SHA256\0\0\0\0';
|
|
46
|
+
export const DICT_VERSION = 3;
|
|
47
|
+
export const KEY_BUNDLE_TYPE = Buffer.from([5]);
|
|
48
|
+
export const NOISE_WA_HEADER = Buffer.from([87, 65, 6, DICT_VERSION]); // last is "DICT_VERSION"
|
|
49
|
+
export const LEXER_REGEX = /(\/\/.*|\/\*[\s\S]*?\*\/|#.*)|("(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|`[\s\S]*?`)|(\b[a-zA-Z_]\w*\b)(?=\s*\()|(\b[a-zA-Z_]\w*\b)|(\b\d+(?:\.\d+)?\b)|(\s+|[^\w\s]+)/g;
|
|
50
|
+
/** from: https://stackoverflow.com/questions/3809401/what-is-a-good-regular-expression-to-match-a-url */
|
|
51
|
+
export const URL_REGEX = /https:\/\/(?![^:@\/\s]+:[^:@\/\s]+@)[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}(:\d+)?(\/[^\s]*)?/g;
|
|
52
|
+
export const WA_CERT_DETAILS = {
|
|
53
|
+
SERIAL: 0,
|
|
54
|
+
ISSUER: 'WhatsAppLongTerm1',
|
|
55
|
+
PUBLIC_KEY: Buffer.from('142375574d0a587166aae71ebe516437c4a28b73e3695c6ce1f7f9545da8ee6b', 'hex')
|
|
56
|
+
};
|
|
57
|
+
export const PROCESSABLE_HISTORY_TYPES = [
|
|
58
|
+
proto.HistorySync.HistorySyncType.INITIAL_BOOTSTRAP,
|
|
59
|
+
proto.HistorySync.HistorySyncType.PUSH_NAME,
|
|
60
|
+
proto.HistorySync.HistorySyncType.RECENT,
|
|
61
|
+
proto.HistorySync.HistorySyncType.FULL,
|
|
62
|
+
proto.HistorySync.HistorySyncType.ON_DEMAND,
|
|
63
|
+
proto.HistorySync.HistorySyncType.NON_BLOCKING_DATA,
|
|
64
|
+
proto.HistorySync.HistorySyncType.INITIAL_STATUS_V3
|
|
65
|
+
];
|
|
66
|
+
export const DEFAULT_CACHE_TTLS = {
|
|
67
|
+
SIGNAL_STORE: 5 * 60, // 5 minutes
|
|
68
|
+
MSG_RETRY: 60 * 60, // 1 hour
|
|
69
|
+
CALL_OFFER: 5 * 60, // 5 minutes
|
|
70
|
+
USER_DEVICES: 5 * 60 // 5 minutes
|
|
71
|
+
};
|
|
72
|
+
export const DEFAULT_CONNECTION_CONFIG = {
|
|
73
|
+
version: version,
|
|
74
|
+
browser: Browsers.macOS('Chrome'),
|
|
75
|
+
waWebSocketUrl: 'wss://web.whatsapp.com/ws/chat',
|
|
76
|
+
connectTimeoutMs: 20000,
|
|
77
|
+
keepAliveIntervalMs: 15000,
|
|
78
|
+
logger: logger.child({ class: 'baileys' }),
|
|
79
|
+
emitOwnEvents: true,
|
|
80
|
+
defaultQueryTimeoutMs: 60000,
|
|
81
|
+
customUploadHosts: [],
|
|
82
|
+
retryRequestDelayMs: 250,
|
|
83
|
+
maxMsgRetryCount: 3,
|
|
84
|
+
fireInitQueries: true,
|
|
85
|
+
auth: undefined,
|
|
86
|
+
markOnlineOnConnect: true,
|
|
87
|
+
syncFullHistory: true,
|
|
88
|
+
patchMessageBeforeSending: msg => msg,
|
|
89
|
+
shouldSyncHistoryMessage: ({ syncType }) => {
|
|
90
|
+
return syncType !== proto.HistorySync.HistorySyncType.FULL;
|
|
91
|
+
},
|
|
92
|
+
shouldIgnoreJid: () => false,
|
|
93
|
+
linkPreviewImageThumbnailWidth: 480, // JAP@Fix: was 192 — too small, bumped to 480
|
|
94
|
+
transactionOpts: { maxCommitRetries: 10, delayBetweenTriesMs: 3000 },
|
|
95
|
+
eventBufferTimeoutMs: 30000, // JAP@Fix (bug 25): configurable — increase if bot is heavy load
|
|
96
|
+
statusBroadcastDelayMs: 1500, // JAP@Fix (bug 5): configurable delay between status broadcasts — default 1500ms
|
|
97
|
+
albumDelayMs: 1500, // JAP@Fix (bug 5): configurable delay between album message sends — default 1500ms
|
|
98
|
+
phashRetryEnabled: false, // JAP@Fix (bug 54/55): enable phash retry on ack (caution: can cause loops if not careful)
|
|
99
|
+
generateHighQualityLinkPreview: true,
|
|
100
|
+
enableAutoSessionRecreation: true,
|
|
101
|
+
enableRecentMessageCache: true,
|
|
102
|
+
options: {},
|
|
103
|
+
appStateMacVerification: {
|
|
104
|
+
patch: false,
|
|
105
|
+
snapshot: false
|
|
106
|
+
},
|
|
107
|
+
countryCode: 'US',
|
|
108
|
+
// JAP@Fix (bug 15): default returns undefined — every retry request will fail unless overridden.
|
|
109
|
+
// Override this in your makeWASocket config to return messages from your store:
|
|
110
|
+
// getMessage: async (key) => store.loadMessage(key.remoteJid, key.id)?.message
|
|
111
|
+
getMessage: async () => undefined,
|
|
112
|
+
cachedGroupMetadata: async () => undefined,
|
|
113
|
+
makeSignalRepository: makeLibSignalRepository
|
|
114
|
+
};
|
|
115
|
+
export const MEDIA_PATH_MAP = {
|
|
116
|
+
image: '/mms/image',
|
|
117
|
+
video: '/mms/video',
|
|
118
|
+
document: '/mms/document',
|
|
119
|
+
audio: '/mms/audio',
|
|
120
|
+
sticker: '/mms/image',
|
|
121
|
+
'sticker-pack': '/mms/sticker-pack',
|
|
122
|
+
'thumbnail-sticker-pack': '/mms/thumbnail-sticker-pack',
|
|
123
|
+
'thumbnail-link': '/mms/image',
|
|
124
|
+
'thumbnail-image': '/mms/image',
|
|
125
|
+
'thumbnail-video': '/mms/video',
|
|
126
|
+
'thumbnail-document': '/mms/document',
|
|
127
|
+
'product-catalog-image': '/product/image',
|
|
128
|
+
'md-app-state': '',
|
|
129
|
+
'md-msg-hist': '/mms/md-app-state',
|
|
130
|
+
'biz-cover-photo': '/pps/biz-cover-photo'
|
|
131
|
+
};
|
|
132
|
+
// JAP@Changes 06-02-26 --- Add newsletter media path for "/m1/" instead of "/o1/" (≧▽≦)
|
|
133
|
+
export const NEWSLETTER_MEDIA_PATH_MAP = {
|
|
134
|
+
image: '/newsletter/newsletter-image',
|
|
135
|
+
video: '/newsletter/newsletter-video',
|
|
136
|
+
document: '/newsletter/newsletter-document',
|
|
137
|
+
audio: '/newsletter/newsletter-audio',
|
|
138
|
+
sticker: '/newsletter/newsletter-image',
|
|
139
|
+
'thumbnail-link': '/newsletter/newsletter-thumbnail-link'
|
|
140
|
+
};
|
|
141
|
+
export const MEDIA_HKDF_KEY_MAPPING = {
|
|
142
|
+
audio: 'Audio',
|
|
143
|
+
document: 'Document',
|
|
144
|
+
gif: 'Video',
|
|
145
|
+
image: 'Image',
|
|
146
|
+
ppic: '',
|
|
147
|
+
product: 'Image',
|
|
148
|
+
ptt: 'Audio',
|
|
149
|
+
'sticker-pack': 'Sticker Pack',
|
|
150
|
+
'thumbnail-sticker-pack': 'Sticker Pack Thumbnail',
|
|
151
|
+
sticker: 'Image',
|
|
152
|
+
video: 'Video',
|
|
153
|
+
'thumbnail-document': 'Document Thumbnail',
|
|
154
|
+
'thumbnail-image': 'Image Thumbnail',
|
|
155
|
+
'thumbnail-video': 'Video Thumbnail',
|
|
156
|
+
'thumbnail-link': 'Link Thumbnail',
|
|
157
|
+
'md-msg-hist': 'History',
|
|
158
|
+
'md-app-state': 'App State',
|
|
159
|
+
'product-catalog-image': 'Product Catalog Image',
|
|
160
|
+
'payment-bg-image': 'Payment Background',
|
|
161
|
+
ptv: 'Video',
|
|
162
|
+
'biz-cover-photo': 'Image',
|
|
163
|
+
location: 'Location',
|
|
164
|
+
contact: 'Contact',
|
|
165
|
+
'voip-token': 'Voip Token'
|
|
166
|
+
};
|
|
167
|
+
export const MEDIA_KEYS = Object.keys(MEDIA_PATH_MAP);
|
|
168
|
+
/** 120s timeout for history sync stall detection, same as WA Web's handleChunkProgress / restartPausedTimer (g = 120) */
|
|
169
|
+
export const HISTORY_SYNC_PAUSED_TIMEOUT_MS = 120000;
|
|
170
|
+
export const MIN_PREKEY_COUNT = 5;
|
|
171
|
+
export const INITIAL_PREKEY_COUNT = 812;
|
|
172
|
+
export const UPLOAD_TIMEOUT = 30000; // 30 seconds
|
|
173
|
+
export const TimeMs = {
|
|
174
|
+
Minute: 60 * 1000,
|
|
175
|
+
Hour: 60 * 60 * 1000,
|
|
176
|
+
Day: 24 * 60 * 60 * 1000,
|
|
177
|
+
Week: 7 * 24 * 60 * 60 * 1000
|
|
178
|
+
};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type { Context } from './Context.js';
|
|
2
|
+
import type { SessionManager } from './SessionManager.js';
|
|
3
|
+
import type { StatsManager } from './StatsManager.js';
|
|
4
|
+
import type { SQLiteStore } from './Store/SQLiteStore.js';
|
|
5
|
+
|
|
6
|
+
export interface BotConfig {
|
|
7
|
+
/** Passed straight to makeWASocket() (auth, logger, browser, …). */
|
|
8
|
+
socketConfig?: any;
|
|
9
|
+
/** SQLite path for sessions/stats. Default: 'baileys_store.db'. */
|
|
10
|
+
dbPath?: string;
|
|
11
|
+
/** Enable group StatsManager. Default off. */
|
|
12
|
+
enableStats?: boolean;
|
|
13
|
+
/**
|
|
14
|
+
* Resolve the freshest WA Web version via fetchBestWaVersion() on every
|
|
15
|
+
* start()/reconnect (WA sw.js -> baileys fork -> hardcoded fallback).
|
|
16
|
+
* Default true; set false or pin `socketConfig.version` to opt out.
|
|
17
|
+
*/
|
|
18
|
+
versionCheck?: boolean;
|
|
19
|
+
logger?: any;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Resolve the socketConfig a Bot connects with: injects the freshest WA Web
|
|
24
|
+
* version via fetchBestWaVersion() unless `versionCheck: false` or
|
|
25
|
+
* `socketConfig.version` is pinned. Pure and never throws.
|
|
26
|
+
*/
|
|
27
|
+
export function resolveSocketVersionConfig(config: BotConfig, logger?: any): Promise<any>;
|
|
28
|
+
|
|
29
|
+
export type BotMiddleware = (ctx: Context, next: () => Promise<void>) => Promise<void> | void;
|
|
30
|
+
export type BotCommandHandler = (ctx: Context) => Promise<void> | void;
|
|
31
|
+
|
|
32
|
+
export class Bot {
|
|
33
|
+
constructor(config: BotConfig);
|
|
34
|
+
middlewares: BotMiddleware[];
|
|
35
|
+
messageQueue: any[];
|
|
36
|
+
isConnected: boolean;
|
|
37
|
+
reconnectAttempts: number;
|
|
38
|
+
socket: any | null;
|
|
39
|
+
sessions: SessionManager | null;
|
|
40
|
+
stats: StatsManager | null;
|
|
41
|
+
store: SQLiteStore | null;
|
|
42
|
+
config: BotConfig;
|
|
43
|
+
logger: any;
|
|
44
|
+
/** Register a middleware function (Express/Koa-style `use()`/`next()`). */
|
|
45
|
+
use(middleware: BotMiddleware): this;
|
|
46
|
+
/** Register a `!command` handler (exact match or prefix + whitespace). */
|
|
47
|
+
command(cmd: string, handler: BotCommandHandler): this;
|
|
48
|
+
/** Register a handler that fires on any non-empty text. */
|
|
49
|
+
onText(handler: BotCommandHandler): this;
|
|
50
|
+
/** Send a message — queues automatically when disconnected. */
|
|
51
|
+
sendMessage(jid: string, content: any, options?: any): Promise<any>;
|
|
52
|
+
/** Drain queued messages after reconnect. */
|
|
53
|
+
drainQueue(): void;
|
|
54
|
+
/** Reject and clear the message queue. */
|
|
55
|
+
rejectQueue(reason: any): void;
|
|
56
|
+
/** Start the socket and wire all event handlers. */
|
|
57
|
+
start(): Promise<void>;
|
|
58
|
+
executeMiddlewares(ctx: Context): Promise<void>;
|
|
59
|
+
/** Gracefully stop the bot (cancels reconnect, rejects queue, closes stores). */
|
|
60
|
+
stop(): void;
|
|
61
|
+
}
|
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
import { Boom } from '@hapi/boom';
|
|
2
|
+
import makeWASocket from '../Socket/index.js';
|
|
3
|
+
import { DisconnectReason } from '../Types/index.js';
|
|
4
|
+
import { isJidGroup } from '../WABinary/index.js';
|
|
5
|
+
import defaultLogger from '../Utils/logger.js';
|
|
6
|
+
import { fetchBestWaVersion } from '../Utils/generics.js';
|
|
7
|
+
import { SQLiteStore } from './Store/SQLiteStore.js';
|
|
8
|
+
import { Context } from './Context.js';
|
|
9
|
+
import { SessionManager } from './SessionManager.js';
|
|
10
|
+
import { StatsManager } from './StatsManager.js';
|
|
11
|
+
|
|
12
|
+
// JAP@Framework --- high-level Bot class: middleware routing (Express/Koa-style
|
|
13
|
+
// `use()`/`next()`), a `!command` dispatcher, an outgoing message queue that
|
|
14
|
+
// survives disconnects, and exponential-backoff auto-reconnect.
|
|
15
|
+
//
|
|
16
|
+
// Kept from upstream's fix pass:
|
|
17
|
+
// - command boundary check: `!sticker` no longer matches `!stickerSpam`
|
|
18
|
+
// - queue is rejected (not left hanging) on DisconnectReason.loggedOut
|
|
19
|
+
// - stats.observeMessage runs inside the per-message try block, so a
|
|
20
|
+
// SQLite error can't silently skip middleware for that message
|
|
21
|
+
// - reconnect timer handle is stored so stop()/restart() can cancel it
|
|
22
|
+
//
|
|
23
|
+
// JAP@Fix vs upstream: SQLiteStore/StatsManager are now created through an
|
|
24
|
+
// async factory (see Store/SQLiteStore.js) instead of a synchronous
|
|
25
|
+
// constructor, since better-sqlite3 is lazy-loaded as an optional peer dep
|
|
26
|
+
// in this fork. That means store/session/stats setup moved out of the Bot
|
|
27
|
+
// constructor and into start() — the socket is only created *after* the
|
|
28
|
+
// store is ready, so no message can arrive before sessions/stats exist.
|
|
29
|
+
/**
|
|
30
|
+
* Resolve the socketConfig a Bot should connect with. Exported as a pure
|
|
31
|
+
* function so the wiring is unit-testable without opening a real socket.
|
|
32
|
+
* Skips the lookup when `versionCheck: false` or `socketConfig.version` is
|
|
33
|
+
* pinned; never throws (worst case returns socketConfig untouched).
|
|
34
|
+
*/
|
|
35
|
+
export const resolveSocketVersionConfig = async (config, logger = defaultLogger) => {
|
|
36
|
+
const socketConfig = config.socketConfig ?? {};
|
|
37
|
+
if (config.versionCheck === false || socketConfig.version) {
|
|
38
|
+
return socketConfig;
|
|
39
|
+
}
|
|
40
|
+
try {
|
|
41
|
+
const { version, source, isLatest } = await fetchBestWaVersion();
|
|
42
|
+
logger.info({ version, source, isLatest }, 'resolved WA Web version');
|
|
43
|
+
return { ...socketConfig, version };
|
|
44
|
+
}
|
|
45
|
+
catch (err) {
|
|
46
|
+
// fetchBestWaVersion never throws by contract, but a Framework
|
|
47
|
+
// start() must not be able to die on a version lookup either way.
|
|
48
|
+
logger.warn({ err }, 'version lookup failed — using default');
|
|
49
|
+
return socketConfig;
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
export class Bot {
|
|
54
|
+
constructor(config) {
|
|
55
|
+
this.middlewares = [];
|
|
56
|
+
this.messageQueue = [];
|
|
57
|
+
this.isConnected = false;
|
|
58
|
+
this.reconnectAttempts = 0;
|
|
59
|
+
this.reconnectTimer = null;
|
|
60
|
+
this.BASE_RECONNECT_DELAY = 1000;
|
|
61
|
+
this.MAX_RECONNECT_DELAY = 30000;
|
|
62
|
+
this.socket = null;
|
|
63
|
+
this.sessions = null;
|
|
64
|
+
this.stats = null;
|
|
65
|
+
this.store = null;
|
|
66
|
+
this.config = config;
|
|
67
|
+
this.logger = config.logger ?? defaultLogger.child({ module: 'Framework.Bot' });
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Register a middleware function */
|
|
71
|
+
use(middleware) {
|
|
72
|
+
this.middlewares.push(middleware);
|
|
73
|
+
return this;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Register a command handler.
|
|
78
|
+
* Uses exact match OR prefix + whitespace so '!sticker' does NOT
|
|
79
|
+
* match '!stickerSpam' or '!stickerset'.
|
|
80
|
+
*/
|
|
81
|
+
command(cmd, handler) {
|
|
82
|
+
this.use(async (ctx, next) => {
|
|
83
|
+
const text = ctx.text;
|
|
84
|
+
if (text === cmd || text?.startsWith(cmd + ' ')) {
|
|
85
|
+
await handler(ctx);
|
|
86
|
+
}
|
|
87
|
+
await next();
|
|
88
|
+
});
|
|
89
|
+
return this;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** Register a handler that fires on any non-empty text */
|
|
93
|
+
onText(handler) {
|
|
94
|
+
this.use(async (ctx, next) => {
|
|
95
|
+
if (ctx.text) {
|
|
96
|
+
await handler(ctx);
|
|
97
|
+
}
|
|
98
|
+
await next();
|
|
99
|
+
});
|
|
100
|
+
return this;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Send a message — queues automatically when socket is disconnected */
|
|
104
|
+
async sendMessage(jid, content, options = {}) {
|
|
105
|
+
if (this.isConnected && this.socket) {
|
|
106
|
+
return this.socket.sendMessage(jid, content, options);
|
|
107
|
+
}
|
|
108
|
+
return new Promise((resolve, reject) => {
|
|
109
|
+
this.messageQueue.push({ jid, content, options, resolve, reject });
|
|
110
|
+
this.logger.info({ queueLength: this.messageQueue.length }, 'socket not connected — message queued');
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** Drain queued messages after reconnect. */
|
|
115
|
+
drainQueue() {
|
|
116
|
+
if (!this.isConnected || !this.socket || this.messageQueue.length === 0)
|
|
117
|
+
return;
|
|
118
|
+
this.logger.info({ pending: this.messageQueue.length }, 'draining message queue');
|
|
119
|
+
const queue = [...this.messageQueue];
|
|
120
|
+
this.messageQueue = [];
|
|
121
|
+
for (const msg of queue) {
|
|
122
|
+
this.socket.sendMessage(msg.jid, msg.content, msg.options).then(msg.resolve).catch(msg.reject);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** Reject and clear the message queue. Called on loggedOut so callers don't hang forever. */
|
|
127
|
+
rejectQueue(reason) {
|
|
128
|
+
const queue = [...this.messageQueue];
|
|
129
|
+
this.messageQueue = [];
|
|
130
|
+
for (const msg of queue) {
|
|
131
|
+
msg.reject(new Boom(reason, { statusCode: 401 }));
|
|
132
|
+
}
|
|
133
|
+
if (queue.length > 0) {
|
|
134
|
+
this.logger.warn({ rejected: queue.length }, 'message queue rejected — session terminated');
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* JAP@Fix (bug 66): the Bot always connected with whatever `version` sat
|
|
140
|
+
* in socketConfig — usually nothing, i.e. the hardcoded Defaults constant.
|
|
141
|
+
* That is the exact stale-version footgun fetchBestWaVersion() was written
|
|
142
|
+
* for (405 rejections at pairing once WA bumps its minimum, upstream#2370/
|
|
143
|
+
* #2485), and the Framework never actually used it. Now every start()
|
|
144
|
+
* (and therefore every auto-reconnect) resolves the freshest version
|
|
145
|
+
* through the documented chain: WA's own sw.js -> baileys fork -> the
|
|
146
|
+
* hardcoded fallback. Never throws; offline it behaves exactly as before.
|
|
147
|
+
* Opt out with `versionCheck: false` or pin `socketConfig.version`.
|
|
148
|
+
*/
|
|
149
|
+
async #resolveSocketConfig() {
|
|
150
|
+
return resolveSocketVersionConfig(this.config, this.logger);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** Start the socket and wire all event handlers */
|
|
154
|
+
async start() {
|
|
155
|
+
if (this.reconnectTimer) {
|
|
156
|
+
clearTimeout(this.reconnectTimer);
|
|
157
|
+
this.reconnectTimer = null;
|
|
158
|
+
}
|
|
159
|
+
// JAP@Fix: store/sessions must exist before the socket can receive
|
|
160
|
+
// any message, so this now awaits creation instead of doing it
|
|
161
|
+
// synchronously in the constructor.
|
|
162
|
+
if (!this.store) {
|
|
163
|
+
const dbPath = this.config.dbPath ?? 'baileys_store.db';
|
|
164
|
+
this.store = await SQLiteStore.create(dbPath);
|
|
165
|
+
this.sessions = new SessionManager(this.store);
|
|
166
|
+
}
|
|
167
|
+
this.socket = makeWASocket(await this.#resolveSocketConfig());
|
|
168
|
+
if (this.config.enableStats && !this.stats) {
|
|
169
|
+
this.stats = await StatsManager.create(this.config.dbPath ?? 'baileys_store.db', jid => this.socket.groupMetadata(jid));
|
|
170
|
+
}
|
|
171
|
+
this.socket.ev.on('messages.upsert', async ({ messages, type }) => {
|
|
172
|
+
if (type !== 'notify')
|
|
173
|
+
return;
|
|
174
|
+
for (const msg of messages) {
|
|
175
|
+
const ctx = new Context(this, msg);
|
|
176
|
+
try {
|
|
177
|
+
if (this.stats && ctx.remoteJid && isJidGroup(ctx.remoteJid)) {
|
|
178
|
+
const participant = msg.key.participant || msg.participant;
|
|
179
|
+
if (participant) {
|
|
180
|
+
const isSticker = !!msg.message?.stickerMessage;
|
|
181
|
+
this.stats.observeMessage(ctx.remoteJid, participant, isSticker);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
await this.executeMiddlewares(ctx);
|
|
185
|
+
}
|
|
186
|
+
catch (err) {
|
|
187
|
+
this.logger.error({ err }, 'error executing middleware');
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
this.socket.ev.on('connection.update', update => {
|
|
192
|
+
const { connection, lastDisconnect } = update;
|
|
193
|
+
if (connection === 'open') {
|
|
194
|
+
this.logger.info('bot connected');
|
|
195
|
+
this.isConnected = true;
|
|
196
|
+
this.reconnectAttempts = 0;
|
|
197
|
+
this.drainQueue();
|
|
198
|
+
}
|
|
199
|
+
if (connection === 'close') {
|
|
200
|
+
this.isConnected = false;
|
|
201
|
+
const error = lastDisconnect?.error;
|
|
202
|
+
const statusCode = error?.output?.statusCode;
|
|
203
|
+
const isLoggedOut = statusCode === DisconnectReason.loggedOut;
|
|
204
|
+
this.logger.warn({ statusCode }, 'connection closed');
|
|
205
|
+
if (isLoggedOut) {
|
|
206
|
+
this.rejectQueue('session terminated (logged out)');
|
|
207
|
+
this.logger.info('session logged out — not reconnecting');
|
|
208
|
+
}
|
|
209
|
+
else {
|
|
210
|
+
this.reconnectAttempts++;
|
|
211
|
+
const delay = Math.min(this.MAX_RECONNECT_DELAY, this.BASE_RECONNECT_DELAY * Math.pow(2, this.reconnectAttempts - 1));
|
|
212
|
+
this.logger.info({ delay, attempt: this.reconnectAttempts }, 'scheduling reconnect');
|
|
213
|
+
this.reconnectTimer = setTimeout(() => {
|
|
214
|
+
this.reconnectTimer = null;
|
|
215
|
+
this.start().catch(err => {
|
|
216
|
+
this.logger.error({ err }, 'reconnect failed');
|
|
217
|
+
});
|
|
218
|
+
}, delay);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
async executeMiddlewares(ctx) {
|
|
225
|
+
let index = -1;
|
|
226
|
+
const dispatch = async (i) => {
|
|
227
|
+
if (i <= index)
|
|
228
|
+
throw new Error('next() called multiple times');
|
|
229
|
+
index = i;
|
|
230
|
+
const middleware = this.middlewares[i];
|
|
231
|
+
if (middleware) {
|
|
232
|
+
await middleware(ctx, () => dispatch(i + 1));
|
|
233
|
+
}
|
|
234
|
+
};
|
|
235
|
+
await dispatch(0);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/** Gracefully stop the bot */
|
|
239
|
+
stop() {
|
|
240
|
+
if (this.reconnectTimer) {
|
|
241
|
+
clearTimeout(this.reconnectTimer);
|
|
242
|
+
this.reconnectTimer = null;
|
|
243
|
+
}
|
|
244
|
+
this.rejectQueue('bot stopped');
|
|
245
|
+
this.store?.close();
|
|
246
|
+
this.stats?.close();
|
|
247
|
+
}
|
|
248
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { Bot } from './Bot.js';
|
|
2
|
+
|
|
3
|
+
/** Wraps a WAMessage with reply/react/session/media helpers for command handlers. */
|
|
4
|
+
export class Context {
|
|
5
|
+
constructor(bot: Bot, message: any);
|
|
6
|
+
bot: Bot;
|
|
7
|
+
message: any;
|
|
8
|
+
readonly remoteJid: string | undefined;
|
|
9
|
+
/** Text content, including image/video/document captions. */
|
|
10
|
+
readonly text: string | undefined;
|
|
11
|
+
readonly quoted: any;
|
|
12
|
+
session(): any;
|
|
13
|
+
setSession(data: any): void;
|
|
14
|
+
updateSession(updater: (prev: any) => any): void;
|
|
15
|
+
clearSession(): void;
|
|
16
|
+
/** Reply quoting the original message. Throws when remoteJid is missing. */
|
|
17
|
+
reply(content: any, options?: any): Promise<void>;
|
|
18
|
+
react(emoji: string): Promise<void>;
|
|
19
|
+
replySticker(inputPathOrBuffer: string | Buffer, metadata?: { packname?: string; author?: string }): Promise<void>;
|
|
20
|
+
replyVoiceNote(inputPathOrBuffer: string | Buffer): Promise<void>;
|
|
21
|
+
}
|