-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
161 lines (136 loc) · 5.92 KB
/
index.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import NodeCache from 'node-cache'
import makeWASocket, {
DisconnectReason, fetchLatestBaileysVersion, getAggregateVotesInPollMessage,
makeCacheableSignalKeyStore, isJidBroadcast
} from '@whiskeysockets/baileys'
import dotenv from 'dotenv';
dotenv.config();
import logger from './logs.js'
import usePrismaDBAuthStore from './usePrismaDBAuthStore.js';
const doReplies = !process.argv.includes('--no-reply')
// external map to store retry counts of messages when decryption/encryption fails
// keep this out of the socket itself, so as to prevent a message decryption/encryption loop across socket restarts
const msgRetryCounterCache = new NodeCache()
// start a connection
const startSock = async () => {
const { state, saveCreds } = await usePrismaDBAuthStore('bot_teste_123')
// fetch latest version of WA Web
const { version, isLatest } = await fetchLatestBaileysVersion()
console.log(`using WA v${version.join('.')}, isLatest: ${isLatest}`)
const sock = makeWASocket.default({
version,
logger,
printQRInTerminal: true,
auth: {
creds: state.creds,
/** caching makes the store faster to send/recv messages */
keys: makeCacheableSignalKeyStore(state.keys, logger),
},
msgRetryCounterCache,
generateHighQualityLinkPreview: true,
// ignore all broadcast messages -- to receive the same
// comment the line below out
shouldIgnoreJid: jid => isJidBroadcast(jid),
// implement to handle retries & poll updates
})
const sendMessageWTyping = async (msg, jid) => {
await sock.sendMessage(jid, msg)
}
// the process function lets you process all events that just occurred
// efficiently in a batch
sock.ev.process(
// events is a map for event name => event data
async (events) => {
// something about the connection changed
// maybe it closed, or we received all offline message or connection opened
if (events['connection.update']) {
const update = events['connection.update']
const { connection, lastDisconnect } = update
if (connection === 'close') {
// reconnect if not logged out
if ((lastDisconnect?.error)?.output?.statusCode !== DisconnectReason.loggedOut) {
startSock()
} else {
console.log('Connection closed. You are logged out.')
}
}
console.log('connection update', update)
}
// credentials updated -- save them
if (events['creds.update']) {
await saveCreds()
}
if (events.call) {
console.log('recv call event', events.call)
}
// history received
if (events['messaging-history.set']) {
const { chats, contacts, messages, isLatest } = events['messaging-history.set']
console.log(`recv ${chats.length} chats, ${contacts.length} contacts, ${messages.length} msgs (is latest: ${isLatest})`)
}
// received a new message
if (events['messages.upsert']) {
const upsert = events['messages.upsert']
console.log('recv messages ', JSON.stringify(upsert, undefined, 2))
if (upsert.type === 'notify') {
for (const msg of upsert.messages) {
if (!msg.key.fromMe && doReplies) {
console.log('replying to', msg.key.remoteJid)
await sock.readMessages([msg.key])
await sendMessageWTyping({ text: 'Hello there!' }, msg.key.remoteJid)
}
}
}
}
// messages updated like status delivered, message deleted etc.
if (events['messages.update']) {
console.log(
JSON.stringify(events['messages.update'], undefined, 2)
)
for (const { key, update } of events['messages.update']) {
if (update.pollUpdates) {
const pollCreation = await getMessage(key)
if (pollCreation) {
console.log(
'got poll update, aggregation: ',
getAggregateVotesInPollMessage({
message: pollCreation,
pollUpdates: update.pollUpdates,
})
)
}
}
}
}
if (events['message-receipt.update']) {
console.log(events['message-receipt.update'])
}
if (events['messages.reaction']) {
console.log(events['messages.reaction'])
}
if (events['presence.update']) {
console.log(events['presence.update'])
}
if (events['chats.update']) {
console.log(events['chats.update'])
}
if (events['contacts.update']) {
for (const contact of events['contacts.update']) {
if (typeof contact.imgUrl !== 'undefined') {
const newUrl = contact.imgUrl === null
? null
: await sock.profilePictureUrl(contact.id).catch(() => null)
console.log(
`contact ${contact.id} has a new profile pic: ${newUrl}`,
)
}
}
}
if (events['chats.delete']) {
console.log('chats deleted ', events['chats.delete'])
}
}
)
return sock
}
startSock()