Fix and improve Admin commands.

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