diff --git a/src/Commands/AdminCommands/ChatsCommand.php b/src/Commands/AdminCommands/ChatsCommand.php index d1174f02f..3f3c0d13f 100644 --- a/src/Commands/AdminCommands/ChatsCommand.php +++ b/src/Commands/AdminCommands/ChatsCommand.php @@ -41,11 +41,13 @@ public function execute() $text = trim($message->getText(true)); $results = DB::selectChats( - true, //Send to groups (group chat) - true, //Send to supergroups (single chat) - true, //Send to users (single chat) + true, //Select groups (group chat) + true, //Select supergroups (super group chat) + true, //Select users (single chat) null, //'yyyy-mm-dd hh:mm:ss' date range from - null //'yyyy-mm-dd hh:mm:ss' date range to + null, //'yyyy-mm-dd hh:mm:ss' date range to + null, //Specific chat_id to select + ($text === '' || $text == '*') ? null : $text //Text to search in user/group name ); $user_chats = 0; @@ -65,21 +67,26 @@ public function execute() $result['id'] = $result['chat_id']; $chat = new Chat($result); - 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)) { + $whois = $chat->getId(); + if ($this->telegram->getCommandObject('whois')) { + $whois = '/whois' . str_replace('-', 'g', $chat->getId()); //We can't use '-' in command because part of it will become unclickable + } + + if ($chat->isPrivateChat()) { if ($text != '') { - $text_back .= '- P ' . $chat->tryMention() . ' (' . $chat->getId() . ')' . "\n"; + $text_back .= '- P ' . $chat->tryMention() . ' [' . $whois . ']' . "\n"; } ++$user_chats; - } elseif ($chat->isSuperGroup() && ($text === '' || $text == '*' || strpos(strtolower($chat->tryMention()), strtolower($text)) !== false)) { + } elseif ($chat->isSuperGroup()) { if ($text != '') { - $text_back .= '- S ' . $chat->getTitle() . ' (' . $chat->getId() . ')' . "\n"; + $text_back .= '- S ' . $chat->getTitle() . ' [' . $whois . ']' . "\n"; } ++$super_group_chats; - } elseif ($chat->isGroupChat() && ($text === '' || $text == '*' || strpos(strtolower($chat->tryMention()), strtolower($text)) !== false)) { + } elseif ($chat->isGroupChat()) { if ($text != '') { - $text_back .= '- G ' . $chat->getTitle() . ' (' . $chat->getId() . ')' . "\n"; + $text_back .= '- G ' . $chat->getTitle() . ' [' . $whois . ']' . "\n"; } ++$group_chats; diff --git a/src/Commands/AdminCommands/WhoisCommand.php b/src/Commands/AdminCommands/WhoisCommand.php new file mode 100644 index 000000000..54c42e069 --- /dev/null +++ b/src/Commands/AdminCommands/WhoisCommand.php @@ -0,0 +1,159 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + * Written by Jack'lul + */ + +namespace Longman\TelegramBot\Commands\AdminCommands; + +use Longman\TelegramBot\Commands\AdminCommand; +use Longman\TelegramBot\DB; +use Longman\TelegramBot\Entities\Chat; +use Longman\TelegramBot\Request; + +/** + * Admin "/whois" command + */ +class WhoisCommand extends AdminCommand +{ + /**#@+ + * {@inheritdoc} + */ + protected $name = 'whois'; + protected $description = 'Lookup user or group info'; + protected $usage = '/whois or /whois '; + protected $version = '1.1.0'; + protected $need_mysql = true; + /**#@-*/ + + /** + * {@inheritdoc} + */ + 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 ((substr($text, 0, 1) == 'g')) { + $text = str_replace('g', '-', $text); + } + } + + if ($text === '') { + $text = 'Provide the id to lookup: /whois '; + } else { + $user_id = $text; + + if (is_numeric($text)) { + $result = DB::selectChats( + true, //Select groups (group chat) + true, //Select supergroups (super group chat) + true, //Select users (single chat) + null, //'yyyy-mm-dd hh:mm:ss' date range from + null, //'yyyy-mm-dd hh:mm:ss' date range to + $user_id //Specific chat_id to select + ); + + $result = $result[0]; + } else { + $results = DB::selectChats( + true, //Select groups (group chat) + true, //Select supergroups (super group chat) + true, //Select users (single chat) + null, //'yyyy-mm-dd hh:mm:ss' date range from + null, //'yyyy-mm-dd hh:mm:ss' date range to + null, //Specific chat_id to select + $text //Text to search in user/group name + ); + + if (is_array($results) && count($results) == 1) { + $result = $results[0]; + } + } + + if (is_array($result)) { + $result['id'] = $result['chat_id']; + $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 . "\n"; + $text .= 'Name: ' . $chat->getFirstName() . ' ' . $chat->getLastName() . "\n"; + + if ($chat->getUsername() != '') { + $text .= 'Username: @' . $chat->getUsername() . "\n"; + } + + $text .= 'First time seen: ' . $created_at . "\n"; + $text .= 'Last activity: ' . $updated_at . "\n"; + + //Code from Whoami command + $limit = 10; + $offset = null; + $ServerResponse = Request::getUserProfilePhotos([ + 'user_id' => $user_id , + 'limit' => $limit, + 'offset' => $offset, + ]); + + if ($ServerResponse->isOk()) { + $UserProfilePhoto = $ServerResponse->getResult(); + $totalcount = $UserProfilePhoto->getTotalCount(); + } else { + $totalcount = 0; + } + + if ($totalcount > 0) { + $photos = $UserProfilePhoto->getPhotos(); + $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.')' : ''). "\n"; + $text .= 'Type: ' . ucfirst($chat->getType()) . "\n"; + $text .= 'Title: ' . $chat->getTitle() . "\n"; + $text .= 'Bot added to group: ' . $created_at . "\n"; + $text .= 'Last activity: ' . $updated_at . "\n"; + } + } elseif (is_array($results) && count($results) > 1) { + $text = 'Multiple chats matched!'; + } else { + $text = 'Chat not found!'; + } + } + + $data['text'] = $text; + return Request::sendMessage($data); + } +} diff --git a/src/Commands/SystemCommands/GenericCommand.php b/src/Commands/SystemCommands/GenericCommand.php index 245b20e63..369495d13 100644 --- a/src/Commands/SystemCommands/GenericCommand.php +++ b/src/Commands/SystemCommands/GenericCommand.php @@ -34,8 +34,13 @@ public function execute() $message = $this->getMessage(); //You can use $command as param - $command = $message->getCommand(); $chat_id = $message->getChat()->getId(); + $user_id = $message->getFrom()->getId(); + $command = $message->getCommand(); + + if (in_array($user_id, $this->telegram->getAdminList()) && strtolower(substr($command, 0, 5)) == 'whois') { + return $this->telegram->executeCommand('whois', $this->update); + } $data = [ 'chat_id' => $chat_id, diff --git a/src/DB.php b/src/DB.php index 08fdb80a6..4fad1892c 100644 --- a/src/DB.php +++ b/src/DB.php @@ -689,6 +689,8 @@ public static function insertMessageRequest(Message &$message) /** * Select Group and single Chats * + * @todo Seems to not return anything when $select_users = false + * * @param bool $select_groups * @param bool $select_super_groups * @param bool $select_users @@ -702,7 +704,9 @@ public static function selectChats( $select_super_groups = true, $select_users = true, $date_from = null, - $date_to = null + $date_to = null, + $chat_id = null, + $text = null ) { if (!self::isDbConnected()) { return false; @@ -715,6 +719,7 @@ public static function selectChats( try { $query = 'SELECT * , ' . TB_CHAT . '.`id` AS `chat_id`, + ' . TB_CHAT . '.`created_at` AS `chat_created_at`, ' . TB_CHAT . '.`updated_at` AS `chat_updated_at`, ' . TB_USER . '.`id` AS `user_id` FROM `' . TB_CHAT . '` LEFT JOIN `' . TB_USER . '` @@ -747,6 +752,16 @@ public static function selectChats( $tokens[':date_to'] = $date_to; } + if (! is_null($chat_id)) { + $where[] = TB_CHAT . '.`id` = :chat_id'; + $tokens[':chat_id'] = $chat_id; + } + + if (! is_null($text)) { + $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)'; + $tokens[':text'] = '%'.strtolower($text).'%'; + } + $a = 0; foreach ($where as $part) { if ($a) {