-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
146 lines (145 loc) · 7.38 KB
/
index.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
<!DOCTYPE html>
<title>GREEN-API Exercise</title>
<html>
<head>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
</head>
<body>
<div class="container">
<legend>GREEN-API Exercise</legend>
<div class="row">
<div class="col-4">
<div class="card mb-1">
<div class="card-body">
<div class="input-group mb-3">
<span class="input-group-text" id="basic-addon1">#</span>
<input id="idInstance" type="text" class="form-control" placeholder="idInstance" aria-describedby="basic-addon1">
</div>
<div class="input-group mb-3">
<span class="input-group-text" id="basic-addon2">#</span>
<input id="apiTokenInstance" type="text" class="form-control" placeholder="apiTokenInstance" aria-describedby="basic-addon2">
</div>
<div class="input-group">
<div class="d-grid gap-2 col mx-auto">
<button class="btn btn-primary" onclick="getSettings()">getSettings</button>
<button class="btn btn-primary" onclick="getStateInstance()">getStateInstance</button>
</div>
</div>
</div>
</div>
<div class="card mb-1">
<div class="card-body">
<div class="input-group mb-3">
<span class="input-group-text" id="basic-addon3">+</span>
<input id="messagePhone" type="number" class="form-control" placeholder="XXXXXXXXXXX" aria-describedby="basic-addon3">
</div>
<div class="mb-3">
<label for="messageText" class="form-label">Text message:</label>
<textarea class="form-control" id="messageText" rows="3"></textarea>
<div class="d-grid gap-2 col mx-auto">
<button class="btn btn-success mt-1" onclick="sendMessage()">sendMessage</button>
</div>
</div>
</div>
</div><div class="card">
<div class="card-body">
<div class="input-group mb-3">
<span class="input-group-text" id="basic-addon3">+</span>
<input id="filePhone" type="number" class="form-control" placeholder="XXXXXXXXXXX" aria-describedby="basic-addon3">
</div>
<div class="input-group mb-3">
<span class="input-group-text" id="basic-addon2">https://</span>
<input id="url" type="text" class="form-control" placeholder="URL" aria-describedby="basic-addon2">
</div>
<div class="d-grid gap-2 col mx-auto">
<button class="btn btn-success mt-1" onclick="sendFileByUrl()">sendFileByUrl</button>
</div>
</div>
</div>
</div>
<div class="col">
<label for="answer" class="form-label">Answer:</label>
<textarea class="form-control" id="answer" rows="25" disabled></textarea>
</div>
</div>
</div>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script src="https://unpkg.com/@green-api/whatsapp-api-client/lib/whatsapp-api-client.min.js"></script>
<script>
class Credetials {
idInstance
apiTokenInstance
renew() {
this.idInstance = document.getElementById('idInstance').value;
this.apiTokenInstance = document.getElementById('apiTokenInstance').value;
}
}
credetials = new Credetials;
const getSettings = () => {
credetials.renew();
const restAPI = whatsAppClient.restAPI({
idInstance: credetials.idInstance,
apiTokenInstance: credetials.apiTokenInstance
});
restAPI.settings.getSettings().then((data) => {
document.getElementById('answer').value = JSON.stringify(data);
console.log(data);
})
.catch((reason) => {
document.getElementById('answer').value = JSON.stringify(reason);
console.error(reason);
});
}
const getStateInstance = () => {
credetials.renew();
const restAPI = whatsAppClient.restAPI({
idInstance: credetials.idInstance,
apiTokenInstance: credetials.apiTokenInstance
});
restAPI.instance.getStateInstance().then((data) => {
document.getElementById('answer').value = JSON.stringify(data);
console.log(data);
})
.catch((reason) => {
document.getElementById('answer').value = JSON.stringify(reason);
console.error(reason);
});
}
const sendMessage = () => {
credetials.renew();
const restAPI = whatsAppClient.restAPI({
idInstance: credetials.idInstance,
apiTokenInstance: credetials.apiTokenInstance
});
restAPI.message
.sendMessage(document.getElementById('messagePhone').value + "@c.us", null, document.getElementById('messageText').value)
.then((data) => {
document.getElementById('answer').value = JSON.stringify(data);
console.log(data);
})
.catch((reason) => {
document.getElementById('answer').value = JSON.stringify(reason);
console.error(reason);
});
}
const sendFileByUrl = () => {
credetials.renew();
const restAPI = whatsAppClient.restAPI({
idInstance: credetials.idInstance,
apiTokenInstance: credetials.apiTokenInstance
});
url = 'https://' + document.getElementById('url').value;
restAPI.file
.sendFileByUrl(document.getElementById('filePhone').value + "@c.us", null, url, url.split('/').pop(), url.split('/').pop().split('.').shift())
.then((data) => {
document.getElementById('answer').value = JSON.stringify(data);
console.log(data);
})
.catch((reason) => {
document.getElementById('answer').value = JSON.stringify(reason);
console.error(reason);
});
}
</script>
</body>
</html>