· 6 years ago · Dec 27, 2019, 04:26 AM
1<?php
2$token = "tokenku";
3$usernamebot="@namaakun_bot";
4define('BOT_TOKEN', $token);
5
6define('API_URL', 'https://api.telegram.org/bot'.BOT_TOKEN.'/');
7
8$debug = false;
9
10function exec_curl_request($handle)
11{
12 $response = curl_exec($handle);
13
14 if ($response === false) {
15 $errno = curl_errno($handle);
16 $error = curl_error($handle);
17 error_log("Curl returned error $errno: $error\n");
18 curl_close($handle);
19
20 return false;
21 }
22
23 $http_code = intval(curl_getinfo($handle, CURLINFO_HTTP_CODE));
24 curl_close($handle);
25
26 if ($http_code >= 500) {
27 // do not wat to DDOS server if something goes wrong
28 sleep(10);
29
30 return false;
31 } elseif ($http_code != 200) {
32 $response = json_decode($response, true);
33 error_log("Request has failed with error {$response['error_code']}: {$response['description']}\n");
34 if ($http_code == 401) {
35 throw new Exception('Invalid access token provided');
36 }
37
38 return false;
39 } else {
40 $response = json_decode($response, true);
41 if (isset($response['description'])) {
42 error_log("Request was successfull: {$response['description']}\n");
43 }
44 $response = $response['result'];
45 }
46
47 return $response;
48}
49
50function apiRequest($method, $parameters = null)
51{
52 if (!is_string($method)) {
53 error_log("Method name must be a string\n");
54
55 return false;
56 }
57
58 if (!$parameters) {
59 $parameters = [];
60 } elseif (!is_array($parameters)) {
61 error_log("Parameters must be an array\n");
62
63 return false;
64 }
65
66 foreach ($parameters as $key => &$val) {
67 // encoding to JSON array parameters, for example reply_markup
68 if (!is_numeric($val) && !is_string($val)) {
69 $val = json_encode($val);
70 }
71 }
72 $url = API_URL.$method.'?'.http_build_query($parameters);
73
74 $handle = curl_init($url);
75 curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
76 curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 5);
77 curl_setopt($handle, CURLOPT_TIMEOUT, 60);
78 curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
79
80 return exec_curl_request($handle);
81}
82
83function apiRequestJson($method, $parameters)
84{
85 if (!is_string($method)) {
86 error_log("Method name must be a string\n");
87
88 return false;
89 }
90
91 if (!$parameters) {
92 $parameters = [];
93 } elseif (!is_array($parameters)) {
94 error_log("Parameters must be an array\n");
95
96 return false;
97 }
98
99 $parameters['method'] = $method;
100
101 $handle = curl_init(API_URL);
102 curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
103 curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 5);
104 curl_setopt($handle, CURLOPT_TIMEOUT, 60);
105 curl_setopt($handle, CURLOPT_POSTFIELDS, json_encode($parameters));
106 curl_setopt($handle, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
107
108 return exec_curl_request($handle);
109}
110
111if (strlen(BOT_TOKEN) < 20) {
112 die(PHP_EOL."-> -> Token BOT API nya mohon diisi dengan benar!\n");
113}
114
115function getUpdates($last_id = null)
116{
117 $params = [];
118 if (!empty($last_id)) {
119 $params = ['offset' => $last_id + 1, 'limit' => 1];
120 }
121 return apiRequest('getUpdates', $params);
122}
123
124// function sendMessage($idpesan, $idchat, $pesan)
125// {
126// $data = [
127// 'chat_id' => $idchat,
128// 'text' => $pesan,
129// 'parse_mode' => 'Markdown',
130// 'reply_to_message_id' => $idpesan,
131// ];
132
133// return apiRequest('sendMessage', $data);
134// }
135
136function sendMessage($idpesan, $idchat, $pesan, $menu)
137{
138
139 // $replyMarkup = array(
140 // "inline_keyboard" => array(
141 // array(array("text" => "My Button Text", "callback_data" => "help"), array("text" => "My Button Text 2", "callback_data" => "login"))
142 // )
143 // );
144
145 $data = [
146 'chat_id' => $idchat,
147 'text' => $pesan,
148 'parse_mode' => 'HTML',
149 'reply_markup' => $menu,
150 'reply_to_message_id' => $idpesan,
151 ];
152
153 return apiRequest('sendMessage', $data);
154}
155
156function processMessage($message)
157{
158 global $database;
159 if ($GLOBALS['debug']) {
160 print_r($message);
161 }
162
163 if (isset($message['message'])) {
164 $menu = '';
165 $sumber = $message['message'];
166 $idpesan = $sumber['message_id'];
167 $idchat = $sumber['chat']['id'];
168
169 $username = $sumber["from"]["username"];
170 $nama = $sumber['from']['first_name'].' '.$sumber['from']['last_name'];
171 $iduser = $sumber['from']['id'];
172
173 if (isset($sumber['text'])) {
174 $pesan = $sumber['text'];
175
176 if (preg_match("/^\/view_(\d+)$/i", $pesan, $cocok)) {
177 $pesan = "/view $cocok[1]";
178 }
179
180 if (preg_match("/^\/hapus_(\d+)$/i", $pesan, $cocok)) {
181 $pesan = "/hapus $cocok[1]";
182 }
183
184 $pecah2 = explode(' ', $pesan, 3);
185 $katake1 = strtolower($pecah2[0]);
186 $katake2 = strtolower($pecah2[1]);
187 $katake3 = strtolower($pecah2[2]);
188
189 $pecah = explode(' ', $pesan, 2);
190 $katapertama = strtolower($pecah[0]);
191
192 switch ($katapertama) {
193 case '/start':
194 case '/start@namabot':
195 // $text = "Selamat datang..., `$nama`! Untuk bantuan ketik: /help";
196 $text = "Selamat datang..., `$nama`!";
197 $replyMarkup = array(
198 'keyboard' => array(
199 array(array("text" => "Menu PS", "callback_data" => "/ps"), array("text" => "Menu WO", "callback_data" => "/wo"), array("text" => "Menu WO Terkendala", "callback_data" => "/wo_terkendala"))
200 ),
201 'resize_keyboard' => true,
202 'one_time_keyboard' => false,
203 'selective' => true
204 );
205 $menu = json_encode($replyMarkup);
206 break;
207
208 case '/help':
209 case '/help@namabot':
210 $text = "Berikut menu yang tersedia:\n\n";
211 $text .= "/start untuk memulai bot\n";
212 $text .= "/help info bantuan ini\n";
213 $text .= "/password `passwordbaru` untuk ganti password\n";
214 $text .= "/login `username` `password` untuk login\n";
215 $text .= "/myakun untuk melihat akun Anda\n";
216 break;
217
218 default:
219 $text = '_Aku gak ngerti karepmu?!_';
220 $text .= "\n";
221 $text .= "Klik /help untuk bantuan";
222 break;
223
224 /* Menu Utama */
225 case '/home':
226 case '/home@namabot':
227 $text = 'Ini Home';
228 $replyMarkup = array(
229 'keyboard' => array(
230 array(array("text" => "/ps", "callback_data" => "/ps"), array("text" => "/wo", "callback_data" => "/wo"), array("text" => "/wo_terkendala", "callback_data" => "/wo_terkendala"))
231 ),
232 'resize_keyboard' => true,
233 'one_time_keyboard' => false,
234 'selective' => true
235 );
236 $menu = json_encode($replyMarkup);
237 break;
238
239 case '/ps':
240 case '/ps@namabot':
241 $text = 'Ini menu PS';
242 $replyMarkup = array(
243 'keyboard' => array(
244 array(array("text" => "/ps1", "callback_data" => "/ps1"), array("text" => "/ps2", "callback_data" => "/ps2"), array("text" => "/home", "callback_data" => "/home"))
245 ),
246 'resize_keyboard' => true,
247 'one_time_keyboard' => false,
248 'selective' => true
249 );
250 $menu = json_encode($replyMarkup);
251 break;
252
253 case '/wo':
254 case '/wo@namabot':
255 $text = 'Ini menu WO';
256 $replyMarkup = array(
257 'keyboard' => array(
258 array(array("text" => "/wo1", "callback_data" => "/wo1"), array("text" => "/wo2", "callback_data" => "/wo2"), array("text" => "/home", "callback_data" => "/home"))
259 ),
260 'resize_keyboard' => true,
261 'one_time_keyboard' => false,
262 'selective' => true
263 );
264 $menu = json_encode($replyMarkup);
265 break;
266
267 case '/wo_terkendala':
268 case '/wo_terkendala@namabot':
269 $text = 'Ini menu WO Terkendala';
270 $replyMarkup = array(
271 'keyboard' => array(
272 array(array("text" => "/wo_terkendala1", "callback_data" => "/wo_terkendala1"), array("text" => "/wo_terkendala2", "callback_data" => "/wo_terkendala2"), array("text" => "/home", "callback_data" => "/home"))
273 ),
274 'resize_keyboard' => true,
275 'one_time_keyboard' => false,
276 'selective' => true
277 );
278 $menu = json_encode($replyMarkup);
279 break;
280 }
281 } else {
282 $text = 'Silahkan tulis pesan yang akan disampaikan..';
283 $text .= "\n";
284 $text .= "Format: /pesan `pesan`";
285 }
286
287 $hasil = sendMessage($idpesan, $idchat, $text, $menu);
288 if ($GLOBALS['debug']) {
289 // hanya nampak saat metode poll dan debug = true;
290 echo 'Pesan yang dikirim: '.$text.PHP_EOL;
291 print_r($hasil);
292 }
293 }
294}
295
296// pencetakan versi dan info waktu server, berfungsi jika test hook
297echo 'Ver. '.myVERSI.' OK Start!'.PHP_EOL.date('d-m-Y H:i:s').PHP_EOL;
298
299function printUpdates($result)
300{
301 foreach ($result as $obj) {
302 // echo $obj['message']['text'].PHP_EOL;
303 processMessage($obj);
304 $last_id = $obj['update_id'];
305 }
306
307 return $last_id;
308}
309
310
311// AKTIFKAN INI jika menggunakan metode poll
312// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
313/*
314$last_id = null;
315while (true) {
316 $result = getUpdates($last_id);
317 if (!empty($result)) {
318 echo '+';
319 $last_id = printUpdates($result);
320 } else {
321 echo '-';
322 }
323
324 sleep(1);
325}
326*/
327// AKTIFKAN INI jika menggunakan metode webhook
328// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
329$content = file_get_contents("php://input");
330$update = json_decode($content, true);
331
332if (!$update) {
333 exit;
334} else {
335 processMessage($update);
336}
337
338?>