From b2ed2949f61884e6fa76c5e53e61d146976363b1 Mon Sep 17 00:00:00 2001 From: Artem Vasilev Date: Fri, 1 Mar 2024 02:05:58 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9E=D0=BF=D0=B8=D1=81=D0=B0=D0=BB=20=D0=BA?= =?UTF-8?q?=D0=BE=D0=BC=D0=B0=D0=BD=D0=B4=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Commands/UserCommands/IdCommand.php | 60 +++++++++++++++++++++++-- 1 file changed, 57 insertions(+), 3 deletions(-) diff --git a/src/Commands/UserCommands/IdCommand.php b/src/Commands/UserCommands/IdCommand.php index 52da8dd..aa475dc 100644 --- a/src/Commands/UserCommands/IdCommand.php +++ b/src/Commands/UserCommands/IdCommand.php @@ -3,7 +3,9 @@ namespace Webmasterskaya\TelegramBotCommands\Commands\UserCommands; use Longman\TelegramBot\Commands as BotCommands; +use Longman\TelegramBot\Entities\Entity; use Longman\TelegramBot\Entities\ServerResponse; +use Longman\TelegramBot\Entities\User; use Longman\TelegramBot\Request; class IdCommand extends BotCommands\UserCommand @@ -16,12 +18,64 @@ class IdCommand extends BotCommands\UserCommand protected $version = '1.0.0'; - protected $private_only = true; + protected $private_only = false; public function execute(): ServerResponse { - // TODO: Implement execute() method. + $message = $this->getMessage() ?: $this->getChannelPost(); - return Request::EmptyResponse(); + if ($message->getFrom()->getIsBot() && is_null($message->getSenderChat())) { + return Request::emptyResponse(); + } + + $chat = $message->getChat(); + + $data = [ + 'chat_id' => $chat->getId(), + 'reply_to_message_id' => $message->getMessageId(), + 'parse_mode' => 'MarkdownV2' + ]; + + /** @noinspection PhpUndefinedMethodInspection */ + if (!empty(@$message->getExternalReply())) { + $data['text'] = sprintf('*Error*: %s', Entity::escapeMarkdownV2('Replies 2.0 not supported.')); + return Request::sendMessage($data); + } + + $response_data = []; + + if($chat->isPrivateChat()){ + $response_data['Your ID'] = $message->getFrom()->getId() ?: $chat->getId(); + } else { + $response_data['This chat ID'] = $chat->getId(); + } + + if (!is_null($reply = $message->getReplyToMessage())) { + $message = $reply; + if (!$chat->isPrivateChat()) { + $form = $message->getSenderChat() ?: $message->getFrom(); + $form_type = ($form instanceof User) + ? ($form->getIsBot() ? 'bot' : 'user') + : $form->getType(); + + $response_data["Message from $form_type ID"] = $form->getId(); + } + } + + if (!is_null($forward = $message->getForwardFrom() ?: $message->getForwardFromChat())) { + $forward_from_type = ($forward instanceof User) + ? ($forward->getIsBot() ? 'bot' : 'user') + : $forward->getType(); + $response_data["Forward from $forward_from_type ID"] = $forward->getId(); + } + + $data['text'] = ''; + foreach ($response_data as $title => $value) { + $data['text'] .= sprintf('*%s*: `%s`', + Entity::escapeMarkdownV2($title), + Entity::escapeMarkdownV2($value)) . PHP_EOL; + } + + return Request::sendMessage($data); } } \ No newline at end of file