Skip to content

Add /whois command #150

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions src/Commands/AdminCommands/ChatsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
159 changes: 159 additions & 0 deletions src/Commands/AdminCommands/WhoisCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
<?php
/**
* This file is part of the TelegramBot package.
*
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Written by Jack'lul <[email protected]>
*/

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 <id> or /whois <search string>';
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 <id>';
} 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 .= 'First time 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);
}
}
7 changes: 6 additions & 1 deletion src/Commands/SystemCommands/GenericCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
49 changes: 40 additions & 9 deletions src/DB.php
Original file line number Diff line number Diff line change
Expand Up @@ -704,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;
Expand All @@ -717,26 +719,40 @@ public static function selectChats(
try {
$query = 'SELECT * ,
' . TB_CHAT . '.`id` AS `chat_id`,
' . TB_CHAT . '.`updated_at` AS `chat_updated_at`,
' . TB_USER . '.`id` AS `user_id`
FROM `' . TB_CHAT . '` LEFT JOIN `' . TB_USER . '`
ON ' . TB_CHAT . '.`id`=' . TB_USER . '.`id`';
' . TB_CHAT . '.`created_at` AS `chat_created_at`,
' . TB_CHAT . '.`updated_at` AS `chat_updated_at`
' .
(($select_users) ? ', ' . TB_USER . '.`id` AS `user_id` FROM `' . TB_CHAT . '` LEFT JOIN `' . TB_USER . '`
ON ' . TB_CHAT . '.`id`=' . TB_USER . '.`id`' : 'FROM `' . TB_CHAT . '`');

//Building parts of query
$chat_or_user = '';
$where = [];
$tokens = [];

if (!$select_groups || !$select_users || !$select_super_groups) {
$chat_or_user = '';

if ($select_groups) {
$where[] = TB_CHAT . '.`type` = "group"';
$chat_or_user .= TB_CHAT . '.`type` = "group"';
}

if ($select_super_groups) {
$where[] = TB_CHAT . '.`type` = "supergroup"';
if (!empty($chat_or_user)) {
$chat_or_user .= ' OR ';
}

$chat_or_user .= TB_CHAT . '.`type` = "supergroup"';
}

if ($select_users) {
$where[] = TB_CHAT . '.`type` = "private"';
if (!empty($chat_or_user)) {
$chat_or_user .= ' OR ';
}

$chat_or_user .= TB_CHAT . '.`type` = "private"';
}

$where[] = '(' . $chat_or_user . ')';
}

if (! is_null($date_from)) {
Expand All @@ -749,6 +765,21 @@ 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)) {
if ($select_users) {
$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)';
} else {
$where[] = 'LOWER('.TB_CHAT . '.`title`) LIKE :text';
}

$tokens[':text'] = '%'.strtolower($text).'%';
}

$a = 0;
foreach ($where as $part) {
if ($a) {
Expand Down