src/Service/TelegramConnector.php line 38

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use TelegramBot\Api\Client;
  4. use TelegramBot\Api\Types\ReplyKeyboardMarkup;
  5. use \TelegramBot\Api\Types\Inline\InlineKeyboardMarkup;
  6. use Psr\Log\LoggerInterface;
  7. use TelegramBot\Api\BotApi;
  8. class TelegramConnector implements TelegramConnectorInterface
  9. {
  10.     /**
  11.      * @var LoggerInterface
  12.      */
  13.     private $logger;
  14.     private $telegram_token;
  15.     private $bot_username;
  16.     private $hook_url;
  17.     private $telegramBot;
  18.     private $rawDataJson;
  19.     private $message;
  20.     private $textMessage;
  21.     private $chat_id;
  22.     private $user_id;
  23.     private $latitude;
  24.     public function __construct(LoggerInterface $logger)
  25.     {
  26.         $this->logger $logger;
  27.         $this->telegram_token $_ENV['TELEGRAM_TOKEN'];
  28.         $this->bot_username   $_ENV['USERNAME_BOT'];
  29.         $this->hook_url       'https://' $_ENV['DOMAIN'];
  30.     }
  31.     //получение и возврат данных
  32.     public function receiveTgData()
  33.     {
  34.         $this->telegramBot = new Client($this->telegram_token);
  35.         $this->rawDataJson $this->telegramBot->getRawBody();
  36.         $update json_decode($this->rawDataJsontrue);
  37.         $this->message $update["message"];
  38.         if (array_key_exists('text'$this->message)) {
  39.             $this->textMessage $this->removeEmoji($this->message["text"]);
  40.         } else {
  41.             $this->textMessage "Отмена";
  42.         }
  43.         if (array_key_exists('location'$this->message)) {
  44.             $this->latitude =  $this->message['location']['latitude'];
  45.             $this->longitude $this->message['location']['longitude'];
  46.         }
  47.         $this->chat_id $this->message["chat"]["id"];
  48.         $this->user_id $this->message["from"]["id"];
  49.         return $this;
  50.     }
  51.     public function sendMessage($messageText$keyboard null)
  52.     {
  53.         $bot = new BotApi($this->telegram_token);
  54.         if ($keyboard) {
  55.             $keyboard = new ReplyKeyboardMarkup($keyboardtruetrue);
  56.         }
  57.         $bot->sendMessage($this->chat_id$messageText'HTML'falsenull$keyboard);
  58.     }
  59.     public function sendPhoto($photo$caption null)
  60.     {
  61.         $bot = new BotApi($this->telegram_token);
  62.         $bot->sendPhoto($this->chat_id$photo$caption);
  63.     }
  64.     /**
  65.      * @return mixed
  66.      */
  67.     public function getMessage()
  68.     {
  69.         return $this->message;
  70.     }
  71.     /**
  72.      * @param mixed $message
  73.      */
  74.     public function setMessage($message): void
  75.     {
  76.         $this->message $message;
  77.     }
  78.     /**
  79.      * @return mixed
  80.      */
  81.     public function getTextMessage()
  82.     {
  83.         return $this->textMessage;
  84.     }
  85.     /**
  86.      * @param mixed $textMessage
  87.      */
  88.     public function setTextMessage($textMessage): void
  89.     {
  90.         $this->textMessage $textMessage;
  91.     }
  92.     /**
  93.      * @return mixed
  94.      */
  95.     public function getChatId()
  96.     {
  97.         return $this->chat_id;
  98.     }
  99.     /**
  100.      * @param mixed $chat_id
  101.      */
  102.     public function setChatId($chat_id): void
  103.     {
  104.         $this->chat_id $chat_id;
  105.     }
  106.     /**
  107.      * @return mixed
  108.      */
  109.     public function getUserId()
  110.     {
  111.         return $this->user_id;
  112.     }
  113.     /**
  114.      * @param mixed $user_id
  115.      */
  116.     public function setUserId($user_id): void
  117.     {
  118.         $this->user_id $user_id;
  119.     }
  120.     /**
  121.      * @return mixed
  122.      */
  123.     public function getLatitude()
  124.     {
  125.         return $this->latitude;
  126.     }
  127.     /**
  128.      * @return mixed
  129.      */
  130.     public function getLongitude()
  131.     {
  132.         return $this->longitude;
  133.     }
  134.     private $longitude;
  135.     private function removeEmoji($text)
  136.     {
  137.         $clean_text "";
  138.         // Match Emoticons
  139.         $regexEmoticons '/[\x{1F600}-\x{1F64F}]/u';
  140.         $clean_text preg_replace($regexEmoticons''$text);
  141.         // Match Miscellaneous Symbols and Pictographs
  142.         $regexSymbols '/[\x{1F300}-\x{1F5FF}]/u';
  143.         $clean_text preg_replace($regexSymbols''$clean_text);
  144.         // Match Transport And Map Symbols
  145.         $regexTransport '/[\x{1F680}-\x{1F6FF}]/u';
  146.         $clean_text preg_replace($regexTransport''$clean_text);
  147.         // Match Miscellaneous Symbols
  148.         $regexMisc '/[\x{2600}-\x{26FF}]/u';
  149.         $clean_text preg_replace($regexMisc''$clean_text);
  150.         // Match Dingbats
  151.         $regexDingbats '/[\x{2700}-\x{27BF}]/u';
  152.         $clean_text preg_replace($regexDingbats''$clean_text);
  153.         // Match Flags
  154.         $regexDingbats '/[\x{1F1E6}-\x{1F1FF}]/u';
  155.         $clean_text preg_replace($regexDingbats''$clean_text);
  156.         // Others
  157.         $regexDingbats '/[\x{1F910}-\x{1F95E}]/u';
  158.         $clean_text preg_replace($regexDingbats''$clean_text);
  159.         $regexDingbats '/[\x{1F980}-\x{1F991}]/u';
  160.         $clean_text preg_replace($regexDingbats''$clean_text);
  161.         $regexDingbats '/[\x{1F9C0}]/u';
  162.         $clean_text preg_replace($regexDingbats''$clean_text);
  163.         $regexDingbats '/[\x{1F9F9}]/u';
  164.         $clean_text preg_replace($regexDingbats''$clean_text);
  165.         return trim($clean_text);
  166.     }
  167. }