Commit b1d24af5 authored by MBoretto's avatar MBoretto

Merge branch 'develop' of https://github.com/akalongman/php-telegram-bot into develop

parents d04cd3f2 692bd605
...@@ -41,11 +41,13 @@ class ChatsCommand extends AdminCommand ...@@ -41,11 +41,13 @@ class ChatsCommand extends AdminCommand
$text = trim($message->getText(true)); $text = trim($message->getText(true));
$results = DB::selectChats( $results = DB::selectChats(
true, //Send to groups (group chat) true, //Select groups (group chat)
true, //Send to supergroups (single chat) true, //Select supergroups (super group chat)
true, //Send to users (single 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 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; $user_chats = 0;
...@@ -65,21 +67,26 @@ class ChatsCommand extends AdminCommand ...@@ -65,21 +67,26 @@ class ChatsCommand extends AdminCommand
$result['id'] = $result['chat_id']; $result['id'] = $result['chat_id'];
$chat = new Chat($result); $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 != '') { if ($text != '') {
$text_back .= '- P ' . $chat->tryMention() . ' (' . $chat->getId() . ')' . "\n"; $text_back .= '- P ' . $chat->tryMention() . ' [' . $whois . ']' . "\n";
} }
++$user_chats; ++$user_chats;
} elseif ($chat->isSuperGroup() && ($text === '' || $text == '*' || strpos(strtolower($chat->tryMention()), strtolower($text)) !== false)) { } elseif ($chat->isSuperGroup()) {
if ($text != '') { if ($text != '') {
$text_back .= '- S ' . $chat->getTitle() . ' (' . $chat->getId() . ')' . "\n"; $text_back .= '- S ' . $chat->getTitle() . ' [' . $whois . ']' . "\n";
} }
++$super_group_chats; ++$super_group_chats;
} elseif ($chat->isGroupChat() && ($text === '' || $text == '*' || strpos(strtolower($chat->tryMention()), strtolower($text)) !== false)) { } elseif ($chat->isGroupChat()) {
if ($text != '') { if ($text != '') {
$text_back .= '- G ' . $chat->getTitle() . ' (' . $chat->getId() . ')' . "\n"; $text_back .= '- G ' . $chat->getTitle() . ' [' . $whois . ']' . "\n";
} }
++$group_chats; ++$group_chats;
......
<?php
/**
* This file is part of the TelegramBot package.
*
* (c) Avtandil Kikabidze aka LONGMAN <akalongman@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Written by Jack'lul <jacklul@jacklul.com>
*/
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);
}
}
...@@ -34,8 +34,13 @@ class GenericCommand extends SystemCommand ...@@ -34,8 +34,13 @@ class GenericCommand extends SystemCommand
$message = $this->getMessage(); $message = $this->getMessage();
//You can use $command as param //You can use $command as param
$command = $message->getCommand();
$chat_id = $message->getChat()->getId(); $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 = [ $data = [
'chat_id' => $chat_id, 'chat_id' => $chat_id,
......
...@@ -704,7 +704,9 @@ class DB ...@@ -704,7 +704,9 @@ class DB
$select_super_groups = true, $select_super_groups = true,
$select_users = true, $select_users = true,
$date_from = null, $date_from = null,
$date_to = null $date_to = null,
$chat_id = null,
$text = null
) { ) {
if (!self::isDbConnected()) { if (!self::isDbConnected()) {
return false; return false;
...@@ -717,26 +719,40 @@ class DB ...@@ -717,26 +719,40 @@ class DB
try { try {
$query = 'SELECT * , $query = 'SELECT * ,
' . TB_CHAT . '.`id` AS `chat_id`, ' . TB_CHAT . '.`id` AS `chat_id`,
' . TB_CHAT . '.`updated_at` AS `chat_updated_at`, ' . TB_CHAT . '.`created_at` AS `chat_created_at`,
' . TB_USER . '.`id` AS `user_id` ' . TB_CHAT . '.`updated_at` AS `chat_updated_at`
FROM `' . TB_CHAT . '` LEFT JOIN `' . TB_USER . '` ' .
ON ' . TB_CHAT . '.`id`=' . TB_USER . '.`id`'; (($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 //Building parts of query
$chat_or_user = '';
$where = []; $where = [];
$tokens = []; $tokens = [];
if (!$select_groups || !$select_users || !$select_super_groups) { if (!$select_groups || !$select_users || !$select_super_groups) {
$chat_or_user = '';
if ($select_groups) { if ($select_groups) {
$where[] = TB_CHAT . '.`type` = "group"'; $chat_or_user .= TB_CHAT . '.`type` = "group"';
} }
if ($select_super_groups) { 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) { 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)) { if (! is_null($date_from)) {
...@@ -749,6 +765,21 @@ class DB ...@@ -749,6 +765,21 @@ class DB
$tokens[':date_to'] = $date_to; $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; $a = 0;
foreach ($where as $part) { foreach ($where as $part) {
if ($a) { if ($a) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment