forked from ultramsg/whatsapp-php-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ultramsg.class.php
225 lines (181 loc) · 8.56 KB
/
ultramsg.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
<?php
namespace UltraMsg;
/**
* Class WhatsAppApi
* @package Ultramsg
*/
class WhatsAppApi {
protected $token = '';
protected $instance_id = '';
/**
* Ultramsg constructor.
* @param $token
* @param $instance_id
*/
public function __construct($token, $instance_id){
$this->token = $token;
$this->instance_id = "instance".preg_replace('/[^0-9]/', '',$instance_id);
}
// messages
public function getMessages($page=1,$limit=100,$status="all",$sort="asc",$id="",$referenceId="",$from="",$to="",$ack=""){
$params =array("page"=>$page,"limit"=>$limit,"status"=>$status,"sort"=>$sort,"id"=>$id,"referenceId"=>$referenceId,"from"=>$from,"to"=>$to,"ack"=>$ack);
return $this->sendRequest("GET","messages",$params );
}
public function getMessageStatistics(){
return $this->sendRequest("GET","messages/statistics");
}
public function sendChatMessage($to,$body,$priority=10,$referenceId=""){
$params =array("to"=>$to,"body"=>$body,"priority"=>$priority,"referenceId"=>$referenceId);
return $this->sendRequest("POST","messages/chat",$params );
}
public function sendImageMessage($to,$image,$caption="",$priority=10,$referenceId="",$nocache=false){
$params =array("to"=>$to,"caption"=>$caption,"image"=>$image,"priority"=>$priority,"referenceId"=>$referenceId,"nocache"=>$nocache);
return $this->sendRequest("POST","messages/image",$params );
}
public function sendStickerMessage($to,$sticker,$priority=10,$referenceId="",$nocache=false){
$params =array("to"=>$to,"sticker"=>$sticker,"priority"=>$priority,"referenceId"=>$referenceId,"nocache"=>$nocache);
return $this->sendRequest("POST","messages/sticker",$params );
}
public function sendDocumentMessage($to,$filename,$document,$caption="",$priority=10,$referenceId="",$nocache=false){
$params =array("to"=>$to,"filename"=>$filename,"document"=>$document,"caption"=>$caption,"priority"=>$priority,"referenceId"=>$referenceId,"nocache"=>$nocache);
return $this->sendRequest("POST","messages/document",$params );
}
public function sendAudioMessage($to,$audio,$priority=10,$referenceId="",$nocache=false){
$params =array("to"=>$to,"audio"=>$audio,"priority"=>$priority,"referenceId"=>$referenceId,"nocache"=>$nocache);
return $this->sendRequest("POST","messages/audio",$params );
}
public function sendVoiceMessage($to,$audio,$priority=10,$referenceId="",$nocache=false){
$params =array("to"=>$to,"audio"=>$audio,"priority"=>$priority,"referenceId"=>$referenceId,"nocache"=>$nocache);
return $this->sendRequest("POST","messages/voice",$params );
}
public function sendVideoMessage($to,$video,$caption="",$priority=10,$referenceId="",$nocache=false){
$params =array("to"=>$to,"caption"=>$caption,"video"=>$video,"priority"=>$priority,"referenceId"=>$referenceId,"nocache"=>$nocache);
return $this->sendRequest("POST","messages/video",$params );
}
public function sendLinkMessage($to,$link,$priority=10,$referenceId=""){
$params =array("to"=>$to,"link"=>$link,"priority"=>$priority,"referenceId"=>$referenceId);
return $this->sendRequest("POST","messages/link",$params );
}
public function sendContactMessage($to,$contact,$priority=10,$referenceId=""){
$params =array("to"=>$to,"contact"=>$contact,"priority"=>$priority,"referenceId"=>$referenceId);
return $this->sendRequest("POST","messages/contact",$params );
}
public function sendLocationMessage($to,$address,$lat,$lng,$priority=10,$referenceId=""){
$params =array("to"=>$to,"address"=>$address,"lat"=>$lat,"lng"=>$lng,"priority"=>$priority,"referenceId"=>$referenceId);
return $this->sendRequest("POST","messages/location",$params );
}
public function sendVcardMessage($to,$vcard,$priority=10,$referenceId=""){
$params =array("to"=>$to,"vcard"=>$vcard,"priority"=>$priority,"referenceId"=>$referenceId);
return $this->sendRequest("POST","messages/vcard",$params );
}
public function sendClearMessage($status){
$params =array("status"=>$status);
return $this->sendRequest("POST","messages/clear",$params );
}
public function resendByStatus($status){
$params =array("status"=>$status);
return $this->sendRequest("POST","messages/resendByStatus",$params );
}
public function resendById($id){
$params =array("id"=>$id);
return $this->sendRequest("POST","messages/resendById",$params );
}
// instance
public function getInstanceStatus(){
return $this->sendRequest("GET","instance/status");
}
public function getInstanceQr(){
return $this->sendRequest("GET","instance/qr");
}
public function getInstanceQrCode(){
return $this->sendRequest("GET","instance/qrCode");
}
public function getInstanceScreenshot($encoding=""){
return $this->sendRequest("GET","instance/screenshot",array("encoding"=>$encoding));
}
public function getInstanceMe(){
return $this->sendRequest("GET","instance/me");
}
public function getInstanceSettings(){
return $this->sendRequest("GET","instance/settings");
}
public function sendInstanceTakeover(){
return $this->sendRequest("POST","instance/takeover" );
}
public function sendInstanceLogout(){
return $this->sendRequest("POST","instance/logout" );
}
public function sendInstanceRestart(){
return $this->sendRequest("POST","instance/restart" );
}
public function sendInstanceSettings($sendDelay="1",$webhook_url="",$webhook_message_received=false,$webhook_message_create=false,$webhook_message_ack=false,$webhook_message_download_media=false){
$params =array("sendDelay"=>$sendDelay,"webhook_url"=>$webhook_url,"webhook_message_received"=>json_encode($webhook_message_received),"webhook_message_create"=>json_encode($webhook_message_create),"webhook_message_ack"=>json_encode($webhook_message_ack),"webhook_message_download_media"=>json_encode($webhook_message_download_media));
return $this->sendRequest("POST","instance/settings",$params);
}
public function sendInstanceClear(){
return $this->sendRequest("POST","instance/clear" );
}
// Chats
public function getChats(){
return $this->sendRequest("GET","chats");
}
public function getChatsMessages($chatId,$limit=100){
$params =array("chatId"=>$chatId,"limit"=>$limit);
return $this->sendRequest("GET","chats/messages",$params);
}
// Contacts
public function getContacts(){
return $this->sendRequest("GET","contacts");
}
public function getContact($chatId){
$params =array("chatId"=>$chatId);
return $this->sendRequest("GET","contacts/contact",$params);
}
public function getBlockedContacts(){
return $this->sendRequest("GET","contacts/blocked");
}
public function checkContact($chatId){
$params =array("chatId"=>$chatId);
return $this->sendRequest("GET","contacts/check",$params);
}
public function blockContact($chatId){
$params =array("chatId"=>$chatId);
return $this->sendRequest("POST","contacts/block",$params);
}
public function unblockContact($chatId){
$params =array("chatId"=>$chatId);
return $this->sendRequest("POST","contacts/unblock",$params);
}
public function sendRequest($method,$path,$params=array()){
if(!is_callable('curl_init')){
return array("Error"=>"cURL extension is disabled on your server");
}
$url="https://api.ultramsg.com/".$this->instance_id."/".$path;
$params['token'] = $this->token;
$data=http_build_query($params);
if(strtolower($method)=="get")$url = $url . '?' . $data;
$curl = curl_init($url);
if(strtolower($method)=="post"){
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS,$data);
}
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, 1);
$response = curl_exec($curl);
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if($httpCode == 404) {
return array("Error"=>"instance not found or pending please check you instance id");
}
$contentType = curl_getinfo($curl, CURLINFO_CONTENT_TYPE);
$header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);
curl_close($curl);
if (strpos($contentType,'application/json') !== false) {
return json_decode($body,true);
}
return $body;
}
}