Skip to content

Commit b1d24af

Browse files
committed
Merge branch 'develop' of https://github.com/akalongman/php-telegram-bot into develop
2 parents d04cd3f + 692bd60 commit b1d24af

File tree

4 files changed

+222
-20
lines changed

4 files changed

+222
-20
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 .= 'First time 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: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,9 @@ public static function selectChats(
704704
$select_super_groups = true,
705705
$select_users = true,
706706
$date_from = null,
707-
$date_to = null
707+
$date_to = null,
708+
$chat_id = null,
709+
$text = null
708710
) {
709711
if (!self::isDbConnected()) {
710712
return false;
@@ -717,26 +719,40 @@ public static function selectChats(
717719
try {
718720
$query = 'SELECT * ,
719721
' . TB_CHAT . '.`id` AS `chat_id`,
720-
' . TB_CHAT . '.`updated_at` AS `chat_updated_at`,
721-
' . TB_USER . '.`id` AS `user_id`
722-
FROM `' . TB_CHAT . '` LEFT JOIN `' . TB_USER . '`
723-
ON ' . TB_CHAT . '.`id`=' . TB_USER . '.`id`';
722+
' . TB_CHAT . '.`created_at` AS `chat_created_at`,
723+
' . TB_CHAT . '.`updated_at` AS `chat_updated_at`
724+
' .
725+
(($select_users) ? ', ' . TB_USER . '.`id` AS `user_id` FROM `' . TB_CHAT . '` LEFT JOIN `' . TB_USER . '`
726+
ON ' . TB_CHAT . '.`id`=' . TB_USER . '.`id`' : 'FROM `' . TB_CHAT . '`');
724727

725728
//Building parts of query
726-
$chat_or_user = '';
727729
$where = [];
728730
$tokens = [];
729731

730732
if (!$select_groups || !$select_users || !$select_super_groups) {
733+
$chat_or_user = '';
734+
731735
if ($select_groups) {
732-
$where[] = TB_CHAT . '.`type` = "group"';
736+
$chat_or_user .= TB_CHAT . '.`type` = "group"';
733737
}
738+
734739
if ($select_super_groups) {
735-
$where[] = TB_CHAT . '.`type` = "supergroup"';
740+
if (!empty($chat_or_user)) {
741+
$chat_or_user .= ' OR ';
742+
}
743+
744+
$chat_or_user .= TB_CHAT . '.`type` = "supergroup"';
736745
}
746+
737747
if ($select_users) {
738-
$where[] = TB_CHAT . '.`type` = "private"';
748+
if (!empty($chat_or_user)) {
749+
$chat_or_user .= ' OR ';
750+
}
751+
752+
$chat_or_user .= TB_CHAT . '.`type` = "private"';
739753
}
754+
755+
$where[] = '(' . $chat_or_user . ')';
740756
}
741757

742758
if (! is_null($date_from)) {
@@ -749,6 +765,21 @@ public static function selectChats(
749765
$tokens[':date_to'] = $date_to;
750766
}
751767

768+
if (! is_null($chat_id)) {
769+
$where[] = TB_CHAT . '.`id` = :chat_id';
770+
$tokens[':chat_id'] = $chat_id;
771+
}
772+
773+
if (! is_null($text)) {
774+
if ($select_users) {
775+
$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)';
776+
} else {
777+
$where[] = 'LOWER('.TB_CHAT . '.`title`) LIKE :text';
778+
}
779+
780+
$tokens[':text'] = '%'.strtolower($text).'%';
781+
}
782+
752783
$a = 0;
753784
foreach ($where as $part) {
754785
if ($a) {

0 commit comments

Comments
 (0)