@vanzxy/baileys 2.0.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 +830 -0
- package/WAProto/index.d.ts +9 -0
- package/WAProto/index.js +142036 -0
- package/engine-requirements.js +13 -0
- package/lib/Defaults/index.d.ts +152 -0
- package/lib/Defaults/index.js +179 -0
- package/lib/Framework/Bot.js +209 -0
- package/lib/Framework/Context.js +90 -0
- package/lib/Framework/MediaManager.js +152 -0
- package/lib/Framework/SessionManager.js +34 -0
- package/lib/Framework/StatsManager.js +120 -0
- package/lib/Framework/Store/SQLiteStore.js +74 -0
- package/lib/Framework/index.js +11 -0
- package/lib/Signal/Group/ciphertext-message.d.ts +9 -0
- package/lib/Signal/Group/ciphertext-message.js +12 -0
- package/lib/Signal/Group/group-session-builder.d.ts +7 -0
- package/lib/Signal/Group/group-session-builder.js +30 -0
- package/lib/Signal/Group/group_cipher.d.ts +10 -0
- package/lib/Signal/Group/group_cipher.js +82 -0
- package/lib/Signal/Group/index.d.ts +11 -0
- package/lib/Signal/Group/index.js +12 -0
- package/lib/Signal/Group/keyhelper.d.ts +6 -0
- package/lib/Signal/Group/keyhelper.js +18 -0
- package/lib/Signal/Group/sender-chain-key.d.ts +13 -0
- package/lib/Signal/Group/sender-chain-key.js +26 -0
- package/lib/Signal/Group/sender-key-distribution-message.d.ts +16 -0
- package/lib/Signal/Group/sender-key-distribution-message.js +63 -0
- package/lib/Signal/Group/sender-key-message.d.ts +18 -0
- package/lib/Signal/Group/sender-key-message.js +66 -0
- package/lib/Signal/Group/sender-key-name.d.ts +11 -0
- package/lib/Signal/Group/sender-key-name.js +48 -0
- package/lib/Signal/Group/sender-key-record.d.ts +12 -0
- package/lib/Signal/Group/sender-key-record.js +41 -0
- package/lib/Signal/Group/sender-key-state.d.ts +16 -0
- package/lib/Signal/Group/sender-key-state.js +84 -0
- package/lib/Signal/Group/sender-message-key.d.ts +11 -0
- package/lib/Signal/Group/sender-message-key.js +26 -0
- package/lib/Signal/libsignal.d.ts +54 -0
- package/lib/Signal/libsignal.js +431 -0
- package/lib/Signal/lid-mapping.d.ts +20 -0
- package/lib/Signal/lid-mapping.js +277 -0
- package/lib/Socket/Client/index.d.ts +2 -0
- package/lib/Socket/Client/index.js +3 -0
- package/lib/Socket/Client/types.d.ts +5 -0
- package/lib/Socket/Client/types.js +11 -0
- package/lib/Socket/Client/websocket.d.ts +12 -0
- package/lib/Socket/Client/websocket.js +54 -0
- package/lib/Socket/business.d.ts +395 -0
- package/lib/Socket/business.js +380 -0
- package/lib/Socket/chats.d.ts +136 -0
- package/lib/Socket/chats.js +1328 -0
- package/lib/Socket/communities.d.ts +561 -0
- package/lib/Socket/communities.js +434 -0
- package/lib/Socket/dugong.d.ts +117 -0
- package/lib/Socket/dugong.js +799 -0
- package/lib/Socket/groups.d.ts +285 -0
- package/lib/Socket/groups.js +355 -0
- package/lib/Socket/index.d.ts +531 -0
- package/lib/Socket/index.js +34 -0
- package/lib/Socket/messages-recv.d.ts +333 -0
- package/lib/Socket/messages-recv.js +1782 -0
- package/lib/Socket/messages-send.d.ts +329 -0
- package/lib/Socket/messages-send.js +1383 -0
- package/lib/Socket/mex.d.ts +1 -0
- package/lib/Socket/mex.js +42 -0
- package/lib/Socket/newsletter.d.ts +281 -0
- package/lib/Socket/newsletter.js +321 -0
- package/lib/Socket/socket.d.ts +60 -0
- package/lib/Socket/socket.js +1001 -0
- package/lib/Socket/username.js +153 -0
- package/lib/Store/index.d.ts +3 -0
- package/lib/Store/index.js +4 -0
- package/lib/Store/make-in-memory-store.d.ts +62 -0
- package/lib/Store/make-in-memory-store.js +429 -0
- package/lib/Store/make-ordered-dictionary.d.ts +12 -0
- package/lib/Store/make-ordered-dictionary.js +79 -0
- package/lib/Store/object-repository.d.ts +10 -0
- package/lib/Store/object-repository.js +24 -0
- package/lib/Types/Auth.d.ts +117 -0
- package/lib/Types/Auth.js +2 -0
- package/lib/Types/Bussines.d.ts +25 -0
- package/lib/Types/Bussines.js +2 -0
- package/lib/Types/Call.d.ts +15 -0
- package/lib/Types/Call.js +2 -0
- package/lib/Types/Chat.d.ts +124 -0
- package/lib/Types/Chat.js +8 -0
- package/lib/Types/Contact.d.ts +26 -0
- package/lib/Types/Contact.js +2 -0
- package/lib/Types/Events.d.ts +256 -0
- package/lib/Types/Events.js +2 -0
- package/lib/Types/GroupMetadata.d.ts +71 -0
- package/lib/Types/GroupMetadata.js +2 -0
- package/lib/Types/Label.d.ts +2 -0
- package/lib/Types/Label.js +25 -0
- package/lib/Types/LabelAssociation.d.ts +2 -0
- package/lib/Types/LabelAssociation.js +7 -0
- package/lib/Types/Message.d.ts +11 -0
- package/lib/Types/Message.js +18 -0
- package/lib/Types/Mex.d.ts +2 -0
- package/lib/Types/Mex.js +39 -0
- package/lib/Types/Product.d.ts +79 -0
- package/lib/Types/Product.js +2 -0
- package/lib/Types/RichType.d.ts +2 -0
- package/lib/Types/RichType.js +23 -0
- package/lib/Types/Signal.d.ts +87 -0
- package/lib/Types/Signal.js +2 -0
- package/lib/Types/Socket.d.ts +136 -0
- package/lib/Types/Socket.js +2 -0
- package/lib/Types/State.d.ts +5 -0
- package/lib/Types/State.js +56 -0
- package/lib/Types/USync.d.ts +26 -0
- package/lib/Types/USync.js +2 -0
- package/lib/Types/index.d.ts +66 -0
- package/lib/Types/index.js +26 -0
- package/lib/Utils/A2UI.js +277 -0
- package/lib/Utils/MessageBuilder.js +4405 -0
- package/lib/Utils/MessageBuilder_d.ts +411 -0
- package/lib/Utils/PersistentStore.js +592 -0
- package/lib/Utils/PersistentStore_d.ts +60 -0
- package/lib/Utils/anti-delete.d.ts +74 -0
- package/lib/Utils/anti-delete.js +222 -0
- package/lib/Utils/auth-utils.d.ts +57 -0
- package/lib/Utils/auth-utils.js +307 -0
- package/lib/Utils/auto-reply.d.ts +47 -0
- package/lib/Utils/auto-reply.js +158 -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 +28 -0
- package/lib/Utils/business.d.ts +49 -0
- package/lib/Utils/business.js +240 -0
- package/lib/Utils/button-helper-utils.js +314 -0
- package/lib/Utils/button-sender.js +824 -0
- package/lib/Utils/chat-control.d.ts +159 -0
- package/lib/Utils/chat-control.js +232 -0
- package/lib/Utils/chat-history-helpers.d.ts +21 -0
- package/lib/Utils/chat-history-helpers.js +71 -0
- package/lib/Utils/chat-utils.d.ts +407 -0
- package/lib/Utils/chat-utils.js +886 -0
- package/lib/Utils/companion-reg-client-utils.d.ts +4 -0
- package/lib/Utils/companion-reg-client-utils.js +40 -0
- package/lib/Utils/crypto.d.ts +41 -0
- package/lib/Utils/crypto.js +148 -0
- package/lib/Utils/decode-wa-message.d.ts +83 -0
- package/lib/Utils/decode-wa-message.js +317 -0
- package/lib/Utils/event-buffer.d.ts +12 -0
- package/lib/Utils/event-buffer.js +636 -0
- package/lib/Utils/generics.d.ts +69 -0
- package/lib/Utils/generics.js +439 -0
- package/lib/Utils/history.d.ts +90 -0
- package/lib/Utils/history.js +147 -0
- package/lib/Utils/identity-change-handler.d.ts +13 -0
- package/lib/Utils/identity-change-handler.js +50 -0
- package/lib/Utils/index.d.ts +39 -0
- package/lib/Utils/index.js +46 -0
- package/lib/Utils/link-preview.d.ts +12 -0
- package/lib/Utils/link-preview.js +85 -0
- package/lib/Utils/logger.d.ts +2 -0
- package/lib/Utils/logger.js +3 -0
- package/lib/Utils/lt-hash.d.ts +12 -0
- package/lib/Utils/lt-hash.js +27 -0
- package/lib/Utils/make-mutex.d.ts +6 -0
- package/lib/Utils/make-mutex.js +33 -0
- package/lib/Utils/media-messages.d.ts +18 -0
- package/lib/Utils/media-messages.js +71 -0
- package/lib/Utils/media-set.d.ts +13 -0
- package/lib/Utils/media-set.js +172 -0
- package/lib/Utils/message-kind.js +139 -0
- package/lib/Utils/message-retry-manager.d.ts +79 -0
- package/lib/Utils/message-retry-manager.js +265 -0
- package/lib/Utils/message-search.d.ts +44 -0
- package/lib/Utils/message-search.js +189 -0
- package/lib/Utils/messages-media.d.ts +135 -0
- package/lib/Utils/messages-media.js +869 -0
- package/lib/Utils/messages.d.ts +44 -0
- package/lib/Utils/messages.js +2155 -0
- package/lib/Utils/noise-handler.d.ts +20 -0
- package/lib/Utils/noise-handler.js +201 -0
- package/lib/Utils/offline-node-processor.d.ts +9 -0
- package/lib/Utils/offline-node-processor.js +40 -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 +106 -0
- package/lib/Utils/process-message.d.ts +82 -0
- package/lib/Utils/process-message.js +809 -0
- package/lib/Utils/reporting-utils.d.ts +12 -0
- package/lib/Utils/reporting-utils.js +258 -0
- package/lib/Utils/rich-message-utils.d.ts +48 -0
- package/lib/Utils/rich-message-utils.js +445 -0
- package/lib/Utils/scheduling.d.ts +42 -0
- package/lib/Utils/scheduling.js +140 -0
- package/lib/Utils/signal.d.ts +82 -0
- package/lib/Utils/signal.js +201 -0
- package/lib/Utils/stanza-ack.d.ts +16 -0
- package/lib/Utils/stanza-ack.js +38 -0
- package/lib/Utils/status.d.ts +50 -0
- package/lib/Utils/status.js +108 -0
- package/lib/Utils/stickerpack.d.ts +51 -0
- package/lib/Utils/stickerpack.js +275 -0
- package/lib/Utils/sync-action-utils.d.ts +2 -0
- package/lib/Utils/sync-action-utils.js +49 -0
- package/lib/Utils/tc-token-utils.d.ts +29 -0
- package/lib/Utils/tc-token-utils.js +163 -0
- package/lib/Utils/templates.d.ts +76 -0
- package/lib/Utils/templates.js +151 -0
- package/lib/Utils/use-cache-manager-auth-state.d.ts +13 -0
- package/lib/Utils/use-cache-manager-auth-state.js +82 -0
- package/lib/Utils/use-multi-file-auth-state.d.ts +10 -0
- package/lib/Utils/use-multi-file-auth-state.js +167 -0
- package/lib/Utils/use-single-file-auth-state.d.ts +10 -0
- package/lib/Utils/use-single-file-auth-state.js +114 -0
- package/lib/Utils/use-sqlite-auth-state.d.ts +11 -0
- package/lib/Utils/use-sqlite-auth-state.js +170 -0
- package/lib/Utils/validate-connection.d.ts +42 -0
- package/lib/Utils/validate-connection.js +199 -0
- package/lib/Utils/vcard.d.ts +58 -0
- package/lib/Utils/vcard.js +105 -0
- package/lib/VoIP/audio-feeder.d.ts +15 -0
- package/lib/VoIP/audio-feeder.js +132 -0
- package/lib/VoIP/index.js +277 -0
- package/lib/VoIP/relay-transport.d.ts +43 -0
- package/lib/VoIP/relay-transport.js +559 -0
- package/lib/VoIP/signaling.js +624 -0
- package/lib/VoIP/types.d.ts +69 -0
- package/lib/VoIP/types.js +17 -0
- package/lib/VoIP/wasm-engine.d.ts +103 -0
- package/lib/VoIP/wasm-engine.js +1214 -0
- package/lib/VoIP/worker-bootstrap.js +1042 -0
- package/lib/WABinary/constants.d.ts +61 -0
- package/lib/WABinary/constants.js +1467 -0
- package/lib/WABinary/decode.d.ts +9 -0
- package/lib/WABinary/decode.js +262 -0
- package/lib/WABinary/encode.d.ts +2 -0
- package/lib/WABinary/encode.js +220 -0
- package/lib/WABinary/generic-utils.d.ts +74 -0
- package/lib/WABinary/generic-utils.js +277 -0
- package/lib/WABinary/index.d.ts +5 -0
- package/lib/WABinary/index.js +6 -0
- package/lib/WABinary/jid-utils.d.ts +28 -0
- package/lib/WABinary/jid-utils.js +96 -0
- package/lib/WABinary/types.d.ts +19 -0
- package/lib/WABinary/types.js +2 -0
- package/lib/WAM/BinaryInfo.d.ts +7 -0
- package/lib/WAM/BinaryInfo.js +10 -0
- package/lib/WAM/constants.d.ts +34926 -0
- package/lib/WAM/constants.js +22853 -0
- package/lib/WAM/encode.d.ts +1 -0
- package/lib/WAM/encode.js +150 -0
- package/lib/WAM/index.d.ts +3 -0
- package/lib/WAM/index.js +4 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +36 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +52 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +29 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +63 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +12 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +27 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +12 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +38 -0
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.d.ts +9 -0
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +25 -0
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +38 -0
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +51 -0
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +14 -0
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +29 -0
- package/lib/WAUSync/Protocols/index.d.ts +7 -0
- package/lib/WAUSync/Protocols/index.js +8 -0
- package/lib/WAUSync/USyncQuery.d.ts +20 -0
- package/lib/WAUSync/USyncQuery.js +122 -0
- package/lib/WAUSync/USyncUser.d.ts +16 -0
- package/lib/WAUSync/USyncUser.js +31 -0
- package/lib/WAUSync/index.d.ts +3 -0
- package/lib/WAUSync/index.js +4 -0
- package/lib/assets/wasm/loader.js +5 -0
- package/lib/assets/wasm/whatsapp.wasm +0 -0
- package/lib/assets/wasm/worker-modules.js +273 -0
- package/lib/index.d.ts +12 -0
- package/lib/index.js +20 -0
- package/package.json +175 -0
- package/postinstall-banner.js +56 -0
|
@@ -0,0 +1,445 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lia@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
|
+
// Vanz@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
|
+
// Lia@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
|
+
// Vanz@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
|
+
// Vanz@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
|
+
// Vanz@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)) // Lia@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
|
+
// Lia@Note 13-05-26 --- Add disclaimer text on richResponseMessage
|
|
402
|
+
if (disclaimerText) {
|
|
403
|
+
botMetadata.messageDisclaimerText = disclaimerText;
|
|
404
|
+
}
|
|
405
|
+
// Lia@Note 15-05-26 --- Add responseId from unified directly to botMetadata
|
|
406
|
+
botMetadata.botResponseId = uuid;
|
|
407
|
+
return message;
|
|
408
|
+
};
|
|
409
|
+
// Lia@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
|
+
// Lia@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
|
+
});
|
|
445
|
+
//# sourceMappingURL=rich-message-utils.js.map
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { WAMessage, AnyMessageContent } from '../Types/index.js';
|
|
2
|
+
export interface SchedulerOptions {
|
|
3
|
+
maxQueue?: number;
|
|
4
|
+
checkInterval?: number;
|
|
5
|
+
onSent?: (scheduled: ScheduledMessage, message?: WAMessage) => void;
|
|
6
|
+
onFailed?: (scheduled: ScheduledMessage, error: Error) => void;
|
|
7
|
+
}
|
|
8
|
+
export type ScheduledMessageStatus = 'pending' | 'sent' | 'failed' | 'cancelled';
|
|
9
|
+
export interface RepeatOptions {
|
|
10
|
+
repeatIntervalMs?: number;
|
|
11
|
+
maxRepeats?: number;
|
|
12
|
+
}
|
|
13
|
+
export interface ScheduledMessage {
|
|
14
|
+
id: string;
|
|
15
|
+
jid: string;
|
|
16
|
+
content: AnyMessageContent;
|
|
17
|
+
scheduledTime: Date;
|
|
18
|
+
createdAt: Date;
|
|
19
|
+
status: ScheduledMessageStatus;
|
|
20
|
+
messageId?: string;
|
|
21
|
+
error?: string;
|
|
22
|
+
repeatIntervalMs?: number;
|
|
23
|
+
maxRepeats?: number;
|
|
24
|
+
repeatCount?: number;
|
|
25
|
+
}
|
|
26
|
+
export declare class MessageScheduler {
|
|
27
|
+
private queue;
|
|
28
|
+
private timer;
|
|
29
|
+
private sendMessage;
|
|
30
|
+
private options;
|
|
31
|
+
constructor(sendMessage: (jid: string, content: AnyMessageContent) => Promise<WAMessage | undefined>, options?: SchedulerOptions);
|
|
32
|
+
schedule(jid: string, content: AnyMessageContent, scheduledTime: Date, repeatOptions?: RepeatOptions): ScheduledMessage;
|
|
33
|
+
scheduleDelay(jid: string, content: AnyMessageContent, delayMs: number, repeatOptions?: RepeatOptions): ScheduledMessage;
|
|
34
|
+
cancel(id: string): boolean;
|
|
35
|
+
cancelForJid(jid: string): number;
|
|
36
|
+
getPending(): ScheduledMessage[];
|
|
37
|
+
get(id: string): ScheduledMessage | undefined;
|
|
38
|
+
clearAll(): number;
|
|
39
|
+
stop(): void;
|
|
40
|
+
start(): void;
|
|
41
|
+
}
|
|
42
|
+
export declare const createMessageScheduler: (sendMessage: (jid: string, content: AnyMessageContent) => Promise<WAMessage | undefined>, options?: SchedulerOptions) => MessageScheduler;
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Vanz@Add --- ported from Bail-master addons/scheduling.ts + message-scheduler.ts.
|
|
3
|
+
* Bail-master shipped these as two separate, overlapping schedulers (one class-based
|
|
4
|
+
* with polling, one function-based with setTimeout + repeat support). Merged here into
|
|
5
|
+
* a single class-based scheduler so multiple independent instances can be bound to
|
|
6
|
+
* different sendMessage functions (e.g. multi-session bots), while keeping the
|
|
7
|
+
* repeat-interval/maxRepeats feature from the function-based version.
|
|
8
|
+
*/
|
|
9
|
+
export class MessageScheduler {
|
|
10
|
+
queue = new Map();
|
|
11
|
+
timer = null;
|
|
12
|
+
sendMessage;
|
|
13
|
+
options;
|
|
14
|
+
constructor(sendMessage, options = {}) {
|
|
15
|
+
this.sendMessage = sendMessage;
|
|
16
|
+
this.options = {
|
|
17
|
+
maxQueue: options.maxQueue ?? 1000,
|
|
18
|
+
checkInterval: options.checkInterval ?? 1000,
|
|
19
|
+
onSent: options.onSent ?? (() => { }),
|
|
20
|
+
onFailed: options.onFailed ?? (() => { })
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
generateId() {
|
|
24
|
+
return `sched_${Date.now()}_${Math.random().toString(36).substring(2, 9)}`;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Schedule a message for a specific Date. Pass `repeatIntervalMs` to
|
|
28
|
+
* re-queue the job that many ms after each send (optionally capped by
|
|
29
|
+
* `maxRepeats`).
|
|
30
|
+
*/
|
|
31
|
+
schedule(jid, content, scheduledTime, repeatOptions) {
|
|
32
|
+
if (this.queue.size >= this.options.maxQueue)
|
|
33
|
+
throw new Error(`Maximum queue size (${this.options.maxQueue}) reached`);
|
|
34
|
+
if (scheduledTime.getTime() <= Date.now())
|
|
35
|
+
throw new Error('Scheduled time must be in the future');
|
|
36
|
+
const scheduled = {
|
|
37
|
+
id: this.generateId(),
|
|
38
|
+
jid,
|
|
39
|
+
content,
|
|
40
|
+
scheduledTime,
|
|
41
|
+
createdAt: new Date(),
|
|
42
|
+
status: 'pending',
|
|
43
|
+
repeatIntervalMs: repeatOptions?.repeatIntervalMs,
|
|
44
|
+
maxRepeats: repeatOptions?.maxRepeats,
|
|
45
|
+
repeatCount: 0
|
|
46
|
+
};
|
|
47
|
+
this.queue.set(scheduled.id, scheduled);
|
|
48
|
+
this.ensureTimerRunning();
|
|
49
|
+
return scheduled;
|
|
50
|
+
}
|
|
51
|
+
scheduleDelay(jid, content, delayMs, repeatOptions) {
|
|
52
|
+
return this.schedule(jid, content, new Date(Date.now() + delayMs), repeatOptions);
|
|
53
|
+
}
|
|
54
|
+
cancel(id) {
|
|
55
|
+
const s = this.queue.get(id);
|
|
56
|
+
if (s?.status === 'pending') {
|
|
57
|
+
s.status = 'cancelled';
|
|
58
|
+
this.queue.delete(id);
|
|
59
|
+
return true;
|
|
60
|
+
}
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
cancelForJid(jid) {
|
|
64
|
+
let cancelled = 0;
|
|
65
|
+
for (const [id, s] of this.queue) {
|
|
66
|
+
if (s.jid === jid && s.status === 'pending') {
|
|
67
|
+
s.status = 'cancelled';
|
|
68
|
+
this.queue.delete(id);
|
|
69
|
+
cancelled++;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return cancelled;
|
|
73
|
+
}
|
|
74
|
+
getPending() {
|
|
75
|
+
return Array.from(this.queue.values()).filter((s) => s.status === 'pending');
|
|
76
|
+
}
|
|
77
|
+
get(id) {
|
|
78
|
+
return this.queue.get(id);
|
|
79
|
+
}
|
|
80
|
+
clearAll() {
|
|
81
|
+
const count = this.queue.size;
|
|
82
|
+
this.queue.clear();
|
|
83
|
+
this.stopTimer();
|
|
84
|
+
return count;
|
|
85
|
+
}
|
|
86
|
+
async processQueue() {
|
|
87
|
+
const now = Date.now();
|
|
88
|
+
for (const [id, s] of this.queue) {
|
|
89
|
+
if (s.status !== 'pending')
|
|
90
|
+
continue;
|
|
91
|
+
if (s.scheduledTime.getTime() > now)
|
|
92
|
+
continue;
|
|
93
|
+
try {
|
|
94
|
+
const message = await this.sendMessage(s.jid, s.content);
|
|
95
|
+
s.messageId = message?.key?.id ?? undefined;
|
|
96
|
+
this.options.onSent(s, message);
|
|
97
|
+
// Handle repeating jobs — re-arm instead of removing from the queue.
|
|
98
|
+
if (s.repeatIntervalMs && s.repeatIntervalMs > 0) {
|
|
99
|
+
const nextCount = (s.repeatCount ?? 0) + 1;
|
|
100
|
+
if (s.maxRepeats === undefined || nextCount < s.maxRepeats) {
|
|
101
|
+
s.repeatCount = nextCount;
|
|
102
|
+
s.scheduledTime = new Date(Date.now() + s.repeatIntervalMs);
|
|
103
|
+
s.status = 'pending';
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
s.status = 'sent';
|
|
108
|
+
}
|
|
109
|
+
catch (error) {
|
|
110
|
+
s.status = 'failed';
|
|
111
|
+
s.error = error?.message || String(error);
|
|
112
|
+
this.options.onFailed(s, error);
|
|
113
|
+
}
|
|
114
|
+
this.queue.delete(id);
|
|
115
|
+
}
|
|
116
|
+
if (this.queue.size === 0)
|
|
117
|
+
this.stopTimer();
|
|
118
|
+
}
|
|
119
|
+
ensureTimerRunning() {
|
|
120
|
+
if (!this.timer) {
|
|
121
|
+
this.timer = setInterval(() => this.processQueue(), this.options.checkInterval);
|
|
122
|
+
this.timer?.unref?.();
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
stopTimer() {
|
|
126
|
+
if (this.timer) {
|
|
127
|
+
clearInterval(this.timer);
|
|
128
|
+
this.timer = null;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
stop() {
|
|
132
|
+
this.stopTimer();
|
|
133
|
+
}
|
|
134
|
+
start() {
|
|
135
|
+
if (this.queue.size > 0)
|
|
136
|
+
this.ensureTimerRunning();
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
export const createMessageScheduler = (sendMessage, options) => new MessageScheduler(sendMessage, options);
|
|
140
|
+
//# sourceMappingURL=scheduling.js.map
|