|
| 1 | +<?php |
| 2 | +if (!defined('RA')) { |
| 3 | + die('Tidak boleh diakses langsung.'); |
| 4 | +} |
| 5 | +function myPre($value) |
| 6 | +{ |
| 7 | + echo '<pre>'; |
| 8 | + print_r($value); |
| 9 | + echo '</pre>'; |
| 10 | +} |
| 11 | + |
| 12 | +define('BOT_TOKEN', 'TOKEN_BOT_MU_DISINI'); |
| 13 | +// versi official telegram bot |
| 14 | +define('API_URL', 'https://api.telegram.org/bot'.BOT_TOKEN.'/'); |
| 15 | +define('myVERSI','0.03'); |
| 16 | +// aktifkan ini jika ingin menampilkan debugging poll |
| 17 | +$debug = false; |
| 18 | +function exec_curl_request($handle) { |
| 19 | + $response = curl_exec($handle); |
| 20 | + if ($response === false) { |
| 21 | + $errno = curl_errno($handle); |
| 22 | + $error = curl_error($handle); |
| 23 | + error_log("Curl returned error $errno: $error\n"); |
| 24 | + curl_close($handle); |
| 25 | + return false; |
| 26 | + } |
| 27 | + $http_code = intval(curl_getinfo($handle, CURLINFO_HTTP_CODE)); |
| 28 | + curl_close($handle); |
| 29 | + if ($http_code >= 500) { |
| 30 | + // do not wat to DDOS server if something goes wrong |
| 31 | + sleep(10); |
| 32 | + return false; |
| 33 | + } else if ($http_code != 200) { |
| 34 | + $response = json_decode($response, true); |
| 35 | + error_log("Request has failed with error {$response['error_code']}: {$response['description']}\n"); |
| 36 | + if ($http_code == 401) { |
| 37 | + throw new Exception('Invalid access token provided'); |
| 38 | + } |
| 39 | + return false; |
| 40 | + } else { |
| 41 | + $response = json_decode($response, true); |
| 42 | + if (isset($response['description'])) { |
| 43 | + error_log("Request was successfull: {$response['description']}\n"); |
| 44 | + } |
| 45 | + $response = $response['result']; |
| 46 | + } |
| 47 | + return $response; |
| 48 | +} |
| 49 | +function apiRequest($method, $parameters=null) { |
| 50 | + if (!is_string($method)) { |
| 51 | + error_log("Method name must be a string\n"); |
| 52 | + return false; |
| 53 | + } |
| 54 | + if (!$parameters) { |
| 55 | + $parameters = array(); |
| 56 | + } else if (!is_array($parameters)) { |
| 57 | + error_log("Parameters must be an array\n"); |
| 58 | + return false; |
| 59 | + } |
| 60 | + foreach ($parameters as $key => &$val) { |
| 61 | + // encoding to JSON array parameters, for example reply_markup |
| 62 | + if (!is_numeric($val) && !is_string($val)) { |
| 63 | + $val = json_encode($val); |
| 64 | + } |
| 65 | + } |
| 66 | + $url = API_URL.$method.'?'.http_build_query($parameters); |
| 67 | + $handle = curl_init($url); |
| 68 | + curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); |
| 69 | + curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 5); |
| 70 | + curl_setopt($handle, CURLOPT_TIMEOUT, 60); |
| 71 | + curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false); |
| 72 | + return exec_curl_request($handle); |
| 73 | +} |
| 74 | +function apiRequestJson($method, $parameters) { |
| 75 | + if (!is_string($method)) { |
| 76 | + error_log("Method name must be a string\n"); |
| 77 | + return false; |
| 78 | + } |
| 79 | + if (!$parameters) { |
| 80 | + $parameters = array(); |
| 81 | + } else if (!is_array($parameters)) { |
| 82 | + error_log("Parameters must be an array\n"); |
| 83 | + return false; |
| 84 | + } |
| 85 | + $parameters["method"] = $method; |
| 86 | + $handle = curl_init(API_URL); |
| 87 | + curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); |
| 88 | + curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 5); |
| 89 | + curl_setopt($handle, CURLOPT_TIMEOUT, 60); |
| 90 | + curl_setopt($handle, CURLOPT_POSTFIELDS, json_encode($parameters)); |
| 91 | + curl_setopt($handle, CURLOPT_HTTPHEADER, array("Content-Type: application/json")); |
| 92 | + return exec_curl_request($handle); |
| 93 | +} |
| 94 | +// jebakan token, klo ga diisi akan mati |
| 95 | +if (strlen(BOT_TOKEN)<20) |
| 96 | + die(PHP_EOL."-> -> Token BOT API nya mohon diisi dengan benar!\n"); |
| 97 | + |
| 98 | + |
| 99 | +function sendApiPhoto($chatid, $photo, $caption = null, $reply_to_message_id = null, $reply_markup = null) |
| 100 | +{ |
| 101 | + $method = 'sendPhoto'; |
| 102 | + $data = ['chat_id' => $chatid, |
| 103 | + 'photo' => $photo]; |
| 104 | + $result = apiRequest($method, $data); |
| 105 | +} |
| 106 | + |
| 107 | +function sendApiMsg($chatid, $text, $msg_reply_id = false, $parse_mode = true, $disablepreview = false) |
| 108 | +{ |
| 109 | + $method = 'sendMessage'; |
| 110 | + $data = ['chat_id' => $chatid, 'text' => $text, 'parse_mode' => 'Markdown',]; |
| 111 | + if ($msg_reply_id) { |
| 112 | + $data['reply_to_message_id'] = $msg_reply_id; |
| 113 | + } |
| 114 | + //if ($parse_mode) { |
| 115 | + // $data['parse_mode'] = $parse_mode; |
| 116 | + //} |
| 117 | + if ($disablepreview) { |
| 118 | + $data['disable_web_page_preview'] = $disablepreview; |
| 119 | + } |
| 120 | + $result = apiRequest($method, $data); |
| 121 | +} |
| 122 | +function sendApiAction($chatid, $action) |
| 123 | +{ |
| 124 | + $method = 'sendChatAction'; |
| 125 | + $data = [ |
| 126 | + 'chat_id' => $chatid, |
| 127 | + 'action' => $action, |
| 128 | + ]; |
| 129 | + $result = apiRequest($method, $data); |
| 130 | +} |
| 131 | +function sendApiKeyboard($chatid, $text, $keyboard = [], $inline = false) |
| 132 | +{ |
| 133 | + $method = 'sendMessage'; |
| 134 | + $replyMarkup = [ |
| 135 | + 'keyboard' => $keyboard, |
| 136 | + 'resize_keyboard' => true, |
| 137 | + 'one_time_keyboard' => true, |
| 138 | + ]; |
| 139 | + $data = [ |
| 140 | + 'chat_id' => $chatid, |
| 141 | + 'text' => $text, |
| 142 | + 'parse_mode' => 'Markdown', |
| 143 | + ]; |
| 144 | + $inline |
| 145 | + ? $data['reply_markup'] = json_encode(['inline_keyboard' => $keyboard]) |
| 146 | + : $data['reply_markup'] = json_encode($replyMarkup); |
| 147 | + $result = apiRequest($method, $data); |
| 148 | +} |
| 149 | +function editMessageText($chatid, $message_id, $text, $keyboard = [], $inline = false) |
| 150 | +{ |
| 151 | + $method = 'editMessageText'; |
| 152 | + $replyMarkup = [ |
| 153 | + 'keyboard' => $keyboard, |
| 154 | + 'resize_keyboard' => true, |
| 155 | + ]; |
| 156 | + $data = [ |
| 157 | + 'chat_id' => $chatid, |
| 158 | + 'message_id' => $message_id, |
| 159 | + 'text' => $text, |
| 160 | + 'parse_mode' => 'Markdown', |
| 161 | + ]; |
| 162 | + $inline |
| 163 | + ? $data['reply_markup'] = json_encode(['inline_keyboard' => $keyboard]) |
| 164 | + : $data['reply_markup'] = json_encode($replyMarkup); |
| 165 | + $result = apiRequest($method, $data); |
| 166 | +} |
| 167 | +function sendApiHideKeyboard($chatid, $text) |
| 168 | +{ |
| 169 | + $method = 'sendMessage'; |
| 170 | + $data = [ |
| 171 | + 'chat_id' => $chatid, |
| 172 | + 'text' => $text, |
| 173 | + 'parse_mode' => 'Markdown', |
| 174 | + 'reply_markup' => json_encode(['hide_keyboard' => true]), |
| 175 | + ]; |
| 176 | + $result = apiRequest($method, $data); |
| 177 | +} |
| 178 | +function sendApiSticker($chatid, $sticker, $msg_reply_id = false) |
| 179 | +{ |
| 180 | + $method = 'sendSticker'; |
| 181 | + $data = [ |
| 182 | + 'chat_id' => $chatid, |
| 183 | + 'sticker' => $sticker, |
| 184 | + ]; |
| 185 | + if ($msg_reply_id) { |
| 186 | + $data['reply_to_message_id'] = $msg_reply_id; |
| 187 | + } |
| 188 | + $result = apiRequest($method, $data); |
| 189 | +} |
| 190 | +function deleteMsg($chatid, $msgid) |
| 191 | +{ |
| 192 | + $method = 'deleteMessage'; |
| 193 | + $data = [ |
| 194 | + 'chat_id' => $chatid, |
| 195 | + 'message_id' => $msgid, |
| 196 | + ]; |
| 197 | + $result = apiRequest($method, $data); |
| 198 | +} |
0 commit comments