<?php
namespace App\Service;
use TelegramBot\Api\Client;
use TelegramBot\Api\Types\ReplyKeyboardMarkup;
use \TelegramBot\Api\Types\Inline\InlineKeyboardMarkup;
use Psr\Log\LoggerInterface;
use TelegramBot\Api\BotApi;
class TelegramConnector implements TelegramConnectorInterface
{
/**
* @var LoggerInterface
*/
private $logger;
private $telegram_token;
private $bot_username;
private $hook_url;
private $telegramBot;
private $rawDataJson;
private $message;
private $textMessage;
private $chat_id;
private $user_id;
private $latitude;
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
$this->telegram_token = $_ENV['TELEGRAM_TOKEN'];
$this->bot_username = $_ENV['USERNAME_BOT'];
$this->hook_url = 'https://' . $_ENV['DOMAIN'];
}
//получение и возврат данных
public function receiveTgData()
{
$this->telegramBot = new Client($this->telegram_token);
$this->rawDataJson = $this->telegramBot->getRawBody();
$update = json_decode($this->rawDataJson, true);
$this->message = $update["message"];
if (array_key_exists('text', $this->message)) {
$this->textMessage = $this->removeEmoji($this->message["text"]);
} else {
$this->textMessage = "Отмена";
}
if (array_key_exists('location', $this->message)) {
$this->latitude = $this->message['location']['latitude'];
$this->longitude = $this->message['location']['longitude'];
}
$this->chat_id = $this->message["chat"]["id"];
$this->user_id = $this->message["from"]["id"];
return $this;
}
public function sendMessage($messageText, $keyboard = null)
{
$bot = new BotApi($this->telegram_token);
if ($keyboard) {
$keyboard = new ReplyKeyboardMarkup($keyboard, true, true);
}
$bot->sendMessage($this->chat_id, $messageText, 'HTML', false, null, $keyboard);
}
public function sendPhoto($photo, $caption = null)
{
$bot = new BotApi($this->telegram_token);
$bot->sendPhoto($this->chat_id, $photo, $caption);
}
/**
* @return mixed
*/
public function getMessage()
{
return $this->message;
}
/**
* @param mixed $message
*/
public function setMessage($message): void
{
$this->message = $message;
}
/**
* @return mixed
*/
public function getTextMessage()
{
return $this->textMessage;
}
/**
* @param mixed $textMessage
*/
public function setTextMessage($textMessage): void
{
$this->textMessage = $textMessage;
}
/**
* @return mixed
*/
public function getChatId()
{
return $this->chat_id;
}
/**
* @param mixed $chat_id
*/
public function setChatId($chat_id): void
{
$this->chat_id = $chat_id;
}
/**
* @return mixed
*/
public function getUserId()
{
return $this->user_id;
}
/**
* @param mixed $user_id
*/
public function setUserId($user_id): void
{
$this->user_id = $user_id;
}
/**
* @return mixed
*/
public function getLatitude()
{
return $this->latitude;
}
/**
* @return mixed
*/
public function getLongitude()
{
return $this->longitude;
}
private $longitude;
private function removeEmoji($text)
{
$clean_text = "";
// Match Emoticons
$regexEmoticons = '/[\x{1F600}-\x{1F64F}]/u';
$clean_text = preg_replace($regexEmoticons, '', $text);
// Match Miscellaneous Symbols and Pictographs
$regexSymbols = '/[\x{1F300}-\x{1F5FF}]/u';
$clean_text = preg_replace($regexSymbols, '', $clean_text);
// Match Transport And Map Symbols
$regexTransport = '/[\x{1F680}-\x{1F6FF}]/u';
$clean_text = preg_replace($regexTransport, '', $clean_text);
// Match Miscellaneous Symbols
$regexMisc = '/[\x{2600}-\x{26FF}]/u';
$clean_text = preg_replace($regexMisc, '', $clean_text);
// Match Dingbats
$regexDingbats = '/[\x{2700}-\x{27BF}]/u';
$clean_text = preg_replace($regexDingbats, '', $clean_text);
// Match Flags
$regexDingbats = '/[\x{1F1E6}-\x{1F1FF}]/u';
$clean_text = preg_replace($regexDingbats, '', $clean_text);
// Others
$regexDingbats = '/[\x{1F910}-\x{1F95E}]/u';
$clean_text = preg_replace($regexDingbats, '', $clean_text);
$regexDingbats = '/[\x{1F980}-\x{1F991}]/u';
$clean_text = preg_replace($regexDingbats, '', $clean_text);
$regexDingbats = '/[\x{1F9C0}]/u';
$clean_text = preg_replace($regexDingbats, '', $clean_text);
$regexDingbats = '/[\x{1F9F9}]/u';
$clean_text = preg_replace($regexDingbats, '', $clean_text);
return trim($clean_text);
}
}