-
Notifications
You must be signed in to change notification settings - Fork 0
/
fungsi.php
296 lines (281 loc) · 7.73 KB
/
fungsi.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
<?php
if (!defined('KS')) {
die('Tidak boleh diakses langsung.');
}
function myPre($value)
{
echo '<pre>';
print_r($value);
echo '</pre>';
}
define('BOT_TOKEN', '1107717637:AAEdiYuxlIXI-EW7Ut4TKoxS9KiPyCe4DCY');
// versi official telegram bot
define('API_URL', 'https://api.telegram.org/bot'.BOT_TOKEN.'/');
define('myVERSI','0.03');
$debug = false;
function exec_curl_request($handle) {
$response = curl_exec($handle);
if ($response === false) {
$errno = curl_errno($handle);
$error = curl_error($handle);
error_log("Curl returned error $errno: $error\n");
curl_close($handle);
return false;
}
$http_code = intval(curl_getinfo($handle, CURLINFO_HTTP_CODE));
curl_close($handle);
if ($http_code >= 500) {
// do not wat to DDOS server if something goes wrong
sleep(10);
return false;
} else if ($http_code != 200) {
$response = json_decode($response, true);
error_log("Request has failed with error {$response['error_code']}: {$response['description']}\n");
if ($http_code == 401) {
throw new Exception('Invalid access token provided');
}
return false;
} else {
$response = json_decode($response, true);
if (isset($response['description'])) {
error_log("Request was successfull: {$response['description']}\n");
}
$response = $response['result'];
}
return $response;
}
function apiRequest($method, $parameters=null) {
if (!is_string($method)) {
error_log("Method name must be a string\n");
return false;
}
if (!$parameters) {
$parameters = array();
} else if (!is_array($parameters)) {
error_log("Parameters must be an array\n");
return false;
}
foreach ($parameters as $key => &$val) {
// encoding to JSON array parameters, for example reply_markup
if (!is_numeric($val) && !is_string($val)) {
$val = json_encode($val);
}
}
$url = API_URL.$method.'?'.http_build_query($parameters);
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($handle, CURLOPT_TIMEOUT, 60);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
return exec_curl_request($handle);
}
function apiRequestJson($method, $parameters) {
if (!is_string($method)) {
error_log("Method name must be a string\n");
return false;
}
if (!$parameters) {
$parameters = array();
} else if (!is_array($parameters)) {
error_log("Parameters must be an array\n");
return false;
}
$parameters["method"] = $method;
$handle = curl_init(API_URL);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($handle, CURLOPT_TIMEOUT, 60);
curl_setopt($handle, CURLOPT_POSTFIELDS, json_encode($parameters));
curl_setopt($handle, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
return exec_curl_request($handle);
}
// jebakan token, klo ga diisi akan mati
if (strlen(BOT_TOKEN)<20)
die(PHP_EOL."-> -> Token BOT API nya mohon diisi dengan benar!\n");
//fungsi kirim pesan
function sendApiMsg($chatid, $text, $msg_reply_id = false, $parse_mode = true, $disablepreview = false)
{
$method = 'sendMessage';
$data = ['chat_id' => $chatid, 'text' => $text];
if ($msg_reply_id) {
$data['reply_to_message_id'] = $msg_reply_id;
}
if ($parse_mode) {
$data['parse_mode'] = 'Markdown';
}
else{
$data['parse_mode'] = 'HTML';
}
if ($disablepreview) {
$data['disable_web_page_preview'] = $disablepreview;
}
$result = apiRequest($method, $data);
return $result;
}
//kirim foto
function sendApiPhoto($chatid, $photo, $caption = null, $reply_to_message_id = null, $reply_markup = null)
{
$method = 'sendPhoto';
$data = ['chat_id' => $chatid,
'photo' => $photo];
$result = apiRequest($method, $data);
return $result;
}
function sendApiAction($chatid, $action)
{
$method = 'sendChatAction';
$data = [
'chat_id' => $chatid,
'action' => $action,
];
$result = apiRequest($method, $data);
return $result;
}
function sendApiKeyboard($chatid, $text, $keyboard = [], $inline = false, $msg_reply_id = false)
{
$method = 'sendMessage';
$replyMarkup = [
'keyboard' => $keyboard,
'resize_keyboard' => true,
'one_time_keyboard' => false,
];
$data = [
'chat_id' => $chatid,
'text' => $text,
'parse_mode' => 'Markdown',
];
if ($msg_reply_id) {
$data['reply_to_message_id'] = $msg_reply_id;
}
$inline
? $data['reply_markup'] = json_encode(['inline_keyboard' => $keyboard])
: $data['reply_markup'] = json_encode($replyMarkup);
$result = apiRequest($method, $data);
return $result;
}
function editMessageText($chatid, $message_id, $text, $keyboard = [], $inline = false)
{
$method = 'editMessageText';
$replyMarkup = [
'keyboard' => $keyboard,
'resize_keyboard' => true,
];
$data = [
'chat_id' => $chatid,
'message_id' => $message_id,
'text' => $text,
'parse_mode' => 'Markdown',
];
$inline
? $data['reply_markup'] = json_encode(['inline_keyboard' => $keyboard])
: $data['reply_markup'] = json_encode($replyMarkup);
$result = apiRequest($method, $data);
return $result;
}
function sendApiHideKeyboard($chatid, $text)
{
$method = 'sendMessage';
$data = [
'chat_id' => $chatid,
'text' => $text,
'parse_mode' => 'Markdown',
'reply_markup' => json_encode(['hide_keyboard' => true]),
];
$result = apiRequest($method, $data);
return $result;
}
function sendApiSticker($chatid, $sticker, $msg_reply_id = false)
{
$method = 'sendSticker';
$data = [
'chat_id' => $chatid,
'sticker' => $sticker,
];
if ($msg_reply_id) {
$data['reply_to_message_id'] = $msg_reply_id;
}
$result = apiRequest($method, $data);
return $result;
}
function deleteMsg($chatid, $msgid)
{
$method = 'deleteMessage';
$data = [
'chat_id' => $chatid,
'message_id' => $msgid,
];
$result = apiRequest($method, $data);
return $result;
}
function sendLocation($chatid, $lat, $long, $msg_reply_id = false)
{
$method = 'sendLocation';
$data = [
'chat_id' => $chatid,
'latitude' => $lat,
'longitude' => $long,
];
if ($msg_reply_id) {
$data['reply_to_message_id'] = $msg_reply_id;
}
$result = apiRequest($method, $data);
return $result;
}
function kickChatMember($chatid, $user_id)
{
$method = 'kickChatMember';
$data = [
'chat_id' => $chatid,
'user_id' => $user_id,
];
$result = apiRequest($method, $data);
return $result;
}
function getChatMember($chatid, $user_id)
{
$method = 'getChatMember';
$data = [
'chat_id' => $chatid,
'user_id' => $user_id,
];
$result = apiRequest($method, $data);
return $result;
}
function pinChatMessage($chatid, $msg_id)
{
$method = 'pinChatMessage';
$data = [
'chat_id' => $chatid,
'message_id' => $msg_id,
];
$result = apiRequest($method, $data);
return $result;
}
function unpinChatMessage($chatid)
{
$method = 'unpinChatMessage';
$data = [
'chat_id' => $chatid,
];
$result = apiRequest($method, $data);
return $result;
}
function getChatAdministrators($chatid)
{
$method = 'getChatAdministrators';
$data = [
'chat_id' => $chatid,
];
$result = apiRequest($method, $data);
return $result;
}
function leaveChat($chatid)
{
$method = 'leaveChat';
$data = [
'chat_id' => $chatid,
];
$result = apiRequest($method, $data);
return $result;
}
?>