|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of the PHP Telegram Support Bot. |
| 5 | + * |
| 6 | + * (c) PHP Telegram Bot Team (https://github.com/php-telegram-bot) |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace Longman\TelegramBot\Commands\UserCommands; |
| 15 | + |
| 16 | +use Longman\TelegramBot\Commands\UserCommand; |
| 17 | +use Longman\TelegramBot\Entities\ServerResponse; |
| 18 | +use Longman\TelegramBot\Exception\TelegramException; |
| 19 | +use Longman\TelegramBot\Request; |
| 20 | + |
| 21 | +/** |
| 22 | + * Display user and chat information. |
| 23 | + */ |
| 24 | +class IdCommand extends UserCommand |
| 25 | +{ |
| 26 | + /** |
| 27 | + * @var string |
| 28 | + */ |
| 29 | + protected $name = 'id'; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var string |
| 33 | + */ |
| 34 | + protected $description = 'Get all identifying information about the current user and chat'; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var string |
| 38 | + */ |
| 39 | + protected $version = '0.1.0'; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var bool |
| 43 | + */ |
| 44 | + protected $private_only = true; |
| 45 | + |
| 46 | + /** |
| 47 | + * @return ServerResponse |
| 48 | + * @throws TelegramException |
| 49 | + */ |
| 50 | + public function preExecute(): ServerResponse |
| 51 | + { |
| 52 | + $this->isPrivateOnly() && $this->removeNonPrivateMessage(); |
| 53 | + |
| 54 | + // Make sure we only reply to messages. |
| 55 | + if (!$this->getMessage()) { |
| 56 | + return Request::emptyResponse(); |
| 57 | + } |
| 58 | + |
| 59 | + return $this->execute(); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Execute command |
| 64 | + * |
| 65 | + * @return ServerResponse |
| 66 | + * @throws TelegramException |
| 67 | + */ |
| 68 | + public function execute(): ServerResponse |
| 69 | + { |
| 70 | + $user_info = '👤 *User Info*' . PHP_EOL . $this->getUserInfo(); |
| 71 | + $chat_info = '🗣 *Chat Info*' . PHP_EOL . $this->getChatInfo(); |
| 72 | + |
| 73 | + return $this->replyToUser($user_info . PHP_EOL . PHP_EOL . $chat_info, ['parse_mode' => 'markdown']); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Get the information of the user. |
| 78 | + * |
| 79 | + * @return string |
| 80 | + */ |
| 81 | + protected function getUserInfo(): string |
| 82 | + { |
| 83 | + $user = $this->getMessage()->getFrom(); |
| 84 | + |
| 85 | + return implode(PHP_EOL, [ |
| 86 | + "User Id: `{$user->getId()}`", |
| 87 | + 'First Name: ' . (($first_name = $user->getFirstName()) ? "`{$first_name}`" : '_n/a_'), |
| 88 | + 'Last Name: ' . (($last_name = $user->getLastName()) ? "`{$last_name}`" : '_n/a_'), |
| 89 | + 'Username: ' . (($username = $user->getUsername()) ? "`{$username}`" : '_n/a_'), |
| 90 | + 'Language Code: ' . (($language_code = $user->getLanguageCode()) ? "`{$language_code}`" : '_n/a_'), |
| 91 | + ]); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Get the information of the chat. |
| 96 | + * |
| 97 | + * @return string |
| 98 | + */ |
| 99 | + protected function getChatInfo(): string |
| 100 | + { |
| 101 | + $message = $this->getMessage(); |
| 102 | + $chat = $message->getForwardFromChat() ?? $message->getChat(); |
| 103 | + |
| 104 | + if (!$chat || $chat->isPrivateChat()) { |
| 105 | + return '`Private chat`'; |
| 106 | + } |
| 107 | + |
| 108 | + return implode(PHP_EOL, [ |
| 109 | + "Type: `{$chat->getType()}`", |
| 110 | + "Chat Id: `{$chat->getId()}`", |
| 111 | + 'Title: ' . (($title = $chat->getTitle()) ? "`{$title}`" : '_n/a_'), |
| 112 | + 'First Name: ' . (($first_name = $chat->getFirstName()) ? "`{$first_name}`" : '_n/a_'), |
| 113 | + 'Last Name: ' . (($last_name = $chat->getLastName()) ? "`{$last_name}`" : '_n/a_'), |
| 114 | + 'Username: ' . (($username = $chat->getUsername()) ? "`{$username}`" : '_n/a_'), |
| 115 | + ]); |
| 116 | + } |
| 117 | +} |
0 commit comments