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