forked from ookamiiixd/baileys-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwhatsapp.js
146 lines (117 loc) · 3.51 KB
/
whatsapp.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import fs from 'fs'
import path from 'path'
import { fileURLToPath } from 'url'
import { WAConnection } from '@adiwajshing/baileys'
import { toDataURL } from 'qrcode'
import response from './response.js'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
const sessions = new Map()
const isCreatedSession = (sessionId) => {
return fs.existsSync(path.join(__dirname, 'sessions', `${sessionId}.json`))
}
const isSessionExists = (sessionId) => {
return sessions.has(sessionId)
}
const getSession = (sessionId) => {
return sessions.get(sessionId) ?? null
}
const createSession = async (sessionId, res = null) => {
const isCreated = isCreatedSession(sessionId)
const wa = new WAConnection()
const timeout = setTimeout(() => {
wa.close()
}, 20000)
wa.version = [3, 3234, 9]
wa.browserDescription = ['Windows', 'Chrome', '10']
if (isCreated) {
wa.loadAuthInfo(path.join(__dirname, 'sessions', `${sessionId}.json`))
}
wa.on('open', () => {
const authInfo = wa.base64EncodedAuthInfo()
fs.writeFileSync(path.join(__dirname, 'sessions', `${sessionId}.json`), JSON.stringify(authInfo, null, '\t'))
sessions.set(sessionId, wa)
clearTimeout(timeout)
})
wa.on('qr', (qr) => {
if (!res || res.headersSent || isCreated) {
return deleteSession(sessionId)
}
toDataURL(qr)
.then((url) => {
return response(res, 200, true, 'QR code received, please scan the QR code.', { qr: url })
})
.catch(() => {
return response(res, 500, false, 'Unable to create QR code.')
})
})
wa.on('close', () => {
deleteSession(sessionId)
})
await wa.connect().catch((err) => {
console.log('An error occured during connecting: ' + err)
if (res && !res.headersSent) {
return response(res, 500, false, 'Unable to create session.')
}
})
}
const triggerDeleteSession = (sessionId) => {
if (sessions.has(sessionId)) {
getSession(sessionId).close()
}
}
const deleteSession = (sessionId) => {
if (isCreatedSession(sessionId)) {
fs.unlinkSync(path.join(__dirname, 'sessions', `${sessionId}.json`))
}
sessions.delete(sessionId)
}
const getChatList = (session, isGroup = false) => {
const filter = isGroup ? '@g.us' : '@s.whatsapp.net'
const { chats } = session.loadChats()
return chats.reduce((carry, item) => {
if (item.jid.endsWith(filter)) {
if ('messages' in item) {
delete item.messages
}
carry.push(item)
}
return carry
}, [])
}
const formatPhone = (phone) => {
if (phone.endsWith('@s.whatsapp.net')) {
return phone
}
let formatted = phone.replace(/\D/g, '')
return (formatted += '@s.whatsapp.net')
}
const formatGroup = (group) => {
if (group.endsWith('@g.us')) {
return group
}
let formatted = group.replace(/[^\d-]/g, '')
return (formatted += '@g.us')
}
const init = () => {
fs.readdir(path.join(__dirname, 'sessions'), (err, files) => {
if (err) {
throw err
}
files.forEach((file) => {
if (file.endsWith('.json')) {
createSession(file.replace('.json', ''))
}
})
})
}
export {
isCreatedSession,
isSessionExists,
getSession,
createSession,
triggerDeleteSession,
getChatList,
formatPhone,
formatGroup,
init,
}