@crysnovax/baileys 1.0.3

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 (112) hide show
  1. package/README.md +467 -0
  2. package/WAProto/V +1 -0
  3. package/WAProto/index.js +104236 -0
  4. package/engine-requirements.js +13 -0
  5. package/lib/Defaults/index.js +148 -0
  6. package/lib/Signal/Group/ciphertext-message.js +11 -0
  7. package/lib/Signal/Group/group-session-builder.js +29 -0
  8. package/lib/Signal/Group/group_cipher.js +81 -0
  9. package/lib/Signal/Group/index.js +11 -0
  10. package/lib/Signal/Group/keyhelper.js +17 -0
  11. package/lib/Signal/Group/sender-chain-key.js +25 -0
  12. package/lib/Signal/Group/sender-key-distribution-message.js +62 -0
  13. package/lib/Signal/Group/sender-key-message.js +65 -0
  14. package/lib/Signal/Group/sender-key-name.js +47 -0
  15. package/lib/Signal/Group/sender-key-record.js +40 -0
  16. package/lib/Signal/Group/sender-key-state.js +83 -0
  17. package/lib/Signal/Group/sender-message-key.js +25 -0
  18. package/lib/Signal/libsignal.js +406 -0
  19. package/lib/Signal/lid-mapping.js +276 -0
  20. package/lib/Socket/Client/index.js +2 -0
  21. package/lib/Socket/Client/types.js +10 -0
  22. package/lib/Socket/Client/websocket.js +53 -0
  23. package/lib/Socket/business.js +378 -0
  24. package/lib/Socket/chats.js +1059 -0
  25. package/lib/Socket/communities.js +430 -0
  26. package/lib/Socket/groups.js +328 -0
  27. package/lib/Socket/index.js +11 -0
  28. package/lib/Socket/messages-recv.js +1476 -0
  29. package/lib/Socket/messages-send.js +1268 -0
  30. package/lib/Socket/mex.js +41 -0
  31. package/lib/Socket/newsletter.js +251 -0
  32. package/lib/Socket/socket.js +949 -0
  33. package/lib/Store/index.js +3 -0
  34. package/lib/Store/make-in-memory-store.js +420 -0
  35. package/lib/Store/make-ordered-dictionary.js +78 -0
  36. package/lib/Store/object-repository.js +23 -0
  37. package/lib/Types/Auth.js +1 -0
  38. package/lib/Types/Bussines.js +1 -0
  39. package/lib/Types/Call.js +1 -0
  40. package/lib/Types/Chat.js +7 -0
  41. package/lib/Types/Contact.js +1 -0
  42. package/lib/Types/Events.js +1 -0
  43. package/lib/Types/GroupMetadata.js +1 -0
  44. package/lib/Types/Label.js +24 -0
  45. package/lib/Types/LabelAssociation.js +6 -0
  46. package/lib/Types/Message.js +17 -0
  47. package/lib/Types/Newsletter.js +33 -0
  48. package/lib/Types/Product.js +1 -0
  49. package/lib/Types/RichType.js +22 -0
  50. package/lib/Types/Signal.js +1 -0
  51. package/lib/Types/Socket.js +2 -0
  52. package/lib/Types/State.js +12 -0
  53. package/lib/Types/USync.js +1 -0
  54. package/lib/Types/index.js +25 -0
  55. package/lib/Utils/auth-utils.js +289 -0
  56. package/lib/Utils/bot-planning-replay.js +206 -0
  57. package/lib/Utils/browser-utils.js +28 -0
  58. package/lib/Utils/business.js +230 -0
  59. package/lib/Utils/chat-utils.js +811 -0
  60. package/lib/Utils/companion-reg-client-utils.js +32 -0
  61. package/lib/Utils/crypto.js +117 -0
  62. package/lib/Utils/decode-wa-message.js +282 -0
  63. package/lib/Utils/event-buffer.js +589 -0
  64. package/lib/Utils/generics.js +385 -0
  65. package/lib/Utils/history.js +130 -0
  66. package/lib/Utils/identity-change-handler.js +48 -0
  67. package/lib/Utils/index.js +26 -0
  68. package/lib/Utils/link-preview.js +84 -0
  69. package/lib/Utils/logger.js +2 -0
  70. package/lib/Utils/lt-hash.js +7 -0
  71. package/lib/Utils/make-mutex.js +32 -0
  72. package/lib/Utils/message-retry-manager.js +241 -0
  73. package/lib/Utils/messages-media.js +830 -0
  74. package/lib/Utils/messages.js +1891 -0
  75. package/lib/Utils/meta-compositing.js +208 -0
  76. package/lib/Utils/noise-handler.js +200 -0
  77. package/lib/Utils/offline-node-processor.js +39 -0
  78. package/lib/Utils/pre-key-manager.js +105 -0
  79. package/lib/Utils/process-message.js +527 -0
  80. package/lib/Utils/reporting-utils.js +257 -0
  81. package/lib/Utils/rich-message-utils.js +387 -0
  82. package/lib/Utils/signal.js +158 -0
  83. package/lib/Utils/stanza-ack.js +37 -0
  84. package/lib/Utils/sync-action-utils.js +47 -0
  85. package/lib/Utils/tc-token-utils.js +17 -0
  86. package/lib/Utils/use-multi-file-auth-state.js +120 -0
  87. package/lib/Utils/use-single-file-auth-state.js +96 -0
  88. package/lib/Utils/validate-connection.js +206 -0
  89. package/lib/Utils//360/237/224/226 +217 -0
  90. package/lib/WABinary/constants.js +1372 -0
  91. package/lib/WABinary/decode.js +261 -0
  92. package/lib/WABinary/encode.js +219 -0
  93. package/lib/WABinary/generic-utils.js +227 -0
  94. package/lib/WABinary/index.js +5 -0
  95. package/lib/WABinary/jid-utils.js +95 -0
  96. package/lib/WABinary/types.js +1 -0
  97. package/lib/WAM/BinaryInfo.js +9 -0
  98. package/lib/WAM/constants.js +22852 -0
  99. package/lib/WAM/encode.js +149 -0
  100. package/lib/WAM/index.js +3 -0
  101. package/lib/WAUSync/Protocols/USyncContactProtocol.js +28 -0
  102. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +53 -0
  103. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +26 -0
  104. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +37 -0
  105. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +50 -0
  106. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +28 -0
  107. package/lib/WAUSync/Protocols/index.js +4 -0
  108. package/lib/WAUSync/USyncQuery.js +93 -0
  109. package/lib/WAUSync/USyncUser.js +22 -0
  110. package/lib/WAUSync/index.js +3 -0
  111. package/lib/index.js +11 -0
  112. package/package.json +83 -0
