This repository was archived by the owner on Dec 19, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 557
Expand file tree
/
Copy pathclientController.js
More file actions
1370 lines (1324 loc) · 42.2 KB
/
clientController.js
File metadata and controls
1370 lines (1324 loc) · 42.2 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
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const { MessageMedia, Location, Buttons, List, Poll } = require('whatsapp-web.js')
const { sessions } = require('../sessions')
const { sendErrorResponse } = require('../utils')
/**
* Send a message to a chat using the WhatsApp API
*
* @async
* @function sendMessage
* @param {Object} req - The request object containing the request parameters
* @param {Object} req.body - The request body containing the chatId, content, contentType and options
* @param {string} req.body.chatId - The chat id where the message will be sent
* @param {string|Object} req.body.content - The message content to be sent, can be a string or an object containing the MessageMedia data
* @param {string} req.body.contentType - The type of the message content, must be one of the following: 'string', 'MessageMedia', 'MessageMediaFromURL', 'Location', 'Buttons', or 'List'
* @param {Object} req.body.options - Additional options to be passed to the WhatsApp API
* @param {string} req.params.sessionId - The id of the WhatsApp session to be used
* @param {Object} res - The response object
* @returns {Object} - The response object containing a success flag and the sent message data
* @throws {Error} - If there is an error while sending the message
*/
const sendMessage = async (req, res) => {
/*
#swagger.requestBody = {
required: true,
'@content': {
"application/json": {
schema: {
type: 'object',
properties: {
chatId: {
type: 'string',
description: 'The Chat id which contains the message (Group or Individual)',
},
contentType: {
type: 'string',
description: 'The type of message content, must be one of the following: string, MessageMedia, MessageMediaFromURL, Location, Buttons, or List',
},
content: {
type: 'object',
description: 'The content of the message, can be a string or an object',
},
options: {
type: 'object',
description: 'The message send options',
}
}
},
examples: {
string: { value: { chatId: '6281288888888@c.us', contentType: 'string', content: 'Hello World!' } },
MessageMedia: { value: { chatId: '6281288888888@c.us', contentType: 'MessageMedia', content: { mimetype: 'image/jpeg', data: 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=', filename: 'image.jpg' } } },
MessageMediaFromURL: { value: { chatId: '6281288888888@c.us', contentType: 'MessageMediaFromURL', content: 'https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=Example' } },
Location: { value: { chatId: '6281288888888@c.us', contentType: 'Location', content: { latitude: -6.2, longitude: 106.8, description: 'Jakarta' } } },
Buttons: { value: { chatId: '6281288888888@c.us', contentType: 'Buttons', content: { body: 'Hello World!', buttons: [{ body: 'button 1' }], title: 'Hello World!', footer: 'Hello World!' } } },
List: {
value: { chatId: '6281288888888@c.us', contentType: 'List', content: { body: 'Hello World!', buttonText: 'Hello World!', sections: [{ title: 'sectionTitle', rows: [{ id: 'customId', title: 'ListItem2', description: 'desc' }, { title: 'ListItem2' }] }], title: 'Hello World!', footer: 'Hello World!' } }
},
Contact: {
value: { chatId: '6281288888888@c.us', contentType: 'Contact', content: { contactId: '6281288888889@c.us' } }
},
Poll: {
value: { chatId: '6281288888888@c.us', contentType: 'Poll', content: { pollName: 'Cats or Dogs?', pollOptions: ['Cats', 'Dogs'], options: { allowMultipleAnswers: true } } }
},
}
}
}
}
*/
try {
const { chatId, content, contentType, options } = req.body
const client = sessions.get(req.params.sessionId)
let messageOut
switch (contentType) {
case 'string':
if (options?.media) {
const media = options.media
media.filename = null
media.filesize = null
options.media = new MessageMedia(media.mimetype, media.data, media.filename, media.filesize)
}
messageOut = await client.sendMessage(chatId, content, options)
break
case 'MessageMediaFromURL': {
const messageMediaFromURL = await MessageMedia.fromUrl(content, { unsafeMime: true })
messageOut = await client.sendMessage(chatId, messageMediaFromURL, options)
break
}
case 'MessageMedia': {
const messageMedia = new MessageMedia(content.mimetype, content.data, content.filename, content.filesize)
messageOut = await client.sendMessage(chatId, messageMedia, options)
break
}
case 'Location': {
const location = new Location(content.latitude, content.longitude, content.description)
messageOut = await client.sendMessage(chatId, location, options)
break
}
case 'Buttons': {
const buttons = new Buttons(content.body, content.buttons, content.title, content.footer)
messageOut = await client.sendMessage(chatId, buttons, options)
break
}
case 'List': {
const list = new List(content.body, content.buttonText, content.sections, content.title, content.footer)
messageOut = await client.sendMessage(chatId, list, options)
break
}
case 'Contact': {
const contactId = content.contactId.endsWith('@c.us') ? content.contactId : `${content.contactId}@c.us`
const contact = await client.getContactById(contactId)
messageOut = await client.sendMessage(chatId, contact, options)
break
}
case 'Poll': {
const poll = new Poll(content.pollName, content.pollOptions, content.options)
messageOut = await client.sendMessage(chatId, poll, options)
break
}
default:
return sendErrorResponse(res, 404, 'contentType invalid, must be string, MessageMedia, MessageMediaFromURL, Location, Buttons, List, Contact or Poll')
}
res.json({ success: true, message: messageOut })
} catch (error) {
console.log(error)
sendErrorResponse(res, 500, error.message)
}
}
/**
* Get session information for a given sessionId
*
* @async
* @function getClientInfo
* @param {Object} req - Express request object
* @param {Object} res - Express response object
* @param {string} req.params.sessionId - The sessionId for which the session info is requested
* @returns {Object} - Response object with session info
* @throws Will throw an error if session info cannot be retrieved
*/
const getClassInfo = async (req, res) => {
try {
const client = sessions.get(req.params.sessionId)
const sessionInfo = await client.info
res.json({ success: true, sessionInfo })
} catch (error) {
sendErrorResponse(res, 500, error.message)
}
}
/**
* Check if a user is registered on WhatsApp
*
* @async
* @function isRegisteredUser
* @param {Object} req - Express request object
* @param {Object} res - Express response object
* @param {string} req.params.sessionId - The sessionId in which the user is registered
* @param {string} req.body.id - The id of the user to check
* @returns {Object} - Response object with a boolean indicating whether the user is registered
* @throws Will throw an error if user registration cannot be checked
*/
const isRegisteredUser = async (req, res) => {
/*
#swagger.requestBody = {
required: true,
schema: {
type: 'object',
properties: {
number: {
type: 'string',
description: 'The number or ID (\"@c.us\" will be automatically appended if not specified)',
example: '6281288888888'
},
}
},
}
*/
try {
const { number } = req.body
const client = sessions.get(req.params.sessionId)
const result = await client.isRegisteredUser(number)
res.json({ success: true, result })
} catch (error) {
sendErrorResponse(res, 500, error.message)
}
}
/**
* Retrieves the registered WhatsApp ID for a number
*
* @async
* @function getNumberId
* @param {Object} req - Express request object
* @param {Object} res - Express response object
* @param {string} req.params.sessionId - The sessionId in which the user is registered
* @param {string} req.body.id - The id of the user to check
* @returns {Object} - Response object with a boolean indicating whether the user is registered
* @throws Will throw an error if user registration cannot be checked
*/
const getNumberId = async (req, res) => {
/*
#swagger.requestBody = {
required: true,
schema: {
type: 'object',
properties: {
number: {
type: 'string',
description: 'The number or ID (\"@c.us\" will be automatically appended if not specified)',
example: '6281288888888'
},
}
},
}
*/
try {
const { number } = req.body
const client = sessions.get(req.params.sessionId)
const result = await client.getNumberId(number)
res.json({ success: true, result })
} catch (error) {
sendErrorResponse(res, 500, error.message)
}
}
/**
* Create a group with the given name and participants
*
* @async
* @function createGroup
* @param {Object} req - Express request object
* @param {Object} res - Express response object
* @param {string} req.params.sessionId - The sessionId in which to create the group
* @param {string} req.body.name - The name of the group to create
* @param {Array} req.body.participants - Array of user ids to add to the group
* @returns {Object} - Response object with information about the created group
* @throws Will throw an error if group cannot be created
*/
const createGroup = async (req, res) => {
try {
const { name, participants } = req.body
const client = sessions.get(req.params.sessionId)
const response = await client.createGroup(name, participants)
res.json({ success: true, response })
} catch (error) {
sendErrorResponse(res, 500, error.message)
}
}
/**
* Set the status of the user in a given session
*
* @async
* @function setStatus
* @param {Object} req - Express request object
* @param {Object} res - Express response object
* @param {string} req.params.sessionId - The sessionId in which to set the status
* @param {string} req.body.status - The status to set
* @returns {Object} - Response object indicating success
* @throws Will throw an error if status cannot be set
*/
const setStatus = async (req, res) => {
/*
#swagger.requestBody = {
required: true,
schema: {
type: 'object',
properties: {
status: {
type: 'string',
description: 'New status message',
example: 'I\'m running WhatsApp Web Api'
},
}
},
}
*/
try {
const { status } = req.body
const client = sessions.get(req.params.sessionId)
await client.setStatus(status)
res.json({ success: true })
} catch (error) {
sendErrorResponse(res, 500, error.message)
}
}
/**
* Retrieves the contacts of the current session.
* @async
* @function
* @param {Object} req - The request object.
* @param {string} req.params.sessionId - The session ID associated with the client.
* @param {Object} res - The response object.
* @returns {Promise<void>} - A Promise that resolves with the retrieved contacts or rejects with an error.
*/
const getContacts = async (req, res) => {
try {
const client = sessions.get(req.params.sessionId)
const contacts = await client.getContacts()
res.json({ success: true, contacts })
} catch (error) {
sendErrorResponse(res, 500, error.message)
}
}
/**
* Retrieve all chats for the given session ID.
*
* @function
* @async
*
* @param {Object} req - The request object.
* @param {string} req.params.sessionId - The session ID.
* @param {Object} res - The response object.
*
* @returns {Promise<void>} A Promise that resolves when the operation is complete.
*
* @throws {Error} If the operation fails, an error is thrown.
*/
const getChats = async (req, res) => {
try {
const client = sessions.get(req.params.sessionId)
const chats = await client.getChats()
res.json({ success: true, chats })
} catch (error) {
sendErrorResponse(res, 500, error.message)
}
}
/**
* Returns the profile picture URL for a given contact ID.
*
* @async
* @function
* @param {Object} req - Express request object.
* @param {Object} res - Express response object.
* @param {string} req.params.sessionId - The ID of the current session.
* @param {string} req.body.contactId - The ID of the contact to get the profile picture for.
* @returns {Promise<void>} - A Promise that resolves with the profile picture URL.
* @throws {Error} - If there is an error retrieving the profile picture URL.
*/
const getProfilePictureUrl = async (req, res) => {
/*
#swagger.requestBody = {
required: true,
schema: {
type: 'object',
properties: {
contactId: {
type: 'string',
description: 'The contact ID\'s of profile',
example: '6281288888888@c.us'
},
}
},
}
*/
try {
const { contactId } = req.body
const client = sessions.get(req.params.sessionId)
const result = await client.getProfilePicUrl(contactId)
res.json({ success: true, result })
} catch (error) {
sendErrorResponse(res, 500, error.message)
}
}
/**
* Accepts an invite.
*
* @async
* @function
* @param {Object} req - The HTTP request object.
* @param {Object} req.body - The request body.
* @param {Object} req.params - The request parameters.
* @param {string} req.params.sessionId - The ID of the session.
* @param {Object} res - The HTTP response object.
* @returns {Object} The response object.
* @throws {Error} If there is an error while accepting the invite.
*/
const acceptInvite = async (req, res) => {
/*
#swagger.requestBody = {
required: true,
schema: {
type: 'object',
properties: {
inviteCode: {
type: 'string',
description: 'Invitation code',
example: ''
},
}
},
}
*/
try {
const { inviteCode } = req.body
const client = sessions.get(req.params.sessionId)
const acceptInvite = await client.acceptInvite(inviteCode)
res.json({ success: true, acceptInvite })
} catch (error) {
sendErrorResponse(res, 500, error.message)
}
}
/**
* Retrieves the version of WhatsApp Web currently being run.
*
* @async
* @function getWWebVersion
* @param {Object} req - The HTTP request object.
* @param {Object} req.params - The request parameters.
* @param {string} req.params.sessionId - The ID of the session.
* @param {Object} res - The HTTP response object.
* @returns {Object} The response object.
* @throws {Error} If there is an error while accepting the invite.
*/
const getWWebVersion = async (req, res) => {
try {
const client = sessions.get(req.params.sessionId)
const result = await client.getWWebVersion()
res.json({ success: true, result })
} catch (error) {
sendErrorResponse(res, 500, error.message)
}
}
/**
* Archives a chat.
*
* @async
* @function
* @param {Object} req - The HTTP request object.
* @param {Object} req.body - The request body.
* @param {Object} req.params - The request parameters.
* @param {string} req.params.sessionId - The ID of the session.
* @param {Object} res - The HTTP response object.
* @returns {Object} The response object.
* @throws {Error} If there is an error while archiving the chat.
*/
const archiveChat = async (req, res) => {
/*
#swagger.requestBody = {
required: true,
schema: {
type: 'object',
properties: {
chatId: {
type: 'string',
description: 'ID of the chat',
example: ''
},
}
},
}
*/
try {
const { chatId } = req.body
const client = sessions.get(req.params.sessionId)
const result = await client.archiveChat(chatId)
res.json({ success: true, result })
} catch (error) {
sendErrorResponse(res, 500, error.message)
}
}
/**
* Get the list of blocked contacts for the user's client.
*
* @async
* @function getBlockedContacts
* @param {Object} req - The request object.
* @param {string} req.params.sessionId - The session ID to use for the client.
* @param {Object} res - The response object.
* @returns {Promise<Object>} - A promise that resolves to an object with a success property and an array of blocked contacts.
* @throws {Error} - Throws an error if the operation fails.
*/
const getBlockedContacts = async (req, res) => {
try {
const client = sessions.get(req.params.sessionId)
const blockedContacts = await client.getBlockedContacts()
res.json({ success: true, blockedContacts })
} catch (error) {
sendErrorResponse(res, 500, error.message)
}
}
/**
* Get the chat with the given ID.
*
* @async
* @function getChatById
* @param {Object} req - The request object.
* @param {string} req.params.sessionId - The session ID to use for the client.
* @param {string} req.body.chatId - The ID of the chat to get.
* @param {Object} res - The response object.
* @returns {Promise<Object>} - A promise that resolves to an object with a success property and the chat object.
* @throws {Error} - Throws an error if the operation fails.
*/
const getChatById = async (req, res) => {
/*
#swagger.requestBody = {
required: true,
schema: {
type: 'object',
properties: {
chatId: {
type: 'string',
description: 'ID of the chat',
example: ''
},
}
},
}
*/
try {
const { chatId } = req.body
const client = sessions.get(req.params.sessionId)
const chat = await client.getChatById(chatId)
res.json({ success: true, chat })
} catch (error) {
sendErrorResponse(res, 500, error.message)
}
}
/**
* Get the labels for the chat with the given ID.
*
* @async
* @function getChatLabels
* @param {Object} req - The request object.
* @param {string} req.params.sessionId - The session ID to use for the client.
* @param {string} req.body.chatId - The ID of the chat to get labels for.
* @param {Object} res - The response object.
* @returns {Promise<Object>} - A promise that resolves to an object with a success property and an array of labels for the chat.
* @throws {Error} - Throws an error if the operation fails.
*/
const getChatLabels = async (req, res) => {
/*
#swagger.requestBody = {
required: true,
schema: {
type: 'object',
properties: {
chatId: {
type: 'string',
description: 'ID of the chat',
example: ''
},
}
},
}
*/
try {
const { chatId } = req.body
const client = sessions.get(req.params.sessionId)
const chatLabels = await client.getChatLabels(chatId)
res.json({ success: true, chatLabels })
} catch (error) {
sendErrorResponse(res, 500, error.message)
}
}
/**
* Get the chats with the given label ID.
*
* @async
* @function getChatsByLabelId
* @param {Object} req - The request object.
* @param {string} req.params.sessionId - The session ID to use for the client.
* @param {string} req.body.labelId - The ID of the label to get chats for.
* @param {Object} res - The response object.
* @returns {Promise<Object>} - A promise that resolves to an object with a success property and an array of chats with the given label.
* @throws {Error} - Throws an error if the operation fails.
*/
const getChatsByLabelId = async (req, res) => {
/*
#swagger.requestBody = {
required: true,
schema: {
type: 'object',
properties: {
labelId: {
type: 'string',
description: 'ID of the label',
example: ''
},
}
},
}
*/
try {
const { labelId } = req.body
const client = sessions.get(req.params.sessionId)
const chats = await client.getChatsByLabelId(labelId)
res.json({ success: true, chats })
} catch (error) {
sendErrorResponse(res, 500, error.message)
}
}
/**
* Retrieves the common groups between the client's session and the specified contact.
* @async
* @function getCommonGroups
* @param {Object} req - The request object.
* @param {string} req.params.sessionId - The session ID of the client.
* @param {string} req.body.contactId - The ID of the contact to retrieve the common groups with.
* @param {Object} res - The response object.
* @returns {Object} - An object containing a success flag and the retrieved groups.
* @throws {Error} - If an error occurs while retrieving the common groups.
*/
const getCommonGroups = async (req, res) => {
/*
#swagger.requestBody = {
required: true,
schema: {
type: 'object',
properties: {
contactId: {
type: 'string',
description: 'The whatsapp user\'s ID (_serialized format)',
example: ''
},
}
},
}
*/
try {
const { contactId } = req.body
const client = sessions.get(req.params.sessionId)
const groups = await client.getCommonGroups(contactId)
res.json({ success: true, groups })
} catch (error) {
sendErrorResponse(res, 500, error.message)
}
}
/**
* Retrieves the contact with the specified ID.
* @async
* @function getContactById
* @param {Object} req - The request object.
* @param {string} req.params.sessionId - The session ID of the client.
* @param {string} req.body.contactId - The ID of the contact to retrieve.
* @param {Object} res - The response object.
* @returns {Object} - An object containing a success flag and the retrieved contact.
* @throws {Error} - If an error occurs while retrieving the contact.
*/
const getContactById = async (req, res) => {
/*
#swagger.requestBody = {
required: true,
schema: {
type: 'object',
properties: {
contactId: {
type: 'string',
description: 'The whatsapp user\'s ID',
example: ''
},
}
},
}
*/
try {
const { contactId } = req.body
const client = sessions.get(req.params.sessionId)
const contact = await client.getContactById(contactId)
res.json({ success: true, contact })
} catch (error) {
sendErrorResponse(res, 500, error.message)
}
}
/**
* Retrieves the invite information for the specified invite code.
* @async
* @function getInviteInfo
* @param {Object} req - The request object.
* @param {string} req.params.sessionId - The session ID of the client.
* @param {string} req.body.inviteCode - The invite code to retrieve information for.
* @param {Object} res - The response object.
* @returns {Object} - An object containing a success flag and the retrieved invite information.
* @throws {Error} - If an error occurs while retrieving the invite information.
*/
const getInviteInfo = async (req, res) => {
/*
#swagger.requestBody = {
required: true,
schema: {
type: 'object',
properties: {
inviteCode: {
type: 'string',
description: 'Invitation code',
example: ''
},
}
},
}
*/
try {
const { inviteCode } = req.body
const client = sessions.get(req.params.sessionId)
const inviteInfo = await client.getInviteInfo(inviteCode)
res.json({ success: true, inviteInfo })
} catch (error) {
sendErrorResponse(res, 500, error.message)
}
}
/**
* Retrieves the label with the given ID for a particular session.
* @async
* @function
* @param {Object} req - The request object.
* @param {string} req.params.sessionId - The ID of the session to retrieve the label for.
* @param {Object} req.body - The request body object.
* @param {string} req.body.labelId - The ID of the label to retrieve.
* @param {Object} res - The response object.
* @returns {Promise<void>}
* @throws {Error} If there is an error retrieving the label.
*/
const getLabelById = async (req, res) => {
/*
#swagger.requestBody = {
required: true,
schema: {
type: 'object',
properties: {
labelId: {
type: 'string',
description: 'ID of the label',
example: ''
},
}
},
}
*/
try {
const { labelId } = req.body
const client = sessions.get(req.params.sessionId)
const label = await client.getLabelById(labelId)
res.json({ success: true, label })
} catch (error) {
sendErrorResponse(res, 500, error.message)
}
}
/**
* Retrieves all labels for a particular session.
* @async
* @function
* @param {Object} req - The request object.
* @param {string} req.params.sessionId - The ID of the session to retrieve the labels for.
* @param {Object} res - The response object.
* @returns {Promise<void>}
* @throws {Error} If there is an error retrieving the labels.
*/
const getLabels = async (req, res) => {
try {
const client = sessions.get(req.params.sessionId)
const labels = await client.getLabels()
res.json({ success: true, labels })
} catch (error) {
sendErrorResponse(res, 500, error.message)
}
}
/**
* update the profile Picture of the session user
* @param {Object} req - The request object.
* @param {Object} res - The response object.
* @throws {Error} If there is an issue setting the profile picture, an error will be thrown.
*/
const getScreenshotImage = async (req, res) => {
// #swagger.summary = 'Get client screenshot image'
// #swagger.description = 'Screenshot of the client with the given session ID.'
try {
const sessionId = req.params.sessionId
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 imgBase64Buffer = await session.pupPage.screenshot({
encoding: 'base64',
type: 'png'
});
const img = Buffer.from(imgBase64Buffer, 'base64');
/* #swagger.responses[200] = {
description: "Screenshot image.",
content: {
"image/png": {}
}
}
*/
res.writeHead(200, {
'Content-Type': 'image/png',
'Content-Length': img.length
});
return res.end(img);
} catch (error) {
console.log('getScreenshotImage ERROR', error)
/* #swagger.responses[500] = {
description: "Server Failure.",
content: {
"application/json": {
schema: { "$ref": "#/definitions/ErrorResponse" }
}
}
}
*/
sendErrorResponse(res, 500, error.message)
}
}
/**
* Adds or removes labels to/from chats.
* @async
* @function
* @param {Object} req - the request object
* @param {Object} res - the response object
* @return {Promise} a Promise that resolves to the JSON response with success status and labels
* @throws {Error} if an error occurs
*/
const addOrRemoveLabels = async (req, res) => {
/*
#swagger.requestBody = {
required: true,
schema: {
type: 'object',
properties: {
labelIds: {
type: 'array',
description: 'Array of label IDs',
example: []
},
chatIds: {
type: 'array',
description: 'Array of chat IDs',
example: []
},
}
},
}
*/
try {
const { labelIds, chatIds } = req.body
const client = sessions.get(req.params.sessionId)
const labels = await client.addOrRemoveLabels(labelIds, chatIds)
res.json({ success: true, labels })
} catch (error) {
sendErrorResponse(res, 500, error.message)
}
}
/**
* Retrieves the state for a particular session.
* @async
* @function
* @param {Object} req - The request object.
* @param {string} req.params.sessionId - The ID of the session to retrieve the state for.
* @param {Object} res - The response object.
* @returns {Promise<void>}
* @throws {Error} If there is an error retrieving the state.
*/
const getState = async (req, res) => {
try {
const client = sessions.get(req.params.sessionId)
const state = await client.getState()
res.json({ success: true, state })
} catch (error) {
sendErrorResponse(res, 500, error.message)
}
}
/**
* Marks a chat as unread.
*
* @async
* @function markChatUnread
* @param {Object} req - The request object.
* @param {Object} res - The response object.
* @param {string} req.params.sessionId - The session ID.
* @param {string} req.body.chatId - The ID of the chat to mark as unread.
* @returns {Promise<void>} - A Promise that resolves when the chat is marked as unread.
* @throws {Error} - If an error occurs while marking the chat as unread.
*/
const markChatUnread = async (req, res) => {
/*
#swagger.requestBody = {
required: true,
schema: {
type: 'object',
properties: {
chatId: {
type: 'string',
description: 'ID of the chat',
example: ''
},
}
},
}
*/
try {
const { chatId } = req.body
const client = sessions.get(req.params.sessionId)
const mark = await client.markChatUnread(chatId)
res.json({ success: true, mark })
} catch (error) {
sendErrorResponse(res, 500, error.message)
}
}
/**
* Mutes a chat.
*
* @async
* @function muteChat
* @param {Object} req - The request object.
* @param {Object} res - The response object.
* @param {string} req.params.sessionId - The session ID.
* @param {string} req.body.chatId - The ID of the chat to mute.
* @param {Date} [req.body.unmuteDate] - The date and time when the chat should be unmuted. If not provided, the chat will be muted indefinitely.
* @returns {Promise<void>} - A Promise that resolves when the chat is muted.
* @throws {Error} - If an error occurs while muting the chat.
*/
const muteChat = async (req, res) => {
/*
#swagger.requestBody = {
required: true,
schema: {
type: 'object',
properties: {
chatId: {
type: 'string',
description: 'ID of the chat',
example: ''
},
unmuteDate: {
type: 'string',
description: 'Date when the chat will be muted, leave as is to mute forever',
example: ''
},
}
},
}
*/
try {
const { chatId, unmuteDate } = req.body
const client = sessions.get(req.params.sessionId)
let mute
if (unmuteDate) {
mute = await client.muteChat(chatId, new Date(unmuteDate))
} else {
mute = await client.muteChat(chatId, null)
}
res.json({ success: true, mute })
} catch (error) {
sendErrorResponse(res, 500, error.message)
}
}
/**
* Pins a chat.
*
* @async
* @function pinChat
* @param {Object} req - The request object.
* @param {Object} res - The response object.
* @param {string} req.params.sessionId - The session ID.
* @param {string} req.body.chatId - The ID of the chat to pin.
* @returns {Promise<void>} - A Promise that resolves when the chat is pinned.
* @throws {Error} - If an error occurs while pinning the chat.
*/
const pinChat = async (req, res) => {
/*
#swagger.requestBody = {
required: true,
schema: {
type: 'object',
properties: {
chatId: {
type: 'string',
description: 'ID of the chat',
example: ''
},