Advertisement
tristamoff

Untitled

Feb 19th, 2017
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.10 KB | None | 0 0
  1. //запись в лог
  2. function teleToLog($log) {
  3.   $myFile = 'log.txt';
  4.   $fh = fopen($myFile, 'a') or die('can\'t open file');
  5.   if ((is_array($log)) || (is_object($log))) {
  6.     $updateArray = print_r($log, TRUE);
  7.     fwrite($fh, $updateArray."\n");
  8.   } else {
  9.     fwrite($fh, $log . "\n");
  10.   }
  11.   fclose($fh);
  12. }
  13.  
  14. //обработка запроса от пользователя
  15. function getUserRequest($text, $chat_id) {
  16.   $resp = commIsHello($text);
  17.   if (!empty($resp)) {
  18.     $resp['chat_id'] = $chat_id;
  19.     requestToTelegram($resp);
  20.     return TRUE;
  21.   }
  22.   $resp = commIsUser($text);
  23.   if (!empty($resp)) {
  24.     $resp['chat_id'] = $chat_id;
  25.     requestToTelegram($resp);
  26.     return TRUE;
  27.   }
  28. }
  29.  
  30. //проверка на приветствие
  31. function commIsHello($text) {
  32.   $hello = array();
  33.   $hello[] = 'привет';
  34.   $hello[] = 'хай';
  35.   $hello[] = 'здорова';
  36.   $hello[] = 'здравствуйте';
  37.   $hello[] = 'здрасьте';
  38.   $hello[] = 'йо';
  39.  
  40.   $bot_hello = array();
  41.   $bot_hello[] = 'И тебе привет';
  42.   $bot_hello[] = 'Привет от голоса';
  43.   $bot_hello[] = 'Доброго времени суток';
  44.   $bot_hello[] = 'Привет привет';
  45.  
  46.   if (in_array(mb_strtolower($text), $hello)) {
  47.     //пользователь поздоровался.
  48.     //случайная фраза привет от бота
  49.     $bot_resp = $bot_hello[rand(0, (count($bot_hello) - 1))];
  50.     $data = array(
  51.       'text' => $bot_resp,
  52.     );
  53.     return $data;
  54.   }
  55.   return NULL;
  56. }
  57.  
  58. //проверка на ник
  59. function commIsUser($text) {
  60.   $text = trim($text);//обрезаем пробелы в начале и в конце
  61.   $space = strpos($text, ' ');
  62.   if (($space === FALSE) && (mb_substr($text, 0, 1) == '@')) {
  63.     //возможно это ник пользователя
  64.     //подключаемся к блокчейну
  65.     require('vendor/autoload.php');
  66.     $client = new WebSocket\Client("wss://ws.golos.io/");
  67.     $req = json_encode(
  68.       [
  69.         'id' => 1, 'method' => 'get_accounts', 'params' => [[mb_substr($text, 1)]]
  70.       ]
  71.     );
  72.     $client->send($req);
  73.     $golos_resp = $client->receive();
  74.     $resp_object = json_decode($golos_resp);
  75.     if (!empty($resp_object->result)) {
  76.       $obj = $resp_object->result[0];
  77.       $user = array();
  78.       $user[] = 'ID: ' . $obj->id;
  79.       $user[] = 'Логин: ' . $obj->name;
  80.       $user[] = 'Аккаунт создан: ' . $obj->created;
  81.       $user[] = 'Последний раз голосовал: ' . $obj->last_vote_time;
  82.       $user[] = 'Голосов: ' . $obj->balance;
  83.       $user[] = 'Золота: ' . $obj->sbd_balance;
  84.       $user[] = 'Создано постов: ' . $obj->post_count;
  85.  
  86.       //расчёт репутации
  87.       $reputation = $obj->reputation;
  88.       $user[] = 'Репутация: ' . round((max(log10(abs($reputation)) - 9,0) * (($reputation >= 0) ? 1 : -1) * 9 + 25), 3);
  89.  
  90.       $json_metadata = json_decode($obj->json_metadata);
  91.       if (!empty($json_metadata->user_image)) {
  92.         //фото
  93.         // передавать не буду, так как у некоторых логинов "заколдованные" аватары и сообщение в телеграм не приходит
  94.        // $user[] = 'Аватар: ' . $json_metadata->user_image;
  95.       }
  96.       $text = implode("\n", $user);
  97.  
  98.       $data = array(
  99.         'text' => $text,
  100.         'parse_mode' => 'Markdown',
  101.       );
  102.     }
  103.     else {
  104.       $data = array(
  105.         'text' => 'Пользователь не найден.',
  106.       );
  107.     }
  108.     $client->close();
  109.  
  110.     if (!empty($data)) {
  111.       return $data;
  112.     }
  113.   }
  114.   return NULL;
  115. }
  116.  
  117. //отправка запроса в чат
  118. function requestToTelegram($data, $type = 'sendMessage') {
  119.   if( $curl = curl_init() ) {
  120.     curl_setopt($curl, CURLOPT_URL, API_URL . $type);
  121.     curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
  122.     curl_setopt($curl, CURLOPT_POST, TRUE);
  123.     curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  124.     curl_exec($curl);
  125.     curl_close($curl);
  126.   }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement