/
browserExampleMethods.html
1097 lines (1014 loc) · 48.6 KB
/
browserExampleMethods.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN"
crossorigin="anonymous">
<script defer type="text/javascript" src="https://unpkg.com/@green-api/whatsapp-api-client/lib/whatsapp-api-client.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<title>API HTML5</title>
<style>
.button {
border: none;
color: white;
padding: 16px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
transition-duration: 0.4s;
cursor: pointer;
}
.buttonGreen {
font-size: 16px;
background-color: hsla(122, 39%, 49%, 1);
color: #fff;
border: 2px solid #4CAF50;
border-radius: 8px;
box-shadow: 0 0 4px #999;
}
.buttonGreen:hover {
background-color: #4CAF50;
color: white;
}
span {
font-size: 12px;
}
.form-wrapper {
display: flex;
padding: 10px 10px;
margin-bottom: 10px;
border-bottom: 1px solid #cacaca;
gap: 50px;
}
.date-container {
display: flex;
flex-direction: column;
justify-content: center;
gap: 5px;
}
input {
min-width: 300px;
}
</style>
</head>
<body>
<div class="container">
<h1>WhatsApp client browser example</h1>
<!-- Instance Date
To send a message or to execute some other Green-API method,
you have to have the WhatsApp account in the phone application
to be authorized. To authorize your account please go to the personal
area and scan a QR-code using the WhatsApp application.
https://console.green-api.com/instanceList/
-->
<form class="form-wrapper">
<div class="date-container">
<h2 class="section-title">Instance data</h2>
<label for="idInstance">Id Instance:</label>
<input required type="text" id="idInstance" name="idInstance">
<label for="apiTokenIsntance">API Token:</label>
<input required type="text" id="apiTokenIsntance" name="apiTokenIsntance" placeholder="c40205cffda74409be1f66819b47116...">
<a href="https://console.green-api.com/instanceList">To authorize your account please go to the personal area and scan a QR-code using the WhatsApp application</a>
</div>
</form>
<!-- Send message
You can read more about this metods here
https://green-api.com/en/docs/api/sending/SendMessage/
-->
<form class="form-wrapper">
<div class="date-container">
<h2 class="section-title">Send Message</h2>
<label for="phoneNumber">Phone Number:</label>
<input required type="tel" id="phoneNumber" name="phoneNumber" value="" placeholder="77076665544">
<span>if you sending on mobile phone use @c.us or if you sending in group @g.us</span>
<label for="messageText">Message:</label>
<input required type="text" id="messageText" name="messageText" value="Hello world!">
<a href="https://green-api.com/en/docs/api/sending/SendMessage/"> You can read more about this metods here</a>
</div>
<div class="date-container">
<button id="SendWhatsAppBtn" type="button" class="button buttonGreen">Send Message</button>
</div>
<div class="date-container"><p id="result-text-sendMessage"></p></div>
</form>
<!-- Send file from local
You can read more about this metods here
https://green-api.com/en/docs/api/sending/UploadFile/
-->
<form class="form-wrapper" id="upload-form">
<div class="date-container">
<h2 class="section-title">Upload file, method not work</h2>
<label for="file-input">Choose a file to upload:</label>
<input type="file" id="file-input" name="file"/>
<label for="phoneNumber">Phone Number:</label>
<input required type="tel" id="phoneNumber-upload" name="phoneNumber" value="" placeholder="77076665544">
<label for="messageText">Message:</label>
<input required type="text" id="messageText-upload" name="messageText" value="Hello world!">
<a href="https://green-api.com/en/docs/api/sending/UploadFile/"> You can read more about this metods here</a> </div>
<div class="date-container">
<button id="SendFileWhatsAppBtn" type="button" class="button buttonGreen">Send File</button>
</div>
<div class="date-container"><p id="result-text-uploadFile"></p></div>
</form>
<!-- Send file by URL
You can read more about this metods here
https://green-api.com/en/docs/api/sending/SendFileByUrl/
-->
<form class="form-wrapper" id="sendFileByUrl">
<div class="date-container">
<h2 class="section-title">Upload file by url</h2>
<label for="fileByUrl">Past your link to file in to input</label>
<input type="text" id="fileByUrl" name="fileUrl" placeholder="Write your URL">
<input type="text" id="fileName" name="fileExtention" placeholder="Write your file name and file extention">
<label for="phoneNumber">Phone Number:</label>
<input required type="tel" id="phoneNumber-uploadByUrl" name="phoneNumber" value="" placeholder="77076665544">
<label for="messageText">Message:</label>
<input required type="text" id="messageText-uploadByUrl" name="messageText" value="Hello world!">
<a href="https://green-api.com/en/docs/api/sending/SendFileByUrl/"> You can read more about this metods here</a>
</div>
<div class="date-container">
<button id="SendFileByUrlWhatsAppBtn" type="button" class="button buttonGreen">Send File by URL</button>
</div>
<div class="date-container"><p id="result-text-sendFileByUrl"></p></div>
</form>
<!-- Send file by Upload Not working!
You can read more about this metods here
https://green-api.com/en/docs/api/sending/SendFileByUpload/
-->
<form class="form-wrapper" id="sendFileByUpload">
<div class="date-container">
<h2 class="section-title">Upload file by upload</h2>
<label for="file-input">Enter the file path:</label>
<input type="text" id="file-input-upload" name="filePath-upload" placeholder="C://ProgramFiles/{file}"/>
<a href="https://green-api.com/en/docs/api/sending/SendFileByUrl/"> You can read more about this metods here</a>
</div>
<div class="date-container">
<button id="SendFileByUploadWhatsAppBtn" type="button" class="button buttonGreen">Send File by upload</button>
</div>
<div class="date-container"><p id="result-text-sendFileByUpload"></p></div>
</form>
<!-- Create group
You can read more about this metods here
https://green-api.com/en/docs/api/groups/CreateGroup/
-->
<form class="form-wrapper" id="createGroup">
<div class="date-container">
<h2 class="section-title">Create group</h2>
<label for="fileByUrl">Write name of your group</label>
<input type="text" id="groupName" name="groupName" placeholder="Write your group name">
<input type="text" id="chatIds" name="chatIds" placeholder="Enter group member numbers ">
<button id="chatPlus" type="button" class="btn buttonGreen btn-sm">Добавить чат</button>
<a href="https://green-api.com/en/docs/api/groups/CreateGroup/"> You can read more about this metods here</a>
</div>
<div class="date-container">
<button id="create-group" type="button" class="button buttonGreen">Create Group</button>
</div>
<div class="date-container"><p id="result-text-createGroup"></p></div>
</form>
<!-- Update group name
You can read more about this metods here
https://green-api.com/en/docs/api/groups/UpdateGroupName/
-->
<form class="form-wrapper" id="UpdateGroupName">
<div class="date-container">
<h2 class="section-title">Update group name</h2>
<label for="groupName">Write name of your group</label>
<input type="text" id="groupName-upadteName" name="groupName" placeholder="Write your group name">
<input type="text" id="groupId-updateName" name="chatId" placeholder="Enter group id ">
<a href="https://green-api.com/en/docs/api/groups/UpdateGroupName/"> You can read more about this metods here</a>
</div>
<div class="date-container">
<button id="updateGroupName" type="button" class="button buttonGreen">Update group name</button>
</div>
<div class="date-container"><p id="result-text-UpdateGroupName"></p></div>
</form>
<!-- Get Group Data method
You can read more about this metods here
https://green-api.com/en/docs/api/groups/GetGroupData/
-->
<form class="form-wrapper" id="GetGroupData">
<div class="date-container">
<h2 class="section-title">Get group data</h2>
<label for="groupName">Write id of your group</label>
<input type="text" id="groupId-GetGroupData" name="groupName" placeholder="Write your group id">
<a href="https://green-api.com/en/docs/api/groups/GetGroupData/"> You can read more about this metods here</a>
</div>
<div class="date-container">
<button id="getGroupData" type="button" class="button buttonGreen">Get group data</button>
</div>
<div class="date-container"><p id="result-text-GetGroupData"></p></div>
</form>
<!-- Set group admin method
You can read more about this metods here
https://green-api.com/en/docs/api/groups/SetGroupAdmin/
-->
<form class="form-wrapper" id="SetGroupAdmin">
<div class="date-container">
<h2 class="section-title">Set Group Admin</h2>
<label for="groupName">Write id of your group</label>
<input type="text" id="groupId-setAdmin" name="groupName" placeholder="Write your group id">
<input type="text" id="chatId-setAdmin" name="groupName" placeholder="Write your group id">
<a href="https://green-api.com/en/docs/api/groups/SetGroupAdmin/"> You can read more about this metods here</a>
</div>
<div class="date-container">
<button id="setGroupAdmin" type="button" class="button buttonGreen">Set Group Admin</button>
</div>
<div class="date-container"><p id="result-text-setGroupAdmin"></p></div>
</form>
<!-- Add Group Participant method
You can read more about this metods here
https://green-api.com/en/docs/api/groups/AddGroupParticipant/
-->
<form class="form-wrapper" id="AddGroupParticipant">
<div class="date-container">
<h2 class="section-title">Add group participant</h2>
<input type="text" id="groupId-AddGroupParticipant" name="groupName" placeholder="Write your group id">
<input type="text" id="participantChatId-AddGroupParticipant" name="participantChatId" placeholder="Write your participant id">
<a href="https://green-api.com/en/docs/api/groups/AddGroupParticipant/"> You can read more about this metods here</a>
</div>
<div class="date-container">
<button id="addGroupParticipant" type="button" class="button buttonGreen">Add group participant</button>
</div>
<div class="date-container"><p id="result-text-AddGroupParticipant"></p></div>
</form>
<!-- Leave group method
You can read more about this metods here
https://green-api.com/en/docs/api/groups/leaveGroup/
-->
<form class="form-wrapper" id="leaveGroup">
<div class="date-container">
<h2 class="section-title"> Leave group</h2>
<input type="text" id="groupId-leaveGroup" name="groupName" placeholder="Write your group id">
<a href="https://green-api.com/en/docs/api/groups/leaveGroup/"> You can read more about this metods here</a>
</div>
<div class="date-container">
<button id="addLeaveGroup" type="button" class="button buttonGreen"> Leave group</button>
</div>
<div class="date-container"><p id="result-text-leaveGroup"></p></div>
</form>
<!--log section
You can read more about this metods here
https://green-api.com/en/docs/api/journals/GetChatHistory/ --About chat history
https://green-api.com/en/docs/api/journals/GetMessage/ --About message in chat
-->
<form class="form-wrapper" id="log" style="flex-wrap: wrap;">
<div class="date-container">
<h2 class="section-title">Information output</h2>
<input type="text" id="chatId-chatHistory" placeholder="77059998882@c.ca">
</div>
<div class="date-container">
<button id="GetChatHistory" type="button" class="button buttonGreen">GetChatHistory</button>
</div>
<div class="date-container-down">
<div id="result-log"></div>
</div>
</form>
<form class="form-wrapper" id="getMessage" style="flex-wrap: wrap;">
<div class="date-container">
<h2 class="section-title">Information output</h2>
<input type="text" id="chatId-getMessage" placeholder="77059998882@c.ca">
<input type="text" id="messageId-getMessage" placeholder="BAC23ZXC3R2">
</div>
<div class="date-container">
<button id="GetMessage" type="button" class="button buttonGreen">Get message</button>
</div>
<div class="date-container-down">
<div id="result-getMessage"></div>
</div>
</form>
<!--queue section
You can read more about this metods here
https://green-api.com/en/docs/api/journals/ShowMessagesQueue/ --About Show Messages Queue
https://green-api.com/en/docs/api/account/GetSettings/ --About Clear Messages Queue
-->
<form class="form-wrapper" id="queue" style="flex-wrap: wrap;">
<div class="date-container">
<h2 class="section-title">Information output</h2>
</div>
<div class="date-container">
<button id="ShowMessageQueue" type="button" class="button buttonGreen">Show Messages Queue</button>
<button id="ClearMessageQueue" type="button" class="button buttonGreen">Show Messages Queue</button>
</div>
<div class="date-container-down">
<div id="result-queue"></div>
</div>
</form>
<!-- Check WhatsApp method
You can read more about this metods here
https://green-api.com/en/docs/api/groups/checkWhatsapp/
-->
<form class="form-wrapper" id="CheckWhatsapp">
<div class="date-container">
<h2 class="section-title"> Check WhatsApp</h2>
<input type="text" id="chatNumber-checkWhatsapp" name="chatNumber" placeholder="Write your chat id">
<a href="https://green-api.com/en/docs/api/groups/checkWhatsapp/"> You can read more about this metods here</a>
</div>
<div class="date-container">
<button id="checkWhatsapp" type="button" class="button buttonGreen"> Check WhatsApp</button>
</div>
<div class="date-container"><p id="result-text-checkWhatsapp"></p></div>
</form>
<!-- Get avatar method
You can read more about this metods here
https://green-api.com/en/docs/api/groups/GetAvatar/
-->
<form class="form-wrapper" id="GetAvatar">
<div class="date-container">
<h2 class="section-title"> Get avatar</h2>
<input type="text" id="chatNumber-GetAvatar" name="chatNumber" placeholder="Write your chat id">
<a href="https://green-api.com/en/docs/api/groups/GetAvatar/"> You can read more about this metods here</a>
</div>
<div class="date-container">
<button id="getAvatar" type="button" class="button buttonGreen"> Get avatar</button>
</div>
<div class="date-container"><p id="result-text-GetAvatar"></p></div>
</form>
<!-- Get Contacts method
You can read more about this metods here
https://green-api.com/en/docs/api/groups/GetContacts/
-->
<form class="form-wrapper" id="GetContacts">
<div class="date-container">
<h2 class="section-title"> Get Contacts</h2>
<a href="https://green-api.com/en/docs/api/groups/GetContacts/"> You can read more about this metods here</a>
</div>
<div class="date-container">
<button id="getContacts" type="button" class="button buttonGreen"> Get Contacts</button>
</div>
<div class="date-container" id="result-text-GetContacts"></div>
</form>
<!-- Instance and account section
You can read more about this metods here
https://green-api.com/en/docs/api/account/QR/ --About QR code
https://green-api.com/en/docs/api/account/GetSettings/ --About acount settings
https://green-api.com/en/docs/api/account/GetStateInstance/ --About acount state
-->
<form class="form-wrapper" id="info">
<div class="date-container">
<h2 class="section-title">Information output</h2>
<p id="result-text"></p>
<div id="reult"></div>
<p>To get API Token and ID Instance please visit <a href="https://green-api.com/#section-connect">green-api.com</a></p>
<p>Press Send Message button to dispatch hello world to phone</p>
<p style="color:blue" id="resultText"></p>
</div>
<div class="date-container">
<button id="ReceiveWhatsAppBtn" type="button" class="button buttonGreen">Receive message</button>
<button id="recive-qr" type="button" class="button buttonGreen">Recive QR</button>
<button id="getSettings" type="button" class="button buttonGreen">Get settings</button>
<button id="getStateInstance" type="button" class="button buttonGreen">Get State Instance</button>
</div>
<div class="date-container">
<button id="reboot" type="button" class="button buttonGreen">Reboot instance</button>
<button id="getWaSettings" type="button" class="button buttonGreen">Get WhatsApp settings</button>
</div>
</form>
</div>
<script>
// Here is the code for processing data and subsequent sending
const link = 'https://api.green-api.com/waInstance'; //string
const chatIds = []; //array
// Send message method
document.getElementById("SendWhatsAppBtn").addEventListener("click", function () {
/**
* @param {String} apiTokenInstance
* @param {String} idInstance
* @param {String} chatId
* @param {String} message
*/
const apiTokenInstance = document.getElementById("apiTokenIsntance").value;
const idInstance = document.getElementById("idInstance").value;
const chatId = `${document.getElementById("phoneNumber").value}`;
const message = document.getElementById("messageText").value;
const resultText = document.querySelector("#result-text-sendMessage");
const restAPI = whatsAppClient.restAPI({
idInstance: idInstance,
apiTokenInstance: apiTokenInstance
});
restAPI.message.sendMessage(chatId, null, message)
.then((data) => {
console.log(`Message send with ID ${data.idMessage}`);
resultText.textContent = ''
resultText.textContent = `Message send with ID ${data.idMessage}`;
})
.catch((error) => {
console.error("There was a problem sending message:", error);
resultText.textContent = ''
resultText.textContent = `There was a problem sending message: ${error}`;
});
});
// Send receive notification method
document.getElementById("ReceiveWhatsAppBtn").addEventListener("click", function () {
/**
* @param {String} apiTokenInstance
* @param {String} idInstance
*/
try {
const restAPI = whatsAppClient.restAPI(({
idInstance: document.getElementById("idInstance").value,
apiTokenInstance: document.getElementById("apiTokenIsntance").value
}))
restAPI.webhookService.receiveNotification()
.then((data) => {
console.log(data);
if (data.receiptId) {
restAPI.webhookService.deleteNotification(data.receiptId)
.then(() => {
document.getElementById("resultText").innerHTML = "Message was received successfully. Message text: " + JSON.stringify(data.body);
}).catch(reason => {
console.error(reason);
document.getElementById("resultText").innerHTML = "Message was not confirmed. Reason: " + reason + ". See logs for details";
})
}
}).catch((reason) =>{
console.error(reason);
document.getElementById("resultText").innerHTML = "Message was not received. Reason: " + reason + ". See logs for details";
});
} catch (reason) {
console.error(reason);
document.getElementById("resultText").innerHTML = "Message was not received. Reason: " + reason + " See logs for details";
}
});
// Send file by upload method
document.getElementById("SendFileWhatsAppBtn").addEventListener("click", function () {
/**
* @param {String} idInstance
* @param {String} apiTokenInstance
* @param {String} chatId
* @param {String} fileInput
* @param {String} caption
*/
const idInstance = document.getElementById("idInstance").value
const apiTokenInstance = document.getElementById("apiTokenIsntance").value
const form = document.querySelector("#upload-form");
const resultText = document.querySelector("#result-text-uploadFile");
const fileInput = form.querySelector('input[type="file"]');
const chatId = `${document.getElementById("phoneNumber-upload").value}@c.us`;
const caption = document.getElementById("messageText-upload").value;
const formData = new FormData();
formData.append("chatId", chatId);
formData.append("caption", caption);
formData.append("file", fileInput.files[0]);
const restAPI = whatsAppClient.restAPI({
idInstance: idInstance,
apiTokenInstance: apiTokenInstance
})
restAPI.file.sendFileByUpload(formData).then((response) => {
if (!response.ok) {
throw new Error("Network response was not ok");
}
return response.json();
})
.then((data) => {
console.log(`File uploaded with ID ${data.idMessage}`);
resultText.textContent = ''
resultText.textContent = `File uploaded with ID ${data.idMessage}`;
})
.catch((error) => {
console.error("There was a problem uploading the file:", error);
resultText.textContent = ''
resultText.textContent = `There was a problem uploading the file: ${error}`;
resultText.style.color = 'red'
});
});
// Send file by URL method
document.getElementById("SendFileByUrlWhatsAppBtn").addEventListener("click", () => {
/**
* @param {String} idInstance
* @param {String} apiTokenInstance
* @param {String} chatId
* @param {String} Url
* @param {String} caption
* @param {String} fileName
*/
const apiTokenInstance = document.getElementById("apiTokenIsntance").value;
const idInstance = document.getElementById("idInstance").value
const chatId = `${document.getElementById("phoneNumber-uploadByUrl").value}@c.us`;
const caption = document.getElementById("messageText-uploadByUrl").value;
const url = document.getElementById('fileByUrl').value;
const fileName = document.getElementById('fileName').value;
const resultText = document.querySelector("#result-text-sendFileByUrl");
const restAPI = whatsAppClient.restAPI({
idInstance: idInstance,
apiTokenInstance: apiTokenInstance
});
restAPI.file.sendFileByUrl(chatId, null, url, fileName, caption).then((data) => {
console.log(`File upload ${data.idMessage}`);
resultText.textContent = ''
resultText.textContent = `File upload ${data.idMessage}`;
}).catch((error) => {
console.error("There was a problem sending message:", error);
resultText.textContent = ''
resultText.textContent = `There was a problem sending message: ${error}`;
})
})
//Does not work from the browser because FS is used
// Send file by upload method
document.getElementById("SendFileByUploadWhatsAppBtn").addEventListener("click", () => {
/**
* @param {String} idInstance
* @param {String} apiTokenInstance
* @param {String} caption
* @param {String} fileName
*/
const apiTokenInstance = document.getElementById("apiTokenIsntance").value;
const idInstance = document.getElementById("idInstance").value
const filePath = document.getElementById('file-input-upload').value
const resultText = document.querySelector("#result-text-sendFileByUpload");
const restAPI = whatsAppClient.restAPI({
idInstance: idInstance,
apiTokenInstance: apiTokenInstance
});
restAPI.file.uploadFile(filePath).then((data) => {
console.log(`File upload ${data}`);
resultText.textContent = ''
resultText.textContent = `File upload ${data}`;
}).catch((error) => {
console.error("There was a problem sending message:", error);
resultText.textContent = ''
resultText.textContent = `There was a problem sending message: ${error}`;
})
})
//Reboot
document.getElementById("reboot").addEventListener("click", () => {
/**
* @param {String} idInstance
* @param {String} apiTokenInstance
*/
const apiTokenInstance = document.getElementById("apiTokenIsntance").value;
const idInstance = document.getElementById("idInstance").value
const restAPI = whatsAppClient.restAPI({
idInstance: idInstance,
apiTokenInstance: apiTokenInstance
});
restAPI.instance.reboot().then((data) => {
console.log(`Instance reboot ${data.isReboot}`);
resultText.textContent = ''
resultText.textContent = `Instance reboot ${data.isReboot}`;
}).catch((error) => {
console.error("There was a problem with rebooting:", error);
resultText.textContent = ''
resultText.textContent = `There was a problem with rebooting: ${error}`;
})
})
// Recive qr method
document.getElementById('recive-qr').addEventListener('click', () => {
/**
* @param {String} idInstance
* @param {String} apiTokenInstance
*/
const apiTokenInstance = document.getElementById("apiTokenIsntance").value;
const idInstance = document.getElementById("idInstance").value
const restAPI = whatsAppClient.restAPI({
idInstance: idInstance,
apiTokenInstance: apiTokenInstance
});
restAPI.instance.qr().then((data) => {
console.log(` ${data.message}`);
resultText.textContent = ''
resultText.textContent = ` ${data.message}`;
}).catch((error) => {
console.error("Problem getting qr:", error);
resultText.textContent = ''
resultText.textContent = `Problem getting qr: ${error}`;
})
})
// This function for saving your phone numbers when you creating a group
document.getElementById('chatPlus').addEventListener('click',function () {
const num = `${(document.getElementById('chatIds').value)}@c.us` ;
return chatIds.push(num);
})
// Create group method
document.getElementById('createGroup').addEventListener('click',() => {
/**
* @param {String} idInstance
* @param {String} apiTokenInstance
* @param {String} chatName
*/
const apiTokenInstance = document.getElementById("apiTokenIsntance").value;
const idInstance = document.getElementById("idInstance").value
const chatName = document.getElementById('groupName').value
const restAPI = whatsAppClient.restAPI({
idInstance: idInstance,
apiTokenInstance: apiTokenInstance
});
restAPI.group.createGroup(chatName, chatIds, null).then((response) => {
if (!response.ok) {
throw new Error ('Network is not ok')
}
return response.json()
}).then((data) => {
console.log(`Group created with ID ${data.idMessage}`);
resultText.textContent = ''
resultText.textContent = `Group created with ID ${data.idMessage}`;
}).catch((error) => {
console.error(`Group not created ${error}`)
resultText.textContent = ''
resultText.textContent = `Group not created ${error}`;
})
})
// Update group name method
document.getElementById('updateGroupName').addEventListener('click',() => {
/**
* @param {String} idInstance
* @param {String} apiTokenInstance
* @param {String} chatName
* @param {String} groupId
*/
const apiTokenInstance = document.getElementById("apiTokenIsntance").value;
const idInstance = document.getElementById("idInstance").value
const chatName = document.getElementById('groupName-upadteName').value
const groupId = document.getElementById('groupId-updateName').value
const result = document.getElementById('result-text-UpdateGroupName')
const restAPI = whatsAppClient.restAPI({
idInstance: idInstance,
apiTokenInstance: apiTokenInstance
});
restAPI.group.updateGroupName(groupId, chatName).then((data) => {
console.log(`Group update ${data}`);
result.textContent = ''
for(const item in data) {
result.textContent += `Group update ${item} `;
}
}).catch((error) => {
console.error(`Group not created ${error}`)
result.textContent = ''
result.textContent = `Group not created ${error}`;
})
})
// Get group data method
document.getElementById('getGroupData').addEventListener('click',() => {
/**
* @param {String} idInstance
* @param {String} apiTokenInstance
* @param {String} groupId
*/
const apiTokenInstance = document.getElementById("apiTokenIsntance").value;
const idInstance = document.getElementById("idInstance").value
const groupId = document.getElementById('groupId-GetGroupData').value
const result = document.getElementById('result-text-GetGroupData')
const restAPI = whatsAppClient.restAPI({
idInstance: idInstance,
apiTokenInstance: apiTokenInstance
});
restAPI.group.getGroupData(groupId).then((data) => {
console.log(`${data}`);
result.textContent = ''
const newData = JSON.stringify(data);
result.textContent = `${newData}`
}).catch((error) => {
console.error(`Group not created ${error}`)
result.textContent = ''
result.textContent = `Group not created ${error}`;
})
})
// Add Group Participant method
document.getElementById('addGroupParticipant').addEventListener('click',() => {
/**
* @param {String} idInstance
* @param {String} apiTokenInstance
* @param {String} groupId
* @param {String} participantChatId
*/
const apiTokenInstance = document.getElementById("apiTokenIsntance").value;
const idInstance = document.getElementById("idInstance").value
const groupId = document.getElementById('groupId-AddGroupParticipant').value
const participantChatId = document.getElementById('participantChatId-AddGroupParticipant').value
const result = document.getElementById('result-text-AddGroupParticipant')
const restAPI = whatsAppClient.restAPI({
idInstance: idInstance,
apiTokenInstance: apiTokenInstance
});
restAPI.group.addGroupParticipant(groupId, participantChatId, null).then((data) => {
console.log(`${data}`);
const newData = JSON.stringify(data)
result.textContent = ''
result.textContent = `${newData}`
}).catch((error) => {
console.error(`Group not created ${error}`)
result.textContent = ''
result.textContent = `Group not created ${error}`;
})
})
//Set group admin
document.getElementById('setGroupAdmin').addEventListener('click',() => {
/**
* @param {String} idInstance
* @param {String} apiTokenInstance
* @param {String} groupId
*/
const apiTokenInstance = document.getElementById("apiTokenIsntance").value;
const idInstance = document.getElementById("idInstance").value
const chatId = document.getElementById('chatId-setAdmin')
const groupId = document.getElementById('groupId-setAdmin').value
const result = document.getElementById('result-text-setGroupAdmin')
const restAPI = whatsAppClient.restAPI({
idInstance: idInstance,
apiTokenInstance: apiTokenInstance
});
restAPI.group.setGroupAdmin(groupId, chatId, null).then((data) => {
console.log(`${data}`);
result.textContent = ''
const newData = JSON.stringify(data);
result.textContent = `${newData}`
}).catch((error) => {
console.error(`Group not created ${error}`)
result.textContent = ''
result.textContent = `Group not created ${error}`;
})
})
//Leave group
document.getElementById('addLeaveGroup').addEventListener('click',() => {
/**
* @param {String} idInstance
* @param {String} apiTokenInstance
* @param {String} groupId
*/
const apiTokenInstance = document.getElementById("apiTokenIsntance").value;
const idInstance = document.getElementById("idInstance").value
const groupId = document.getElementById('groupId-leaveGroup').value
const result = document.getElementById('result-text-leaveGroup')
const restAPI = whatsAppClient.restAPI({
idInstance: idInstance,
apiTokenInstance: apiTokenInstance
});
restAPI.group.leaveGroup(groupId).then((data) => {
console.log(`${data}`);
result.textContent = ''
const newData = JSON.stringify(data);
result.textContent = `${newData}`
}).catch((error) => {
console.error(`Group not created ${error}`)
result.textContent = ''
result.textContent = `Group not created ${error}`;
})
})
// Get settings method
document.getElementById('getSettings').addEventListener('click',() => {
/**
* @param {String} idInstance
* @param {String} apiTokenInstance
*/
const apiTokenInstance = document.getElementById("apiTokenIsntance").value;
const idInstance = document.getElementById("idInstance").value
const reult = document.getElementById("reult");
const restAPI = whatsAppClient.restAPI({
idInstance: idInstance,
apiTokenInstance: apiTokenInstance
});
restAPI.settings.getSettings().then((data) => {
console.log(`Your settings ${data}`);
console.log(data)
resultText.textContent = ''
resultText.textContent += `Your settings:`;
for (items in data) {
const sett = document.createElement('p');
sett.textContent = `${items}:${data[items]}`;
reult.appendChild(sett)
}
}).catch((error) => {
console.error(`Problems with your settings ${error}`)
resultText.textContent = ''
resultText.textContent = `Problems with your settings ${error}`;
})
})
// Check WhatsApp method
document.getElementById('checkWhatsapp').addEventListener('click',() => {
/**
* @param {String} idInstance
* @param {String} apiTokenInstance
*/
const apiTokenInstance = document.getElementById("apiTokenIsntance").value;
const idInstance = document.getElementById("idInstance").value
const number = Number(document.getElementById("chatNumber-checkWhatsapp").value)
const result = document.getElementById("result-text-checkWhatsapp");
const restAPI = whatsAppClient.restAPI({
idInstance: idInstance,
apiTokenInstance: apiTokenInstance
});
restAPI.instance.checkWhatsapp(number).then((data) => {
console.log(`existsWhatsapp ${data.existsWhatsapp}`);
result.textContent = ''
result.textContent += `existsWhatsapp: ${data.existsWhatsapp}`;
}).catch((error) => {
console.error(`Problems with your method ${error}`)
result.textContent = ''
result.textContent = `Problems with your method ${error}`;
})
})
// Get avatar method
document.getElementById('getAvatar').addEventListener('click',() => {
/**
* @param {String} idInstance
* @param {String} apiTokenInstance
*/
const apiTokenInstance = document.getElementById("apiTokenIsntance").value;
const idInstance = document.getElementById("idInstance").value
const number = document.getElementById("chatNumber-GetAvatar").value
const result = document.getElementById("result-text-GetAvatar");
const restAPI = whatsAppClient.restAPI({
idInstance: idInstance,
apiTokenInstance: apiTokenInstance
});
restAPI.instance.getAvatar(number, null).then((data) => {
console.log(`Avatar ${data.urlAvatar}`);
result.textContent = ''
for (const item in data) {
result.textContent += `Avatar: ${data.urlAvatar}`;
}
}).catch((error) => {
console.error(`Problems with your method ${error}`)
result.textContent = ''
result.textContent = `Problems with your method ${error}`;
})
})
// Get avatar method
document.getElementById('getContacts').addEventListener('click',() => {
/**
* @param {String} idInstance
* @param {String} apiTokenInstance
*/
const apiTokenInstance = document.getElementById("apiTokenIsntance").value;
const idInstance = document.getElementById("idInstance").value
const result = document.getElementById("result-text-GetContacts");
const restAPI = whatsAppClient.restAPI({
idInstance: idInstance,
apiTokenInstance: apiTokenInstance
});
restAPI.instance.getContacts().then((data) => {
console.log(`Contact ${data}`);
result.textContent = ''
for (const key in data) {
const container = document.createElement('div')
const contact = data[key];
container.textContent += `Contact Name: ${contact.id}, Contact Email: ${contact.name},Contact type: ${contact.type}\n`;
result.appendChild(container)
}
}).catch((error) => {
console.error(`Problems with your method ${error}`)
result.textContent = ''
result.textContent = `Problems with your method ${error}`;
})
})
// Get state instance method
document.getElementById('getStateInstance').addEventListener('click',() => {
/**
* @param {String} idInstance
* @param {String} apiTokenInstance
*/
const apiTokenInstance = document.getElementById("apiTokenIsntance").value;
const idInstance = document.getElementById("idInstance").value
const restAPI = whatsAppClient.restAPI({
idInstance: idInstance,
apiTokenInstance: apiTokenInstance
});
restAPI.instance.getStateInstance().then((data) => {
console.log(`Yours status ${data.stateInstance}`);
resultText.textContent = ''
resultText.textContent = `Yours status ${data.stateInstance}`;
}).catch((error) => {
console.log(`Problems whith getting ${error}`);
resultText.textContent = ''
resultText.textContent = `Problems whith getting ${error}`;
}).catch((error) => {
})
})
// Get WhatsApp status method
document.getElementById('getWaSettings').addEventListener('click',() => {
/**
* @param {String} idInstance
* @param {String} apiTokenInstance
*/
const apiTokenInstance = document.getElementById("apiTokenIsntance").value;
const idInstance = document.getElementById("idInstance").value
const restAPI = whatsAppClient.restAPI({
idInstance: idInstance,
apiTokenInstance: apiTokenInstance
});
restAPI.instance.getWaSettings().then((data) => {
console.log(`Yours status ${data}`);
resultText.textContent = ''
resultText.textContent = `Yours status ${data}`;
}).catch((error) => {
console.log(`Problems whith getting ${error}`);
resultText.textContent = ''
resultText.textContent = `Problems whith getting ${error}`;
}).catch((error) => {
})
})
// Get chat history method
document.getElementById('GetChatHistory').addEventListener('click',() => {
/**
* @param {String} idInstance
* @param {String} apiTokenInstance
* @param {String} chatId
*/
const apiTokenInstance = document.getElementById("apiTokenIsntance").value;
const idInstance = document.getElementById("idInstance").value
const chatId = document.getElementById("chatId-chatHistory").value
const logs = document.getElementById("result-log")
const restAPI = whatsAppClient.restAPI({
idInstance: idInstance,
apiTokenInstance: apiTokenInstance
});
restAPI.message.getChatHistory(chatId).then((data) => {
logs.textContent = ''
console.log(data)
for (const dataItem of data) {
const chat = document.createElement('div')
console.log(`Yours chats ${JSON.stringify(dataItem)}`);
chat.textContent += `Yours chats ${JSON.stringify(dataItem)}`;
logs.appendChild(chat);