-
-
Notifications
You must be signed in to change notification settings - Fork 958
Expand file tree
/
Copy pathWhoisCommand.php
More file actions
190 lines (159 loc) · 6.04 KB
/
Copy pathWhoisCommand.php
File metadata and controls
190 lines (159 loc) · 6.04 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
/**
* This file is part of the TelegramBot package.
*
* (c) Avtandil Kikabidze aka LONGMAN <akalongman@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Written by Jack'lul <jacklul@jacklul.com>
*/
namespace PhpTelegramBot\Core\Commands\AdminCommands;
use PhpTelegramBot\Core\Commands\AdminCommand;
use PhpTelegramBot\Core\DB;
use PhpTelegramBot\Core\Entities\Chat;
use PhpTelegramBot\Core\Entities\PhotoSize;
use PhpTelegramBot\Core\Entities\ServerResponse;
use PhpTelegramBot\Core\Entities\UserProfilePhotos;
use PhpTelegramBot\Core\Exception\TelegramException;
use PhpTelegramBot\Core\Request;
/**
* Admin "/whois" command
*/
class WhoisCommand extends AdminCommand
{
/**
* @var string
*/
protected $name = 'whois';
/**
* @var string
*/
protected $description = 'Lookup user or group info';
/**
* @var string
*/
protected $usage = '/whois <id> or /whois <search string>';
/**
* @var string
*/
protected $version = '1.3.0';
/**
* @var bool
*/
protected $need_mysql = true;
/**
* Command execute method
*
* @return ServerResponse
* @throws TelegramException
*/
public function execute()
{
$message = $this->getMessage();
$chat_id = $message->getChat()->getId();
$command = $message->getCommand();
$text = trim($message->getText(true));
$data = ['chat_id' => $chat_id];
//No point in replying to messages in private chats
if (!$message->getChat()->isPrivateChat()) {
$data['reply_to_message_id'] = $message->getMessageId();
}
if ($command !== 'whois') {
$text = substr($command, 5);
//We need that '-' now, bring it back
if (strpos($text, 'g') === 0) {
$text = str_replace('g', '-', $text);
}
}
if ($text === '') {
$text = 'Provide the id to lookup: /whois <id>';
} else {
$user_id = $text;
$chat = null;
$created_at = null;
$updated_at = null;
$result = null;
if (is_numeric($text)) {
$results = DB::selectChats([
'groups' => true,
'supergroups' => true,
'channels' => true,
'users' => true,
'chat_id' => $user_id, //Specific chat_id to select
]);
if (!empty($results)) {
$result = reset($results);
}
} else {
$results = DB::selectChats([
'groups' => true,
'supergroups' => true,
'channels' => true,
'users' => true,
'text' => $text //Text to search in user/group name
]);
if (is_array($results) && count($results) === 1) {
$result = reset($results);
}
}
if (is_array($result)) {
$result['id'] = $result['chat_id'];
$result['username'] = $result['chat_username'];
$chat = new Chat($result);
$user_id = $result['id'];
$created_at = $result['chat_created_at'];
$updated_at = $result['chat_updated_at'];
$old_id = $result['old_id'];
}
if ($chat !== null) {
if ($chat->isPrivateChat()) {
$text = 'User ID: ' . $user_id . PHP_EOL;
$text .= 'Name: ' . $chat->getFirstName() . ' ' . $chat->getLastName() . PHP_EOL;
$username = $chat->getUsername();
if ($username !== null && $username !== '') {
$text .= 'Username: @' . $username . PHP_EOL;
}
$text .= 'First time seen: ' . $created_at . PHP_EOL;
$text .= 'Last activity: ' . $updated_at . PHP_EOL;
//Code from Whoami command
$limit = 10;
$offset = null;
$response = Request::getUserProfilePhotos(
[
'user_id' => $user_id,
'limit' => $limit,
'offset' => $offset,
]
);
if ($response->isOk()) {
/** @var UserProfilePhotos $user_profile_photos */
$user_profile_photos = $response->getResult();
if ($user_profile_photos->getTotalCount() > 0) {
$photos = $user_profile_photos->getPhotos();
/** @var PhotoSize $photo */
$photo = $photos[0][2];
$file_id = $photo->getFileId();
$data['photo'] = $file_id;
$data['caption'] = $text;
return Request::sendPhoto($data);
}
}
} elseif ($chat->isGroupChat()) {
$text = 'Chat ID: ' . $user_id . (!empty($old_id) ? ' (previously: ' . $old_id . ')' : '') . PHP_EOL;
$text .= 'Type: ' . ucfirst($chat->getType()) . PHP_EOL;
$text .= 'Title: ' . $chat->getTitle() . PHP_EOL;
$text .= 'First time added to group: ' . $created_at . PHP_EOL;
$text .= 'Last activity: ' . $updated_at . PHP_EOL;
}
} elseif (is_array($results) && count($results) > 1) {
$text = 'Multiple chats matched!';
} else {
$text = 'Chat not found!';
}
}
$data['text'] = $text;
return Request::sendMessage($data);
}
}