BimoSora

bot

Oct 23rd, 2020
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.32 KB | None | 0 0
  1. <?php
  2. require_once('/home/webgonit/id/bot/Emoji.php');
  3. try{
  4. $db = new PDO ("mysql:host=localhost;dbname=bottelegram;","gonit","b0bc1b62");
  5. }catch(PDOException $e){
  6. echo $e->getmessage();
  7. }
  8.  
  9. $bot_user = $db->prepare("SELECT * FROM bot_user");
  10. $bot_user->execute();
  11. while($data = $bot_user->fetch(PDO::FETCH_ASSOC)){
  12.  
  13. $usernamebot = $data['bot_username'];
  14. $botToken = $data['token_bot'];
  15.  
  16.  
  17. $bot_message = $db->prepare("SELECT * FROM bot_message");
  18. $bot_message->execute();
  19. while($data2 = $bot_message->fetch(PDO::FETCH_ASSOC)){
  20.  
  21. $debug = false;
  22.  
  23. function request_url($method)
  24. {
  25. global $botToken;
  26. return "https://api.telegram.org/bot" . $botToken . "/". $method;
  27. }
  28.  
  29. function send_reply($chatid, $msgid, $text)
  30. {
  31. global $debug;
  32. $send = array(
  33. 'chat_id' => $chatid,
  34. 'text' => $text,
  35. 'parse_mode' => 'html',
  36. 'disable_web_page_preview' => true,
  37. );
  38. // use key 'http' even if you send the request to https://...
  39. $options = array(
  40. 'http' => array(
  41. 'header' => "Content-type: application/x-www-form-urlencoded\r\n",
  42. 'method' => 'POST',
  43. 'content' => http_build_query($send),
  44. ),
  45. );
  46. $context = stream_context_create($options);
  47. $result = file_get_contents(request_url('sendMessage'), false, $context);
  48.  
  49. if ($debug)
  50. print_r($result);
  51. }
  52.  
  53. function send_group($chatid, $msgid2, $text)
  54. {
  55. global $debug;
  56. $send2 = array(
  57. 'chat_id' => $chatid,
  58. 'text' => $text,
  59. 'reply_to_message_id' => $msgid2, // <---- biar ada reply nya balasannya, opsional, bisa dihapus baris ini
  60. 'parse_mode' => 'html',
  61. );
  62. // use key 'http' even if you send the request to https://...
  63. $options2 = array(
  64. 'http' => array(
  65. 'header' => "Content-type: application/x-www-form-urlencoded\r\n",
  66. 'method' => 'POST',
  67. 'content' => http_build_query($send2),
  68. ),
  69. );
  70. $context2 = stream_context_create($options2);
  71. $result2 = file_get_contents(request_url('sendMessage'), false, $context2);
  72.  
  73. if ($debug)
  74. print_r($result2);
  75. }
  76.  
  77. function send_group2($chatid, $msgid3, $text)
  78. {
  79. global $debug;
  80. $send3 = array(
  81. 'chat_id' => $chatid,
  82. 'text' => $text,
  83. 'parse_mode' => 'html',
  84. );
  85. // use key 'http' even if you send the request to https://...
  86. $options3 = array(
  87. 'http' => array(
  88. 'header' => "Content-type: application/x-www-form-urlencoded\r\n",
  89. 'method' => 'POST',
  90. 'content' => http_build_query($send3),
  91. ),
  92. );
  93. $context3 = stream_context_create($options3);
  94. $result3 = file_get_contents(request_url('sendMessage'), false, $context3);
  95.  
  96. if ($debug)
  97. print_r($result3);
  98. }
  99.  
  100. // fungsi mengolahan pesan, menyiapkan pesan untuk dikirimkan
  101.  
  102. function create_response($text, $message)
  103. {
  104. global $usernamebot;
  105. // inisiasi variable hasil yang mana merupakan hasil olahan pesan
  106. $hasil = '';
  107.  
  108. $fromid = $message["from"]["id"]; // variable penampung id user
  109. $chatid = $message["chat"]["id"]; // variable penampung id chat
  110. $pesanid= $message['message_id']; // variable penampung id message
  111.  
  112. // variable penampung username nya user
  113. isset($message["from"]["username"])
  114. ? $chatuser = $message["from"]["username"]
  115. : $chatuser = '';
  116.  
  117. // variable penampung nama user
  118.  
  119. isset($message["from"]["last_name"])
  120. ? $namakedua = $message["from"]["last_name"]
  121. : $namakedua = '';
  122. $namauser = $message["from"]["first_name"]. ' ' .$namakedua;
  123.  
  124. //Disini di koreksi karena new chat member tidak menampung teks dibuat kondisi
  125. if(isset($message['new_chat_member'])){
  126. $hasil = $text;
  127. return $hasil; //Hasil langsung direturn supaya kode dibawah tidak dieksekusi
  128. }
  129.  
  130. // ini saya pergunakan untuk menghapus kelebihan pesan spasi yang dikirim ke bot.
  131. $textur = preg_replace('/\s\s+/', ' ', $text);
  132.  
  133. // memecah pesan dalam 2 blok array, kita ambil yang array pertama saja
  134. $command = explode(' ',$textur,2); //
  135.  
  136. // identifikasi perintah (yakni kata pertama, atau array pertamanya)
  137. switch ($command[0]) {
  138.  
  139. // pesan selamat datang
  140. case '/start':
  141. $hasil = "<b>$namauser</b> \n";
  142. $hasil .= "Selamat datang di $usernamebot \n\n";
  143. $hasil .= "/start - Mulai bot \n";
  144. $hasil .= "/id - Untuk cek nama, username dan id Anda \n";
  145. $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";
  146. break;
  147.  
  148. // jika ada pesan /id, bot akan membalas dengan menyebutkan idnya user
  149. case '/id':
  150. $hasil = "Name: <b>$namauser</b> \n";
  151. $hasil .= "Username: <b>@$chatuser</b> \n";
  152. $hasil .= "My ID: <b>$fromid</b> \n";
  153. break;
  154.  
  155. case '/timeworld'.$input2:
  156. $input = "$text";
  157. $input2 = substr($input, 10);
  158. if ($input > "/timeworld $input2") {
  159. $regex = preg_match('/\/timeworld (.*)/', $input, $hasil);
  160. date_default_timezone_set($hasil[1]);
  161. $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";
  162. $hasil .= "Tanggal: ";
  163. $hasil .= "<b>";
  164. $hasil .= date('d/m/Y', time());
  165. $hasil .= "</b>";
  166. $hasil .= "\n";
  167. $hasil .= "Waktu: ";
  168. $hasil .= "<b>";
  169. $hasil .= date('H:i:s', time());
  170. $hasil .= "</b>";
  171. $hasil .= "\n";
  172. $hasil .= "Zona: ";
  173. $hasil .= "<b>";
  174. $hasil .= date_default_timezone_get();
  175. $hasil .= "</b>";
  176. } else if ($input == "/timeworld") {
  177. $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)";
  178. }
  179. }
  180. return $hasil;
  181. }
  182.  
  183. function process_message($message)
  184. {
  185.  
  186. try{
  187. $db2 = new PDO ("mysql:host=localhost;dbname=bottelegram;","gonit","b0bc1b62");
  188. }catch(PDOException $e){
  189. echo $e->getmessage();
  190. }
  191.  
  192. //trusted chatid
  193. $bot_user2 = $db2->prepare("SELECT * FROM bot_user");
  194. $bot_user2->execute();
  195. while($data3 = $bot_user2->fetch(PDO::FETCH_ASSOC)){
  196. $trustedid = $data3["trustedid"];
  197.  
  198. //trusted chatgroup
  199. $bot_message3 = $db2->prepare("SELECT * FROM bot_message");
  200. $bot_message3->execute();
  201. while($data4 = $bot_message3->fetch(PDO::FETCH_ASSOC)){
  202. $trustedgroup = $data4["message_chatid"];
  203.  
  204. $updateid = $message["update_id"];
  205. $messages = $message["message"];
  206. $trusted = $message["message"]["chat"]["id"];
  207. $trustgroup = $message["message"]["chat"]["id"];
  208. $jenis = $message["message"]["chat"]["type"];
  209.  
  210. if (isset($messages["text"])) {
  211. /*
  212. if($trusted == $trustedid){
  213. */
  214. if($jenis == "private"){
  215. $chatid = $messages["chat"]["id"];
  216. $message_id = $messages["message_id"];
  217. $text = $messages["text"];
  218. }
  219. /*
  220. }
  221. */
  222. $response = create_response($text, $messages);
  223. if (!empty($response))
  224. send_reply($chatid, $message_id, $response);
  225. }
  226.  
  227. //welcome group
  228. if (isset($messages["new_chat_member"])) {
  229. $chatid = $messages["chat"]["id"];
  230. if($chatid == $trustedgroup) {
  231. $message_id = $messages["message_id"];
  232. $firstName = $messages["new_chat_member"]["first_name"];
  233. $title = $messages["chat"]["title"];
  234. $new_chat_member = "Selamat datang <b>$firstName</b> di $title \n\n ";
  235. $new_chat_member .= date("d/m/Y, H:i:s");
  236. }
  237. $response = create_response($new_chat_member, $messages);
  238. if (!empty($response))
  239. send_group($chatid, $message_id, $response);
  240. }
  241.  
  242. return $updateid;
  243. }
  244. }
  245. }
  246.  
  247. $entityBody = file_get_contents('php://input');
  248. $message = json_decode($entityBody, true);
  249. process_message($message);
  250.  
  251. ?>
  252.  
  253. <?php
  254. $pesan = $data2['message_run'];
  255. $pesan2 = Emoji::Decode($pesan);
  256. $website = 'https://api.telegram.org/bot'.$botToken;
  257. $chatId2 = $data2['message_chatid'];
  258. $params = [
  259. 'chat_id' => $chatId2,
  260. 'parse_mode' => 'HTML',
  261. 'text' => $pesan2,
  262. 'disable_web_page_preview' => true,
  263. ];
  264.  
  265. $datenow = date('H:i');
  266. $delay = $data2['message_delay'];
  267. $daterun = $data2['message_daterun'];
  268. $timerun = $data2['message_timerun'];
  269. if ($datenow == $daterun){
  270. //sendMessage
  271. $ch = curl_init($website . '/sendMessage');
  272. curl_setopt($ch, CURLOPT_HEADER, false);
  273. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  274. curl_setopt($ch, CURLOPT_POST, 1);
  275. curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
  276. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  277. $messageid = curl_exec($ch);
  278. $messageid2 = json_decode($messageid, true);
  279. curl_close($ch);
  280.  
  281. /* print_r($messageid2); */
  282.  
  283. //pinChatMessage
  284. $id = $messageid2['result']['message_id'];
  285. $chatidbot = $messageid2['result']['chat']['id'];
  286.  
  287. $params2 = [
  288. 'chat_id' => $chatidbot,
  289. 'message_id' => $id,
  290. 'disable_notification' => false,
  291. ];
  292.  
  293. $ch2 = curl_init($website . '/pinChatMessage');
  294. curl_setopt($ch2, CURLOPT_HEADER, false);
  295. curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
  296. curl_setopt($ch2, CURLOPT_POST, 1);
  297. curl_setopt($ch2, CURLOPT_POSTFIELDS, $params2);
  298. curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, false);
  299. $messageid3 = curl_exec($ch2);
  300. $messageid4 = json_decode($messageid3, true);
  301. curl_close($ch2);
  302.  
  303. /* print_r($messageid4); */
  304.  
  305. $message_timerun = date("H:i", time() + $delay);
  306. $bot_message2 = $db->prepare("UPDATE bot_message SET message_timerun = :message_timerun");
  307. $bot_message2->BindParam(':message_timerun',$message_timerun);
  308. $bot_message2->execute();
  309.  
  310. } else if ($datenow == $timerun){
  311. //sendMessage
  312. $ch = curl_init($website . '/sendMessage');
  313. curl_setopt($ch, CURLOPT_HEADER, false);
  314. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  315. curl_setopt($ch, CURLOPT_POST, 1);
  316. curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
  317. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  318. $messageid = curl_exec($ch);
  319. $messageid2 = json_decode($messageid, true);
  320. curl_close($ch);
  321.  
  322. /* print_r($messageid2); */
  323.  
  324. //pinChatMessage
  325. $id = $messageid2['result']['message_id'];
  326. $chatidbot = $messageid2['result']['chat']['id'];
  327.  
  328. $params2 = [
  329. 'chat_id' => $chatidbot,
  330. 'message_id' => $id,
  331. 'disable_notification' => false,
  332. ];
  333.  
  334. $ch2 = curl_init($website . '/pinChatMessage');
  335. curl_setopt($ch2, CURLOPT_HEADER, false);
  336. curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
  337. curl_setopt($ch2, CURLOPT_POST, 1);
  338. curl_setopt($ch2, CURLOPT_POSTFIELDS, $params2);
  339. curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, false);
  340. $messageid3 = curl_exec($ch2);
  341. $messageid4 = json_decode($messageid3, true);
  342. curl_close($ch2);
  343.  
  344. /* print_r($messageid4); */
  345.  
  346. $message_timerun = date("H:i", time() + $delay);
  347. $bot_message2 = $db->prepare("UPDATE bot_message SET message_timerun = :message_timerun");
  348. $bot_message2->BindParam(':message_timerun',$message_timerun);
  349. $bot_message2->execute();
  350.  
  351. } else {
  352.  
  353. }
  354.  
  355. ?>
  356. <?php } ?>
  357. <?php } ?>
Add Comment
Please, Sign In to add comment