@areumtecnologia/wasockets 2.3.2 → 2.3.4
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 +24 -9
- package/lib/Client/Context/CallContext.js +19 -0
- package/lib/Client/Context/MediaObject.js +24 -0
- package/lib/Client/Context/MessageContext.js +26 -0
- package/lib/Client/Events/ConnectionEvents.js +235 -0
- package/lib/Client/Events/index.js +9 -0
- package/lib/Client/Normalizers/EventNormalizer.js +9 -6
- package/lib/Client/WaClient.js +19 -13
- package/lib/Client/index.d.ts +83 -9
- package/lib/Client/index.js +20 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -288,22 +288,37 @@ await ctx.unstar();
|
|
|
288
288
|
```
|
|
289
289
|
|
|
290
290
|
#### Utilitários de Mídia (`MediaObject`):
|
|
291
|
-
Se a mensagem contiver imagem, vídeo, áudio ou documento, `ctx.media`
|
|
291
|
+
Se a mensagem contiver imagem, vídeo, áudio, figurinha ou documento, `ctx.media` centraliza todos os metadados e métodos de manipulação em uma única instância:
|
|
292
292
|
|
|
293
|
+
##### Propriedades de `ctx.media`:
|
|
294
|
+
- `ctx.media.mimetype`: Tipo MIME real (ex: `'image/jpeg'`, `'audio/ogg; codecs=opus'`).
|
|
295
|
+
- `ctx.media.extension`: Extensão inferida do arquivo (ex: `'jpg'`, `'ogg'`, `'pdf'`, `'mp4'`).
|
|
296
|
+
- `ctx.media.fileLength`: Tamanho do arquivo em bytes.
|
|
297
|
+
- `ctx.media.fileName`: Nome original do arquivo (se enviado como documento).
|
|
298
|
+
- `ctx.media.caption`: Legenda de texto anexada à mídia.
|
|
299
|
+
- `ctx.media.seconds` / `duration`: Duração em segundos (para áudios e vídeos).
|
|
300
|
+
- `ctx.media.isVoice` / `isPtt`: Booleano se é áudio gravado no WhatsApp (Push-To-Talk).
|
|
301
|
+
- `ctx.media.isGif`: Booleano se é vídeo reproduzido como GIF animado.
|
|
302
|
+
- `ctx.media.isViewOnce`: Booleano se é mídia de visualização única.
|
|
303
|
+
|
|
304
|
+
##### Métodos de `ctx.media`:
|
|
293
305
|
```javascript
|
|
294
306
|
if (ctx.media) {
|
|
295
|
-
// 1. Salva diretamente em disco
|
|
307
|
+
// 1. Salva diretamente em disco (cria diretórios se não existirem)
|
|
296
308
|
await ctx.media.save('./pasta/comprovante.pdf');
|
|
297
309
|
|
|
298
|
-
// 2.
|
|
299
|
-
const
|
|
300
|
-
|
|
310
|
+
// 2. Obtém como Buffer em memória (com cache automático)
|
|
311
|
+
const buffer = await ctx.media.toBuffer();
|
|
312
|
+
|
|
313
|
+
// 3. Converte para Base64 (ideal para LLMs como Gemini / OpenAI)
|
|
314
|
+
const base64 = await ctx.media.toBase64();
|
|
315
|
+
const dataUri = await ctx.media.toDataUri(); // 'data:image/jpeg;base64,...'
|
|
301
316
|
|
|
302
|
-
//
|
|
303
|
-
const stream = ctx.media.toStream();
|
|
317
|
+
// 4. Obtém como Stream legível (Node Readable)
|
|
318
|
+
const stream = await ctx.media.toStream();
|
|
304
319
|
|
|
305
|
-
//
|
|
306
|
-
const sharpInstance = ctx.media.toSharp();
|
|
320
|
+
// 5. Integração nativa com Sharp (se instalado) para recorte/redimensionamento
|
|
321
|
+
const sharpInstance = await ctx.media.toSharp();
|
|
307
322
|
}
|
|
308
323
|
```
|
|
309
324
|
|
|
@@ -90,6 +90,25 @@ class CallContext {
|
|
|
90
90
|
}
|
|
91
91
|
throw new Error('No active audio stream or call is not connected yet.')
|
|
92
92
|
}
|
|
93
|
+
|
|
94
|
+
toString() {
|
|
95
|
+
return `[Call ${this.id}] ${this.fromNumber} (${this.isVideo ? 'Vídeo' : 'Voz'}) - status: ${this.status}`
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
toJSON() {
|
|
99
|
+
return {
|
|
100
|
+
id: this.id,
|
|
101
|
+
from: this.from,
|
|
102
|
+
fromNumber: this.fromNumber,
|
|
103
|
+
isVideo: this.isVideo,
|
|
104
|
+
status: this.status,
|
|
105
|
+
startTime: this.startTime
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
[Symbol.toPrimitive]() {
|
|
110
|
+
return this.toString()
|
|
111
|
+
}
|
|
93
112
|
}
|
|
94
113
|
|
|
95
114
|
exports.CallContext = CallContext
|
|
@@ -155,6 +155,30 @@ class MediaObject {
|
|
|
155
155
|
const buffer = await this.toBuffer(options)
|
|
156
156
|
return sharp(buffer)
|
|
157
157
|
}
|
|
158
|
+
|
|
159
|
+
toString() {
|
|
160
|
+
const sizeStr = this.fileLength ? `${this.fileLength} bytes` : 'unknown size'
|
|
161
|
+
return `[MediaObject ${this.mediaType}: ${this.mimetype} (${sizeStr})]`
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
toJSON() {
|
|
165
|
+
return {
|
|
166
|
+
mediaType: this.mediaType,
|
|
167
|
+
mimetype: this.mimetype,
|
|
168
|
+
extension: this.extension,
|
|
169
|
+
fileLength: this.fileLength,
|
|
170
|
+
fileName: this.fileName,
|
|
171
|
+
caption: this.caption,
|
|
172
|
+
duration: this.duration,
|
|
173
|
+
isPtt: this.isPtt,
|
|
174
|
+
isGif: this.isGif,
|
|
175
|
+
isViewOnce: this.isViewOnce
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
[Symbol.toPrimitive]() {
|
|
180
|
+
return this.toString()
|
|
181
|
+
}
|
|
158
182
|
}
|
|
159
183
|
|
|
160
184
|
exports.MediaObject = MediaObject
|
|
@@ -255,6 +255,32 @@ class MessageContext {
|
|
|
255
255
|
}
|
|
256
256
|
return this.client.sock.pinInChatMessage(this.chatId, this.raw.key, 2)
|
|
257
257
|
}
|
|
258
|
+
|
|
259
|
+
toString() {
|
|
260
|
+
const author = this.fromPushName || this.senderName || this.fromNumber || this.from
|
|
261
|
+
const preview = this.body ? `"${this.body}"` : `<${this.type}>`
|
|
262
|
+
return `[Message ${this.id}] ${author}: ${preview}`
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
toJSON() {
|
|
266
|
+
return {
|
|
267
|
+
id: this.id,
|
|
268
|
+
chatId: this.chatId,
|
|
269
|
+
from: this.from,
|
|
270
|
+
to: this.to,
|
|
271
|
+
fromMe: this.fromMe,
|
|
272
|
+
fromPushName: this.fromPushName,
|
|
273
|
+
fromNumber: this.fromNumber,
|
|
274
|
+
type: this.type,
|
|
275
|
+
body: this.body,
|
|
276
|
+
isEdited: this.isEdited,
|
|
277
|
+
timestamp: this.timestamp
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
[Symbol.toPrimitive]() {
|
|
282
|
+
return this.toString()
|
|
283
|
+
}
|
|
258
284
|
}
|
|
259
285
|
|
|
260
286
|
exports.MessageContext = MessageContext
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
"use strict"
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true })
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Event emitted when connection status transitions between states
|
|
7
|
+
*/
|
|
8
|
+
class ConnectionStatusChange {
|
|
9
|
+
constructor(current, previous) {
|
|
10
|
+
this.current = current
|
|
11
|
+
this.previous = previous
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
toString() {
|
|
15
|
+
return JSON.stringify({ current: this.current, previous: this.previous })
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
toJSON() {
|
|
19
|
+
return { current: this.current, previous: this.previous }
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
[Symbol.toPrimitive]() {
|
|
23
|
+
return this.toString()
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Event emitted when a QR code is generated for authentication
|
|
29
|
+
*/
|
|
30
|
+
class QrCodeEvent {
|
|
31
|
+
constructor(qr) {
|
|
32
|
+
this.qr = qr || ''
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
toString() {
|
|
36
|
+
return this.qr
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
toJSON() {
|
|
40
|
+
return { qr: this.qr }
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
[Symbol.toPrimitive]() {
|
|
44
|
+
return this.qr
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Event emitted when a 8-digit pairing code is received
|
|
50
|
+
*/
|
|
51
|
+
class PairingCodeEvent {
|
|
52
|
+
constructor(code) {
|
|
53
|
+
this.code = code || ''
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
toString() {
|
|
57
|
+
return this.code
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
toJSON() {
|
|
61
|
+
return { code: this.code }
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
[Symbol.toPrimitive]() {
|
|
65
|
+
return this.code
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Event emitted when client successfully authenticates and opens connection
|
|
71
|
+
*/
|
|
72
|
+
class ConnectedEvent {
|
|
73
|
+
constructor(user) {
|
|
74
|
+
this.user = user || {}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
get name() {
|
|
78
|
+
return this.user?.name || this.user?.notify || 'WhatsApp User'
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
get id() {
|
|
82
|
+
return this.user?.id || ''
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
toString() {
|
|
86
|
+
return `[Connected: ${this.name} (${this.id})]`
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
toJSON() {
|
|
90
|
+
return { user: this.user }
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
[Symbol.toPrimitive]() {
|
|
94
|
+
return this.toString()
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Event emitted when connection drops or closes
|
|
100
|
+
*/
|
|
101
|
+
class DisconnectedEvent {
|
|
102
|
+
constructor(options = {}) {
|
|
103
|
+
this.error = options.error || null
|
|
104
|
+
this.willReconnect = Boolean(options.willReconnect)
|
|
105
|
+
this.attempt = options.attempt || 0
|
|
106
|
+
this.delayMs = options.delayMs || 0
|
|
107
|
+
this.reason = options.reason || options.error?.message || 'Connection closed'
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
toString() {
|
|
111
|
+
const reconnectInfo = this.willReconnect
|
|
112
|
+
? `Reconnecting attempt ${this.attempt} in ${this.delayMs}ms`
|
|
113
|
+
: 'No reconnect'
|
|
114
|
+
return `[Disconnected: ${this.reason} (${reconnectInfo})]`
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
toJSON() {
|
|
118
|
+
return {
|
|
119
|
+
error: this.error ? { message: this.error.message, stack: this.error.stack } : null,
|
|
120
|
+
willReconnect: this.willReconnect,
|
|
121
|
+
attempt: this.attempt,
|
|
122
|
+
delayMs: this.delayMs,
|
|
123
|
+
reason: this.reason
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
[Symbol.toPrimitive]() {
|
|
128
|
+
return this.toString()
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Event emitted when device session is permanently logged out
|
|
134
|
+
*/
|
|
135
|
+
class LoggedOutEvent {
|
|
136
|
+
constructor(error) {
|
|
137
|
+
this.error = error || null
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
toString() {
|
|
141
|
+
return '[LoggedOut: Session logged out on WhatsApp device]'
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
toJSON() {
|
|
145
|
+
return { error: this.error ? { message: this.error.message } : null }
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
[Symbol.toPrimitive]() {
|
|
149
|
+
return this.toString()
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Event emitted when one or more messages are deleted/revoked
|
|
155
|
+
*/
|
|
156
|
+
class MessageDeleteEvent {
|
|
157
|
+
constructor(keys) {
|
|
158
|
+
this.keys = Array.isArray(keys) ? keys : [keys].filter(Boolean)
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
get ids() {
|
|
162
|
+
return this.keys.map(k => k?.id || k).filter(Boolean)
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
toString() {
|
|
166
|
+
return `[MessageDelete: ${this.ids.join(', ')}]`
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
toJSON() {
|
|
170
|
+
return { keys: this.keys }
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
[Symbol.toPrimitive]() {
|
|
174
|
+
return this.toString()
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Event emitted when participants in a group are added, removed, promoted or demoted
|
|
180
|
+
*/
|
|
181
|
+
class GroupParticipantsUpdateEvent {
|
|
182
|
+
constructor(update = {}) {
|
|
183
|
+
this.id = update.id || ''
|
|
184
|
+
this.participants = update.participants || []
|
|
185
|
+
this.action = update.action || 'update'
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
toString() {
|
|
189
|
+
return `[GroupParticipantsUpdate ${this.action}: ${this.participants.join(', ')} in ${this.id}]`
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
toJSON() {
|
|
193
|
+
return { id: this.id, participants: this.participants, action: this.action }
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
[Symbol.toPrimitive]() {
|
|
197
|
+
return this.toString()
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Event emitted on contact or group presence status updates
|
|
203
|
+
*/
|
|
204
|
+
class PresenceUpdateEvent {
|
|
205
|
+
constructor(presence = {}) {
|
|
206
|
+
this.id = presence.id || ''
|
|
207
|
+
this.presences = presence.presences || {}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
toString() {
|
|
211
|
+
const keys = Object.keys(this.presences)
|
|
212
|
+
const summary = keys.length
|
|
213
|
+
? keys.map(k => `${k}:${this.presences[k]?.lastKnownPresence || 'active'}`).join(', ')
|
|
214
|
+
: 'active'
|
|
215
|
+
return `[PresenceUpdate ${this.id}: ${summary}]`
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
toJSON() {
|
|
219
|
+
return { id: this.id, presences: this.presences }
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
[Symbol.toPrimitive]() {
|
|
223
|
+
return this.toString()
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
exports.ConnectionStatusChange = ConnectionStatusChange
|
|
228
|
+
exports.QrCodeEvent = QrCodeEvent
|
|
229
|
+
exports.PairingCodeEvent = PairingCodeEvent
|
|
230
|
+
exports.ConnectedEvent = ConnectedEvent
|
|
231
|
+
exports.DisconnectedEvent = DisconnectedEvent
|
|
232
|
+
exports.LoggedOutEvent = LoggedOutEvent
|
|
233
|
+
exports.MessageDeleteEvent = MessageDeleteEvent
|
|
234
|
+
exports.GroupParticipantsUpdateEvent = GroupParticipantsUpdateEvent
|
|
235
|
+
exports.PresenceUpdateEvent = PresenceUpdateEvent
|
|
@@ -5,6 +5,11 @@ Object.defineProperty(exports, "__esModule", { value: true })
|
|
|
5
5
|
const { Events } = require("../Constants")
|
|
6
6
|
const { MessageContext } = require("../Context/MessageContext")
|
|
7
7
|
const { CallContext } = require("../Context/CallContext")
|
|
8
|
+
const {
|
|
9
|
+
MessageDeleteEvent,
|
|
10
|
+
GroupParticipantsUpdateEvent,
|
|
11
|
+
PresenceUpdateEvent
|
|
12
|
+
} = require("../Events/ConnectionEvents")
|
|
8
13
|
|
|
9
14
|
class EventNormalizer {
|
|
10
15
|
/**
|
|
@@ -26,9 +31,7 @@ class EventNormalizer {
|
|
|
26
31
|
// Handle protocol revoke messages explicitly (delete for everyone)
|
|
27
32
|
const isRevoke = rawMsg.message?.protocolMessage?.type === 0
|
|
28
33
|
if (isRevoke) {
|
|
29
|
-
client.emit(Events.MESSAGE_DELETE,
|
|
30
|
-
keys: [rawMsg.message.protocolMessage.key || rawMsg.key]
|
|
31
|
-
})
|
|
34
|
+
client.emit(Events.MESSAGE_DELETE, new MessageDeleteEvent([rawMsg.message.protocolMessage.key || rawMsg.key]))
|
|
32
35
|
continue // Do not emit as a new received/sent message
|
|
33
36
|
}
|
|
34
37
|
|
|
@@ -58,12 +61,12 @@ class EventNormalizer {
|
|
|
58
61
|
|
|
59
62
|
// 3. Message deletion (revoke)
|
|
60
63
|
sock.ev.on('messages.delete', (item) => {
|
|
61
|
-
client.emit(Events.MESSAGE_DELETE, item)
|
|
64
|
+
client.emit(Events.MESSAGE_DELETE, new MessageDeleteEvent(item?.keys || item))
|
|
62
65
|
})
|
|
63
66
|
|
|
64
67
|
// 4. Presence updates (typing, online, offline)
|
|
65
68
|
sock.ev.on('presence.update', (presence) => {
|
|
66
|
-
client.emit(Events.PRESENCE_UPDATE, presence)
|
|
69
|
+
client.emit(Events.PRESENCE_UPDATE, new PresenceUpdateEvent(presence))
|
|
67
70
|
})
|
|
68
71
|
|
|
69
72
|
// 5. Contacts upsert
|
|
@@ -73,7 +76,7 @@ class EventNormalizer {
|
|
|
73
76
|
|
|
74
77
|
// 6. Group participants updates (join, leave, promote, demote)
|
|
75
78
|
sock.ev.on('group-participants.update', (update) => {
|
|
76
|
-
client.emit(Events.GROUP_PARTICIPANTS_UPDATE, update)
|
|
79
|
+
client.emit(Events.GROUP_PARTICIPANTS_UPDATE, new GroupParticipantsUpdateEvent(update))
|
|
77
80
|
})
|
|
78
81
|
|
|
79
82
|
// 7. VoIP incoming calls & status
|
package/lib/Client/WaClient.js
CHANGED
|
@@ -8,6 +8,14 @@ const { default: makeWASocket } = require("../Socket")
|
|
|
8
8
|
const { useMultiFileAuthState } = require("../Utils/use-multi-file-auth-state")
|
|
9
9
|
const { shouldReconnect, getReconnectDelay } = require("../Utils/reconnect")
|
|
10
10
|
const { Events, ConnectionStatus } = require("./Constants")
|
|
11
|
+
const {
|
|
12
|
+
ConnectionStatusChange,
|
|
13
|
+
QrCodeEvent,
|
|
14
|
+
PairingCodeEvent,
|
|
15
|
+
ConnectedEvent,
|
|
16
|
+
DisconnectedEvent,
|
|
17
|
+
LoggedOutEvent
|
|
18
|
+
} = require("./Events/ConnectionEvents")
|
|
11
19
|
const { JidNormalizer } = require("./Normalizers/JidNormalizer")
|
|
12
20
|
const { EventNormalizer } = require("./Normalizers/EventNormalizer")
|
|
13
21
|
const {
|
|
@@ -161,14 +169,14 @@ class WaClient extends EventEmitter {
|
|
|
161
169
|
// QR Code issued
|
|
162
170
|
if (qr) {
|
|
163
171
|
this._setStatus(ConnectionStatus.QR_READY)
|
|
164
|
-
this.emit(Events.QR_CODE,
|
|
172
|
+
this.emit(Events.QR_CODE, new QrCodeEvent(qr))
|
|
165
173
|
}
|
|
166
174
|
|
|
167
175
|
// Connection successfully established
|
|
168
176
|
if (connection === 'open') {
|
|
169
177
|
this.reconnectAttempts = 0
|
|
170
178
|
this._setStatus(ConnectionStatus.CONNECTED)
|
|
171
|
-
this.emit(Events.CONNECTED,
|
|
179
|
+
this.emit(Events.CONNECTED, new ConnectedEvent(this.sock.user))
|
|
172
180
|
}
|
|
173
181
|
|
|
174
182
|
// Connection closed or dropped
|
|
@@ -178,12 +186,12 @@ class WaClient extends EventEmitter {
|
|
|
178
186
|
|
|
179
187
|
if (!canReconnect) {
|
|
180
188
|
this._setStatus(ConnectionStatus.LOGGED_OUT)
|
|
181
|
-
this.emit(Events.LOGGED_OUT,
|
|
189
|
+
this.emit(Events.LOGGED_OUT, new LoggedOutEvent(error))
|
|
182
190
|
} else if (this.options.restartOnClose && !this._isDestroyed) {
|
|
183
191
|
this._scheduleReconnect(error)
|
|
184
192
|
} else {
|
|
185
193
|
this._setStatus(ConnectionStatus.DISCONNECTED)
|
|
186
|
-
this.emit(Events.DISCONNECTED, { error, willReconnect: false })
|
|
194
|
+
this.emit(Events.DISCONNECTED, new DisconnectedEvent({ error, willReconnect: false }))
|
|
187
195
|
}
|
|
188
196
|
}
|
|
189
197
|
})
|
|
@@ -203,7 +211,7 @@ class WaClient extends EventEmitter {
|
|
|
203
211
|
try {
|
|
204
212
|
this._setStatus(ConnectionStatus.PAIRING_READY)
|
|
205
213
|
const code = await this.sock.requestPairingCode(cleanNumber)
|
|
206
|
-
this.emit(Events.PAIRING_CODE,
|
|
214
|
+
this.emit(Events.PAIRING_CODE, new PairingCodeEvent(code))
|
|
207
215
|
} catch (err) {
|
|
208
216
|
if (retryCount < 3 && !this._isDestroyed) {
|
|
209
217
|
setTimeout(() => attemptPairing(retryCount + 1), 3000)
|
|
@@ -224,11 +232,11 @@ class WaClient extends EventEmitter {
|
|
|
224
232
|
_scheduleReconnect(error) {
|
|
225
233
|
if (this.reconnectAttempts >= this.options.maxReconnectAttempts) {
|
|
226
234
|
this._setStatus(ConnectionStatus.DISCONNECTED)
|
|
227
|
-
this.emit(Events.DISCONNECTED, {
|
|
235
|
+
this.emit(Events.DISCONNECTED, new DisconnectedEvent({
|
|
228
236
|
error,
|
|
229
237
|
willReconnect: false,
|
|
230
238
|
reason: 'Max reconnect attempts exceeded'
|
|
231
|
-
})
|
|
239
|
+
}))
|
|
232
240
|
return
|
|
233
241
|
}
|
|
234
242
|
|
|
@@ -238,12 +246,12 @@ class WaClient extends EventEmitter {
|
|
|
238
246
|
})
|
|
239
247
|
|
|
240
248
|
this._setStatus(ConnectionStatus.RECONNECTING)
|
|
241
|
-
this.emit(Events.DISCONNECTED, {
|
|
249
|
+
this.emit(Events.DISCONNECTED, new DisconnectedEvent({
|
|
242
250
|
error,
|
|
243
251
|
willReconnect: true,
|
|
244
252
|
attempt: this.reconnectAttempts,
|
|
245
253
|
delayMs
|
|
246
|
-
})
|
|
254
|
+
}))
|
|
247
255
|
|
|
248
256
|
this._clearReconnectTimer()
|
|
249
257
|
this._reconnectTimer = setTimeout(() => {
|
|
@@ -264,10 +272,7 @@ class WaClient extends EventEmitter {
|
|
|
264
272
|
if (previousStatus === newStatus) return
|
|
265
273
|
|
|
266
274
|
this.status = newStatus
|
|
267
|
-
this.emit(Events.STATUS_CHANGE,
|
|
268
|
-
current: newStatus,
|
|
269
|
-
previous: previousStatus
|
|
270
|
-
})
|
|
275
|
+
this.emit(Events.STATUS_CHANGE, new ConnectionStatusChange(newStatus, previousStatus))
|
|
271
276
|
}
|
|
272
277
|
|
|
273
278
|
_clearReconnectTimer() {
|
|
@@ -435,4 +440,5 @@ class WaClient extends EventEmitter {
|
|
|
435
440
|
}
|
|
436
441
|
|
|
437
442
|
exports.WaClient = WaClient
|
|
443
|
+
exports.ConnectionStatusChange = ConnectionStatusChange
|
|
438
444
|
exports.default = WaClient
|
package/lib/Client/index.d.ts
CHANGED
|
@@ -37,6 +37,80 @@ export declare const ConnectionStatus: {
|
|
|
37
37
|
|
|
38
38
|
export type ConnectionStatusType = typeof ConnectionStatus[keyof typeof ConnectionStatus]
|
|
39
39
|
|
|
40
|
+
export declare class ConnectionStatusChange {
|
|
41
|
+
current: ConnectionStatusType
|
|
42
|
+
previous: ConnectionStatusType
|
|
43
|
+
constructor(current: ConnectionStatusType, previous: ConnectionStatusType)
|
|
44
|
+
toString(): string
|
|
45
|
+
toJSON(): { current: ConnectionStatusType; previous: ConnectionStatusType }
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export declare class QrCodeEvent {
|
|
49
|
+
qr: string
|
|
50
|
+
constructor(qr: string)
|
|
51
|
+
toString(): string
|
|
52
|
+
toJSON(): { qr: string }
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export declare class PairingCodeEvent {
|
|
56
|
+
code: string
|
|
57
|
+
constructor(code: string)
|
|
58
|
+
toString(): string
|
|
59
|
+
toJSON(): { code: string }
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export declare class ConnectedEvent {
|
|
63
|
+
user: any
|
|
64
|
+
readonly name: string
|
|
65
|
+
readonly id: string
|
|
66
|
+
constructor(user: any)
|
|
67
|
+
toString(): string
|
|
68
|
+
toJSON(): { user: any }
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export declare class DisconnectedEvent {
|
|
72
|
+
error: any | null
|
|
73
|
+
willReconnect: boolean
|
|
74
|
+
attempt: number
|
|
75
|
+
delayMs: number
|
|
76
|
+
reason: string
|
|
77
|
+
constructor(options?: any)
|
|
78
|
+
toString(): string
|
|
79
|
+
toJSON(): any
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export declare class LoggedOutEvent {
|
|
83
|
+
error: any | null
|
|
84
|
+
constructor(error?: any)
|
|
85
|
+
toString(): string
|
|
86
|
+
toJSON(): any
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export declare class MessageDeleteEvent {
|
|
90
|
+
keys: any[]
|
|
91
|
+
readonly ids: string[]
|
|
92
|
+
constructor(keys: any | any[])
|
|
93
|
+
toString(): string
|
|
94
|
+
toJSON(): { keys: any[] }
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export declare class GroupParticipantsUpdateEvent {
|
|
98
|
+
id: string
|
|
99
|
+
participants: string[]
|
|
100
|
+
action: string
|
|
101
|
+
constructor(update?: any)
|
|
102
|
+
toString(): string
|
|
103
|
+
toJSON(): { id: string; participants: string[]; action: string }
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export declare class PresenceUpdateEvent {
|
|
107
|
+
id: string
|
|
108
|
+
presences: Record<string, any>
|
|
109
|
+
constructor(presence?: any)
|
|
110
|
+
toString(): string
|
|
111
|
+
toJSON(): { id: string; presences: Record<string, any> }
|
|
112
|
+
}
|
|
113
|
+
|
|
40
114
|
export interface WaClientOptions {
|
|
41
115
|
sessionName?: string
|
|
42
116
|
dataPath?: string
|
|
@@ -530,21 +604,21 @@ export declare class WaClient extends EventEmitter {
|
|
|
530
604
|
get isConnected(): boolean
|
|
531
605
|
get rawSocket(): any
|
|
532
606
|
|
|
533
|
-
on(event: 'status.change', listener: (status:
|
|
534
|
-
on(event: 'qr', listener: (data:
|
|
535
|
-
on(event: 'pairing.code', listener: (data:
|
|
536
|
-
on(event: 'connected', listener: (data:
|
|
537
|
-
on(event: 'disconnected', listener: (data:
|
|
538
|
-
on(event: 'logged.out', listener: (data:
|
|
607
|
+
on(event: 'status.change', listener: (status: ConnectionStatusChange) => void): this
|
|
608
|
+
on(event: 'qr', listener: (data: QrCodeEvent) => void): this
|
|
609
|
+
on(event: 'pairing.code', listener: (data: PairingCodeEvent) => void): this
|
|
610
|
+
on(event: 'connected', listener: (data: ConnectedEvent) => void): this
|
|
611
|
+
on(event: 'disconnected', listener: (data: DisconnectedEvent) => void): this
|
|
612
|
+
on(event: 'logged.out', listener: (data: LoggedOutEvent) => void): this
|
|
539
613
|
on(event: 'call.incoming', listener: (call: CallContext) => void): this
|
|
540
614
|
on(event: 'call.status', listener: (calls: any[]) => void): this
|
|
541
615
|
on(event: 'message.received', listener: (ctx: MessageContext, raw: proto.IWebMessageInfo) => void): this
|
|
542
616
|
on(event: 'message.sent', listener: (ctx: MessageContext, raw: proto.IWebMessageInfo) => void): this
|
|
543
617
|
on(event: 'message.update', listener: (updates: any[]) => void): this
|
|
544
|
-
on(event: 'message.delete', listener: (item:
|
|
545
|
-
on(event: 'presence.update', listener: (presence:
|
|
618
|
+
on(event: 'message.delete', listener: (item: MessageDeleteEvent) => void): this
|
|
619
|
+
on(event: 'presence.update', listener: (presence: PresenceUpdateEvent) => void): this
|
|
546
620
|
on(event: 'contacts.upsert', listener: (contacts: any[]) => void): this
|
|
547
|
-
on(event: 'group.participants.update', listener: (update:
|
|
621
|
+
on(event: 'group.participants.update', listener: (update: GroupParticipantsUpdateEvent) => void): this
|
|
548
622
|
on(event: 'error', listener: (error: any) => void): this
|
|
549
623
|
on(event: string | symbol, listener: (...args: any[]) => void): this
|
|
550
624
|
}
|
package/lib/Client/index.js
CHANGED
|
@@ -4,6 +4,17 @@ Object.defineProperty(exports, "__esModule", { value: true })
|
|
|
4
4
|
|
|
5
5
|
const { WaClient } = require("./WaClient")
|
|
6
6
|
const { Events, ConnectionStatus } = require("./Constants")
|
|
7
|
+
const {
|
|
8
|
+
ConnectionStatusChange,
|
|
9
|
+
QrCodeEvent,
|
|
10
|
+
PairingCodeEvent,
|
|
11
|
+
ConnectedEvent,
|
|
12
|
+
DisconnectedEvent,
|
|
13
|
+
LoggedOutEvent,
|
|
14
|
+
MessageDeleteEvent,
|
|
15
|
+
GroupParticipantsUpdateEvent,
|
|
16
|
+
PresenceUpdateEvent
|
|
17
|
+
} = require("./Events/ConnectionEvents")
|
|
7
18
|
const { MessageContext } = require("./Context/MessageContext")
|
|
8
19
|
const { MediaObject } = require("./Context/MediaObject")
|
|
9
20
|
const { CallContext } = require("./Context/CallContext")
|
|
@@ -40,6 +51,15 @@ const {
|
|
|
40
51
|
exports.WaClient = WaClient
|
|
41
52
|
exports.Events = Events
|
|
42
53
|
exports.ConnectionStatus = ConnectionStatus
|
|
54
|
+
exports.ConnectionStatusChange = ConnectionStatusChange
|
|
55
|
+
exports.QrCodeEvent = QrCodeEvent
|
|
56
|
+
exports.PairingCodeEvent = PairingCodeEvent
|
|
57
|
+
exports.ConnectedEvent = ConnectedEvent
|
|
58
|
+
exports.DisconnectedEvent = DisconnectedEvent
|
|
59
|
+
exports.LoggedOutEvent = LoggedOutEvent
|
|
60
|
+
exports.MessageDeleteEvent = MessageDeleteEvent
|
|
61
|
+
exports.GroupParticipantsUpdateEvent = GroupParticipantsUpdateEvent
|
|
62
|
+
exports.PresenceUpdateEvent = PresenceUpdateEvent
|
|
43
63
|
exports.MessageContext = MessageContext
|
|
44
64
|
exports.MediaObject = MediaObject
|
|
45
65
|
exports.CallContext = CallContext
|