@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,444 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* JAP@Changes 09-04-26 [WIP]
|
|
3
|
+
* Adds support for tables and code blocks with richResponseMessage (wrapped inside botForwardedMessage).
|
|
4
|
+
*
|
|
5
|
+
* If you use or copy this code, please credit my name or project. AND DO NOT CHANGE THIS NOTE
|
|
6
|
+
*/
|
|
7
|
+
import { getRandomValues, randomUUID } from 'crypto';
|
|
8
|
+
import { DONATE_URL, LEXER_REGEX } from '../Defaults/index.js';
|
|
9
|
+
import { LANGUAGE_KEYWORDS } from '../WABinary/constants.js';
|
|
10
|
+
import { CodeHighlightType, RichSubMessageType } from '../Types/RichType.js';
|
|
11
|
+
import { proto } from '../../WAProto/index.js';
|
|
12
|
+
import { unixTimestampSeconds } from './generics.js';
|
|
13
|
+
const NOOP = new Set([]);
|
|
14
|
+
// JAP@Fix (bug 40 / inline image): AIRichResponseInlineImageMetadata.imageUrl is a nested
|
|
15
|
+
// AIRichResponseImageURL message (imagePreviewUrl/imageHighResUrl/sourceUrl), NOT a plain string.
|
|
16
|
+
// Passing a raw string made protobufjs silently encode an empty embedded message (no throw),
|
|
17
|
+
// so the image URL data was dropped on the wire and the image never rendered.
|
|
18
|
+
const toImageUrlObject = (url) => {
|
|
19
|
+
if (!url)
|
|
20
|
+
return undefined;
|
|
21
|
+
if (typeof url === 'object')
|
|
22
|
+
return { imagePreviewUrl: url.imagePreviewUrl || url.url, imageHighResUrl: url.imageHighResUrl || url.url, sourceUrl: url.sourceUrl || url.url };
|
|
23
|
+
return { imagePreviewUrl: url, imageHighResUrl: url, sourceUrl: url };
|
|
24
|
+
};
|
|
25
|
+
const IMAGE_ALIGNMENT = { leading: 0, trailing: 1, center: 2 };
|
|
26
|
+
const toImageAlignment = (alignment) => {
|
|
27
|
+
if (typeof alignment === 'number')
|
|
28
|
+
return alignment;
|
|
29
|
+
return IMAGE_ALIGNMENT[String(alignment || 'center').toLowerCase()] ?? IMAGE_ALIGNMENT.center;
|
|
30
|
+
};
|
|
31
|
+
export const tokenizeCode = (code, language = 'javascript') => {
|
|
32
|
+
const keywords = LANGUAGE_KEYWORDS[language] || NOOP;
|
|
33
|
+
const blocks = [];
|
|
34
|
+
LEXER_REGEX.lastIndex = 0;
|
|
35
|
+
let match;
|
|
36
|
+
while ((match = LEXER_REGEX.exec(code)) !== null) {
|
|
37
|
+
if (match[1]) {
|
|
38
|
+
blocks.push({ highlightType: CodeHighlightType.COMMENT, codeContent: match[1] });
|
|
39
|
+
}
|
|
40
|
+
else if (match[2]) {
|
|
41
|
+
blocks.push({ highlightType: CodeHighlightType.STRING, codeContent: match[2] });
|
|
42
|
+
}
|
|
43
|
+
else if (match[3]) {
|
|
44
|
+
blocks.push({
|
|
45
|
+
highlightType: keywords.has(match[3]) ? CodeHighlightType.KEYWORD : CodeHighlightType.METHOD,
|
|
46
|
+
codeContent: match[3],
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
else if (match[4]) {
|
|
50
|
+
blocks.push({
|
|
51
|
+
highlightType: keywords.has(match[4]) ? CodeHighlightType.KEYWORD : CodeHighlightType.DEFAULT,
|
|
52
|
+
codeContent: match[4],
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
else if (match[5]) {
|
|
56
|
+
blocks.push({ highlightType: CodeHighlightType.NUMBER, codeContent: match[5] });
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
blocks.push({ highlightType: CodeHighlightType.DEFAULT, codeContent: match[6] });
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return blocks;
|
|
63
|
+
};
|
|
64
|
+
// JAP@Changes 09-04-26 --- Inject buffer into unifiedResponse.data to support proper rendering of rich messages (ex: tables and code blocks)
|
|
65
|
+
export const toUnified = (submessages, uuid) => ({
|
|
66
|
+
response_id: uuid || randomUUID(),
|
|
67
|
+
sections: submessages.map((submessage, index) => {
|
|
68
|
+
switch (submessage.messageType) {
|
|
69
|
+
case RichSubMessageType.CODE:
|
|
70
|
+
const codeMetadata = submessage.codeMetadata;
|
|
71
|
+
return {
|
|
72
|
+
view_model: {
|
|
73
|
+
primitive: {
|
|
74
|
+
language: codeMetadata.codeLanguage,
|
|
75
|
+
code_blocks: codeMetadata.codeBlocks.map((block) => ({ content: block.codeContent, type: CodeHighlightType[block.highlightType] })),
|
|
76
|
+
__typename: 'GenAICodeUXPrimitive'
|
|
77
|
+
},
|
|
78
|
+
__typename: 'GenAISingleLayoutViewModel'
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
case RichSubMessageType.CONTENT_ITEMS:
|
|
82
|
+
// JAP@Fix (bug 40): was returning {} — now renders content items carousel
|
|
83
|
+
const contentItemsMeta = submessage.contentItemsMetadata;
|
|
84
|
+
return {
|
|
85
|
+
view_model: {
|
|
86
|
+
primitive: {
|
|
87
|
+
items: contentItemsMeta?.itemsMetadata || [],
|
|
88
|
+
content_type: contentItemsMeta?.contentType ?? 1,
|
|
89
|
+
__typename: 'GenAIContentItemsUXPrimitive'
|
|
90
|
+
},
|
|
91
|
+
__typename: 'GenAISingleLayoutViewModel'
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
case RichSubMessageType.INLINE_IMAGE:
|
|
95
|
+
// JAP@Fix (bug 40, round 2): imageUrl is now a real AIRichResponseImageURL
|
|
96
|
+
// object (imagePreviewUrl/imageHighResUrl/sourceUrl) instead of a bare string
|
|
97
|
+
// that protobufjs silently dropped on encode. alignment is mapped back to its
|
|
98
|
+
// enum name (matches how the compiled proto's toObject() renders other enums).
|
|
99
|
+
const imgMeta = submessage.imageMetadata;
|
|
100
|
+
const ALIGNMENT_NAME = ['AI_RICH_RESPONSE_IMAGE_LAYOUT_LEADING_ALIGNED', 'AI_RICH_RESPONSE_IMAGE_LAYOUT_TRAILING_ALIGNED', 'AI_RICH_RESPONSE_IMAGE_LAYOUT_CENTER_ALIGNED'];
|
|
101
|
+
return {
|
|
102
|
+
view_model: {
|
|
103
|
+
primitive: {
|
|
104
|
+
image_url: {
|
|
105
|
+
image_preview_url: imgMeta?.imageUrl?.imagePreviewUrl || '',
|
|
106
|
+
image_high_res_url: imgMeta?.imageUrl?.imageHighResUrl || '',
|
|
107
|
+
source_url: imgMeta?.imageUrl?.sourceUrl || ''
|
|
108
|
+
},
|
|
109
|
+
image_text: imgMeta?.imageText || '',
|
|
110
|
+
alignment: ALIGNMENT_NAME[imgMeta?.alignment ?? 2],
|
|
111
|
+
tap_link_url: imgMeta?.tapLinkUrl || '',
|
|
112
|
+
__typename: 'GenAIInlineImageUXPrimitive'
|
|
113
|
+
},
|
|
114
|
+
__typename: 'GenAISingleLayoutViewModel'
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
case RichSubMessageType.LATEX:
|
|
118
|
+
// JAP@Fix (bug 40): was returning {} — now renders LaTeX expressions
|
|
119
|
+
const latexMeta = submessage.latexMetadata;
|
|
120
|
+
return {
|
|
121
|
+
view_model: {
|
|
122
|
+
primitive: {
|
|
123
|
+
text: latexMeta?.text || '',
|
|
124
|
+
expressions: latexMeta?.expressions || [],
|
|
125
|
+
__typename: 'GenAILatexUXPrimitive'
|
|
126
|
+
},
|
|
127
|
+
__typename: 'GenAISingleLayoutViewModel'
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
case RichSubMessageType.TABLE:
|
|
131
|
+
const tableMetadata = submessage.tableMetadata || {};
|
|
132
|
+
// Accept both the internal row shape ({ items, isHeading }) and
|
|
133
|
+
// the raw string[][] shape used by some rich-response callers.
|
|
134
|
+
const rawRows = Array.isArray(tableMetadata.rows) ? tableMetadata.rows : [];
|
|
135
|
+
const normalizedRows = rawRows.map((row, index) => {
|
|
136
|
+
if (Array.isArray(row)) {
|
|
137
|
+
return { is_header: index === 0, cells: row.map(String) };
|
|
138
|
+
}
|
|
139
|
+
const cells = Array.isArray(row?.items) ? row.items.map(String) : [];
|
|
140
|
+
return { is_header: !!row?.isHeading, cells };
|
|
141
|
+
});
|
|
142
|
+
return {
|
|
143
|
+
view_model: {
|
|
144
|
+
primitive: {
|
|
145
|
+
title: tableMetadata.title || '',
|
|
146
|
+
rows: normalizedRows.map((row) => ({
|
|
147
|
+
is_header: row.is_header,
|
|
148
|
+
cells: row.cells,
|
|
149
|
+
markdown_cells: row.cells.map((item) => ({ text: item }))
|
|
150
|
+
})),
|
|
151
|
+
__typename: 'GenATableUXPrimitive'
|
|
152
|
+
},
|
|
153
|
+
__typename: 'GenAISingleLayoutViewModel'
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
case RichSubMessageType.TEXT:
|
|
157
|
+
return {
|
|
158
|
+
view_model: {
|
|
159
|
+
primitive: {
|
|
160
|
+
text: submessage.messageText,
|
|
161
|
+
inline_entities: submessage.inlineEntities || [],
|
|
162
|
+
__typename: 'GenAIMarkdownTextUXPrimitive'
|
|
163
|
+
},
|
|
164
|
+
__typename: 'GenAISingleLayoutViewModel'
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
return submessage;
|
|
169
|
+
})
|
|
170
|
+
});
|
|
171
|
+
export const prepareRichResponseMessage = (content) => {
|
|
172
|
+
const { alignment, code, contentText, disclaimerText, footerText, headerText, imageText, inlineImage, inlineVideo, items, language, latex, links, noHeading, posts, products, suggested, richResponse, table, tapLinkUrl, title } = content;
|
|
173
|
+
let submessages = [];
|
|
174
|
+
if (Array.isArray(richResponse)) {
|
|
175
|
+
submessages = richResponse.map((submessage) => {
|
|
176
|
+
if (submessage.text) {
|
|
177
|
+
return {
|
|
178
|
+
messageType: RichSubMessageType.TEXT,
|
|
179
|
+
messageText: submessage.text,
|
|
180
|
+
inlineEntities: submessage.inlineEntities
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
else if (submessage.code) {
|
|
184
|
+
return {
|
|
185
|
+
messageType: RichSubMessageType.CODE,
|
|
186
|
+
codeMetadata: {
|
|
187
|
+
codeLanguage: submessage.language,
|
|
188
|
+
codeBlocks: submessage.code
|
|
189
|
+
}
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
else if (submessage.items) {
|
|
193
|
+
return {
|
|
194
|
+
messageType: RichSubMessageType.CONTENT_ITEMS,
|
|
195
|
+
contentItemsMetadata: {
|
|
196
|
+
itemsMetadata: submessage.items,
|
|
197
|
+
contentType: proto.AIRichResponseContentItemsMetadata.ContentType.CAROUSEL
|
|
198
|
+
}
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
else if (submessage.inlineImage) {
|
|
202
|
+
return {
|
|
203
|
+
messageType: RichSubMessageType.INLINE_IMAGE,
|
|
204
|
+
imageMetadata: {
|
|
205
|
+
imageUrl: toImageUrlObject(submessage.inlineImage),
|
|
206
|
+
imageText: submessage.imageText,
|
|
207
|
+
alignment: toImageAlignment(submessage.alignment),
|
|
208
|
+
tapLinkUrl: submessage.tapLinkUrl
|
|
209
|
+
}
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
else if (submessage.inlineVideo) {
|
|
213
|
+
return {
|
|
214
|
+
messageType: RichSubMessageType.TEXT,
|
|
215
|
+
messageText: 'INLINE_VIDEO'
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
else if (submessage.latex) {
|
|
219
|
+
return {
|
|
220
|
+
messageType: RichSubMessageType.LATEX,
|
|
221
|
+
latexMetadata: {
|
|
222
|
+
text: submessage.text,
|
|
223
|
+
expressions: submessage.latex
|
|
224
|
+
}
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
else if (submessage.posts) {
|
|
228
|
+
return {
|
|
229
|
+
messageType: RichSubMessageType.TEXT,
|
|
230
|
+
messageText: 'POSTS'
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
else if (submessage.products) {
|
|
234
|
+
return {
|
|
235
|
+
messageType: RichSubMessageType.TEXT,
|
|
236
|
+
messageText: 'PRODUCTS'
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
else if (submessage.suggested) {
|
|
240
|
+
return {
|
|
241
|
+
messageType: RichSubMessageType.TEXT,
|
|
242
|
+
messageText: 'SUGGESTED_PROMPT'
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
else if (submessage.table) {
|
|
246
|
+
const rows = Array.isArray(submessage.table)
|
|
247
|
+
? submessage.table.map((row, index) => Array.isArray(row)
|
|
248
|
+
? { isHeading: index === 0, items: row.map(String) }
|
|
249
|
+
: row)
|
|
250
|
+
: [];
|
|
251
|
+
return {
|
|
252
|
+
messageType: RichSubMessageType.TABLE,
|
|
253
|
+
tableMetadata: {
|
|
254
|
+
title: submessage.title || '',
|
|
255
|
+
rows
|
|
256
|
+
}
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
return submessage;
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
else {
|
|
263
|
+
if (headerText) {
|
|
264
|
+
submessages.push({
|
|
265
|
+
messageType: RichSubMessageType.TEXT,
|
|
266
|
+
messageText: headerText
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
if (contentText) {
|
|
270
|
+
submessages.push({
|
|
271
|
+
messageType: RichSubMessageType.TEXT,
|
|
272
|
+
messageText: contentText
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
if (code) {
|
|
276
|
+
language ||= 'javascript';
|
|
277
|
+
submessages.push({
|
|
278
|
+
messageType: RichSubMessageType.CODE,
|
|
279
|
+
codeMetadata: {
|
|
280
|
+
codeLanguage: language,
|
|
281
|
+
codeBlocks: tokenizeCode(code, language)
|
|
282
|
+
}
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
if (items) {
|
|
286
|
+
submessages.push({
|
|
287
|
+
messageType: RichSubMessageType.CONTENT_ITEMS,
|
|
288
|
+
contentItemsMetadata: {
|
|
289
|
+
itemsMetadata: items,
|
|
290
|
+
contentType: proto.AIRichResponseContentItemsMetadata.ContentType.CAROUSEL
|
|
291
|
+
}
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
if (inlineImage) {
|
|
295
|
+
submessages.push({
|
|
296
|
+
messageType: RichSubMessageType.INLINE_IMAGE,
|
|
297
|
+
imageMetadata: {
|
|
298
|
+
imageUrl: toImageUrlObject(inlineImage),
|
|
299
|
+
imageText,
|
|
300
|
+
alignment: toImageAlignment(alignment),
|
|
301
|
+
tapLinkUrl
|
|
302
|
+
}
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
if (inlineVideo) {
|
|
306
|
+
submessages.push({
|
|
307
|
+
messageType: RichSubMessageType.TEXT,
|
|
308
|
+
messageText: 'INLINE_VIDEO'
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
if (latex) {
|
|
312
|
+
submessages.push({
|
|
313
|
+
messageType: RichSubMessageType.LATEX,
|
|
314
|
+
latexMetadata: {
|
|
315
|
+
text,
|
|
316
|
+
expressions: latex
|
|
317
|
+
}
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
if (links) {
|
|
321
|
+
links.forEach((linkField, index) => {
|
|
322
|
+
const prefix = 'SS_' + index;
|
|
323
|
+
const url = linkField.url || DONATE_URL;
|
|
324
|
+
const sources = linkField.sources?.map((sourceField) => ({
|
|
325
|
+
source_type: 'THIRD_PARTY',
|
|
326
|
+
source_display_name: sourceField.displayName || 'Donate',
|
|
327
|
+
source_subtitle: sourceField.subtitle || 'Saweria',
|
|
328
|
+
source_url: sourceField.url || url
|
|
329
|
+
}));
|
|
330
|
+
submessages.push({
|
|
331
|
+
messageType: RichSubMessageType.TEXT,
|
|
332
|
+
messageText: linkField.text + ` {{${prefix}}}¹{{/${prefix}}} `,
|
|
333
|
+
inlineEntities: [{
|
|
334
|
+
key: prefix,
|
|
335
|
+
metadata: {
|
|
336
|
+
reference_id: index + 1,
|
|
337
|
+
reference_url: url,
|
|
338
|
+
reference_title: linkField.title || 'For Donation via Saweria',
|
|
339
|
+
reference_display_name: linkField.displayName || 'Donation',
|
|
340
|
+
sources: sources || [],
|
|
341
|
+
__typename: 'GenAISearchCitationItem'
|
|
342
|
+
}
|
|
343
|
+
}]
|
|
344
|
+
});
|
|
345
|
+
});
|
|
346
|
+
}
|
|
347
|
+
if (posts) {
|
|
348
|
+
submessages.push({
|
|
349
|
+
messageType: RichSubMessageType.TEXT,
|
|
350
|
+
messageText: 'POSTS'
|
|
351
|
+
});
|
|
352
|
+
}
|
|
353
|
+
if (products) {
|
|
354
|
+
submessages.push({
|
|
355
|
+
messageType: RichSubMessageType.TEXT,
|
|
356
|
+
messageText: 'PRODUCTS'
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
if (suggested) {
|
|
360
|
+
submessages.push({
|
|
361
|
+
messageType: RichSubMessageType.TEXT,
|
|
362
|
+
messageText: 'SUGGESTED_PROMPT'
|
|
363
|
+
});
|
|
364
|
+
}
|
|
365
|
+
if (table) {
|
|
366
|
+
submessages.push({
|
|
367
|
+
messageType: RichSubMessageType.TABLE,
|
|
368
|
+
tableMetadata: {
|
|
369
|
+
title,
|
|
370
|
+
rows: table.map((items, index) => ({
|
|
371
|
+
isHeading: !noHeading && index == 0,
|
|
372
|
+
items
|
|
373
|
+
}))
|
|
374
|
+
}
|
|
375
|
+
});
|
|
376
|
+
}
|
|
377
|
+
if (footerText) {
|
|
378
|
+
submessages.push({
|
|
379
|
+
messageType: RichSubMessageType.TEXT,
|
|
380
|
+
messageText: footerText
|
|
381
|
+
});
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
const uuid = randomUUID();
|
|
385
|
+
const unified = toUnified(submessages, uuid);
|
|
386
|
+
const richResponseMessage = proto.AIRichResponseMessage.create({
|
|
387
|
+
submessages,
|
|
388
|
+
messageType: proto.AIRichResponseMessageType.AI_RICH_RESPONSE_TYPE_STANDARD,
|
|
389
|
+
unifiedResponse: {
|
|
390
|
+
data: Buffer.from(JSON.stringify(unified)) // JAP@Note 25-04-26 --- Expects "ArrayBufferLike"
|
|
391
|
+
},
|
|
392
|
+
contextInfo: {
|
|
393
|
+
isForwarded: true,
|
|
394
|
+
forwardingScore: 1,
|
|
395
|
+
forwardedAiBotMessageInfo: { botJid: '867051314767696@bot' },
|
|
396
|
+
forwardOrigin: 4
|
|
397
|
+
}
|
|
398
|
+
});
|
|
399
|
+
const message = wrapToBotForwardedMessage(richResponseMessage);
|
|
400
|
+
const botMetadata = message.messageContextInfo.botMetadata;
|
|
401
|
+
// JAP@Note 13-05-26 --- Add disclaimer text on richResponseMessage
|
|
402
|
+
if (disclaimerText) {
|
|
403
|
+
botMetadata.messageDisclaimerText = disclaimerText;
|
|
404
|
+
}
|
|
405
|
+
// JAP@Note 15-05-26 --- Add responseId from unified directly to botMetadata
|
|
406
|
+
botMetadata.botResponseId = uuid;
|
|
407
|
+
return message;
|
|
408
|
+
};
|
|
409
|
+
// JAP@Note 17-04-26 --- signature and certificateChain for proofs[] field
|
|
410
|
+
export const botMetadataSignature = () => {
|
|
411
|
+
const signature = new Uint8Array(64);
|
|
412
|
+
getRandomValues(signature);
|
|
413
|
+
return signature;
|
|
414
|
+
};
|
|
415
|
+
export const botMetadataCertificate = (length = 685) => {
|
|
416
|
+
const certificate = new Uint8Array(length);
|
|
417
|
+
certificate[0] = 48;
|
|
418
|
+
certificate[1] = 130;
|
|
419
|
+
getRandomValues(certificate.subarray(2));
|
|
420
|
+
return certificate;
|
|
421
|
+
};
|
|
422
|
+
export const wrapToBotForwardedMessage = (richResponseMessage) => ({
|
|
423
|
+
messageContextInfo: {
|
|
424
|
+
botMetadata: {
|
|
425
|
+
// JAP@Note 09-04-26 --- TODO: Fill verificationMetadata field
|
|
426
|
+
verificationMetadata: {
|
|
427
|
+
proofs: [
|
|
428
|
+
{
|
|
429
|
+
certificateChain: [
|
|
430
|
+
botMetadataCertificate(),
|
|
431
|
+
botMetadataCertificate(892)
|
|
432
|
+
],
|
|
433
|
+
version: 1,
|
|
434
|
+
useCase: 1,
|
|
435
|
+
signature: botMetadataSignature()
|
|
436
|
+
}
|
|
437
|
+
]
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
},
|
|
441
|
+
botForwardedMessage: {
|
|
442
|
+
message: { richResponseMessage }
|
|
443
|
+
}
|
|
444
|
+
});
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* JAP@Add --- Type declarations for the command router.
|
|
3
|
+
*/
|
|
4
|
+
export declare const extractCommandText: (webMessage: any) => string;
|
|
5
|
+
export interface RouterContext {
|
|
6
|
+
sock: any;
|
|
7
|
+
msg: any;
|
|
8
|
+
key: any;
|
|
9
|
+
jid: string;
|
|
10
|
+
sender: string;
|
|
11
|
+
pushName: string;
|
|
12
|
+
text: string;
|
|
13
|
+
raw: string;
|
|
14
|
+
command: string;
|
|
15
|
+
args: string[];
|
|
16
|
+
reply: (content: any, opts?: any) => Promise<any>;
|
|
17
|
+
react: (emoji: string) => Promise<any>;
|
|
18
|
+
}
|
|
19
|
+
export interface RouterOptions {
|
|
20
|
+
prefix?: string | string[];
|
|
21
|
+
ignoreMe?: boolean;
|
|
22
|
+
help?: boolean;
|
|
23
|
+
onError?: (err: any, ctx: RouterContext) => void;
|
|
24
|
+
}
|
|
25
|
+
export interface Router {
|
|
26
|
+
command(names: string | string[], handler: (ctx: RouterContext) => any, opts?: {
|
|
27
|
+
desc?: string;
|
|
28
|
+
}): Router;
|
|
29
|
+
use(mw: (ctx: RouterContext, next: () => Promise<void>) => any): Router;
|
|
30
|
+
handle(sock: any, webMessage: any): Promise<boolean>;
|
|
31
|
+
attach(sock: any): () => void;
|
|
32
|
+
list(): {
|
|
33
|
+
names: string[];
|
|
34
|
+
desc: string;
|
|
35
|
+
}[];
|
|
36
|
+
}
|
|
37
|
+
export declare const createRouter: (opts?: RouterOptions) => Router;
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* JAP@Add --- Minimal command router for prefix bots.
|
|
3
|
+
*
|
|
4
|
+
* ```js
|
|
5
|
+
* import { createRouter } from '@japofc/baileys'
|
|
6
|
+
* const router = createRouter({ prefix: '!' })
|
|
7
|
+
* router.command('ping', async (ctx) => ctx.reply('pong! 🏓'), { desc: 'Check bot' })
|
|
8
|
+
* router.command(['hi', 'halo'], async (ctx) => ctx.reply(`Halo @${ctx.sender.split('@')[0]}!`))
|
|
9
|
+
* router.attach(sock) // listens to messages.upsert; returns detach()
|
|
10
|
+
* ```
|
|
11
|
+
*
|
|
12
|
+
* Context: `{ sock, msg, key, jid, sender, pushName, text, raw, command, args,
|
|
13
|
+
* reply(), react() }`. A `help`/`menu` command is registered automatically
|
|
14
|
+
* (disable with `{ help: false }`). Handler errors go to `onError`
|
|
15
|
+
* (default: `console.error`, router keeps running).
|
|
16
|
+
*/
|
|
17
|
+
import { extractMessageContent } from './messages.js';
|
|
18
|
+
/** Pull reply-able text out of any incoming message (text/extended/captions). */
|
|
19
|
+
export const extractCommandText = (webMessage) => {
|
|
20
|
+
const content = extractMessageContent(webMessage?.message);
|
|
21
|
+
if (!content) {
|
|
22
|
+
return '';
|
|
23
|
+
}
|
|
24
|
+
return content.conversation
|
|
25
|
+
|| content.extendedTextMessage?.text
|
|
26
|
+
|| content.imageMessage?.caption
|
|
27
|
+
|| content.videoMessage?.caption
|
|
28
|
+
|| '';
|
|
29
|
+
};
|
|
30
|
+
export const createRouter = ({ prefix = '!', ignoreMe = true, help = true, onError } = {}) => {
|
|
31
|
+
const prefixes = (Array.isArray(prefix) ? prefix : [prefix]).filter((p) => typeof p === 'string' && p);
|
|
32
|
+
const commands = new Map();
|
|
33
|
+
const middlewares = [];
|
|
34
|
+
const handleError = typeof onError === 'function'
|
|
35
|
+
? onError
|
|
36
|
+
: (err, ctx) => console.error(`[router:${ctx?.command || '?'}]`, err);
|
|
37
|
+
const parse = (text) => {
|
|
38
|
+
const clean = String(text || '').trim();
|
|
39
|
+
if (!clean) {
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
const hit = prefixes.find((p) => clean.startsWith(p));
|
|
43
|
+
if (!hit) {
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
const [command, ...args] = clean.slice(hit.length).trim().split(/\s+/).filter(Boolean);
|
|
47
|
+
if (!command) {
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
return { command: command.toLowerCase(), args, raw: clean };
|
|
51
|
+
};
|
|
52
|
+
const makeCtx = (sock, webMessage, parsed) => {
|
|
53
|
+
const key = webMessage.key || {};
|
|
54
|
+
const jid = key.remoteJid;
|
|
55
|
+
const sender = key.participant || jid;
|
|
56
|
+
return {
|
|
57
|
+
sock,
|
|
58
|
+
msg: webMessage,
|
|
59
|
+
key,
|
|
60
|
+
jid,
|
|
61
|
+
sender,
|
|
62
|
+
pushName: webMessage.pushName || '',
|
|
63
|
+
text: parsed.raw,
|
|
64
|
+
raw: parsed.raw,
|
|
65
|
+
command: parsed.command,
|
|
66
|
+
args: parsed.args,
|
|
67
|
+
reply: (content, opts = {}) => sock.sendMessage(jid, typeof content === 'string' ? { text: content } : content, { quoted: webMessage, ...opts }),
|
|
68
|
+
react: (emoji) => sock.sendMessage(jid, { react: { text: emoji, key } })
|
|
69
|
+
};
|
|
70
|
+
};
|
|
71
|
+
const runMiddlewares = async (ctx, handler) => {
|
|
72
|
+
let index = -1;
|
|
73
|
+
const next = async () => {
|
|
74
|
+
index++;
|
|
75
|
+
if (index < middlewares.length) {
|
|
76
|
+
await middlewares[index](ctx, next);
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
await handler(ctx);
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
await next();
|
|
83
|
+
};
|
|
84
|
+
const router = {
|
|
85
|
+
/** Register `command('name' | ['alias', ...], handler, { desc })`. */
|
|
86
|
+
command(names, handler, { desc = '' } = {}) {
|
|
87
|
+
const list = (Array.isArray(names) ? names : [names])
|
|
88
|
+
.map((n) => String(n || '').toLowerCase())
|
|
89
|
+
.filter(Boolean);
|
|
90
|
+
if (!list.length || typeof handler !== 'function') {
|
|
91
|
+
throw new TypeError("router.command(names, handler) requires name(s) + function");
|
|
92
|
+
}
|
|
93
|
+
const entry = { handler, desc: String(desc || ''), names: list };
|
|
94
|
+
for (const name of list) {
|
|
95
|
+
commands.set(name, entry);
|
|
96
|
+
}
|
|
97
|
+
return router;
|
|
98
|
+
},
|
|
99
|
+
/** Register middleware `(ctx, next) => { ...; await next() }`. */
|
|
100
|
+
use(mw) {
|
|
101
|
+
if (typeof mw !== 'function') {
|
|
102
|
+
throw new TypeError('router.use(mw) requires a function');
|
|
103
|
+
}
|
|
104
|
+
middlewares.push(mw);
|
|
105
|
+
return router;
|
|
106
|
+
},
|
|
107
|
+
/** Handle one incoming message. Returns true when a command matched. */
|
|
108
|
+
async handle(sock, webMessage) {
|
|
109
|
+
if (!webMessage?.key || (ignoreMe && webMessage.key.fromMe)) {
|
|
110
|
+
return false;
|
|
111
|
+
}
|
|
112
|
+
const parsed = parse(extractCommandText(webMessage));
|
|
113
|
+
if (!parsed) {
|
|
114
|
+
return false;
|
|
115
|
+
}
|
|
116
|
+
const entry = commands.get(parsed.command);
|
|
117
|
+
if (!entry) {
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
const ctx = makeCtx(sock, webMessage, parsed);
|
|
121
|
+
try {
|
|
122
|
+
await runMiddlewares(ctx, entry.handler);
|
|
123
|
+
}
|
|
124
|
+
catch (err) {
|
|
125
|
+
handleError(err, ctx);
|
|
126
|
+
}
|
|
127
|
+
return true;
|
|
128
|
+
},
|
|
129
|
+
/** Attach to `sock.ev('messages.upsert')`. Returns a detach function. */
|
|
130
|
+
attach(sock) {
|
|
131
|
+
const onUpsert = ({ messages } = {}) => {
|
|
132
|
+
for (const m of messages || []) {
|
|
133
|
+
void router.handle(sock, m);
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
sock.ev.on('messages.upsert', onUpsert);
|
|
137
|
+
return () => {
|
|
138
|
+
try {
|
|
139
|
+
sock.ev.off('messages.upsert', onUpsert);
|
|
140
|
+
}
|
|
141
|
+
catch { }
|
|
142
|
+
};
|
|
143
|
+
},
|
|
144
|
+
/** List registered primary command names (for custom help screens). */
|
|
145
|
+
list() {
|
|
146
|
+
const seen = new Set();
|
|
147
|
+
const out = [];
|
|
148
|
+
for (const entry of commands.values()) {
|
|
149
|
+
if (seen.has(entry)) {
|
|
150
|
+
continue;
|
|
151
|
+
}
|
|
152
|
+
seen.add(entry);
|
|
153
|
+
out.push({ names: entry.names, desc: entry.desc });
|
|
154
|
+
}
|
|
155
|
+
return out;
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
if (help) {
|
|
159
|
+
router.command(['help', 'menu'], async (ctx) => {
|
|
160
|
+
const lines = router.list().map(({ names, desc }) => `• ${prefixes[0]}${names[0]}${desc ? ` — ${desc}` : ''}`);
|
|
161
|
+
await ctx.reply(`🤖 *Bot Menu*\n\n${lines.join('\n') || '(no commands)'}`);
|
|
162
|
+
}, { desc: 'Show this menu' });
|
|
163
|
+
}
|
|
164
|
+
return router;
|
|
165
|
+
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* JAP@Add --- Type declarations for the status/channel schedulers.
|
|
3
|
+
*/
|
|
4
|
+
export interface TimedPostOptions {
|
|
5
|
+
repeatMs?: number;
|
|
6
|
+
maxRepeats?: number;
|
|
7
|
+
}
|
|
8
|
+
export interface StatusPostOptions extends TimedPostOptions {
|
|
9
|
+
jids?: string[];
|
|
10
|
+
}
|
|
11
|
+
declare class BaseTimedPoster {
|
|
12
|
+
protected sock: any;
|
|
13
|
+
constructor(sock: any, opts?: {
|
|
14
|
+
onError?: (err: any, id: string) => void;
|
|
15
|
+
});
|
|
16
|
+
cancel(id: string): boolean;
|
|
17
|
+
clear(): void;
|
|
18
|
+
pending(): {
|
|
19
|
+
id: string;
|
|
20
|
+
runs: number;
|
|
21
|
+
}[];
|
|
22
|
+
}
|
|
23
|
+
export declare class StatusScheduler extends BaseTimedPoster {
|
|
24
|
+
schedule(content: any, when: Date | number, opts?: StatusPostOptions): string;
|
|
25
|
+
scheduleIn(content: any, delayMs: number, opts?: StatusPostOptions): string;
|
|
26
|
+
}
|
|
27
|
+
export declare class ChannelScheduler extends BaseTimedPoster {
|
|
28
|
+
schedule(channelJid: string, content: any, when: Date | number, opts?: TimedPostOptions): string;
|
|
29
|
+
scheduleIn(channelJid: string, content: any, delayMs: number, opts?: TimedPostOptions): string;
|
|
30
|
+
}
|