BimoSora

bottelegram

Oct 9th, 2020 (edited)
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.00 KB | None | 0 0
  1. <?php
  2. $TOKEN = "token";
  3. $usernamebot= "@namabot";
  4.  
  5. $debug = false;
  6.  
  7. function request_url($method)
  8. {
  9. global $TOKEN;
  10. return "https://api.telegram.org/bot" . $TOKEN . "/". $method;
  11. }
  12.  
  13. function send_reply($chatid, $msgid, $text)
  14. {
  15. global $debug;
  16. $data = array(
  17. 'chat_id' => $chatid,
  18. 'text' => $text,
  19. 'parse_mode' => 'html',
  20. 'disable_web_page_preview' => true,
  21. );
  22. // use key 'http' even if you send the request to https://...
  23. $options = array(
  24. 'http' => array(
  25. 'header' => "Content-type: application/x-www-form-urlencoded\r\n",
  26. 'method' => 'POST',
  27. 'content' => http_build_query($data),
  28. ),
  29. );
  30. $context = stream_context_create($options);
  31. $result = file_get_contents(request_url('sendMessage'), false, $context);
  32.  
  33. if ($debug)
  34. print_r($result);
  35. }
  36.  
  37. function send_group($chatid, $msgid2, $text)
  38. {
  39. global $debug;
  40. $data2 = array(
  41. 'chat_id' => $chatid,
  42. 'text' => $text,
  43. 'reply_to_message_id' => $msgid2, // <---- biar ada reply nya balasannya, opsional, bisa dihapus baris ini
  44. 'parse_mode' => 'html',
  45. );
  46. // use key 'http' even if you send the request to https://...
  47. $options2 = array(
  48. 'http' => array(
  49. 'header' => "Content-type: application/x-www-form-urlencoded\r\n",
  50. 'method' => 'POST',
  51. 'content' => http_build_query($data2),
  52. ),
  53. );
  54. $context2 = stream_context_create($options2);
  55. $result2 = file_get_contents(request_url('sendMessage'), false, $context2);
  56.  
  57. if ($debug)
  58. print_r($result2);
  59. }
  60.  
  61. function send_group2($chatid, $msgid3, $text)
  62. {
  63. global $debug;
  64. $data3 = array(
  65. 'chat_id' => $chatid,
  66. 'text' => $text,
  67. 'parse_mode' => 'html',
  68. );
  69. // use key 'http' even if you send the request to https://...
  70. $options3 = array(
  71. 'http' => array(
  72. 'header' => "Content-type: application/x-www-form-urlencoded\r\n",
  73. 'method' => 'POST',
  74. 'content' => http_build_query($data3),
  75. ),
  76. );
  77. $context3 = stream_context_create($options3);
  78. $result3 = file_get_contents(request_url('sendMessage'), false, $context3);
  79.  
  80. if ($debug)
  81. print_r($result3);
  82. }
  83.  
  84. // fungsi mengolahan pesan, menyiapkan pesan untuk dikirimkan
  85.  
  86. function create_response($text, $message)
  87. {
  88. global $usernamebot;
  89. // inisiasi variable hasil yang mana merupakan hasil olahan pesan
  90. $hasil = '';
  91.  
  92. $fromid = $message["from"]["id"]; // variable penampung id user
  93. $chatid = $message["chat"]["id"]; // variable penampung id chat
  94. $pesanid= $message['message_id']; // variable penampung id message
  95.  
  96. // variable penampung username nya user
  97. isset($message["from"]["username"])
  98. ? $chatuser = $message["from"]["username"]
  99. : $chatuser = '';
  100.  
  101. // variable penampung nama user
  102.  
  103. isset($message["from"]["last_name"])
  104. ? $namakedua = $message["from"]["last_name"]
  105. : $namakedua = '';
  106. $namauser = $message["from"]["first_name"]. ' ' .$namakedua;
  107.  
  108. //Disini di koreksi karena new chat member tidak menampung teks dibuat kondisi
  109. if(isset($message['new_chat_member'])){
  110. $hasil = $text;
  111. return $hasil; //Hasil langsung direturn supaya kode dibawah tidak dieksekusi
  112. }
  113.  
  114. if(isset($message['promotion'])){
  115. $hasil = $text;
  116. return $hasil; //Hasil langsung direturn supaya kode dibawah tidak dieksekusi
  117. }
  118.  
  119. // ini saya pergunakan untuk menghapus kelebihan pesan spasi yang dikirim ke bot.
  120. $textur = preg_replace('/\s\s+/', ' ', $text);
  121.  
  122. // memecah pesan dalam 2 blok array, kita ambil yang array pertama saja
  123. $command = explode(' ',$textur,2); //
  124.  
  125. // identifikasi perintah (yakni kata pertama, atau array pertamanya)
  126. switch ($command[0]) {
  127.  
  128. // pesan selamat datang
  129. case '/start':
  130. $hasil = "<b>$namauser</b> \n";
  131. $hasil .= "Selamat datang di $usernamebot \n\n";
  132. $hasil .= "/start - Mulai bot \n";
  133. $hasil .= "/id - Untuk cek nama, username dan id Anda \n";
  134. $hasil .= "/timeworld - Cek waktu seluruh dunia, lihat disini format timezone nya <a href='https://www.w3schools.com/php/php_ref_timezones.asp'>Timezone list</a>. Penulisan (/timeworld Asia/Jakarta) \n";
  135. break;
  136.  
  137. // jika ada pesan /id, bot akan membalas dengan menyebutkan idnya user
  138. case '/id':
  139. $hasil = "Name: <b>$namauser</b> \n";
  140. $hasil .= "Username: <b>@$chatuser</b> \n";
  141. $hasil .= "My ID: <b>$fromid</b>";
  142. break;
  143.  
  144. case '/timeworld'.$input2;
  145. $input = "$text";
  146. $input2 = substr($input, 10);
  147. if ($input > "/timeworld $input2") {
  148. $regex = preg_match('/\/timeworld (.*)/', $input, $hasil);
  149. date_default_timezone_set($hasil[1]);
  150. $hasil = "Cek waktu seluruh dunia, lihat disini format timezone nya <a href='https://www.w3schools.com/php/php_ref_timezones.asp'>Timezone list</a>. Penulisan (/timeworld Asia/Jakarta) \n\n";
  151. $hasil .= "Tanggal: ";
  152. $hasil .= "<b>";
  153. $hasil .= date('d/m/Y', time());
  154. $hasil .= "</b>";
  155. $hasil .= "\n";
  156. $hasil .= "Waktu: ";
  157. $hasil .= "<b>";
  158. $hasil .= date('H:i:s', time());
  159. $hasil .= "</b>";
  160. $hasil .= "\n";
  161. $hasil .= "Zona: ";
  162. $hasil .= "<b>";
  163. $hasil .= date_default_timezone_get();
  164. $hasil .= "</b>";
  165. } else if ($input == "/timeworld") {
  166. $hasil = "Penulisan salah atau kurang lengkap, lihat disini format timezone nya <a href='https://www.w3schools.com/php/php_ref_timezones.asp'>Timezone list</a>. Penulisan (/timeworld Asia/Jakarta)";
  167. }
  168. break;
  169. }
  170. return $hasil;
  171. }
  172.  
  173. function process_message($message)
  174. {
  175. $trustedid = "1100853905";
  176. $trustedgroup = "-1001345383575";
  177. $updateid = $message["update_id"];
  178. $message_data = $message["message"];
  179. $trusted = $message["message"]["chat"]["id"];
  180. $trustgroup = $message["message"]["chat"]["id"];
  181. $jenis = $message["chat"]["type"];
  182.  
  183. if (isset($message_data["text"])) {
  184. /*
  185. if($trusted == $trustedid){
  186. */
  187. $chatid = $message_data["chat"]["id"];
  188. $message_id = $message_data["message_id"];
  189. $text = $message_data["text"];
  190. /*
  191. }
  192. */
  193. $response = create_response($text, $message_data);
  194. if (!empty($response))
  195. send_reply($chatid, $message_id, $response);
  196. }
  197.  
  198. //welcome group
  199. if (isset($message_data["new_chat_member"])) {
  200. if($trustgroup == $trustedgroup){
  201. $chatid = $message_data["chat"]["id"];
  202. $message_id = $message_data["message_id"];
  203. $firstName = $message_data["new_chat_member"]["first_name"];
  204. $title = $message_data["chat"]["title"];
  205. $new_chat_member = "Selamat datang <b>$firstName</b> di $title \n\n ";
  206. $new_chat_member .= date("d/m/Y, H:i:s");
  207. }
  208. $response = create_response($new_chat_member, $message_data);
  209. if (!empty($response))
  210. send_group($chatid, $message_id, $response);
  211. }
  212.  
  213. //sendMessage group
  214. if (isset($message_data["promotion"])) {
  215. if($trustgroup == $trustedgroup){
  216. $chatid = $message_data["chat"]["id"];
  217. $message_id = $message_data["message_id"];
  218. $promotion = "hallo";
  219. }
  220. $response = create_response($promotion, $message_data);
  221. if (!empty($response))
  222. send_group2($chatid, $message_id, $response);
  223. }
  224.  
  225. return $updateid;
  226. }
  227.  
  228. $entityBody = file_get_contents('php://input');
  229. $message = json_decode($entityBody, true);
  230. process_message($message);
  231.  
  232. ?>
Add Comment
Please, Sign In to add comment