@@ -0,0 +1,217 @@
1
+ /**
2
+ * Lia@Changes [WIP]
3
+ * Welcome Flow — Meta Business-style startup template.
4
+ *
5
+ * Fires automatically on first message from any new contact.
6
+ * Sends a fully configurable greeting with interactive FAQ buttons,
7
+ * exactly like Meta Business account welcome templates.
8
+ *
9
+ * Tracks seen JIDs in memory + optional persistent JSON file so the
10
+ * greeting fires ONCE per contact — never on repeat messages.
11
+ *
12
+ * Usage:
13
+ * const welcome = createWelcomeFlow(sock, config)
14
+ * welcome.listen() // start listening
15
+ * welcome.stop() // stop listening
16
+ * welcome.reset(jid) // force re-greet a contact
17
+ * welcome.resetAll() // clear all seen contacts
18
+ *
19
+ * If you use or copy this code, please credit @crysnovax/bailey.
20
+ */
21
+
22
+ import { readFileSync, writeFileSync, existsSync } from 'fs';
23
+ import { isJidGroup, isJidNewsletter, isJidBroadcast, jidNormalizedUser } from '../WABinary/index.js';
24
+ import { delay } from './generics.js';
25
+
26
+ // ─── Defaults ────────────────────────────────────────────────────────────────
27
+
28
+ const DEFAULT_CONFIG = {
29
+ greeting: '👋 Welcome! How can I help you today?',
30
+ footer: 'Powered by @crysnovax/bailey',
31
+ buttonText: '📋 Choose an option',
32
+ faqs: [
33
+ { id: 'faq_1', title: '📦 Track my order', description: 'Check order status' },
34
+ { id: 'faq_2', title: '💳 Billing & payments', description: 'Payment issues & invoices' },
35
+ { id: 'faq_3', title: '🛠️ Technical support', description: 'Get help with a problem' },
36
+ { id: 'faq_4', title: '📞 Talk to a human', description: 'Connect with support staff' },
37
+ ],
38
+ sectionTitle: 'How can we help?',
39
+ typingDelayMs: 1200, // simulate typing before greeting appears
40
+ persistPath: null, // set to a file path to persist seen JIDs across restarts
41
+ ignoreGroups: true, // don't fire in group chats
42
+ ignoreNewsletter: true, // don't fire for newsletter messages
43
+ ignoreBroadcast: true, // don't fire for broadcast messages
44
+ onGreet: null, // async (jid, message) => {} — called after greeting sent
45
+ onFaqReply: null, // async (jid, faqId, message) => {} — called when user picks a FAQ
46
+ };
47
+
48
+ // ─── Persistent seen-JID store ───────────────────────────────────────────────
49
+
50
+ const createSeenStore = (persistPath) => {
51
+ const seen = new Set();
52
+
53
+ // Load from file if path given and file exists
54
+ if (persistPath && existsSync(persistPath)) {
55
+ try {
56
+ const data = JSON.parse(readFileSync(persistPath, 'utf8'));
57
+ if (Array.isArray(data)) data.forEach(jid => seen.add(jid));
58
+ } catch (_) {}
59
+ }
60
+
61
+ const save = () => {
62
+ if (!persistPath) return;
63
+ try {
64
+ writeFileSync(persistPath, JSON.stringify([...seen]), 'utf8');
65
+ } catch (_) {}
66
+ };
67
+
68
+ return {
69
+ has: (jid) => seen.has(jid),
70
+ add: (jid) => { seen.add(jid); save(); },
71
+ delete: (jid) => { seen.delete(jid); save(); },
72
+ clear: () => { seen.clear(); save(); }
73
+ };
74
+ };
75
+
76
+ // ─── Build the welcome interactive list message ───────────────────────────────
77
+
78
+ const buildWelcomeMessage = (config, quotedMsg = null) => {
79
+ const { greeting, footer, buttonText, faqs, sectionTitle } = config;
80
+
81
+ const content = {
82
+ text: greeting,
83
+ footer,
84
+ buttonText,
85
+ sections: [
86
+ {
87
+ title: sectionTitle,
88
+ rows: faqs.map(faq => ({
89
+ title: faq.title,
90
+ description: faq.description || '',
91
+ rowId: faq.id
92
+ }))
93
+ }
94
+ ]
95
+ };
96
+
97
+ if (quotedMsg) {
98
+ content.quoted = quotedMsg;
99
+ }
100
+
101
+ return content;
102
+ };
103
+
104
+ // ─── Main factory ─────────────────────────────────────────────────────────────
105
+
106
+ /**
107
+ * createWelcomeFlow — attach a Meta Business-style welcome template to a socket.
108
+ *
109
+ * @param {object} sock - Baileys socket
110
+ * @param {object} config - Partial config — merges with DEFAULT_CONFIG
111
+ * @returns {{ listen, stop, reset, resetAll }}
112
+ */
113
+ export const createWelcomeFlow = (sock, config = {}) => {
114
+ const cfg = { ...DEFAULT_CONFIG, ...config };
115
+ const seen = createSeenStore(cfg.persistPath);
116
+
117
+ // ── FAQ reply handler — called when user selects a list option ────────────
118
+ const handleFaqReply = async (msg) => {
119
+ if (typeof cfg.onFaqReply !== 'function') return;
120
+ const reply = msg.message?.listResponseMessage;
121
+ if (!reply) return;
122
+ const faqId = reply.singleSelectReply?.selectedRowId;
123
+ const jid = jidNormalizedUser(msg.key.remoteJid);
124
+ if (faqId) {
125
+ await cfg.onFaqReply(jid, faqId, msg);
126
+ }
127
+ };
128
+
129
+ // ── First-message handler ─────────────────────────────────────────────────
130
+ const handleMessage = async ({ messages, type }) => {
131
+ if (type !== 'notify') return;
132
+
133
+ for (const msg of messages) {
134
+ try {
135
+ const jid = msg.key?.remoteJid;
136
+ if (!jid || msg.key?.fromMe) continue;
137
+
138
+ // Skip ignored JID types
139
+ if (cfg.ignoreGroups && isJidGroup(jid)) continue;
140
+ if (cfg.ignoreNewsletter && isJidNewsletter(jid)) continue;
141
+ if (cfg.ignoreBroadcast && isJidBroadcast(jid)) continue;
142
+
143
+ const normalizedJid = jidNormalizedUser(jid);
144
+
145
+ // Check if it's a FAQ reply first
146
+ if (msg.message?.listResponseMessage) {
147
+ await handleFaqReply(msg);
148
+ continue;
149
+ }
150
+
151
+ // Already greeted this contact — skip
152
+ if (seen.has(normalizedJid)) continue;
153
+
154
+ // Mark as seen immediately to prevent race conditions
155
+ seen.add(normalizedJid);
156
+
157
+ // Simulate typing before the greeting
158
+ if (cfg.typingDelayMs > 0) {
159
+ await sock.sendPresenceUpdate('composing', jid);
160
+ await delay(cfg.typingDelayMs);
161
+ await sock.sendPresenceUpdate('paused', jid);
162
+ }
163
+
164
+ // Send the welcome message
165
+ const welcomeContent = buildWelcomeMessage(cfg, msg);
166
+ await sock.sendMessage(jid, welcomeContent);
167
+
168
+ // Fire onGreet callback if provided
169
+ if (typeof cfg.onGreet === 'function') {
170
+ await cfg.onGreet(normalizedJid, msg).catch(() => {});
171
+ }
172
+ } catch (_) {}
173
+ }
174
+ };
175
+
176
+ // ── Public API ────────────────────────────────────────────────────────────
177
+ return {
178
+ /**
179
+ * Start listening for new contacts.
180
+ */
181
+ listen() {
182
+ sock.ev.on('messages.upsert', handleMessage);
183
+ },
184
+
185
+ /**
186
+ * Stop listening.
187
+ */
188
+ stop() {
189
+ sock.ev.off('messages.upsert', handleMessage);
190
+ },
191
+
192
+ /**
193
+ * Force re-greet a specific contact next time they message.
194
+ * @param {string} jid
195
+ */
196
+ reset(jid) {
197
+ seen.delete(jidNormalizedUser(jid));
198
+ },
199
+
200
+ /**
201
+ * Clear all seen contacts — everyone gets re-greeted.
202
+ */
203
+ resetAll() {
204
+ seen.clear();
205
+ },
206
+
207
+ /**
208
+ * Check if a contact has already been greeted.
209
+ * @param {string} jid
210
+ * @returns {boolean}
211
+ */
212
+ hasGreeted(jid) {
213
+ return seen.has(jidNormalizedUser(jid));
214
+ }
215
+ };
216
+ };
217
+