forked from bssth/whatsapp-chatapi-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatapi.class.php
315 lines (272 loc) · 9.32 KB
/
chatapi.class.php
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
<?php
/**
* Class ChatApi
* @author Mike Chip
*/
class ChatApi
{
protected $token = '';
protected $url = '';
protected $instance_url = 'https://us-central1-app-chat-api-com.cloudfunctions.net/';
protected $instance_key = '12345';
protected $_mem = [];
/**
* Create instance
* @param string $token
* @param string $url
* @return void
*/
public function __construct($token, $url = 'https://foo.chat-api.com')
{
$this->token = $token;
$this->url = $url;
}
/**
* @param $method
* @param array $args
* @return string
*/
public function createUrl($method, $args = [])
{
$args['token'] = $this->token;
return $this->url.'/'.$method.'?'.http_build_query($args);
}
/**
* Send chat-api query
* @param string $method
* @param null|array $args
* @param string $qmethod
* @return bool|string
*/
public function query($method, $args = null, $qmethod = 'GET')
{
$url = $this->createUrl($method);
if($qmethod == "POST" && isset($args) && is_array($args)) {
$json = json_encode($args);
$options = stream_context_create(['http' => [
'method' => $qmethod,
'header' => 'Content-type: application/json',
'content' => $json
]]);
} elseif($qmethod == "GET" && isset($args) && is_array($args)) {
$url = $this->createUrl($method, $args);
$options = stream_context_create(['http' => [
'method' => $qmethod,
'header' => 'Content-type: application/json',
]]);
}
return file_get_contents($url, false, isset($options) ? $options : null);
}
/**
* @return array|mixed|null
*/
public function getFullInbox()
{
$inbox = [];
while(true) {
$inb = json_decode($this->query('messages', isset($offset) ? ['lastMessageNumber' => $offset] : ['last' => 1], "GET"), true);
if(!is_array($inb) || !isset($inb['messages']) || !count($inb['messages'])) {
var_dump($inb);
break;
}
$offset = $inb['lastMessageNumber'];
$inbox = array_merge($inbox, $inb['messages']);
}
return $inbox;
}
/**
* @return false|string
*/
public function createInstance()
{
$options = stream_context_create(['http' => [
'method' => 'POST',
'header' => 'Content-type: application/json',
'content' => json_encode(['uid' => $this->instance_key, 'type' => 'whatsapp'])
]]);
return json_decode(file_get_contents($this->instance_url.'newInstance', false, isset($options) ? $options : null), true);
}
/**
* @param int $id
* @return mixed
*/
public function deleteInstance(int $id)
{
$options = stream_context_create(['http' => [
'method' => 'POST',
'header' => 'Content-type: application/json',
'content' => json_encode(['uid' => $this->instance_key, 'instanceId' => (string)$id])
]]);
return json_decode(file_get_contents($this->instance_url.'deleteInstance', false, isset($options) ? $options : null), true);
}
/**
* Get all inbox messages ( include queue)
* @return null|array
*/
public function getInbox($offset = 0)
{
if(isset($this->_mem[$offset]) && is_array($this->_mem[$offset]))
return $this->_mem[$offset];
$inbox = json_decode($this->query('messages', ($offset > 0) ? ['lastMessageNumber' => $offset] : ['last' => 1]), 1);
$newOffset = $inbox['lastMessageNumber'];
$mess = $inbox['messages'];
$inbox = [];
foreach($mess as $val) {
$val['offset'] = $newOffset;
$inbox[] = $val;
}
if($offset < 50) {
$que = json_decode($this->query('showMessagesQueue'), 1)['first100'];
if (is_array($que)) {
foreach ($que as $k => $v) {
$inbox[] = [
'id' => $v['id'],
'body' => $v['body'],
'type' => $v['type'],
'senderName' => '',
'fromMe' => true,
'queue' => true,
'author' => $v['chatId'],
'time' => $v['last_try'],
'chatId' => $v['chatId']
];
}
}
}
usort($inbox, function ($a, $b) {
if ($a['time'] == $b['time'])
return 0;
return ($a['time'] < $b['time']) ? -1 : 1;
});
$this->_mem[$offset] = $inbox;
return $inbox;
}
/**
* Get status (logged in / error / loading) for current instance
* @return string
*/
public function getStatus()
{
$js = json_decode($this->query('status'), 1);
if(isset($js['accountStatus']))
return $js['accountStatus'];
else
return 'error';
}
/**
* Get all messages from chat by ID
* @param string $author
* @return array
*/
public function getChatMessages($author)
{
$ib = $this->getInbox();
$msgs = [];
foreach($ib as $message) {
if(isset($author) && $author == $message['chatId'])
$msgs[] = $message;
}
return $msgs;
}
/**
* Send message to phone number
* @param string $chat
* @param string $text
* @return boolean
*/
public function sendPhoneMessage($chat, $text)
{
return json_decode($this->query('sendMessage', ['phone' => $chat, 'body' => $text]), 1)['sent'];
}
/**
* Gets currently installed webhook
* @return null|array
*/
public function getWebhook()
{
return json_decode($this->query('webhook', []));
}
/**
* Installs new webhook
* @return null|array
*/
public function setWebhook($url = 'https://requestb.in/1f9aj261')
{
return json_decode($this->query('webhook', ['webhookUrl' => $url]));
}
/**
* Log out current instance
* @return null|array
*/
public function logout()
{
return json_decode($this->query('logout', []));
}
/**
* Send reboot signal to instance
* @return null|array
*/
public function reboot()
{
return json_decode($this->query('reboot', []));
}
/**
* Generate QR-code direct link
* @return string
*/
public function getQRCode()
{
return $this->createUrl('qr_code');
}
/**
* Show screenshot of instance
* @return string
*/
public function getScreenshot()
{
return $this->createUrl('screenshot');
}
/**
* Send file to chat
* @param string $chat
* @param string $body
* @param string $filename
* @return boolean
*/
public function sendFile($chat, $body, $filename)
{
return json_decode($this->query('sendFile', ['chatId' => $chat, 'filename' => $filename, 'body' => $body]), 1)['sent'];
}
/**
* Send message to chat (by not phone but chat ID)
* @param string $chat
* @param string $text
* @return boolean
*/
public function sendMessage($chat, $text)
{
return json_decode($this->query('sendMessage', ['chatId' => $chat, 'body' => $text]), 1)['sent'];
}
/**
* Generate dialogues list from messages
* @param int $offset
* @return array
*/
public function getDialogs($offset = 0)
{
$ib = $this->getInbox($offset);
$contacts = [];
foreach($ib as $message) {
$contacts[ $message['chatId'] ] = [
'name' => $message['senderName'],
'answered' => $message['fromMe'],
'offset' => isset($message['offset']) ? $message['offset'] : 0,
'last' => mb_substr($message['body'], 0, 50),
'lastFull' => $message['body'],
'chatId' => $message['chatId'],
'num' => '+' . (int)filter_var($message['chatId'], FILTER_SANITIZE_NUMBER_INT)
];
}
return $contacts;
}
}