Skip to content

Commit 28bd0f9

Browse files
committed
Added 'whois' command, integrated with 'chats' command
1 parent 452c344 commit 28bd0f9

File tree

4 files changed

+198
-12
lines changed

4 files changed

+198
-12
lines changed

src/Commands/AdminCommands/ChatsCommand.php

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,13 @@ public function execute()
4141
$text = trim($message->getText(true));
4242

4343
$results = DB::selectChats(
44-
true, //Send to groups (group chat)
45-
true, //Send to supergroups (single chat)
46-
true, //Send to users (single chat)
44+
true, //Select groups (group chat)
45+
true, //Select supergroups (super group chat)
46+
true, //Select users (single chat)
4747
null, //'yyyy-mm-dd hh:mm:ss' date range from
48-
null //'yyyy-mm-dd hh:mm:ss' date range to
48+
null, //'yyyy-mm-dd hh:mm:ss' date range to
49+
null, //Specific chat_id to select
50+
($text === '' || $text == '*') ? null : $text //Text to search in user/group name
4951
);
5052

5153
$user_chats = 0;
@@ -65,21 +67,26 @@ public function execute()
6567
$result['id'] = $result['chat_id'];
6668
$chat = new Chat($result);
6769

68-
if ($chat->isPrivateChat() && ($text === '' || $text == '*' || strpos(strtolower($chat->tryMention()), strtolower($text)) !== false || strpos(strtolower($chat->getFirstName()), strtolower($text)) !== false || strpos(strtolower($chat->getLastName()), strtolower($text)) !== false)) {
70+
$whois = $chat->getId();
71+
if ($this->telegram->getCommandObject('whois')) {
72+
$whois = '/whois' . str_replace('-', 'g', $chat->getId()); //We can't use '-' in command because part of it will become unclickable
73+
}
74+
75+
if ($chat->isPrivateChat()) {
6976
if ($text != '') {
70-
$text_back .= '- P ' . $chat->tryMention() . ' (' . $chat->getId() . ')' . "\n";
77+
$text_back .= '- P ' . $chat->tryMention() . ' [' . $whois . ']' . "\n";
7178
}
7279

7380
++$user_chats;
74-
} elseif ($chat->isSuperGroup() && ($text === '' || $text == '*' || strpos(strtolower($chat->tryMention()), strtolower($text)) !== false)) {
81+
} elseif ($chat->isSuperGroup()) {
7582
if ($text != '') {
76-
$text_back .= '- S ' . $chat->getTitle() . ' (' . $chat->getId() . ')' . "\n";
83+
$text_back .= '- S ' . $chat->getTitle() . ' [' . $whois . ']' . "\n";
7784
}
7885

7986
++$super_group_chats;
80-
} elseif ($chat->isGroupChat() && ($text === '' || $text == '*' || strpos(strtolower($chat->tryMention()), strtolower($text)) !== false)) {
87+
} elseif ($chat->isGroupChat()) {
8188
if ($text != '') {
82-
$text_back .= '- G ' . $chat->getTitle() . ' (' . $chat->getId() . ')' . "\n";
89+
$text_back .= '- G ' . $chat->getTitle() . ' [' . $whois . ']' . "\n";
8390
}
8491

8592
++$group_chats;
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
<?php
2+
/**
3+
* This file is part of the TelegramBot package.
4+
*
5+
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*
10+
* Written by Jack'lul <[email protected]>
11+
*/
12+
13+
namespace Longman\TelegramBot\Commands\AdminCommands;
14+
15+
use Longman\TelegramBot\Commands\AdminCommand;
16+
use Longman\TelegramBot\DB;
17+
use Longman\TelegramBot\Entities\Chat;
18+
use Longman\TelegramBot\Request;
19+
20+
/**
21+
* Admin "/whois" command
22+
*/
23+
class WhoisCommand extends AdminCommand
24+
{
25+
/**#@+
26+
* {@inheritdoc}
27+
*/
28+
protected $name = 'whois';
29+
protected $description = 'Lookup user or group info';
30+
protected $usage = '/whois <id> or /whois <search string>';
31+
protected $version = '1.1.0';
32+
protected $need_mysql = true;
33+
/**#@-*/
34+
35+
/**
36+
* {@inheritdoc}
37+
*/
38+
public function execute()
39+
{
40+
$message = $this->getMessage();
41+
42+
$chat_id = $message->getChat()->getId();
43+
$command = $message->getCommand();
44+
$text = trim($message->getText(true));
45+
46+
$data = [ 'chat_id' => $chat_id ];
47+
48+
//No point in replying to messages in private chats
49+
if (!$message->getChat()->isPrivateChat()) {
50+
$data['reply_to_message_id'] = $message->getMessageId();
51+
}
52+
53+
if ($command !== 'whois') {
54+
$text = substr($command, 5);
55+
56+
//We need that '-' now, bring it back
57+
if ((substr($text, 0, 1) == 'g')) {
58+
$text = str_replace('g', '-', $text);
59+
}
60+
}
61+
62+
if ($text === '') {
63+
$text = 'Provide the id to lookup: /whois <id>';
64+
} else {
65+
$user_id = $text;
66+
67+
if (is_numeric($text)) {
68+
$result = DB::selectChats(
69+
true, //Select groups (group chat)
70+
true, //Select supergroups (super group chat)
71+
true, //Select users (single chat)
72+
null, //'yyyy-mm-dd hh:mm:ss' date range from
73+
null, //'yyyy-mm-dd hh:mm:ss' date range to
74+
$user_id //Specific chat_id to select
75+
);
76+
77+
$result = $result[0];
78+
} else {
79+
$results = DB::selectChats(
80+
true, //Select groups (group chat)
81+
true, //Select supergroups (super group chat)
82+
true, //Select users (single chat)
83+
null, //'yyyy-mm-dd hh:mm:ss' date range from
84+
null, //'yyyy-mm-dd hh:mm:ss' date range to
85+
null, //Specific chat_id to select
86+
$text //Text to search in user/group name
87+
);
88+
89+
if (is_array($results) && count($results) == 1) {
90+
$result = $results[0];
91+
}
92+
}
93+
94+
if (is_array($result)) {
95+
$result['id'] = $result['chat_id'];
96+
$chat = new Chat($result);
97+
98+
$user_id = $result['id'];
99+
$created_at = $result['chat_created_at'];
100+
$updated_at = $result['chat_updated_at'];
101+
$old_id = $result['old_id'];
102+
}
103+
104+
if ($chat != null) {
105+
if ($chat->isPrivateChat()) {
106+
$text = 'User ID: ' . $user_id . "\n";
107+
$text .= 'Name: ' . $chat->getFirstName() . ' ' . $chat->getLastName() . "\n";
108+
109+
if ($chat->getUsername() != '') {
110+
$text .= 'Username: @' . $chat->getUsername() . "\n";
111+
}
112+
113+
$text .= 'First time seen: ' . $created_at . "\n";
114+
$text .= 'Last activity: ' . $updated_at . "\n";
115+
116+
//Code from Whoami command
117+
$limit = 10;
118+
$offset = null;
119+
$ServerResponse = Request::getUserProfilePhotos([
120+
'user_id' => $user_id ,
121+
'limit' => $limit,
122+
'offset' => $offset,
123+
]);
124+
125+
if ($ServerResponse->isOk()) {
126+
$UserProfilePhoto = $ServerResponse->getResult();
127+
$totalcount = $UserProfilePhoto->getTotalCount();
128+
} else {
129+
$totalcount = 0;
130+
}
131+
132+
if ($totalcount > 0) {
133+
$photos = $UserProfilePhoto->getPhotos();
134+
$photo = $photos[0][2];
135+
$file_id = $photo->getFileId();
136+
137+
$data['photo'] = $file_id;
138+
$data['caption'] = $text;
139+
140+
return Request::sendPhoto($data);
141+
}
142+
} elseif ($chat->isGroupChat()) {
143+
$text = 'Chat ID: ' . $user_id . (!empty($old_id) ? ' (previously: '.$old_id.')' : ''). "\n";
144+
$text .= 'Type: ' . ucfirst($chat->getType()) . "\n";
145+
$text .= 'Title: ' . $chat->getTitle() . "\n";
146+
$text .= 'Bot added to group: ' . $created_at . "\n";
147+
$text .= 'Last activity: ' . $updated_at . "\n";
148+
}
149+
} elseif (is_array($results) && count($results) > 1) {
150+
$text = 'Multiple chats matched!';
151+
} else {
152+
$text = 'Chat not found!';
153+
}
154+
}
155+
156+
$data['text'] = $text;
157+
return Request::sendMessage($data);
158+
}
159+
}

src/Commands/SystemCommands/GenericCommand.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,13 @@ public function execute()
3434
$message = $this->getMessage();
3535

3636
//You can use $command as param
37-
$command = $message->getCommand();
3837
$chat_id = $message->getChat()->getId();
38+
$user_id = $message->getFrom()->getId();
39+
$command = $message->getCommand();
40+
41+
if (in_array($user_id, $this->telegram->getAdminList()) && strtolower(substr($command, 0, 5)) == 'whois') {
42+
return $this->telegram->executeCommand('whois', $this->update);
43+
}
3944

4045
$data = [
4146
'chat_id' => $chat_id,

src/DB.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,8 @@ public static function insertMessageRequest(Message &$message)
691691
/**
692692
* Select Group and single Chats
693693
*
694+
* @todo Seems to not return anything when $select_users = false
695+
*
694696
* @param bool $select_groups
695697
* @param bool $select_super_groups
696698
* @param bool $select_users
@@ -704,7 +706,9 @@ public static function selectChats(
704706
$select_super_groups = true,
705707
$select_users = true,
706708
$date_from = null,
707-
$date_to = null
709+
$date_to = null,
710+
$chat_id = null,
711+
$text = null
708712
) {
709713
if (!self::isDbConnected()) {
710714
return false;
@@ -717,6 +721,7 @@ public static function selectChats(
717721
try {
718722
$query = 'SELECT * ,
719723
' . TB_CHAT . '.`id` AS `chat_id`,
724+
' . TB_CHAT . '.`created_at` AS `chat_created_at`,
720725
' . TB_CHAT . '.`updated_at` AS `chat_updated_at`,
721726
' . TB_USER . '.`id` AS `user_id`
722727
FROM `' . TB_CHAT . '` LEFT JOIN `' . TB_USER . '`
@@ -749,6 +754,16 @@ public static function selectChats(
749754
$tokens[':date_to'] = $date_to;
750755
}
751756

757+
if (! is_null($chat_id)) {
758+
$where[] = TB_CHAT . '.`id` = :chat_id';
759+
$tokens[':chat_id'] = $chat_id;
760+
}
761+
762+
if (! is_null($text)) {
763+
$where[] = '(LOWER('.TB_CHAT . '.`title`) LIKE :text OR LOWER(' . TB_USER . '.`first_name`) LIKE :text OR LOWER(' . TB_USER . '.`last_name`) LIKE :text OR LOWER(' . TB_USER . '.`username`) LIKE :text)';
764+
$tokens[':text'] = '%'.strtolower($text).'%';
765+
}
766+
752767
$a = 0;
753768
foreach ($where as $part) {
754769
if ($a) {

0 commit comments

Comments
 (0)