@dafav/dafa-baileys 0.0.0-stage → 1.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/README.md +82 -2
- package/index.js +172 -0
- package/package.json +26 -4
package/README.md
CHANGED
|
@@ -1,3 +1,83 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @dafav/dafa-baileys
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Custom Baileys wrapper (ESM, Node.js 20+) yang siap dipakai siapa saja untuk bot WhatsApp dengan:
|
|
4
|
+
- Support `interactiveButtons` (`quick_reply`, `single_select`, `cta_url`, `cta_copy`, `cta_call`) langsung di `sock.sendMessage(...)`
|
|
5
|
+
- Helper `sock.sendButtonMenu(...)` dan `sock.sendDocCard(...)`
|
|
6
|
+
- `fetchLiveWaWebVersion()` untuk ambil versi WA Web terbaru langsung dari `web.whatsapp.com/sw.js`
|
|
7
|
+
- `sock.requestPairingCode(nomor, customCode)` dengan validasi otomatis (8 karakter)
|
|
8
|
+
- Zero `git` dependencies (aman untuk panel Pterodactyl / VPS / Termux)
|
|
9
|
+
|
|
10
|
+
## Instalasi
|
|
11
|
+
|
|
12
|
+
Jika sudah di-publish ke npm:
|
|
13
|
+
```bash
|
|
14
|
+
npm install @dafav/dafa-baileys pino
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Atau pakai langsung dari folder lokal:
|
|
18
|
+
```json
|
|
19
|
+
{
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"@dafav/dafa-baileys": "file:./dafa-baileys"
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Contoh Pemakaian
|
|
27
|
+
|
|
28
|
+
```js
|
|
29
|
+
import makeWASocket, {
|
|
30
|
+
DisconnectReason,
|
|
31
|
+
fetchLiveWaWebVersion,
|
|
32
|
+
useMultiFileAuthState,
|
|
33
|
+
} from '@dafav/dafa-baileys';
|
|
34
|
+
import pino from 'pino';
|
|
35
|
+
|
|
36
|
+
const { state, saveCreds } = await useMultiFileAuthState('./auth_info');
|
|
37
|
+
const version = await fetchLiveWaWebVersion();
|
|
38
|
+
|
|
39
|
+
const sock = makeWASocket({
|
|
40
|
+
version,
|
|
41
|
+
auth: state,
|
|
42
|
+
logger: pino({ level: 'silent' }),
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
sock.ev.on('creds.update', saveCreds);
|
|
46
|
+
|
|
47
|
+
sock.ev.on('connection.update', async ({ qr, connection }) => {
|
|
48
|
+
if (qr && !sock.authState.creds.registered) {
|
|
49
|
+
const code = await sock.requestPairingCode('628123456789', 'WABOT123');
|
|
50
|
+
console.log('Pairing Code:', code);
|
|
51
|
+
}
|
|
52
|
+
if (connection === 'open') console.log('Terhubung!');
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
sock.ev.on('messages.upsert', async ({ messages }) => {
|
|
56
|
+
const msg = messages[0];
|
|
57
|
+
if (!msg.message || msg.key.fromMe) return;
|
|
58
|
+
const text = msg.message.conversation || msg.message.extendedTextMessage?.text || '';
|
|
59
|
+
if (text === '.menu') {
|
|
60
|
+
await sock.sendButtonMenu(
|
|
61
|
+
msg.key.remoteJid,
|
|
62
|
+
{
|
|
63
|
+
text: 'Halo! Pilih menu di bawah:',
|
|
64
|
+
footer: 'dafa-baileys',
|
|
65
|
+
buttons: [
|
|
66
|
+
{ name: 'quick_reply', buttonParamsJson: JSON.stringify({ display_text: 'Info', id: 'info' }) },
|
|
67
|
+
],
|
|
68
|
+
},
|
|
69
|
+
{ quoted: msg }
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## Cara Publish ke npm (Supaya Bisa Dipakai Semua Orang)
|
|
76
|
+
|
|
77
|
+
1. Login akun npm `dafav` di [npmjs.com](https://www.npmjs.com).
|
|
78
|
+
2. Jalankan di terminal:
|
|
79
|
+
```bash
|
|
80
|
+
cd dafa-baileys
|
|
81
|
+
npm login
|
|
82
|
+
npm publish
|
|
83
|
+
```
|
package/index.js
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import elainaMakeWASocket, {
|
|
2
|
+
Browsers,
|
|
3
|
+
DisconnectReason,
|
|
4
|
+
MB,
|
|
5
|
+
fetchLatestBaileysVersion,
|
|
6
|
+
isJidGroup,
|
|
7
|
+
normalizeMessageContent,
|
|
8
|
+
proto,
|
|
9
|
+
useMultiFileAuthState,
|
|
10
|
+
} from '@rexxhayanasi/elaina-baileys';
|
|
11
|
+
|
|
12
|
+
export * from '@rexxhayanasi/elaina-baileys';
|
|
13
|
+
|
|
14
|
+
const FALLBACK_WA_VERSION = [2, 3000, 1056359382];
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Fetch the latest WhatsApp Web client revision directly from web.whatsapp.com/sw.js.
|
|
18
|
+
*/
|
|
19
|
+
export async function fetchLiveWaWebVersion() {
|
|
20
|
+
try {
|
|
21
|
+
const res = await fetch('https://web.whatsapp.com/sw.js', {
|
|
22
|
+
signal: AbortSignal.timeout(8000),
|
|
23
|
+
headers: {
|
|
24
|
+
'sec-fetch-site': 'none',
|
|
25
|
+
'user-agent':
|
|
26
|
+
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36',
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
if (!res.ok) return FALLBACK_WA_VERSION;
|
|
30
|
+
const text = await res.text();
|
|
31
|
+
const match = text.match(/\\?"client_revision\\?":\s*(\d+)/);
|
|
32
|
+
if (match?.[1]) {
|
|
33
|
+
return [2, 3000, Number(match[1])];
|
|
34
|
+
}
|
|
35
|
+
} catch {
|
|
36
|
+
// Fallback to known working WA Web version
|
|
37
|
+
}
|
|
38
|
+
return FALLBACK_WA_VERSION;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Normalize interactiveButtons authoring format into Elaina Baileys nativeFlow payload.
|
|
43
|
+
*/
|
|
44
|
+
function mapInteractiveButtons(interactiveButtons = []) {
|
|
45
|
+
return interactiveButtons.map((btn) => {
|
|
46
|
+
if (btn.name === 'quick_reply' && btn.buttonParamsJson) {
|
|
47
|
+
const parsed = JSON.parse(btn.buttonParamsJson);
|
|
48
|
+
return { text: parsed.display_text || 'Button', id: parsed.id || 'btn' };
|
|
49
|
+
}
|
|
50
|
+
if (btn.name === 'cta_url' && btn.buttonParamsJson) {
|
|
51
|
+
const parsed = JSON.parse(btn.buttonParamsJson);
|
|
52
|
+
return { text: parsed.display_text || 'Open URL', url: parsed.url };
|
|
53
|
+
}
|
|
54
|
+
if (btn.name === 'cta_copy' && btn.buttonParamsJson) {
|
|
55
|
+
const parsed = JSON.parse(btn.buttonParamsJson);
|
|
56
|
+
return { text: parsed.display_text || 'Copy', copy: parsed.copy_code };
|
|
57
|
+
}
|
|
58
|
+
if (btn.name === 'cta_call' && btn.buttonParamsJson) {
|
|
59
|
+
const parsed = JSON.parse(btn.buttonParamsJson);
|
|
60
|
+
return { text: parsed.display_text || 'Call', call: parsed.phone_number };
|
|
61
|
+
}
|
|
62
|
+
if (btn.name === 'single_select' && btn.buttonParamsJson) {
|
|
63
|
+
const parsed = JSON.parse(btn.buttonParamsJson);
|
|
64
|
+
return { text: parsed.title || 'Pilih', sections: parsed.sections || [] };
|
|
65
|
+
}
|
|
66
|
+
return btn;
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Create a WhatsApp socket with built-in interactiveButtons, custom pairing normalization,
|
|
72
|
+
* and automatic private/group binary node handling.
|
|
73
|
+
*/
|
|
74
|
+
export function makeWASocket(config = {}) {
|
|
75
|
+
const sock = elainaMakeWASocket({
|
|
76
|
+
browser: Browsers.ubuntu('Chrome'),
|
|
77
|
+
...config,
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
const originalSendMessage = sock.sendMessage.bind(sock);
|
|
81
|
+
const originalRequestPairingCode = sock.requestPairingCode.bind(sock);
|
|
82
|
+
|
|
83
|
+
sock.requestPairingCode = async (phoneNumber, customCode) => {
|
|
84
|
+
const cleanedNumber = String(phoneNumber || '').replace(/[^0-9]/g, '');
|
|
85
|
+
if (!cleanedNumber || cleanedNumber.startsWith('0') || cleanedNumber.length < 8) {
|
|
86
|
+
throw new Error('Nomor WA wajib format internasional tanpa 0 di depan (contoh: 628123456789)');
|
|
87
|
+
}
|
|
88
|
+
if (customCode !== undefined && customCode !== null && customCode !== '') {
|
|
89
|
+
const cleanedCode = String(customCode).toUpperCase().replace(/[^A-Z0-9]/g, '');
|
|
90
|
+
if (cleanedCode.length !== 8) {
|
|
91
|
+
throw new Error('Custom pairing code wajib tepat 8 karakter huruf/angka');
|
|
92
|
+
}
|
|
93
|
+
return originalRequestPairingCode(cleanedNumber, cleanedCode);
|
|
94
|
+
}
|
|
95
|
+
return originalRequestPairingCode(cleanedNumber);
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
sock.sendMessage = async (jid, content, options = {}) => {
|
|
99
|
+
if (content && Array.isArray(content.interactiveButtons)) {
|
|
100
|
+
const { interactiveButtons, ...rest } = content;
|
|
101
|
+
const mapped = mapInteractiveButtons(interactiveButtons);
|
|
102
|
+
const nextContent = {
|
|
103
|
+
...rest,
|
|
104
|
+
nativeFlow: mapped,
|
|
105
|
+
};
|
|
106
|
+
return originalSendMessage(jid, nextContent, options);
|
|
107
|
+
}
|
|
108
|
+
return originalSendMessage(jid, content, options);
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Helper to send quick-reply or single-select button menus cleanly.
|
|
113
|
+
*/
|
|
114
|
+
sock.sendButtonMenu = async (jid, { text, footer = '', buttons = [], mentions = [] }, options = {}) => {
|
|
115
|
+
return sock.sendMessage(
|
|
116
|
+
jid,
|
|
117
|
+
{
|
|
118
|
+
text,
|
|
119
|
+
footer,
|
|
120
|
+
mentions,
|
|
121
|
+
interactiveButtons: buttons,
|
|
122
|
+
},
|
|
123
|
+
options
|
|
124
|
+
);
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Helper to send document card with JPEG thumbnail and optional caption.
|
|
129
|
+
*/
|
|
130
|
+
sock.sendDocCard = async (
|
|
131
|
+
jid,
|
|
132
|
+
{
|
|
133
|
+
document = Buffer.from('doc'),
|
|
134
|
+
mimetype = 'image/png',
|
|
135
|
+
fileName = 'Menu',
|
|
136
|
+
fileLength = 1000000000,
|
|
137
|
+
jpegThumbnail,
|
|
138
|
+
caption = '',
|
|
139
|
+
mentions = [],
|
|
140
|
+
},
|
|
141
|
+
options = {}
|
|
142
|
+
) => {
|
|
143
|
+
return originalSendMessage(
|
|
144
|
+
jid,
|
|
145
|
+
{
|
|
146
|
+
document,
|
|
147
|
+
mimetype,
|
|
148
|
+
fileName,
|
|
149
|
+
fileLength,
|
|
150
|
+
...(jpegThumbnail ? { jpegThumbnail } : {}),
|
|
151
|
+
...(mentions.length ? { mentions } : {}),
|
|
152
|
+
caption,
|
|
153
|
+
},
|
|
154
|
+
options
|
|
155
|
+
);
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
return sock;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export {
|
|
162
|
+
Browsers,
|
|
163
|
+
DisconnectReason,
|
|
164
|
+
MB,
|
|
165
|
+
fetchLatestBaileysVersion,
|
|
166
|
+
isJidGroup,
|
|
167
|
+
normalizeMessageContent,
|
|
168
|
+
proto,
|
|
169
|
+
useMultiFileAuthState,
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
export default makeWASocket;
|
package/package.json
CHANGED
|
@@ -1,6 +1,28 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dafav/dafa-baileys",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Custom Baileys wrapper with interactiveButtons, custom pairing code, and live WA Web version fetcher",
|
|
5
|
+
"author": "dafav",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./index.js",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": "./index.js"
|
|
10
|
+
},
|
|
11
|
+
"publishConfig": {
|
|
12
|
+
"access": "public"
|
|
13
|
+
},
|
|
14
|
+
"engines": {
|
|
15
|
+
"node": ">=20.0.0"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [
|
|
18
|
+
"whatsapp",
|
|
19
|
+
"baileys",
|
|
20
|
+
"whatsapp-bot",
|
|
21
|
+
"interactive-buttons",
|
|
22
|
+
"pairing-code"
|
|
23
|
+
],
|
|
24
|
+
"license": "MIT",
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"@rexxhayanasi/elaina-baileys": "^1.4.7"
|
|
27
|
+
}
|
|
28
|
+
}
|