-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathconnect.js
43 lines (35 loc) · 1.24 KB
/
connect.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import P from 'pino'
import readline from 'readline'
import { useMultiFileAuthState, makeWASocket } from '@whiskeysockets/baileys'
const rl = readline.createInterface(process.stdin, process.stdout)
const question = (text) => new Promise((resolve) => rl.question(text, resolve))
async function startSock() {
const authState = await useMultiFileAuthState('session')
const conn = makeWASocket({
version: [ 2, 3000, 1015901307 ],
auth: authState.state,
browser: ['Linux', 'Chrome', '22'],
logger: P({ level: 'silent' }),
markOnlineOnConnect: true,
printQRInTerminal: false
})
if (!(conn.authState.creds.registered && (conn.user || {}).id)) {
let phoneNumber = await question('Please enter your mobile phone number:\n')
phoneNumber = phoneNumber.replace(/\D/g, '')
setTimeout(async () => {
let code = await conn.requestPairingCode(phoneNumber)
console.log('Pairing code:', (code.match(/.{1,4}/g) || []).join('-'))
}, 3000)
}
conn.ev.on('creds.update', authState.saveCreds.bind(conn))
conn.ev.on('connection.update', async (update) => {
console.log(update)
if (update.connection === 'close') {
await startSock()
} else if (update.connection === 'open') {
console.log(conn.user)
}
})
return conn
}
startSock()