@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.
Files changed (281) hide show
  1. package/NOTICE.md +111 -0
  2. package/README.md +830 -0
  3. package/WAProto/index.d.ts +9 -0
  4. package/WAProto/index.js +142036 -0
  5. package/engine-requirements.js +13 -0
  6. package/lib/Defaults/index.d.ts +152 -0
  7. package/lib/Defaults/index.js +179 -0
  8. package/lib/Framework/Bot.js +209 -0
  9. package/lib/Framework/Context.js +90 -0
  10. package/lib/Framework/MediaManager.js +152 -0
  11. package/lib/Framework/SessionManager.js +34 -0
  12. package/lib/Framework/StatsManager.js +120 -0
  13. package/lib/Framework/Store/SQLiteStore.js +74 -0
  14. package/lib/Framework/index.js +11 -0
  15. package/lib/Signal/Group/ciphertext-message.d.ts +9 -0
  16. package/lib/Signal/Group/ciphertext-message.js +12 -0
  17. package/lib/Signal/Group/group-session-builder.d.ts +7 -0
  18. package/lib/Signal/Group/group-session-builder.js +30 -0
  19. package/lib/Signal/Group/group_cipher.d.ts +10 -0
  20. package/lib/Signal/Group/group_cipher.js +82 -0
  21. package/lib/Signal/Group/index.d.ts +11 -0
  22. package/lib/Signal/Group/index.js +12 -0
  23. package/lib/Signal/Group/keyhelper.d.ts +6 -0
  24. package/lib/Signal/Group/keyhelper.js +18 -0
  25. package/lib/Signal/Group/sender-chain-key.d.ts +13 -0
  26. package/lib/Signal/Group/sender-chain-key.js +26 -0
  27. package/lib/Signal/Group/sender-key-distribution-message.d.ts +16 -0
  28. package/lib/Signal/Group/sender-key-distribution-message.js +63 -0
  29. package/lib/Signal/Group/sender-key-message.d.ts +18 -0
  30. package/lib/Signal/Group/sender-key-message.js +66 -0
  31. package/lib/Signal/Group/sender-key-name.d.ts +11 -0
  32. package/lib/Signal/Group/sender-key-name.js +48 -0
  33. package/lib/Signal/Group/sender-key-record.d.ts +12 -0
  34. package/lib/Signal/Group/sender-key-record.js +41 -0
  35. package/lib/Signal/Group/sender-key-state.d.ts +16 -0
  36. package/lib/Signal/Group/sender-key-state.js +84 -0
  37. package/lib/Signal/Group/sender-message-key.d.ts +11 -0
  38. package/lib/Signal/Group/sender-message-key.js +26 -0
  39. package/lib/Signal/libsignal.d.ts +54 -0
  40. package/lib/Signal/libsignal.js +431 -0
  41. package/lib/Signal/lid-mapping.d.ts +20 -0
  42. package/lib/Signal/lid-mapping.js +277 -0
  43. package/lib/Socket/Client/index.d.ts +2 -0
  44. package/lib/Socket/Client/index.js +3 -0
  45. package/lib/Socket/Client/types.d.ts +5 -0
  46. package/lib/Socket/Client/types.js +11 -0
  47. package/lib/Socket/Client/websocket.d.ts +12 -0
  48. package/lib/Socket/Client/websocket.js +54 -0
  49. package/lib/Socket/business.d.ts +395 -0
  50. package/lib/Socket/business.js +380 -0
  51. package/lib/Socket/chats.d.ts +136 -0
  52. package/lib/Socket/chats.js +1328 -0
  53. package/lib/Socket/communities.d.ts +561 -0
  54. package/lib/Socket/communities.js +434 -0
  55. package/lib/Socket/dugong.d.ts +117 -0
  56. package/lib/Socket/dugong.js +799 -0
  57. package/lib/Socket/groups.d.ts +285 -0
  58. package/lib/Socket/groups.js +355 -0
  59. package/lib/Socket/index.d.ts +531 -0
  60. package/lib/Socket/index.js +34 -0
  61. package/lib/Socket/messages-recv.d.ts +333 -0
  62. package/lib/Socket/messages-recv.js +1782 -0
  63. package/lib/Socket/messages-send.d.ts +329 -0
  64. package/lib/Socket/messages-send.js +1383 -0
  65. package/lib/Socket/mex.d.ts +1 -0
  66. package/lib/Socket/mex.js +42 -0
  67. package/lib/Socket/newsletter.d.ts +281 -0
  68. package/lib/Socket/newsletter.js +321 -0
  69. package/lib/Socket/socket.d.ts +60 -0
  70. package/lib/Socket/socket.js +1001 -0
  71. package/lib/Socket/username.js +153 -0
  72. package/lib/Store/index.d.ts +3 -0
  73. package/lib/Store/index.js +4 -0
  74. package/lib/Store/make-in-memory-store.d.ts +62 -0
  75. package/lib/Store/make-in-memory-store.js +429 -0
  76. package/lib/Store/make-ordered-dictionary.d.ts +12 -0
  77. package/lib/Store/make-ordered-dictionary.js +79 -0
  78. package/lib/Store/object-repository.d.ts +10 -0
  79. package/lib/Store/object-repository.js +24 -0
  80. package/lib/Types/Auth.d.ts +117 -0
  81. package/lib/Types/Auth.js +2 -0
  82. package/lib/Types/Bussines.d.ts +25 -0
  83. package/lib/Types/Bussines.js +2 -0
  84. package/lib/Types/Call.d.ts +15 -0
  85. package/lib/Types/Call.js +2 -0
  86. package/lib/Types/Chat.d.ts +124 -0
  87. package/lib/Types/Chat.js +8 -0
  88. package/lib/Types/Contact.d.ts +26 -0
  89. package/lib/Types/Contact.js +2 -0
  90. package/lib/Types/Events.d.ts +256 -0
  91. package/lib/Types/Events.js +2 -0
  92. package/lib/Types/GroupMetadata.d.ts +71 -0
  93. package/lib/Types/GroupMetadata.js +2 -0
  94. package/lib/Types/Label.d.ts +2 -0
  95. package/lib/Types/Label.js +25 -0
  96. package/lib/Types/LabelAssociation.d.ts +2 -0
  97. package/lib/Types/LabelAssociation.js +7 -0
  98. package/lib/Types/Message.d.ts +11 -0
  99. package/lib/Types/Message.js +18 -0
  100. package/lib/Types/Mex.d.ts +2 -0
  101. package/lib/Types/Mex.js +39 -0
  102. package/lib/Types/Product.d.ts +79 -0
  103. package/lib/Types/Product.js +2 -0
  104. package/lib/Types/RichType.d.ts +2 -0
  105. package/lib/Types/RichType.js +23 -0
  106. package/lib/Types/Signal.d.ts +87 -0
  107. package/lib/Types/Signal.js +2 -0
  108. package/lib/Types/Socket.d.ts +136 -0
  109. package/lib/Types/Socket.js +2 -0
  110. package/lib/Types/State.d.ts +5 -0
  111. package/lib/Types/State.js +56 -0
  112. package/lib/Types/USync.d.ts +26 -0
  113. package/lib/Types/USync.js +2 -0
  114. package/lib/Types/index.d.ts +66 -0
  115. package/lib/Types/index.js +26 -0
  116. package/lib/Utils/A2UI.js +277 -0
  117. package/lib/Utils/MessageBuilder.js +4405 -0
  118. package/lib/Utils/MessageBuilder_d.ts +411 -0
  119. package/lib/Utils/PersistentStore.js +592 -0
  120. package/lib/Utils/PersistentStore_d.ts +60 -0
  121. package/lib/Utils/anti-delete.d.ts +74 -0
  122. package/lib/Utils/anti-delete.js +222 -0
  123. package/lib/Utils/auth-utils.d.ts +57 -0
  124. package/lib/Utils/auth-utils.js +307 -0
  125. package/lib/Utils/auto-reply.d.ts +47 -0
  126. package/lib/Utils/auto-reply.js +158 -0
  127. package/lib/Utils/baileys-event-stream.d.ts +8 -0
  128. package/lib/Utils/baileys-event-stream.js +62 -0
  129. package/lib/Utils/browser-utils.d.ts +8 -0
  130. package/lib/Utils/browser-utils.js +28 -0
  131. package/lib/Utils/business.d.ts +49 -0
  132. package/lib/Utils/business.js +240 -0
  133. package/lib/Utils/button-helper-utils.js +314 -0
  134. package/lib/Utils/button-sender.js +824 -0
  135. package/lib/Utils/chat-control.d.ts +159 -0
  136. package/lib/Utils/chat-control.js +232 -0
  137. package/lib/Utils/chat-history-helpers.d.ts +21 -0
  138. package/lib/Utils/chat-history-helpers.js +71 -0
  139. package/lib/Utils/chat-utils.d.ts +407 -0
  140. package/lib/Utils/chat-utils.js +886 -0
  141. package/lib/Utils/companion-reg-client-utils.d.ts +4 -0
  142. package/lib/Utils/companion-reg-client-utils.js +40 -0
  143. package/lib/Utils/crypto.d.ts +41 -0
  144. package/lib/Utils/crypto.js +148 -0
  145. package/lib/Utils/decode-wa-message.d.ts +83 -0
  146. package/lib/Utils/decode-wa-message.js +317 -0
  147. package/lib/Utils/event-buffer.d.ts +12 -0
  148. package/lib/Utils/event-buffer.js +636 -0
  149. package/lib/Utils/generics.d.ts +69 -0
  150. package/lib/Utils/generics.js +439 -0
  151. package/lib/Utils/history.d.ts +90 -0
  152. package/lib/Utils/history.js +147 -0
  153. package/lib/Utils/identity-change-handler.d.ts +13 -0
  154. package/lib/Utils/identity-change-handler.js +50 -0
  155. package/lib/Utils/index.d.ts +39 -0
  156. package/lib/Utils/index.js +46 -0
  157. package/lib/Utils/link-preview.d.ts +12 -0
  158. package/lib/Utils/link-preview.js +85 -0
  159. package/lib/Utils/logger.d.ts +2 -0
  160. package/lib/Utils/logger.js +3 -0
  161. package/lib/Utils/lt-hash.d.ts +12 -0
  162. package/lib/Utils/lt-hash.js +27 -0
  163. package/lib/Utils/make-mutex.d.ts +6 -0
  164. package/lib/Utils/make-mutex.js +33 -0
  165. package/lib/Utils/media-messages.d.ts +18 -0
  166. package/lib/Utils/media-messages.js +71 -0
  167. package/lib/Utils/media-set.d.ts +13 -0
  168. package/lib/Utils/media-set.js +172 -0
  169. package/lib/Utils/message-kind.js +139 -0
  170. package/lib/Utils/message-retry-manager.d.ts +79 -0
  171. package/lib/Utils/message-retry-manager.js +265 -0
  172. package/lib/Utils/message-search.d.ts +44 -0
  173. package/lib/Utils/message-search.js +189 -0
  174. package/lib/Utils/messages-media.d.ts +135 -0
  175. package/lib/Utils/messages-media.js +869 -0
  176. package/lib/Utils/messages.d.ts +44 -0
  177. package/lib/Utils/messages.js +2155 -0
  178. package/lib/Utils/noise-handler.d.ts +20 -0
  179. package/lib/Utils/noise-handler.js +201 -0
  180. package/lib/Utils/offline-node-processor.d.ts +9 -0
  181. package/lib/Utils/offline-node-processor.js +40 -0
  182. package/lib/Utils/past-participants.d.ts +14 -0
  183. package/lib/Utils/past-participants.js +40 -0
  184. package/lib/Utils/pre-key-manager.d.ts +25 -0
  185. package/lib/Utils/pre-key-manager.js +106 -0
  186. package/lib/Utils/process-message.d.ts +82 -0
  187. package/lib/Utils/process-message.js +809 -0
  188. package/lib/Utils/reporting-utils.d.ts +12 -0
  189. package/lib/Utils/reporting-utils.js +258 -0
  190. package/lib/Utils/rich-message-utils.d.ts +48 -0
  191. package/lib/Utils/rich-message-utils.js +445 -0
  192. package/lib/Utils/scheduling.d.ts +42 -0
  193. package/lib/Utils/scheduling.js +140 -0
  194. package/lib/Utils/signal.d.ts +82 -0
  195. package/lib/Utils/signal.js +201 -0
  196. package/lib/Utils/stanza-ack.d.ts +16 -0
  197. package/lib/Utils/stanza-ack.js +38 -0
  198. package/lib/Utils/status.d.ts +50 -0
  199. package/lib/Utils/status.js +108 -0
  200. package/lib/Utils/stickerpack.d.ts +51 -0
  201. package/lib/Utils/stickerpack.js +275 -0
  202. package/lib/Utils/sync-action-utils.d.ts +2 -0
  203. package/lib/Utils/sync-action-utils.js +49 -0
  204. package/lib/Utils/tc-token-utils.d.ts +29 -0
  205. package/lib/Utils/tc-token-utils.js +163 -0
  206. package/lib/Utils/templates.d.ts +76 -0
  207. package/lib/Utils/templates.js +151 -0
  208. package/lib/Utils/use-cache-manager-auth-state.d.ts +13 -0
  209. package/lib/Utils/use-cache-manager-auth-state.js +82 -0
  210. package/lib/Utils/use-multi-file-auth-state.d.ts +10 -0
  211. package/lib/Utils/use-multi-file-auth-state.js +167 -0
  212. package/lib/Utils/use-single-file-auth-state.d.ts +10 -0
  213. package/lib/Utils/use-single-file-auth-state.js +114 -0
  214. package/lib/Utils/use-sqlite-auth-state.d.ts +11 -0
  215. package/lib/Utils/use-sqlite-auth-state.js +170 -0
  216. package/lib/Utils/validate-connection.d.ts +42 -0
  217. package/lib/Utils/validate-connection.js +199 -0
  218. package/lib/Utils/vcard.d.ts +58 -0
  219. package/lib/Utils/vcard.js +105 -0
  220. package/lib/VoIP/audio-feeder.d.ts +15 -0
  221. package/lib/VoIP/audio-feeder.js +132 -0
  222. package/lib/VoIP/index.js +277 -0
  223. package/lib/VoIP/relay-transport.d.ts +43 -0
  224. package/lib/VoIP/relay-transport.js +559 -0
  225. package/lib/VoIP/signaling.js +624 -0
  226. package/lib/VoIP/types.d.ts +69 -0
  227. package/lib/VoIP/types.js +17 -0
  228. package/lib/VoIP/wasm-engine.d.ts +103 -0
  229. package/lib/VoIP/wasm-engine.js +1214 -0
  230. package/lib/VoIP/worker-bootstrap.js +1042 -0
  231. package/lib/WABinary/constants.d.ts +61 -0
  232. package/lib/WABinary/constants.js +1467 -0
  233. package/lib/WABinary/decode.d.ts +9 -0
  234. package/lib/WABinary/decode.js +262 -0
  235. package/lib/WABinary/encode.d.ts +2 -0
  236. package/lib/WABinary/encode.js +220 -0
  237. package/lib/WABinary/generic-utils.d.ts +74 -0
  238. package/lib/WABinary/generic-utils.js +277 -0
  239. package/lib/WABinary/index.d.ts +5 -0
  240. package/lib/WABinary/index.js +6 -0
  241. package/lib/WABinary/jid-utils.d.ts +28 -0
  242. package/lib/WABinary/jid-utils.js +96 -0
  243. package/lib/WABinary/types.d.ts +19 -0
  244. package/lib/WABinary/types.js +2 -0
  245. package/lib/WAM/BinaryInfo.d.ts +7 -0
  246. package/lib/WAM/BinaryInfo.js +10 -0
  247. package/lib/WAM/constants.d.ts +34926 -0
  248. package/lib/WAM/constants.js +22853 -0
  249. package/lib/WAM/encode.d.ts +1 -0
  250. package/lib/WAM/encode.js +150 -0
  251. package/lib/WAM/index.d.ts +3 -0
  252. package/lib/WAM/index.js +4 -0
  253. package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +36 -0
  254. package/lib/WAUSync/Protocols/USyncContactProtocol.js +52 -0
  255. package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +29 -0
  256. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +63 -0
  257. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +12 -0
  258. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +27 -0
  259. package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +12 -0
  260. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +38 -0
  261. package/lib/WAUSync/Protocols/USyncUsernameProtocol.d.ts +9 -0
  262. package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +25 -0
  263. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +38 -0
  264. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +51 -0
  265. package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +14 -0
  266. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +29 -0
  267. package/lib/WAUSync/Protocols/index.d.ts +7 -0
  268. package/lib/WAUSync/Protocols/index.js +8 -0
  269. package/lib/WAUSync/USyncQuery.d.ts +20 -0
  270. package/lib/WAUSync/USyncQuery.js +122 -0
  271. package/lib/WAUSync/USyncUser.d.ts +16 -0
  272. package/lib/WAUSync/USyncUser.js +31 -0
  273. package/lib/WAUSync/index.d.ts +3 -0
  274. package/lib/WAUSync/index.js +4 -0
  275. package/lib/assets/wasm/loader.js +5 -0
  276. package/lib/assets/wasm/whatsapp.wasm +0 -0
  277. package/lib/assets/wasm/worker-modules.js +273 -0
  278. package/lib/index.d.ts +12 -0
  279. package/lib/index.js +20 -0
  280. package/package.json +175 -0
  281. package/postinstall-banner.js +56 -0
