AlexDeb

bot

Jan 27th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.89 KB | None | 0 0
  1. <?php
  2.  
  3. class Telebot
  4. {
  5.     private $token;
  6.  
  7.     public function __construct($token)
  8.     {
  9.         $this->token = $token;
  10.     }
  11.  
  12.     private function get($s)
  13.     {
  14.         $ch = curl_init();
  15.         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  16.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  17.         $r = curl_exec($ch);
  18.         if ($r == NULL) {
  19.             return "Ошибка :" . curl_errno($ch) . "-" . curl_error($ch);
  20.         } else {
  21.             return $r;
  22.         }
  23.         curl_close($ch);
  24.     }
  25.  
  26.     public function log($str)
  27.     {
  28.         file_put_contents("log.txt", "[" . date('M-d-y_H:i:s') . "]:" . $str . "\n", FILE_APPEND);
  29.     }
  30.  
  31.     public function update_id($s)
  32.     {
  33.         switch ($s) {
  34.             case 'r' : //чтение последнего обновления
  35.                 if (file_exists("update_id.txt")) {
  36.                     return file_get_contents("update_id.txt");
  37.                 } else {
  38.                     return 0;
  39.                 }
  40.                 break;
  41.  
  42.             default :
  43.                 file_put_contents("update_id.txt", $s);
  44.                 break;
  45.         }
  46.  
  47.     }
  48.  
  49.     public function GetUpdates($offset = 0)
  50.     {
  51.         if (!$this->token) {
  52.             return 21;
  53.  
  54.         }
  55.         $r = json_decode($this->get("https://api.telegram.org/" . $this->token . "/getUpdates?offset=" . $offset), true);
  56.         return $r['result'];
  57.     }
  58.  
  59.     public function SMessage($w, $s, $keyboard = true)
  60.     {
  61.         if ($this->token) {
  62.             return 21;
  63.         }
  64.         if (is_array($keyboard)) {
  65.             $reply = json_encode(array("keyboard" => $keyboard, "resize_keyboard" => true, "one_time_keyboard" => true));
  66.             return $this->get("https://api.telegram.org/" . $this->token . "/sendMessage?" . http_build_query(array("chat_id" => $w, "text" => $s, "reply_markup" => $reply)));
  67.         }
  68.         return $this->get("https://api.telegram.org/" . $this->token . "/sendMessage?" . http_build_query(array("chat_id" => $w, "text" => $s)));
  69.     }
  70. }
  71.  
  72. ////////////////////////////////////////////////////////////////////////////////////////////////
  73. /////////////////////////////////////////////////////////////////////////////////////////////////
  74. ////////////////////////////////////////////////////////////////////////////////////////////
  75. <?php
  76.  
  77. include 'class.php';
  78.  
  79. $c =new Telebot('263229842:AAGKcUo0BKpTThrkaxShRxXg6QNr3mqAC_c');
  80. $r = $c->GetUpdates($c->update_id('r'));
  81.  
  82. foreach ($r as $i => $v){
  83.     if ($c->update_id('r') < $r[$i]['update_id']){
  84.         //сохранение последнего обновления
  85.         $c->update_id($r[$i]['update_id']);
  86.  
  87.         $chatID = $r[$i]['message']['chat']['id'];
  88.         $name = $r[$i]['message']['from']['first_name'];
  89.         $login = $r[$i]['message']['from']['username'];
  90.         $chatID = $r[$i]['message']['from']['id'];
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment