Commit 1d55723a authored by Marco Boretto's avatar Marco Boretto

Merge pull request #82 from noplanman/code_cleanup_Admin

Initial code cleanup of `Admin commands`.
parents 6c03ff17 5ceb2295
<?php <?php
/* /**
* This file is part of the TelegramBot package. * This file is part of the TelegramBot package.
* *
* (c) Avtandil Kikabidze aka LONGMAN <akalongman@gmail.com> * (c) Avtandil Kikabidze aka LONGMAN <akalongman@gmail.com>
* *
* For the full copyright and license information, please view the LICENSE * For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
namespace Longman\TelegramBot\Commands; namespace Longman\TelegramBot\Commands;
use Longman\TelegramBot\Request;
use Longman\TelegramBot\DB;
use Longman\TelegramBot\Command; use Longman\TelegramBot\Command;
use Longman\TelegramBot\Entities\Update; use Longman\TelegramBot\DB;
use Longman\TelegramBot\Entities\Chat; use Longman\TelegramBot\Entities\Chat;
use Longman\TelegramBot\Entities\Update;
use Longman\TelegramBot\Exception\TelegramException; use Longman\TelegramBot\Exception\TelegramException;
use Longman\TelegramBot\Request;
/**
* Admin "/chats" command
*/
class ChatsCommand extends Command class ChatsCommand extends Command
{ {
/**
* Name
*
* @var string
*/
protected $name = 'chats'; protected $name = 'chats';
/**
* Description
*
* @var string
*/
protected $description = 'List all chats stored by the bot'; protected $description = 'List all chats stored by the bot';
protected $usage = '/chats ';
/**
* Usage
*
* @var string
*/
protected $usage = '/chats';
/**
* Version
*
* @var string
*/
protected $version = '1.0.0'; protected $version = '1.0.0';
/**
* If this command is enabled
*
* @var boolean
*/
protected $enabled = true; protected $enabled = true;
/**
* If this command is public
*
* @var boolean
*/
protected $public = true; protected $public = true;
//need Mysql
protected $need_mysql = true;
/**
* If this command needs mysql
*
* @var boolean
*/
protected $need_mysql = false;
/**
* Execution if MySQL is required but not available
*
* @return boolean
*/
public function executeNoDB() public function executeNoDB()
{ {
//Database not setted or without connection
//Preparing message //Preparing message
$message = $this->getMessage(); $message = $this->getMessage();
$chat_id = $message->getChat()->getId(); $chat_id = $message->getChat()->getId();
$data = []; $data = [
$data['chat_id'] = $chat_id; 'chat_id' => $chat_id,
$data['text'] = 'Sorry no database connection, unable to execute '.$this->name.' command.'; 'text' => 'Sorry no database connection, unable to execute "' . $this->name . '" command.',
];
$result = Request::sendMessage($data); $result = Request::sendMessage($data);
return $result->isOk(); return $result->isOk();
} }
/**
* Execute command
*
* @return boolean
*/
public function execute() public function execute()
{ {
$update = $this->getUpdate(); $update = $this->getUpdate();
...@@ -60,38 +114,38 @@ class ChatsCommand extends Command ...@@ -60,38 +114,38 @@ class ChatsCommand extends Command
$user_chats = 0; $user_chats = 0;
$group_chats = 0; $group_chats = 0;
$super_group_chats = 0; $super_group_chats = 0;
$text = "List of bot chats:\n"; $text = 'List of bot chats:' . "\n";
foreach ($results as $result) { foreach ($results as $result) {
//initialize a chat object //Initialize a chat object
$result['id'] = $result['chat_id']; $result['id'] = $result['chat_id'];
$chat = new Chat($result); $chat = new Chat($result);
if ($chat->isPrivateChat()) { if ($chat->isPrivateChat()) {
$text .= '- P '.$chat->tryMention()."\n"; $text .= '- P ' . $chat->tryMention() . "\n";
++$user_chats; ++$user_chats;
} elseif ($chat->isGroupChat()) { } elseif ($chat->isGroupChat()) {
$text .= '- G '.$chat->getTitle()."\n"; $text .= '- G ' . $chat->getTitle() . "\n";
++$group_chats; ++$group_chats;
} elseif ($chat->isSuperGroup()) { } elseif ($chat->isSuperGroup()) {
$text .= '- S '.$chat->getTitle()."\n"; $text .= '- S ' . $chat->getTitle() . "\n";
++$super_group_chats; ++$super_group_chats;
} }
} }
if (($group_chats + $user_chats + $super_group_chats) == 0) {
$text = "No chats found.."; if (($user_chats + $group_chats + $super_group_chats) == 0) {
$text = 'No chats found..';
} else { } else {
$text .= "\nPrivate Chats: ".$user_chats; $text .= "\n" . 'Private Chats: ' . $user_chats;
$text .= "\nGroup: ".$group_chats; $text .= "\n" . 'Group: ' . $group_chats;
$text .= "\nSuper Group: ".$super_group_chats; $text .= "\n" . 'Super Group: ' . $super_group_chats;
$text .= "\nTot: ".($group_chats + $user_chats + $super_group_chats); $text .= "\n" . 'Total: ' . ($user_chats + $group_chats + $super_group_chats);
} }
$data = []; $data = [
$data['chat_id'] = $chat_id; 'chat_id' => $chat_id,
$data['text'] = $text; 'text' => $text,
];
$result = Request::sendMessage($data); $result = Request::sendMessage($data);
return $result->isOk(); return $result->isOk();
} }
......
<?php <?php
/**
/*
* This file is part of the TelegramBot package. * This file is part of the TelegramBot package.
* *
* (c) Avtandil Kikabidze aka LONGMAN <akalongman@gmail.com> * (c) Avtandil Kikabidze aka LONGMAN <akalongman@gmail.com>
* *
* For the full copyright and license information, please view the LICENSE * For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
namespace Longman\TelegramBot\Commands; namespace Longman\TelegramBot\Commands;
use Longman\TelegramBot\Request;
use Longman\TelegramBot\DB;
use Longman\TelegramBot\Command; use Longman\TelegramBot\Command;
use Longman\TelegramBot\DB;
use Longman\TelegramBot\Entities\Update; use Longman\TelegramBot\Entities\Update;
use Longman\TelegramBot\Exception\TelegramException; use Longman\TelegramBot\Exception\TelegramException;
use Longman\TelegramBot\Request;
/**
* Admin "/sendtoall" command
*/
class SendtoallCommand extends Command class SendtoallCommand extends Command
{ {
/**
* Name
*
* @var string
*/
protected $name = 'sendtoall'; protected $name = 'sendtoall';
/**
* Description
*
* @var string
*/
protected $description = 'Send the message to all the user\'s bot'; protected $description = 'Send the message to all the user\'s bot';
/**
* Usage
*
* @var string
*/
protected $usage = '/sendall <message to send>'; protected $usage = '/sendall <message to send>';
/**
* Version
*
* @var string
*/
protected $version = '1.2.0'; protected $version = '1.2.0';
/**
* If this command is enabled
*
* @var boolean
*/
protected $enabled = true; protected $enabled = true;
/**
* If this command is public
*
* @var boolean
*/
protected $public = true; protected $public = true;
//need Mysql
/**
* If this command needs mysql
*
* @var boolean
*/
protected $need_mysql = true; protected $need_mysql = true;
/**
* Execution if MySQL is required but not available
*
* @return boolean
*/
public function executeNoDB() public function executeNoDB()
{ {
//Database not setted or without connection
//Preparing message //Preparing message
$message = $this->getMessage(); $message = $this->getMessage();
$chat_id = $message->getChat()->getId(); $chat_id = $message->getChat()->getId();
$data = []; $data = [
$data['chat_id'] = $chat_id; 'chat_id' => $chat_id,
$data['text'] = 'Sorry no database connection, unable to execute '.$this->name.' command.'; 'text' => 'Sorry no database connection, unable to execute "' . $this->name . '" command.',
];
$result = Request::sendMessage($data); $result = Request::sendMessage($data);
return $result->isOk(); return $result->isOk();
} }
/**
* Execute command
*
* @todo Don't use empty, as a string of '0' is regarded to be empty
*
* @return boolean
*/
public function execute() public function execute()
{ {
$update = $this->getUpdate(); $update = $this->getUpdate();
...@@ -50,11 +105,11 @@ class SendtoallCommand extends Command ...@@ -50,11 +105,11 @@ class SendtoallCommand extends Command
$text = $message->getText(true); $text = $message->getText(true);
if (empty($text)) { if (empty($text)) {
$text = 'Write the message to sent: /sendall <message>'; $text = 'Write the message to send: /sendall <message>';
} else { } else {
$results = Request::sendToActiveChats( $results = Request::sendToActiveChats(
'sendMessage', //callback function to execute (see Request.php methods) 'sendMessage', //callback function to execute (see Request.php methods)
array('text'=> $text), //Param to evaluate the request ['text' => $text], //Param to evaluate the request
true, //Send to groups (group chat) true, //Send to groups (group chat)
true, //Send to super groups chats (super group chat) true, //Send to super groups chats (super group chat)
true, //Send to users (single chat) true, //Send to users (single chat)
...@@ -65,7 +120,7 @@ class SendtoallCommand extends Command ...@@ -65,7 +120,7 @@ class SendtoallCommand extends Command
$tot = 0; $tot = 0;
$fail = 0; $fail = 0;
$text = "Message sended to:\n"; $text = 'Message sent to:' . "\n";
foreach ($results as $result) { foreach ($results as $result) {
$status = ''; $status = '';
$type = ''; $type = '';
...@@ -88,17 +143,18 @@ class SendtoallCommand extends Command ...@@ -88,17 +143,18 @@ class SendtoallCommand extends Command
} }
++$tot; ++$tot;
$text .= $tot.') '.$status.' '.$type.' '.$name."\n"; $text .= $tot . ') ' . $status . ' ' . $type . ' ' . $name . "\n";
} }
$text .= "Delivered: ".($tot - $fail).'/'.$tot."\n"; $text .= 'Delivered: ' . ($tot - $fail) . '/' . $tot . "\n";
} }
if ($tot == 0) { if ($tot == 0) {
$text = "No users or chats found.."; $text = 'No users or chats found..';
} }
$data = []; $data = [
$data['chat_id'] = $chat_id; 'chat_id' => $chat_id,
$data['text'] = $text; 'text' => $text,
];
$result = Request::sendMessage($data); $result = Request::sendMessage($data);
return $result->isOk(); return $result->isOk();
......
<?php <?php
/**
/*
* This file is part of the TelegramBot package. * This file is part of the TelegramBot package.
* *
* (c) Avtandil Kikabidze aka LONGMAN <akalongman@gmail.com> * (c) Avtandil Kikabidze aka LONGMAN <akalongman@gmail.com>
* *
* For the full copyright and license information, please view the LICENSE * For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
namespace Longman\TelegramBot\Commands; namespace Longman\TelegramBot\Commands;
use Longman\TelegramBot\Request;
use Longman\TelegramBot\DB;
use Longman\TelegramBot\Command; use Longman\TelegramBot\Command;
use Longman\TelegramBot\DB;
use Longman\TelegramBot\Entities\Update; use Longman\TelegramBot\Entities\Update;
use Longman\TelegramBot\Exception\TelegramException; use Longman\TelegramBot\Exception\TelegramException;
use Longman\TelegramBot\Request;
/**
* Admin "/sendtochannel" command
*/
class SendtochannelCommand extends Command class SendtochannelCommand extends Command
{ {
/**
* Name
*
* @var string
*/
protected $name = 'sendtochannel'; protected $name = 'sendtochannel';
/**
* Description
*
* @var string
*/
protected $description = 'Send message to a channel'; protected $description = 'Send message to a channel';
/**
* Usage
*
* @var string
*/
protected $usage = '/sendchannel <message to send>'; protected $usage = '/sendchannel <message to send>';
/**
* Version
*
* @var string
*/
protected $version = '0.1.0'; protected $version = '0.1.0';
/**
* If this command is enabled
*
* @var boolean
*/
protected $enabled = true; protected $enabled = true;
/**
* If this command is public
*
* @var boolean
*/
protected $public = true; protected $public = true;
//need Mysql
/**
* If this command needs mysql
*
* @var boolean
*/
protected $need_mysql = false; protected $need_mysql = false;
/**
* Execute command
*
* @todo Don't use empty, as a string of '0' is regarded to be empty
*
* @return boolean
*/
public function execute() public function execute()
{ {
$update = $this->getUpdate(); $update = $this->getUpdate();
...@@ -35,26 +85,29 @@ class SendtochannelCommand extends Command ...@@ -35,26 +85,29 @@ class SendtochannelCommand extends Command
$chat_id = $message->getChat()->getId(); $chat_id = $message->getChat()->getId();
$message_id = $message->getMessageId(); $message_id = $message->getMessageId();
$text = $message->getText(true); $text = $message->getText(true);
if (empty($text)) { if (empty($text)) {
$text_back = 'Write the message to sent: /sendtochannel <message>'; $text_back = 'Write the message to send: /sendtochannel <message>';
} else { } else {
$your_channel = $this->getConfig('your_channel'); $your_channel = $this->getConfig('your_channel');
//Send message to channel //Send message to channel
$data = []; $data = [
$data['chat_id'] = $your_channel; 'chat_id' => $your_channel,
$data['text'] = $text; 'text' => $text,
];
$result = Request::sendMessage($data); $result = Request::sendMessage($data);
if ($result->isOk()) { if ($result->isOk()) {
$text_back = 'Message sent succesfully to: '.$your_channel; $text_back = 'Message sent succesfully to: ' . $your_channel;
} else { } else {
$text_back = 'Sorry message not sent to: '.$your_channel; $text_back = 'Sorry message not sent to: ' . $your_channel;
} }
} }
$data = []; $data = [
$data['chat_id'] = $chat_id; 'chat_id' => $chat_id,
$data['text'] = $text_back; 'text' => $text_back,
];
$result = Request::sendMessage($data); $result = Request::sendMessage($data);
return $result->isOk(); return $result->isOk();
......
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