@@ -0,0 +1,47 @@
1
+ import type { WAMessage, AnyMessageContent } from '../Types/index.js';
2
+ export interface AutoReplyRule {
3
+ id?: string;
4
+ keywords?: string[];
5
+ pattern?: RegExp;
6
+ exactMatch?: string;
7
+ response: AnyMessageContent | ((message: WAMessage, match: RegExpMatchArray | string[]) => Promise<AnyMessageContent> | AnyMessageContent);
8
+ cooldown?: number;
9
+ priority?: number;
10
+ active?: boolean;
11
+ groupsOnly?: boolean;
12
+ privateOnly?: boolean;
13
+ allowedJids?: string[];
14
+ blockedJids?: string[];
15
+ quoted?: boolean;
16
+ }
17
+ export interface AutoReplyOptions {
18
+ globalCooldown?: number;
19
+ simulateTyping?: boolean;
20
+ typingDuration?: number;
21
+ multiMatch?: boolean;
22
+ onReply?: (rule: AutoReplyRule, message: WAMessage, response: AnyMessageContent) => void;
23
+ onError?: (error: Error, rule: AutoReplyRule, message: WAMessage) => void;
24
+ }
25
+ export declare class AutoReplyHandler {
26
+ private rules;
27
+ private cooldowns;
28
+ private globalCooldown;
29
+ private sendMessage;
30
+ private sendPresence?;
31
+ private options;
32
+ constructor(sendMessage: (jid: string, content: AnyMessageContent, options?: any) => Promise<any>, sendPresence?: (jid: string, presence: string) => Promise<void>, options?: AutoReplyOptions);
33
+ addRule(rule: AutoReplyRule): AutoReplyRule & {
34
+ id: string;
35
+ };
36
+ removeRule(id: string): boolean;
37
+ getRules(): (AutoReplyRule & {
38
+ id: string;
39
+ })[];
40
+ getRule(id: string): (AutoReplyRule & {
41
+ id: string;
42
+ }) | undefined;
43
+ setRuleActive(id: string, active: boolean): boolean;
44
+ clearRules(): void;
45
+ processMessage(message: WAMessage): Promise<boolean>;
46
+ }
47
+ export declare const createAutoReply: (sendMessage: (jid: string, content: AnyMessageContent, options?: any) => Promise<any>, sendPresence?: (jid: string, presence: string) => Promise<void>, options?: AutoReplyOptions) => AutoReplyHandler;
@@ -0,0 +1,158 @@
1
+ // Vanz@Add --- ported from Bail-master addons/auto-reply.ts (type-only
2
+ // annotations dropped; behavior unchanged).
3
+ export class AutoReplyHandler {
4
+ rules = new Map();
5
+ cooldowns = new Map();
6
+ globalCooldown = new Map();
7
+ sendMessage;
8
+ sendPresence;
9
+ options;
10
+ constructor(sendMessage, sendPresence, options = {}) {
11
+ this.sendMessage = sendMessage;
12
+ this.sendPresence = sendPresence;
13
+ this.options = {
14
+ globalCooldown: options.globalCooldown ?? 1000,
15
+ simulateTyping: options.simulateTyping ?? false,
16
+ typingDuration: options.typingDuration ?? 1000,
17
+ multiMatch: options.multiMatch ?? false,
18
+ onReply: options.onReply ?? (() => { }),
19
+ onError: options.onError ?? (() => { })
20
+ };
21
+ }
22
+ generateId() {
23
+ return `ar_${Date.now()}_${Math.random().toString(36).substring(2, 9)}`;
24
+ }
25
+ /** Add a rule keyed by keywords, a regex pattern, or an exact-match string. */
26
+ addRule(rule) {
27
+ const fullRule = {
28
+ ...rule,
29
+ id: rule.id ?? this.generateId(),
30
+ active: rule.active ?? true,
31
+ priority: rule.priority ?? 0
32
+ };
33
+ if (!fullRule.keywords && !fullRule.pattern && !fullRule.exactMatch)
34
+ throw new Error('Rule must have keywords, pattern, or exactMatch');
35
+ this.rules.set(fullRule.id, fullRule);
36
+ return fullRule;
37
+ }
38
+ removeRule(id) {
39
+ return this.rules.delete(id);
40
+ }
41
+ getRules() {
42
+ return Array.from(this.rules.values());
43
+ }
44
+ getRule(id) {
45
+ return this.rules.get(id);
46
+ }
47
+ setRuleActive(id, active) {
48
+ const r = this.rules.get(id);
49
+ if (r) {
50
+ r.active = active;
51
+ return true;
52
+ }
53
+ return false;
54
+ }
55
+ clearRules() {
56
+ this.rules.clear();
57
+ }
58
+ checkCooldown(ruleId, jid) {
59
+ return Date.now() - (this.cooldowns.get(`${ruleId}:${jid}`) ?? 0) > 0;
60
+ }
61
+ checkGlobalCooldown(jid) {
62
+ return Date.now() - (this.globalCooldown.get(jid) ?? 0) > this.options.globalCooldown;
63
+ }
64
+ setCooldown(ruleId, jid, cooldown) {
65
+ this.cooldowns.set(`${ruleId}:${jid}`, Date.now() + cooldown);
66
+ }
67
+ matchRule(text, rule) {
68
+ if (!rule.active)
69
+ return null;
70
+ if (text.toLowerCase() === rule.exactMatch?.toLowerCase())
71
+ return [text];
72
+ if (rule.keywords?.length) {
73
+ const lower = text.toLowerCase();
74
+ for (const kw of rule.keywords)
75
+ if (lower.includes(kw.toLowerCase()))
76
+ return [kw];
77
+ }
78
+ if (rule.pattern)
79
+ return text.match(rule.pattern);
80
+ return null;
81
+ }
82
+ isJidAllowed(jid, rule) {
83
+ const isGroup = jid.endsWith('@g.us');
84
+ if (jid.endsWith('@newsletter'))
85
+ return false;
86
+ if (rule.groupsOnly && !isGroup)
87
+ return false;
88
+ if (rule.privateOnly && isGroup)
89
+ return false;
90
+ if (rule.allowedJids?.length && !rule.allowedJids.includes(jid))
91
+ return false;
92
+ if (rule.blockedJids?.includes(jid))
93
+ return false;
94
+ return true;
95
+ }
96
+ /**
97
+ * Feed each incoming message through here (e.g. from `messages.upsert`).
98
+ * Returns true if a rule matched and a reply was sent.
99
+ */
100
+ async processMessage(message) {
101
+ const content = message.message;
102
+ if (!content)
103
+ return false;
104
+ const text = content.conversation ||
105
+ content.extendedTextMessage?.text ||
106
+ content.imageMessage?.caption ||
107
+ content.videoMessage?.caption ||
108
+ content.documentMessage?.caption ||
109
+ '';
110
+ if (!text)
111
+ return false;
112
+ const jid = message.key.remoteJid;
113
+ if (!jid || !this.checkGlobalCooldown(jid))
114
+ return false;
115
+ const sortedRules = Array.from(this.rules.values())
116
+ .filter((r) => r.active)
117
+ .sort((a, b) => (b.priority ?? 0) - (a.priority ?? 0));
118
+ let matched = false;
119
+ for (const rule of sortedRules) {
120
+ if (!this.isJidAllowed(jid, rule))
121
+ continue;
122
+ if (rule.cooldown && !this.checkCooldown(rule.id, jid))
123
+ continue;
124
+ const match = this.matchRule(text, rule);
125
+ if (!match)
126
+ continue;
127
+ try {
128
+ let response;
129
+ if (typeof rule.response === 'function')
130
+ response = await rule.response(message, match);
131
+ else
132
+ response = rule.response;
133
+ if (this.options.simulateTyping && this.sendPresence) {
134
+ await this.sendPresence(jid, 'composing');
135
+ await new Promise((r) => setTimeout(r, this.options.typingDuration));
136
+ await this.sendPresence(jid, 'paused');
137
+ }
138
+ await this.sendMessage(jid, response, rule.quoted ? { quoted: message } : undefined);
139
+ // Vanz@Fix (bug 60): global cooldown used to only get stamped inside
140
+ // setCooldown(), which only ran when the matched rule had its own
141
+ // cooldown — so rules without one never fed the global anti-spam gate.
142
+ this.globalCooldown.set(jid, Date.now());
143
+ if (rule.cooldown)
144
+ this.setCooldown(rule.id, jid, rule.cooldown);
145
+ this.options.onReply(rule, message, response);
146
+ matched = true;
147
+ if (!this.options.multiMatch)
148
+ break;
149
+ }
150
+ catch (error) {
151
+ this.options.onError(error, rule, message);
152
+ }
153
+ }
154
+ return matched;
155
+ }
156
+ }
157
+ export const createAutoReply = (sendMessage, sendPresence, options) => new AutoReplyHandler(sendMessage, sendPresence, options);
158
+ //# sourceMappingURL=auto-reply.js.map
@@ -0,0 +1,8 @@
1
+ import type { BaileysEventEmitter } from '../Types/index.js';
2
+ /** Monkey-patches ev.emit to append every event as an NDJSON line to `filename`. */
3
+ export declare const captureEventStream: (ev: BaileysEventEmitter, filename: string) => void;
4
+ /** Reads an NDJSON file written by captureEventStream and replays each event on a new EventEmitter. */
5
+ export declare const readAndEmitEventStream: (filename: string, delayIntervalMs?: number) => {
6
+ ev: BaileysEventEmitter;
7
+ task: Promise<void>;
8
+ };
@@ -0,0 +1,62 @@
1
+ // Ported from @queenanya/baileys `addons/baileys-event-stream.ts`.
2
+ // Adjustments for @vanzxy/baileys: import paths rewired to this fork's own
3
+ // `generics.js` (delay) and `make-mutex.js` (makeMutex) — both already
4
+ // present, no logic changes otherwise.
5
+ import EventEmitter from 'events';
6
+ import { createReadStream } from 'fs';
7
+ import { writeFile } from 'fs/promises';
8
+ import { createInterface } from 'readline';
9
+ import { delay } from './generics.js';
10
+ import { makeMutex } from './make-mutex.js';
11
+
12
+ /**
13
+ * Monkey-patches `ev.emit` to append every Baileys event as a JSON line
14
+ * (NDJSON) to `filename`. Useful for debugging or replaying sessions.
15
+ *
16
+ * @example
17
+ * captureEventStream(sock.ev, './events.ndjson')
18
+ */
19
+ export const captureEventStream = (ev, filename) => {
20
+ const originalEmit = ev.emit.bind(ev);
21
+ const writeMutex = makeMutex();
22
+ const patchedEmit = (event, ...rest) => {
23
+ const line = JSON.stringify({ timestamp: Date.now(), event, data: rest[0] }) + '\n';
24
+ const result = originalEmit(event, ...rest);
25
+ void writeMutex.mutex(async () => {
26
+ await writeFile(filename, line, { flag: 'a' });
27
+ });
28
+ return result;
29
+ };
30
+ ev.emit = patchedEmit;
31
+ };
32
+
33
+ /**
34
+ * Reads an NDJSON file written by {@link captureEventStream} and replays
35
+ * each event on a new EventEmitter.
36
+ *
37
+ * @param filename Path to the NDJSON file.
38
+ * @param delayIntervalMs Milliseconds to wait between events (default 0).
39
+ * @returns `{ ev, task }` — ev is the emitter, task resolves when done.
40
+ */
41
+ export const readAndEmitEventStream = (filename, delayIntervalMs = 0) => {
42
+ const ev = new EventEmitter();
43
+ const fireEvents = async () => {
44
+ const fileStream = createReadStream(filename);
45
+ const rl = createInterface({ input: fileStream, crlfDelay: Infinity });
46
+ for await (const line of rl) {
47
+ if (!line.trim())
48
+ continue;
49
+ try {
50
+ const { event, data } = JSON.parse(line);
51
+ ev.emit(event, data);
52
+ if (delayIntervalMs)
53
+ await delay(delayIntervalMs);
54
+ }
55
+ catch {
56
+ // skip malformed lines
57
+ }
58
+ }
59
+ fileStream.destroy();
60
+ };
61
+ return { ev, task: fireEvents() };
62
+ };
@@ -0,0 +1,8 @@
1
+ export namespace Browsers {
2
+ function ubuntu(browser: any): any[];
3
+ function macOS(browser: any): any[];
4
+ function baileys(browser: any): any[];
5
+ function windows(browser: any): any[];
6
+ function appropriate(browser: any): any[];
7
+ }
8
+ export function getPlatformId(browser: any): any;
@@ -0,0 +1,28 @@
1
+ import { platform, release } from 'os';
2
+ import { proto } from '../../WAProto/index.js';
3
+ const PLATFORM_MAP = {
4
+ aix: 'AIX',
5
+ darwin: 'Mac OS',
6
+ win32: 'Windows',
7
+ android: 'Android',
8
+ freebsd: 'FreeBSD',
9
+ openbsd: 'OpenBSD',
10
+ sunos: 'Solaris',
11
+ linux: undefined,
12
+ haiku: undefined,
13
+ cygwin: undefined,
14
+ netbsd: undefined
15
+ };
16
+ export const Browsers = {
17
+ ubuntu: browser => ['Ubuntu', browser, '22.04.4'],
18
+ macOS: browser => ['Mac OS', browser, '14.4.1'],
19
+ baileys: browser => ['Baileys', browser, '6.5.0'],
20
+ windows: browser => ['Windows', browser, '10.0.22631'],
21
+ /** The appropriate browser based on your OS & release */
22
+ appropriate: browser => [PLATFORM_MAP[platform()] || 'Ubuntu', browser, release()]
23
+ };
24
+ export const getPlatformId = (browser) => {
25
+ const platformType = proto.DeviceProps.PlatformType[browser.toUpperCase()];
26
+ return platformType ? platformType.toString() : '1'; //chrome
27
+ };
28
+ //# sourceMappingURL=browser-utils.js.map
@@ -0,0 +1,49 @@
1
+ /**
2
+ * Uploads images not already uploaded to WA's servers
3
+ */
4
+ export function uploadingNecessaryImagesOfProduct(product: any, waUploadToServer: any, timeoutMs?: number): Promise<any>;
5
+ export function parseCatalogNode(node: any): {
6
+ products: any;
7
+ nextPageCursor: any;
8
+ };
9
+ export function parseCollectionsNode(node: any): {
10
+ collections: any;
11
+ };
12
+ export function parseOrderDetailsNode(node: any): {
13
+ price: {
14
+ total: number;
15
+ currency: any;
16
+ };
17
+ products: any;
18
+ };
19
+ export function toProductNode(productId: any, product: any): {
20
+ tag: string;
21
+ attrs: {
22
+ compliance_category: string;
23
+ is_hidden: any;
24
+ };
25
+ content: {
26
+ tag: string;
27
+ attrs: {};
28
+ content: any;
29
+ }[];
30
+ };
31
+ export function parseProductNode(productNode: any): {
32
+ id: any;
33
+ imageUrls: {
34
+ requested: any;
35
+ original: any;
36
+ };
37
+ reviewStatus: {
38
+ whatsapp: any;
39
+ };
40
+ availability: string;
41
+ name: any;
42
+ retailerId: any;
43
+ url: any;
44
+ description: any;
45
+ price: number;
46
+ currency: any;
47
+ isHidden: boolean;
48
+ };
49
+ export function uploadingNecessaryImages(images: any, waUploadToServer: any, timeoutMs?: number): Promise<any[]>;
@@ -0,0 +1,240 @@
1
+ import { Boom } from '@hapi/boom';
2
+ import { createHash } from 'crypto';
3
+ import { createWriteStream, promises as fs } from 'fs';
4
+ import { tmpdir } from 'os';
5
+ import { join } from 'path';
6
+ import { getBinaryNodeChild, getBinaryNodeChildren, getBinaryNodeChildString } from '../WABinary/index.js';
7
+ import { generateMessageIDV2 } from './generics.js';
8
+ import logger from './logger.js';
9
+ import { getStream, getUrlFromDirectPath } from './messages-media.js';
10
+ export const parseCatalogNode = (node) => {
11
+ const catalogNode = getBinaryNodeChild(node, 'product_catalog');
12
+ const products = getBinaryNodeChildren(catalogNode, 'product').map(parseProductNode);
13
+ const paging = getBinaryNodeChild(catalogNode, 'paging');
14
+ return {
15
+ products,
16
+ nextPageCursor: paging ? getBinaryNodeChildString(paging, 'after') : undefined
17
+ };
18
+ };
19
+ export const parseCollectionsNode = (node) => {
20
+ const collectionsNode = getBinaryNodeChild(node, 'collections');
21
+ const collections = getBinaryNodeChildren(collectionsNode, 'collection').map(collectionNode => {
22
+ const id = getBinaryNodeChildString(collectionNode, 'id');
23
+ const name = getBinaryNodeChildString(collectionNode, 'name');
24
+ const products = getBinaryNodeChildren(collectionNode, 'product').map(parseProductNode);
25
+ return {
26
+ id,
27
+ name,
28
+ products,
29
+ status: parseStatusInfo(collectionNode)
30
+ };
31
+ });
32
+ return {
33
+ collections
34
+ };
35
+ };
36
+ export const parseOrderDetailsNode = (node) => {
37
+ const orderNode = getBinaryNodeChild(node, 'order');
38
+ const products = getBinaryNodeChildren(orderNode, 'product').map(productNode => {
39
+ const imageNode = getBinaryNodeChild(productNode, 'image');
40
+ return {
41
+ id: getBinaryNodeChildString(productNode, 'id'),
42
+ name: getBinaryNodeChildString(productNode, 'name'),
43
+ imageUrl: getBinaryNodeChildString(imageNode, 'url'),
44
+ price: +getBinaryNodeChildString(productNode, 'price'),
45
+ currency: getBinaryNodeChildString(productNode, 'currency'),
46
+ quantity: +getBinaryNodeChildString(productNode, 'quantity')
47
+ };
48
+ });
49
+ const priceNode = getBinaryNodeChild(orderNode, 'price');
50
+ const orderDetails = {
51
+ price: {
52
+ total: +getBinaryNodeChildString(priceNode, 'total'),
53
+ currency: getBinaryNodeChildString(priceNode, 'currency')
54
+ },
55
+ products
56
+ };
57
+ return orderDetails;
58
+ };
59
+ export const toProductNode = (productId, product) => {
60
+ const attrs = {};
61
+ const content = [];
62
+ if (typeof productId !== 'undefined') {
63
+ content.push({
64
+ tag: 'id',
65
+ attrs: {},
66
+ content: Buffer.from(productId)
67
+ });
68
+ }
69
+ if (typeof product.name !== 'undefined') {
70
+ content.push({
71
+ tag: 'name',
72
+ attrs: {},
73
+ content: Buffer.from(product.name)
74
+ });
75
+ }
76
+ if (typeof product.description !== 'undefined') {
77
+ content.push({
78
+ tag: 'description',
79
+ attrs: {},
80
+ content: Buffer.from(product.description)
81
+ });
82
+ }
83
+ if (typeof product.retailerId !== 'undefined') {
84
+ content.push({
85
+ tag: 'retailer_id',
86
+ attrs: {},
87
+ content: Buffer.from(product.retailerId)
88
+ });
89
+ }
90
+ if (product.images.length) {
91
+ content.push({
92
+ tag: 'media',
93
+ attrs: {},
94
+ content: product.images.map(img => {
95
+ if (!('url' in img)) {
96
+ throw new Boom('Expected img for product to already be uploaded', { statusCode: 400 });
97
+ }
98
+ return {
99
+ tag: 'image',
100
+ attrs: {},
101
+ content: [
102
+ {
103
+ tag: 'url',
104
+ attrs: {},
105
+ content: Buffer.from(img.url.toString())
106
+ }
107
+ ]
108
+ };
109
+ })
110
+ });
111
+ }
112
+ if (typeof product.price !== 'undefined') {
113
+ content.push({
114
+ tag: 'price',
115
+ attrs: {},
116
+ content: Buffer.from(product.price.toString())
117
+ });
118
+ }
119
+ if (typeof product.currency !== 'undefined') {
120
+ content.push({
121
+ tag: 'currency',
122
+ attrs: {},
123
+ content: Buffer.from(product.currency)
124
+ });
125
+ }
126
+ if ('originCountryCode' in product) {
127
+ if (typeof product.originCountryCode === 'undefined') {
128
+ attrs['compliance_category'] = 'COUNTRY_ORIGIN_EXEMPT';
129
+ }
130
+ else {
131
+ content.push({
132
+ tag: 'compliance_info',
133
+ attrs: {},
134
+ content: [
135
+ {
136
+ tag: 'country_code_origin',
137
+ attrs: {},
138
+ content: Buffer.from(product.originCountryCode)
139
+ }
140
+ ]
141
+ });
142
+ }
143
+ }
144
+ if (typeof product.isHidden !== 'undefined') {
145
+ attrs['is_hidden'] = product.isHidden.toString();
146
+ }
147
+ const node = {
148
+ tag: 'product',
149
+ attrs,
150
+ content
151
+ };
152
+ return node;
153
+ };
154
+ export const parseProductNode = (productNode) => {
155
+ const isHidden = productNode.attrs.is_hidden === 'true';
156
+ const id = getBinaryNodeChildString(productNode, 'id');
157
+ const mediaNode = getBinaryNodeChild(productNode, 'media');
158
+ const statusInfoNode = getBinaryNodeChild(productNode, 'status_info');
159
+ const product = {
160
+ id,
161
+ imageUrls: parseImageUrls(mediaNode),
162
+ reviewStatus: {
163
+ whatsapp: getBinaryNodeChildString(statusInfoNode, 'status')
164
+ },
165
+ availability: 'in stock',
166
+ name: getBinaryNodeChildString(productNode, 'name'),
167
+ retailerId: getBinaryNodeChildString(productNode, 'retailer_id'),
168
+ url: getBinaryNodeChildString(productNode, 'url'),
169
+ description: getBinaryNodeChildString(productNode, 'description'),
170
+ price: +getBinaryNodeChildString(productNode, 'price'),
171
+ currency: getBinaryNodeChildString(productNode, 'currency'),
172
+ isHidden
173
+ };
174
+ return product;
175
+ };
176
+ /**
177
+ * Uploads images not already uploaded to WA's servers
178
+ */
179
+ export async function uploadingNecessaryImagesOfProduct(product, waUploadToServer, timeoutMs = 30000) {
180
+ product = {
181
+ ...product,
182
+ images: product.images
183
+ ? await uploadingNecessaryImages(product.images, waUploadToServer, timeoutMs)
184
+ : product.images
185
+ };
186
+ return product;
187
+ }
188
+ /**
189
+ * Uploads images not already uploaded to WA's servers
190
+ */
191
+ export const uploadingNecessaryImages = async (images, waUploadToServer, timeoutMs = 30000) => {
192
+ const results = await Promise.all(images.map(async (img) => {
193
+ if ('url' in img) {
194
+ const url = img.url.toString();
195
+ if (url.includes('.whatsapp.net')) {
196
+ return { url };
197
+ }
198
+ }
199
+ const { stream } = await getStream(img);
200
+ const hasher = createHash('sha256');
201
+ const filePath = join(tmpdir(), 'img' + generateMessageIDV2());
202
+ const encFileWriteStream = createWriteStream(filePath);
203
+ // Vanz@Fix (bug 61): no 'error' listener meant a write failure (e.g. disk
204
+ // full) would surface as an unhandled 'error' event instead of rejecting
205
+ // this promise; and the stream was never closed before unlink() below.
206
+ const writeStreamError = new Promise((_, reject) => encFileWriteStream.on('error', reject));
207
+ for await (const block of stream) {
208
+ hasher.update(block);
209
+ encFileWriteStream.write(block);
210
+ }
211
+ await Promise.race([
212
+ new Promise((resolve) => encFileWriteStream.end(resolve)),
213
+ writeStreamError
214
+ ]);
215
+ const sha = hasher.digest('base64');
216
+ const { directPath } = await waUploadToServer(filePath, {
217
+ mediaType: 'product-catalog-image',
218
+ fileEncSha256B64: sha,
219
+ timeoutMs
220
+ });
221
+ await fs.unlink(filePath).catch(err => logger.warn({ err, filePath }, 'Vanz@Fix (bug 57): error deleting temp file')); // was console.log
222
+ return { url: getUrlFromDirectPath(directPath) };
223
+ }));
224
+ return results;
225
+ };
226
+ const parseImageUrls = (mediaNode) => {
227
+ const imgNode = getBinaryNodeChild(mediaNode, 'image');
228
+ return {
229
+ requested: getBinaryNodeChildString(imgNode, 'request_image_url'),
230
+ original: getBinaryNodeChildString(imgNode, 'original_image_url')
231
+ };
232
+ };
233
+ const parseStatusInfo = (mediaNode) => {
234
+ const node = getBinaryNodeChild(mediaNode, 'status_info');
235
+ return {
236
+ status: getBinaryNodeChildString(node, 'status'),
237
+ canAppeal: getBinaryNodeChildString(node, 'can_appeal') === 'true'
238
+ };
239
+ };
240
+ //# sourceMappingURL=business.js.map