|
| 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 | +} |
0 commit comments