-
-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathsessionController.js
More file actions
594 lines (575 loc) · 18.5 KB
/
Copy pathsessionController.js
File metadata and controls
594 lines (575 loc) · 18.5 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
const qr = require('qr-image')
const { setupSession, deleteSession, reloadSession, validateSession, flushSessions, destroySession, sessions, setSessionWebhook, getSessionWebhook } = require('../sessions')
const { sendErrorResponse, waitForNestedObject, exposeFunctionIfAbsent } = require('../utils')
const { logger } = require('../logger')
/**
* Starts a session for the given session ID.
*
* @function
* @async
* @param {Object} req - The HTTP request object.
* @param {Object} res - The HTTP response object.
* @param {string} req.params.sessionId - The session ID to start.
* @param {string} [req.body.webhookUrl] - Optional webhook URL for this session.
* @returns {Promise<void>}
* @throws {Error} If there was an error starting the session.
*/
const startSession = async (req, res) => {
// #swagger.summary = 'Start new session'
// #swagger.description = 'Starts a session for the given session ID. Optionally accepts a webhookUrl in the request body (POST) to configure a per-session webhook.'
/*
#swagger.requestBody = {
required: false,
schema: {
type: 'object',
properties: {
webhookUrl: {
type: 'string',
description: 'Optional webhook URL for this session. Overrides BASE_WEBHOOK_URL and session env var.',
example: 'https://your-server.com/webhook/my-session'
}
}
}
}
*/
const sessionId = req.params.sessionId
try {
// Read optional webhookUrl from body (works for both GET with empty body and POST with JSON)
const options = {}
if (req.body && req.body.webhookUrl) {
options.webhookUrl = req.body.webhookUrl
}
const setupSessionReturn = await setupSession(sessionId, options)
if (!setupSessionReturn.success) {
/* #swagger.responses[422] = {
description: "Unprocessable Entity.",
content: {
"application/json": {
schema: { "$ref": "#/definitions/ErrorResponse" }
}
}
}
*/
sendErrorResponse(res, 422, setupSessionReturn.message)
return
}
/* #swagger.responses[200] = {
description: "Status of the initiated session.",
content: {
"application/json": {
schema: { "$ref": "#/definitions/StartSessionResponse" }
}
}
}
*/
// wait until the client is created
await waitForNestedObject(setupSessionReturn.client, 'pupPage')
res.json({ success: true, message: setupSessionReturn.message })
} catch (error) {
logger.error({ sessionId, err: error }, 'Failed to start session')
sendErrorResponse(res, 500, error.message)
}
}
/**
* Set or update the webhook URL for an active session.
*
* @function
* @async
* @param {Object} req - The HTTP request object.
* @param {Object} res - The HTTP response object.
* @param {string} req.params.sessionId - The session ID.
* @param {string} req.body.webhookUrl - The new webhook URL (or null/empty to clear).
* @returns {Promise<void>}
*/
const setWebhook = async (req, res) => {
// #swagger.summary = 'Set session webhook URL'
// #swagger.description = 'Set or update the webhook URL for an active session at runtime. Send an empty webhookUrl or null to clear and fall back to environment variables.'
/*
#swagger.requestBody = {
required: true,
schema: {
type: 'object',
properties: {
webhookUrl: {
type: 'string',
description: 'The webhook URL to set for this session. Send empty string or null to clear.',
example: 'https://your-server.com/webhook/my-session'
}
}
}
}
*/
const sessionId = req.params.sessionId
try {
const { webhookUrl } = req.body || {}
const result = setSessionWebhook(sessionId, webhookUrl)
if (!result.success) {
return sendErrorResponse(res, 404, result.message)
}
/* #swagger.responses[200] = {
description: "Webhook URL updated.",
content: {
"application/json": {
schema: {
type: 'object',
properties: {
success: { type: 'boolean' },
message: { type: 'string' },
webhookUrl: { type: 'string' }
}
}
}
}
}
*/
res.json(result)
} catch (error) {
logger.error({ sessionId, err: error }, 'Failed to set session webhook')
sendErrorResponse(res, 500, error.message)
}
}
/**
* Get the current webhook URL for a session.
*
* @function
* @async
* @param {Object} req - The HTTP request object.
* @param {Object} res - The HTTP response object.
* @param {string} req.params.sessionId - The session ID.
* @returns {Promise<void>}
*/
const getWebhook = async (req, res) => {
// #swagger.summary = 'Get session webhook URL'
// #swagger.description = 'Get the current webhook URL for a session, including the source (runtime, env_session, env_global, or none).'
const sessionId = req.params.sessionId
try {
const result = getSessionWebhook(sessionId)
if (!result.success) {
return sendErrorResponse(res, 404, result.message)
}
/* #swagger.responses[200] = {
description: "Current webhook URL and source.",
content: {
"application/json": {
schema: {
type: 'object',
properties: {
success: { type: 'boolean' },
webhookUrl: { type: 'string' },
source: { type: 'string', enum: ['runtime', 'env_session', 'env_global', 'none'] }
}
}
}
}
}
*/
res.json(result)
} catch (error) {
logger.error({ sessionId, err: error }, 'Failed to get session webhook')
sendErrorResponse(res, 500, error.message)
}
}
/**
* Stops a session for the given session ID.
*
* @function
* @async
* @param {Object} req - The HTTP request object.
* @param {Object} res - The HTTP response object.
* @param {string} req.params.sessionId - The session ID to stop.
* @returns {Promise<void>}
* @throws {Error} If there was an error stopping the session.
*/
const stopSession = async (req, res) => {
// #swagger.summary = 'Stop session'
// #swagger.description = 'Stops a session for the given session ID.'
const sessionId = req.params.sessionId
try {
await destroySession(sessionId)
/* #swagger.responses[200] = {
description: "Status of the stopped session.",
content: {
"application/json": {
schema: { "$ref": "#/definitions/StopSessionResponse" }
}
}
}
*/
res.json({ success: true, message: 'Session stopped successfully' })
} catch (error) {
logger.error({ sessionId, err: error }, 'Failed to stop session')
sendErrorResponse(res, 500, error.message)
}
}
/**
* Status of the session with the given session ID.
*
* @function
* @async
* @param {Object} req - The HTTP request object.
* @param {Object} res - The HTTP response object.
* @param {string} req.params.sessionId - The session ID to start.
* @returns {Promise<void>}
* @throws {Error} If there was an error getting status of the session.
*/
const statusSession = async (req, res) => {
// #swagger.summary = 'Get session status'
// #swagger.description = 'Status of the session with the given session ID.'
const sessionId = req.params.sessionId
try {
const sessionData = await validateSession(sessionId)
/* #swagger.responses[200] = {
description: "Status of the session.",
content: {
"application/json": {
schema: { "$ref": "#/definitions/StatusSessionResponse" }
}
}
}
*/
res.json(sessionData)
} catch (error) {
logger.error({ sessionId, err: error }, 'Failed to get session status')
sendErrorResponse(res, 500, error.message)
}
}
/**
* QR code of the session with the given session ID.
*
* @function
* @async
* @param {Object} req - The HTTP request object.
* @param {Object} res - The HTTP response object.
* @param {string} req.params.sessionId - The session ID to start.
* @returns {Promise<void>}
* @throws {Error} If there was an error getting status of the session.
*/
const sessionQrCode = async (req, res) => {
// #swagger.summary = 'Get session QR code'
// #swagger.description = 'QR code of the session with the given session ID.'
const sessionId = req.params.sessionId
try {
const session = sessions.get(sessionId)
if (!session) {
return res.json({ success: false, message: 'session_not_found' })
}
if (session.qr) {
res.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate')
res.setHeader('Expires', 0)
return res.json({ success: true, qr: session.qr })
}
return res.json({ success: false, message: 'qr code not ready or already scanned' })
} catch (error) {
logger.error({ sessionId, err: error }, 'Failed to get session qr code')
sendErrorResponse(res, 500, error.message)
}
}
/**
* QR code as image of the session with the given session ID.
*
* @function
* @async
* @param {Object} req - The HTTP request object.
* @param {Object} res - The HTTP response object.
* @param {string} req.params.sessionId - The session ID to start.
* @returns {Promise<void>}
* @throws {Error} If there was an error getting status of the session.
*/
const sessionQrCodeImage = async (req, res) => {
// #swagger.summary = 'Get session QR code as image'
// #swagger.description = 'QR code as image of the session with the given session ID.'
const sessionId = req.params.sessionId
try {
const session = sessions.get(sessionId)
if (!session) {
return res.json({ success: false, message: 'session_not_found' })
}
if (session.qr) {
const qrImage = qr.image(session.qr)
/* #swagger.responses[200] = {
description: "QR image.",
content: {
"image/png": {}
}
}
*/
res.writeHead(200, {
'Cache-Control': 'no-cache, no-store, must-revalidate',
Expires: 0,
'Content-Type': 'image/png'
})
return qrImage.pipe(res)
}
return res.json({ success: false, message: 'qr code not ready or already scanned' })
} catch (error) {
logger.error({ sessionId, err: error }, 'Failed to get session qr code image')
sendErrorResponse(res, 500, error.message)
}
}
/**
* Restarts the session with the given session ID.
*
* @function
* @async
* @param {Object} req - The HTTP request object.
* @param {Object} res - The HTTP response object.
* @param {string} req.params.sessionId - The session ID to terminate.
* @returns {Promise<void>}
* @throws {Error} If there was an error terminating the session.
*/
const restartSession = async (req, res) => {
// #swagger.summary = 'Restart session'
// #swagger.description = 'Restarts the session with the given session ID.'
const sessionId = req.params.sessionId
try {
const validation = await validateSession(sessionId)
if (validation.message === 'session_not_found') {
return res.json(validation)
}
await reloadSession(sessionId)
/* #swagger.responses[200] = {
description: "Sessions restarted.",
content: {
"application/json": {
schema: { "$ref": "#/definitions/RestartSessionResponse" }
}
}
}
*/
res.json({ success: true, message: 'Restarted successfully' })
} catch (error) {
logger.error({ sessionId, err: error }, 'Failed to restart session')
sendErrorResponse(res, 500, error.message)
}
}
/**
* Terminates the session with the given session ID.
*
* @function
* @async
* @param {Object} req - The HTTP request object.
* @param {Object} res - The HTTP response object.
* @param {string} req.params.sessionId - The session ID to terminate.
* @returns {Promise<void>}
* @throws {Error} If there was an error terminating the session.
*/
const terminateSession = async (req, res) => {
// #swagger.summary = 'Terminate session'
// #swagger.description = 'Terminates the session with the given session ID.'
const sessionId = req.params.sessionId
try {
const validation = await validateSession(sessionId)
if (validation.message === 'session_not_found') {
return res.json(validation)
}
await deleteSession(sessionId, validation)
/* #swagger.responses[200] = {
description: "Sessions terminated.",
content: {
"application/json": {
schema: { "$ref": "#/definitions/TerminateSessionResponse" }
}
}
}
*/
res.json({ success: true, message: 'Logged out successfully' })
} catch (error) {
logger.error({ sessionId, err: error }, 'Failed to terminate session')
sendErrorResponse(res, 500, error.message)
}
}
/**
* Terminates all inactive sessions.
*
* @function
* @async
* @param {Object} req - The HTTP request object.
* @param {Object} res - The HTTP response object.
* @returns {Promise<void>}
* @throws {Error} If there was an error terminating the sessions.
*/
const terminateInactiveSessions = async (req, res) => {
// #swagger.summary = 'Terminate inactive sessions'
// #swagger.description = 'Terminates all inactive sessions.'
try {
await flushSessions(true)
/* #swagger.responses[200] = {
description: "Sessions terminated.",
content: {
"application/json": {
schema: { "$ref": "#/definitions/TerminateSessionsResponse" }
}
}
}
*/
res.json({ success: true, message: 'Flush completed successfully' })
} catch (error) {
logger.error(error, 'Failed to terminate inactive sessions')
sendErrorResponse(res, 500, error.message)
}
}
/**
* Terminates all sessions.
*
* @function
* @async
* @param {Object} req - The HTTP request object.
* @param {Object} res - The HTTP response object.
* @returns {Promise<void>}
* @throws {Error} If there was an error terminating the sessions.
*/
const terminateAllSessions = async (req, res) => {
// #swagger.summary = 'Terminate all sessions'
// #swagger.description = 'Terminates all sessions.'
try {
await flushSessions(false)
/* #swagger.responses[200] = {
description: "Sessions terminated.",
content: {
"application/json": {
schema: { "$ref": "#/definitions/TerminateSessionsResponse" }
}
}
}
*/
res.json({ success: true, message: 'Flush completed successfully' })
} catch (error) {
logger.error(error, 'Failed to terminate all sessions')
sendErrorResponse(res, 500, error.message)
}
}
/**
* Request authentication via pairing code instead of QR code.
*
* @async
* @function
* @param {Object} req - The HTTP request object containing the chatId and sessionId.
* @param {string} req.body.phoneNumber - The phone number in international, symbol-free format (e.g. 12025550108 for US, 551155501234 for Brazil).
* @param {boolean} req.body.showNotification - Show notification to pair on phone number.
* @param {string} req.params.sessionId - The unique identifier of the session associated with the client to use.
* @param {Object} res - The HTTP response object.
* @returns {Promise<Object>} - A Promise that resolves with a JSON object containing a success flag and the result of the operation.
* @throws {Error} - If an error occurs during the operation, it is thrown and handled by the catch block.
*/
const requestPairingCode = async (req, res) => {
/*
#swagger.summary = 'Request authentication via pairing code'
#swagger.requestBody = {
required: true,
schema: {
type: 'object',
properties: {
phoneNumber: {
type: 'string',
description: 'Phone number in international, symbol-free format',
example: '12025550108'
},
showNotification: {
type: 'boolean',
description: 'Show notification to pair on phone number',
example: true
},
}
},
}
*/
try {
const { phoneNumber, showNotification = true } = req.body
const client = sessions.get(req.params.sessionId)
if (!client) {
return res.json({ success: false, message: 'session_not_found' })
}
// hotfix https://github.com/pedroslopez/whatsapp-web.js/pull/3706
await exposeFunctionIfAbsent(client.pupPage, 'onCodeReceivedEvent', async (code) => {
client.emit('code', code)
return code
})
const result = await client.requestPairingCode(phoneNumber, showNotification)
res.json({ success: true, result })
} catch (error) {
sendErrorResponse(res, 500, error.message)
}
}
/**
* Get all sessions.
*
* @function
* @async
* @param {Object} req - The HTTP request object.
* @param {Object} res - The HTTP response object.
* @returns {<Object>}
*/
const getSessions = async (req, res) => {
// #swagger.summary = 'Get all sessions'
// #swagger.description = 'Get all sessions.'
/* #swagger.responses[200] = {
description: "Retrieved all sessions.",
content: {
"application/json": {
schema: { "$ref": "#/definitions/GetSessionsResponse" }
}
}
}
*/
return res.json({ success: true, result: Array.from(sessions.keys()) })
}
/**
* Get pupPage screenshot image
*
* @function
* @async
* @param {Object} req - The request object.
* @param {Object} res - The response object.
* @returns {Promise<Object>} - A Promise that resolves with a JSON object containing a success flag and the result of the operation.
* @throws {Error} If there is an issue setting the profile picture, an error will be thrown.
*/
const getPageScreenshot = async (req, res) => {
// #swagger.summary = 'Get page screenshot'
// #swagger.description = 'Screenshot of the client with the given session ID.'
const sessionId = req.params.sessionId
try {
const session = sessions.get(sessionId)
if (!session) {
return res.json({ success: false, message: 'session_not_found' })
}
if (!session.pupPage) {
return res.json({ success: false, message: 'page_not_ready' })
}
const pngBase64String = await session.pupPage.screenshot({
fullPage: true,
encoding: 'base64',
type: 'png'
})
/* #swagger.responses[200] = {
description: "Screenshot image.",
content: {
"image/png": {}
}
}
*/
res.writeHead(200, {
'Content-Type': 'image/png'
})
res.write(Buffer.from(pngBase64String, 'base64'))
res.end()
} catch (error) {
logger.error({ sessionId, err: error }, 'Failed to get page screenshot')
sendErrorResponse(res, 500, error.message)
}
}
module.exports = {
startSession,
stopSession,
statusSession,
sessionQrCode,
sessionQrCodeImage,
requestPairingCode,
restartSession,
terminateSession,
terminateInactiveSessions,
terminateAllSessions,
getSessions,
getPageScreenshot,
setWebhook,
getWebhook
}