-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTeleAPI.php
More file actions
196 lines (175 loc) · 8.85 KB
/
TeleAPI.php
File metadata and controls
196 lines (175 loc) · 8.85 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
<?php
/*
.---------------------------------------------------------------------------.
| Software: PHP TeleAPI - PHP Telegram Class |
| Version: 2.4 |
| Release: March 29, 2019 (23:17 WIB) |
| Update: October 13, 2019 (11:43 WIB) |
| |
| Pasal 57 ayat (1) UU 28 Tahun 2014 |
| Copyright © 2019, Afdhalul Ichsan Yourdan. All Rights Reserved. |
| ------------------------------------------------------------------------- |
| Hubungi Saya: |
| - Facebook - Afdhalul Ichsan Yourdan - https://facebook.com/shennboku |
| - Instagram - ShennBoku - https://instagram.com/shennboku |
| - WhatsApp - 0857 7290 6190 |
'---------------------------------------------------------------------------'
*/
class TeleAPI {
private $token;
public function __construct($token)
{
$this->token = $token;
}
private function get($command,$value)
{
$context = stream_context_create([
'http' => [
'header' => [
'Content-type: application/x-www-form-urlencoded'
],
'method' => 'POST',
'content' => http_build_query($value)
]
]);
return file_get_contents('https://api.telegram.org/bot'.$this->token.'/'.$command, false, $context);
}
private function curl($command,$value)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.telegram.org/bot'.$this->token.'/'.$command);
curl_setopt($ch, CURLOPT_POST, count($value));
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($value));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$chresult = curl_exec($ch);
curl_close ($ch);
return $chresult;
}
/* Result Beauty Hook
{
"timestamp": "25-03-2019 16:25:40",
"from": {
"id": "123XXX",
"name": "Afdhalul Ichsan Yourdan",
"username": "ShennBoku" },
"chat": {
"id": "822115",
"text": "Halo, ini contoh text :)",
"type": "private" // Private or Group }
}
*/
public function beautyHook($json)
{
date_default_timezone_set('Asia/Jakarta');
$val = $json['message'];
$beautify = ['timestamp' => date('Y-m-d H:i:s', $val['date']),
'from' => ['id' => $val['from']['id'],'name' => $val['from']['first_name'].' '.$val['from']['last_name'],'username' => $val['from']['username']],
'chat' => ['id' => $val['chat']['id'],'text' => $val['text'],'type' => $val['chat']['type']]
];
return $beautify;
}
public function setWebhook($url,$connection = '80')
{
if(is_callable('curl_init')) {
$out = $this->curl('setWebhook',['url' => $url,'max_connections' => $connection,'allowed_updates' => 'message']);
if(!$out) $out = $this->stream('setWebhook',['url' => $url,'max_connections' => $connection,'allowed_updates' => 'message']);
} else {
$out = $this->stream('setWebhook',['url' => $url,'max_connections' => $connection,'allowed_updates' => 'message']);
}
$json = json_decode($out, true);
return ['result' => $json['ok'],'data' => $json['description']];
}
public function delWebhook()
{
if(is_callable('curl_init')) {
$out = $this->curl('setWebhook','');
if(!$out) $out = $this->stream('deleteWebhook','');
} else {
$out = $this->stream('deleteWebhook','');
}
$json = json_decode($out, true);
return ['result' => $json['ok'],'data' => $json['description']];
}
/*
Anda dapat melihat semua metode di:
https://core.telegram.org/bots/api#available-methods
function dibawah akan mengeluarkan result (true / false) dan data (message id bila sukses)
*/
public function sendMessage($cid,$msg)
{
$out = $this->curl('sendMessage',['chat_id' => $cid,'text' => $msg]);
if(!$out) $out = $this->get('sendMessage',['chat_id' => $cid,'text' => $msg]);
$json = json_decode($out, true);
return ['result' => $json['ok'],'data' => $json['description'].$json['result']['message_id']];
}
public function sendPhoto($cid,$pic,$msg)
{
$out = $this->curl('sendPhoto',['chat_id' => $cid,'photo' => $pic,'caption' => $msg]);
if(!$out) $out = $this->get('sendPhoto',['chat_id' => $cid,'photo' => $pic,'caption' => $msg]);
$json = json_decode($out, true);
return ['result' => $json['ok'],'data' => $json['description'].$json['result']['message_id']];
}
public function sendAudio($cid,$audio,$msg)
{
$out = $this->curl('sendAudio',['chat_id' => $cid,'audio' => $audio,'caption' => $msg]);
if(!$out) $out = $this->get('sendAudio',['chat_id' => $cid,'audio' => $audio,'caption' => $msg]);
$json = json_decode($out, true);
return ['result' => $json['ok'],'data' => $json['description'].$json['result']['message_id']];
}
public function sendDocument($cid,$doc,$thumb,$msg)
{
$out = $this->curl('sendDocument',['chat_id' => $cid,'document' => $doc,'thumb' => $thumb,'caption' => $msg]);
if(!$out) $out = $this->get('sendDocument',['chat_id' => $cid,'document' => $doc,'thumb' => $thumb,'caption' => $msg]);
$json = json_decode($out, true);
return ['result' => $json['ok'],'data' => $json['description'].$json['result']['message_id']];
}
public function sendVideo($cid,$vid,$thumb,$msg)
{
$out = $this->curl('sendVideo',['chat_id' => $cid,'video' => $vid,'thumb' => $thumb,'caption' => $msg]);
if(!$out) $out = $this->get('sendVideo',['chat_id' => $cid,'video' => $vid,'thumb' => $thumb,'caption' => $msg]);
$json = json_decode($out, true);
return ['result' => $json['ok'],'data' => $json['description'].$json['result']['message_id']];
}
public function sendAnimation($cid,$animation,$thumb,$msg)
{
$out = $this->curl('sendAnimation',['chat_id' => $cid,'animation' => $animation,'thumb' => $thumb,'caption' => $msg]);
if(!$out) $out = $this->get('sendAnimation',['chat_id' => $cid,'animation' => $animation,'thumb' => $thumb,'caption' => $msg]);
$json = json_decode($out, true);
return ['result' => $json['ok'],'data' => $json['description'].$json['result']['message_id']];
}
public function sendVoice($cid,$audio,$msg)
{
$out = $this->curl('sendVoice',['chat_id' => $cid,'voice' => $audio,'caption' => $msg]);
if(!$out) $out = $this->get('sendVoice',['chat_id' => $cid,'voice' => $audio,'caption' => $msg]);
$json = json_decode($out, true);
return ['result' => $json['ok'],'data' => $json['description'].$json['result']['message_id']];
}
public function sendVideoNote($cid,$vidnote,$thumb)
{
$out = $this->curl('sendVideoNote',['chat_id' => $cid,'video_note' => $vidnote,'thumb' => $thumb]);
if(!$out) $out = $this->get('sendVideoNote',['chat_id' => $cid,'video_note' => $vidnote,'thumb' => $thumb]);
$json = json_decode($out, true);
return ['result' => $json['ok'],'data' => $json['description'].$json['result']['message_id']];
}
public function sendMediaGroup($cid,$media)
{
$out = $this->curl('sendMediaGroup',['chat_id' => $cid,'media' => $media]);
if(!$out) $out = $this->get('sendMediaGroup',['chat_id' => $cid,'media' => $media]);
$json = json_decode($out, true);
return ['result' => $json['ok'],'data' => $json['description'].$json['result']['message_id']];
}
public function sendLocation($cid,$lat,$long)
{
$out = $this->curl('sendLocation',['chat_id' => $cid,'latitude' => $lat,'longitude' => $long]);
if(!$out) $out = $this->get('sendLocation',['chat_id' => $cid,'latitude' => $lat,'longitude' => $long]);
$json = json_decode($out, true);
return ['result' => $json['ok'],'data' => $json['description'].$json['result']['message_id']];
}
public function sendContact($cid,$phone,$fname,$lname)
{
$out = $this->curl('sendContact',['chat_id' => $cid,'phone_number' => $phone,'first_name' => $fname,'last_name' => $lname]);
if(!$out) $out = $this->get('sendContact',['chat_id' => $cid,'phone_number' => $phone,'first_name' => $fname,'last_name' => $lname]);
$json = json_decode($out, true);
return ['result' => $json['ok'],'data' => $json['description'].$json['result']['message_id']];
}
}