Fix and improve Admin commands.

parent 68874bb7
...@@ -35,7 +35,7 @@ class ChatsCommand extends AdminCommand ...@@ -35,7 +35,7 @@ class ChatsCommand extends AdminCommand
/** /**
* @var string * @var string
*/ */
protected $version = '1.0.2'; protected $version = '1.1.0';
/** /**
* @var bool * @var bool
...@@ -46,6 +46,7 @@ class ChatsCommand extends AdminCommand ...@@ -46,6 +46,7 @@ class ChatsCommand extends AdminCommand
* Command execute method * Command execute method
* *
* @return mixed * @return mixed
* @throws \Longman\TelegramBot\Exception\TelegramException
*/ */
public function execute() public function execute()
{ {
...@@ -61,7 +62,7 @@ class ChatsCommand extends AdminCommand ...@@ -61,7 +62,7 @@ class ChatsCommand extends AdminCommand
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 null, //Specific chat_id to select
($text === '' || $text == '*') ? null : $text //Text to search in user/group name ($text === '' || $text === '*') ? null : $text //Text to search in user/group name
); );
$user_chats = 0; $user_chats = 0;
...@@ -70,54 +71,56 @@ class ChatsCommand extends AdminCommand ...@@ -70,54 +71,56 @@ class ChatsCommand extends AdminCommand
if ($text === '') { if ($text === '') {
$text_back = ''; $text_back = '';
} elseif ($text == '*') { } elseif ($text === '*') {
$text_back = 'List of all bot chats:' . "\n"; $text_back = 'List of all bot chats:' . PHP_EOL;
} else { } else {
$text_back = 'Chat search results:' . "\n"; $text_back = 'Chat search results:' . PHP_EOL;
} }
foreach ($results as $result) { if (is_array($results)) {
//Initialize a chat object foreach ($results as $result) {
$result['id'] = $result['chat_id']; //Initialize a chat object
$chat = new Chat($result); $result['id'] = $result['chat_id'];
$chat = new Chat($result);
$whois = $chat->getId(); $whois = $chat->getId();
if ($this->telegram->getCommandObject('whois')) { if ($this->telegram->getCommandObject('whois')) {
// We can't use '-' in command because part of it will become unclickable // We can't use '-' in command because part of it will become unclickable
$whois = '/whois' . str_replace('-', 'g', $chat->getId()); $whois = '/whois' . str_replace('-', 'g', $chat->getId());
}
if ($chat->isPrivateChat()) {
if ($text != '') {
$text_back .= '- P ' . $chat->tryMention() . ' [' . $whois . ']' . "\n";
} }
++$user_chats; if ($chat->isPrivateChat()) {
} elseif ($chat->isSuperGroup()) { if ($text !== '') {
if ($text != '') { $text_back .= '- P ' . $chat->tryMention() . ' [' . $whois . ']' . PHP_EOL;
$text_back .= '- S ' . $chat->getTitle() . ' [' . $whois . ']' . "\n"; }
}
++$super_group_chats; ++$user_chats;
} elseif ($chat->isGroupChat()) { } elseif ($chat->isSuperGroup()) {
if ($text != '') { if ($text !== '') {
$text_back .= '- G ' . $chat->getTitle() . ' [' . $whois . ']' . "\n"; $text_back .= '- S ' . $chat->getTitle() . ' [' . $whois . ']' . PHP_EOL;
} }
++$super_group_chats;
} elseif ($chat->isGroupChat()) {
if ($text !== '') {
$text_back .= '- G ' . $chat->getTitle() . ' [' . $whois . ']' . PHP_EOL;
}
++$group_chats; ++$group_chats;
}
} }
} }
if (($user_chats + $group_chats + $super_group_chats) === 0) { if (($user_chats + $group_chats + $super_group_chats) === 0) {
$text_back = 'No chats found..'; $text_back = 'No chats found..';
} else { } else {
$text_back .= "\n" . 'Private Chats: ' . $user_chats; $text_back .= PHP_EOL . 'Private Chats: ' . $user_chats;
$text_back .= "\n" . 'Group: ' . $group_chats; $text_back .= PHP_EOL . 'Groups: ' . $group_chats;
$text_back .= "\n" . 'Super Group: ' . $super_group_chats; $text_back .= PHP_EOL . 'Super Groups: ' . $super_group_chats;
$text_back .= "\n" . 'Total: ' . ($user_chats + $group_chats + $super_group_chats); $text_back .= PHP_EOL . 'Total: ' . ($user_chats + $group_chats + $super_group_chats);
if ($text === '') { if ($text === '') {
$text_back .= "\n\n" . 'List all chats: /' . $this->name . ' *' . "\n" . 'Search for chats: /' . $this->name . ' <search string>'; $text_back .= PHP_EOL . PHP_EOL . 'List all chats: /' . $this->name . ' *' . PHP_EOL . 'Search for chats: /' . $this->name . ' <search string>';
} }
} }
......
...@@ -11,6 +11,8 @@ ...@@ -11,6 +11,8 @@
namespace Longman\TelegramBot\Commands\AdminCommands; namespace Longman\TelegramBot\Commands\AdminCommands;
use Longman\TelegramBot\Commands\AdminCommand; use Longman\TelegramBot\Commands\AdminCommand;
use Longman\TelegramBot\Entities\Message;
use Longman\TelegramBot\Entities\ServerResponse;
use Longman\TelegramBot\Request; use Longman\TelegramBot\Request;
/** /**
...@@ -36,7 +38,7 @@ class SendtoallCommand extends AdminCommand ...@@ -36,7 +38,7 @@ class SendtoallCommand extends AdminCommand
/** /**
* @var string * @var string
*/ */
protected $version = '1.2.1'; protected $version = '1.3.0';
/** /**
* @var bool * @var bool
...@@ -47,6 +49,7 @@ class SendtoallCommand extends AdminCommand ...@@ -47,6 +49,7 @@ class SendtoallCommand extends AdminCommand
* Execute command * Execute command
* *
* @return boolean * @return boolean
* @throws \Longman\TelegramBot\Exception\TelegramException
*/ */
public function execute() public function execute()
{ {
...@@ -68,18 +71,21 @@ class SendtoallCommand extends AdminCommand ...@@ -68,18 +71,21 @@ class SendtoallCommand extends AdminCommand
null //'yyyy-mm-dd hh:mm:ss' date range to null //'yyyy-mm-dd hh:mm:ss' date range to
); );
$tot = 0; $total = 0;
$fail = 0; $failed = 0;
$text = 'Message sent to:' . "\n"; $text = 'Message sent to:' . "\n";
/** @var ServerResponse $result */
foreach ($results as $result) { foreach ($results as $result) {
$status = ''; $name = '';
$type = ''; $type = '';
if ($result->isOk()) { if ($result->isOk()) {
$status = '✔️'; $status = '✔️';
$ServerResponse = $result->getResult(); /** @var Message $message */
$chat = $ServerResponse->getChat(); $message = $result->getResult();
$chat = $message->getChat();
if ($chat->isPrivateChat()) { if ($chat->isPrivateChat()) {
$name = $chat->getFirstName(); $name = $chat->getFirstName();
$type = 'user'; $type = 'user';
...@@ -89,15 +95,15 @@ class SendtoallCommand extends AdminCommand ...@@ -89,15 +95,15 @@ class SendtoallCommand extends AdminCommand
} }
} else { } else {
$status = '✖️'; $status = '✖️';
++$fail; ++$failed;
} }
++$tot; ++$total;
$text .= $tot . ') ' . $status . ' ' . $type . ' ' . $name . "\n"; $text .= $total . ') ' . $status . ' ' . $type . ' ' . $name . "\n";
} }
$text .= 'Delivered: ' . ($tot - $fail) . '/' . $tot . "\n"; $text .= 'Delivered: ' . ($total - $failed) . '/' . $total . "\n";
if ($tot === 0) { if ($total === 0) {
$text = 'No users or chats found..'; $text = 'No users or chats found..';
} }
} }
......
...@@ -15,6 +15,8 @@ namespace Longman\TelegramBot\Commands\AdminCommands; ...@@ -15,6 +15,8 @@ namespace Longman\TelegramBot\Commands\AdminCommands;
use Longman\TelegramBot\Commands\AdminCommand; use Longman\TelegramBot\Commands\AdminCommand;
use Longman\TelegramBot\DB; use Longman\TelegramBot\DB;
use Longman\TelegramBot\Entities\Chat; use Longman\TelegramBot\Entities\Chat;
use Longman\TelegramBot\Entities\PhotoSize;
use Longman\TelegramBot\Entities\UserProfilePhotos;
use Longman\TelegramBot\Request; use Longman\TelegramBot\Request;
/** /**
...@@ -40,7 +42,7 @@ class WhoisCommand extends AdminCommand ...@@ -40,7 +42,7 @@ class WhoisCommand extends AdminCommand
/** /**
* @var string * @var string
*/ */
protected $version = '1.1.0'; protected $version = '1.2.0';
/** /**
* @var bool * @var bool
...@@ -51,6 +53,7 @@ class WhoisCommand extends AdminCommand ...@@ -51,6 +53,7 @@ class WhoisCommand extends AdminCommand
* Command execute method * Command execute method
* *
* @return mixed * @return mixed
* @throws \Longman\TelegramBot\Exception\TelegramException
*/ */
public function execute() public function execute()
{ {
...@@ -71,7 +74,7 @@ class WhoisCommand extends AdminCommand ...@@ -71,7 +74,7 @@ class WhoisCommand extends AdminCommand
$text = substr($command, 5); $text = substr($command, 5);
//We need that '-' now, bring it back //We need that '-' now, bring it back
if ((substr($text, 0, 1) == 'g')) { if (strpos($text, 'g') === 0) {
$text = str_replace('g', '-', $text); $text = str_replace('g', '-', $text);
} }
} }
...@@ -79,10 +82,14 @@ class WhoisCommand extends AdminCommand ...@@ -79,10 +82,14 @@ class WhoisCommand extends AdminCommand
if ($text === '') { if ($text === '') {
$text = 'Provide the id to lookup: /whois <id>'; $text = 'Provide the id to lookup: /whois <id>';
} else { } else {
$user_id = $text; $user_id = $text;
$chat = null;
$created_at = null;
$updated_at = null;
$result = null;
if (is_numeric($text)) { if (is_numeric($text)) {
$result = DB::selectChats( $results = DB::selectChats(
true, //Select groups (group chat) true, //Select groups (group chat)
true, //Select supergroups (super group chat) true, //Select supergroups (super group chat)
true, //Select users (single chat) true, //Select users (single chat)
...@@ -91,7 +98,9 @@ class WhoisCommand extends AdminCommand ...@@ -91,7 +98,9 @@ class WhoisCommand extends AdminCommand
$user_id //Specific chat_id to select $user_id //Specific chat_id to select
); );
$result = $result[0]; if (!empty($results)) {
$result = reset($results);
}
} else { } else {
$results = DB::selectChats( $results = DB::selectChats(
true, //Select groups (group chat) true, //Select groups (group chat)
...@@ -103,8 +112,8 @@ class WhoisCommand extends AdminCommand ...@@ -103,8 +112,8 @@ class WhoisCommand extends AdminCommand
$text //Text to search in user/group name $text //Text to search in user/group name
); );
if (is_array($results) && count($results) == 1) { if (is_array($results) && count($results) === 1) {
$result = $results[0]; $result = reset($results);
} }
} }
...@@ -118,50 +127,53 @@ class WhoisCommand extends AdminCommand ...@@ -118,50 +127,53 @@ class WhoisCommand extends AdminCommand
$old_id = $result['old_id']; $old_id = $result['old_id'];
} }
if ($chat != null) { if ($chat !== null) {
if ($chat->isPrivateChat()) { if ($chat->isPrivateChat()) {
$text = 'User ID: ' . $user_id . "\n"; $text = 'User ID: ' . $user_id . PHP_EOL;
$text .= 'Name: ' . $chat->getFirstName() . ' ' . $chat->getLastName() . "\n"; $text .= 'Name: ' . $chat->getFirstName() . ' ' . $chat->getLastName() . PHP_EOL;
if ($chat->getUsername() != '') { $username = $chat->getUsername();
$text .= 'Username: @' . $chat->getUsername() . "\n"; if ($username !== null && $username !== '') {
$text .= 'Username: @' . $username . PHP_EOL;
} }
$text .= 'First time seen: ' . $created_at . "\n"; $text .= 'First time seen: ' . $created_at . PHP_EOL;
$text .= 'Last activity: ' . $updated_at . "\n"; $text .= 'Last activity: ' . $updated_at . PHP_EOL;
//Code from Whoami command //Code from Whoami command
$limit = 10; $limit = 10;
$offset = null; $offset = null;
$ServerResponse = Request::getUserProfilePhotos([ $ServerResponse = Request::getUserProfilePhotos(
'user_id' => $user_id, [
'limit' => $limit, 'user_id' => $user_id,
'offset' => $offset, 'limit' => $limit,
]); 'offset' => $offset,
]
);
if ($ServerResponse->isOk()) { if ($ServerResponse->isOk()) {
/** @var UserProfilePhotos $UserProfilePhoto */
$UserProfilePhoto = $ServerResponse->getResult(); $UserProfilePhoto = $ServerResponse->getResult();
$totalcount = $UserProfilePhoto->getTotalCount();
} else {
$totalcount = 0;
}
if ($totalcount > 0) { if ($UserProfilePhoto->getTotalCount() > 0) {
$photos = $UserProfilePhoto->getPhotos(); $photos = $UserProfilePhoto->getPhotos();
$photo = $photos[0][2];
$file_id = $photo->getFileId();
$data['photo'] = $file_id; /** @var PhotoSize $photo */
$data['caption'] = $text; $photo = $photos[0][2];
$file_id = $photo->getFileId();
return Request::sendPhoto($data); $data['photo'] = $file_id;
$data['caption'] = $text;
return Request::sendPhoto($data);
}
} }
} elseif ($chat->isGroupChat()) { } elseif ($chat->isGroupChat()) {
$text = 'Chat ID: ' . $user_id . (!empty($old_id) ? ' (previously: ' . $old_id . ')' : '') . "\n"; $text = 'Chat ID: ' . $user_id . (!empty($old_id) ? ' (previously: ' . $old_id . ')' : '') . PHP_EOL;
$text .= 'Type: ' . ucfirst($chat->getType()) . "\n"; $text .= 'Type: ' . ucfirst($chat->getType()) . PHP_EOL;
$text .= 'Title: ' . $chat->getTitle() . "\n"; $text .= 'Title: ' . $chat->getTitle() . PHP_EOL;
$text .= 'First time added to group: ' . $created_at . "\n"; $text .= 'First time added to group: ' . $created_at . PHP_EOL;
$text .= 'Last activity: ' . $updated_at . "\n"; $text .= 'Last activity: ' . $updated_at . PHP_EOL;
} }
} elseif (is_array($results) && count($results) > 1) { } elseif (is_array($results) && count($results) > 1) {
$text = 'Multiple chats matched!'; $text = 'Multiple chats matched!';
...@@ -171,6 +183,7 @@ class WhoisCommand extends AdminCommand ...@@ -171,6 +183,7 @@ class WhoisCommand extends AdminCommand
} }
$data['text'] = $text; $data['text'] = $text;
return Request::sendMessage($data); return Request::sendMessage($data);
} }
} }
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