Advertisement
CaFc_Versace

Bot Telegram SimSimi

Jul 21st, 2015
21,483
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.81 KB | None | 0 0
  1. <?php
  2.   /** dwisiswanto.my.id **/
  3.   /***********************/
  4.  
  5. $aing = array(
  6.     "tkn"=>"...", // isi dengan token dari akun bot Anda
  7.     "log"=>"piro.txt",
  8.     "udh"=>"simpen.txt"
  9. );
  10.  
  11. # untuk mengirim pesan dengan metode cURL
  12. function hajar($yuerel, $dataAing = null) {
  13.     $cuih = curl_init();
  14.     curl_setopt($cuih, CURLOPT_URL, $yuerel);
  15.     if ($dataAing != null){
  16.         curl_setopt($cuih, CURLOPT_POST, true);
  17.         curl_setopt($cuih, CURLOPT_POSTFIELDS, $dataAing);
  18.     }
  19.     curl_setopt($cuih, CURLOPT_FOLLOWLOCATION, true);
  20.     curl_setopt($cuih, CURLOPT_RETURNTRANSFER, true);
  21.     curl_setopt($cuih, CURLOPT_SSL_VERIFYPEER, false);
  22.     curl_setopt($cuih, CURLOPT_COOKIEFILE, 'cookie.txt');
  23.     curl_setopt($cuih, CURLOPT_COOKIEJAR, 'bot.txt');
  24.     curl_setopt($cuih, CURLOPT_COOKIESESSION, true);
  25.     $eks = curl_exec($cuih);
  26.     curl_close($cuih);
  27.     return $eks;
  28. }
  29.  
  30. # untuk menyimpan yang sudah dikirimkan pesan
  31. function simpen($dmn, $apa) {
  32.     fwrite(fopen($dmn, "a+"), $apa . PHP_EOL);
  33.     fclose(fopen($dmn, "a+"));
  34. }
  35.  
  36. $upd = json_decode(hajar("https://api.telegram.org/bot" . $aing['tkn'] . "/getUpdates"), true); // mengambil pesan dari akun bot
  37. $buka = fopen($aing['log'], "r");
  38. $scan = fscanf($buka, "%d");
  39.  
  40. # jika mengambil pesan dari bot bernilai true
  41. if ($upd['ok'] == 1) {
  42.     for ($brp = $scan[0]; $brp<=count($upd['result']); $brp++) {
  43.         $psn = $upd['result'][$brp - 1]['message'];
  44.         $simi = json_decode(hajar("http://www.simsimi.com/requestChat?lc=id&ft=1.0&req=" . urlencode($psn['text'])), true); // mengirimkan pesan dari akun bot ke server SimSimi
  45.  
  46.         if (ereg($psn['date'], file_get_contents($aing['udh']))) { # jika pesan sudah dibalas
  47.            echo "[" . $brp . "/" . count($upd['result']) . "] Udah pernah di kirim.\n";
  48.         } elseif (preg_match("/I HAVE NO RESPONSE./i", $simi['res'])) { # jika server SimSimi tidak mendapatkan balasan untuk pesan dari akun bot, maka akan mengirim pesan secara acak bahwa akun bot tidak mengerti
  49.            echo "[" . $brp . "/" . count($upd['result']) . "] Uh, oh! Tidak ada respon untuk pesan \"" . $psn['text'] . "\". Mencoba mengirim pesan sibuk: ";
  50.             $sibuk = array("Ngomong apaan sih?", "Maksudnya?", "Apaan itu?", "Aku gak ngerti maksudnya kak.", "Sakkarepmu!", "Mbuh!", "Kalo ngomong yang jelas dong.", "Lu lagi ngomong?", "Ogah banget ngeladenin chat lu.");
  51.             $krm = json_decode(hajar("https://api.telegram.org/bot" . $aing['tkn'] . "/sendMessage", array(
  52.                 "chat_id"=>$psn['chat']['id'],
  53.                 "text"=>$sibuk[array_rand($sibuk)],
  54.                 "reply_to_message_id"=>$psn['message_id'])
  55.             ), true);
  56.             if ($krm['ok'] == 1) {
  57.                 echo "DONE.\n";
  58.                 simpen($aing['udh'], $psn['date']);
  59.             } else {
  60.                 echo "FAIL!\n";
  61.             }
  62.         } else {
  63.             # ini kondisi dimana server SimSimi menerima pesan bot dengan baik
  64.            $krm = json_decode(hajar("https://api.telegram.org/bot" . $aing['tkn'] . "/sendMessage", array(
  65.                 "chat_id"=>$psn['chat']['id'],
  66.                 "text"=>$simi['res'],
  67.                 "reply_to_message_id"=>$psn['message_id'])), true);
  68.             if ($krm['ok'] == 1) {
  69.                 echo "[" . $brp . "/" . count($upd['result']) . "] Berhasil mengirim ke " . $psn['from']['first_name'] . " " . $psn['from']['last_name'] . " (" . $psn['from']['id'] . ")\n";
  70.                 simpen($aing['udh'], $psn['date']);
  71.             } else {
  72.                 echo "[" . $brp . "/" . count($upd['result']) . "] " . $krm['description'] . "\n";
  73.             }
  74.         }
  75.     }
  76.     file_put_contents($aing['log'], count($upd['result'])); // me-replace daftar log untuk limit
  77. } else {
  78.     # jika gagal mengambil pesan dari bot
  79.    echo $upd['description'];
  80.     exit;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement