Commit 1968e938 authored by Armando Lüscher's avatar Armando Lüscher

Restructure command files.

parent 7147479e
......@@ -8,8 +8,9 @@
* file that was distributed with this source code.
*/
namespace Longman\TelegramBot;
namespace Longman\TelegramBot\Commands;
use Longman\TelegramBot\Telegram;
use Longman\TelegramBot\Entities\Chat;
use Longman\TelegramBot\Entities\Update;
use Longman\TelegramBot\Entities\User;
......@@ -82,13 +83,6 @@ abstract class Command
*/
protected $enabled = true;
/**
* If this command is public
*
* @var boolean
*/
protected $public = false;
/**
* If this command needs mysql
*
......@@ -120,10 +114,12 @@ abstract class Command
* @param Entities\Update $update
* @return Command
*/
public function setUpdate(Update $update)
public function setUpdate(Update $update = null)
{
$this->update = $update;
$this->message = $this->update->getMessage();
if (!empty($update)) {
$this->update = $update;
$this->message = $this->update->getMessage();
}
return $this;
}
......@@ -134,9 +130,7 @@ abstract class Command
*/
public function preExecute()
{
if (!$this->need_mysql |
$this->need_mysql & $this->telegram->isDbEnabled() & DB::isDbConnected()
) {
if (!$this->need_mysql || ($this->telegram->isDbEnabled() && DB::isDbConnected())) {
return $this->execute();
}
return $this->executeNoDB();
......@@ -148,14 +142,25 @@ abstract class Command
abstract public function execute();
/**
* This methods is executed if $need_mysql is true
* but DB connection for some reason is not avaiable
* Execution if MySQL is required but not available
*
* @return boolean
*/
public function executeNoDB()
{
//Preparing message
$message = $this->getMessage();
$chat_id = $message->getChat()->getId();
$data = [
'chat_id' => $chat_id,
'text' => 'Sorry no database connection, unable to execute "' . $this->name . '" command.',
];
return Request::sendMessage($data)->isOk();
}
/**
* Get update object
*
......@@ -267,14 +272,4 @@ abstract class Command
{
return $this->enabled;
}
/**
* Check if command is public
*
* @return boolean
*/
public function isPublic()
{
return $this->public;
}
}
<?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.
*/
namespace Longman\TelegramBot\Commands;
use Longman\TelegramBot\Telegram;
/**
* Abstract Admin Command Class
*/
abstract class AdminCommand extends Command
{
/**
* Constructor
*
* @param Telegram $telegram
*/
public function __construct(Telegram $telegram)
{
parent::__construct($telegram);
}
}
......@@ -8,9 +8,9 @@
* file that was distributed with this source code.
*/
namespace Longman\TelegramBot\Commands;
namespace Longman\TelegramBot\Commands\AdminCommands;
use Longman\TelegramBot\Command;
use Longman\TelegramBot\Commands\AdminCommand;
use Longman\TelegramBot\DB;
use Longman\TelegramBot\Entities\Chat;
use Longman\TelegramBot\Request;
......@@ -18,7 +18,7 @@ use Longman\TelegramBot\Request;
/**
* Admin "/chats" command
*/
class ChatsCommand extends Command
class ChatsCommand extends AdminCommand
{
/**#@+
* {@inheritdoc}
......@@ -27,29 +27,9 @@ class ChatsCommand extends Command
protected $description = 'List all chats stored by the bot';
protected $usage = '/chats';
protected $version = '1.0.1';
protected $public = true;
protected $need_mysql = false;
/**#@-*/
/**
* Execution if MySQL is required but not available
*
* @return boolean
*/
public function executeNoDB()
{
//Preparing message
$message = $this->getMessage();
$chat_id = $message->getChat()->getId();
$data = [
'chat_id' => $chat_id,
'text' => 'Sorry no database connection, unable to execute "' . $this->name . '" command.',
];
return Request::sendMessage($data)->isOk();
}
/**
* Execute command
*
......
......@@ -8,46 +8,26 @@
* file that was distributed with this source code.
*/
namespace Longman\TelegramBot\Commands;
namespace Longman\TelegramBot\Commands\AdminCommands;
use Longman\TelegramBot\Command;
use Longman\TelegramBot\Commands\AdminCommand;
use Longman\TelegramBot\Request;
/**
* Admin "/sendtoall" command
*/
class SendtoallCommand extends Command
class SendtoallCommand extends AdminCommand
{
/**#@+
* {@inheritdoc}
*/
protected $name = 'sendtoall';
protected $description = 'Send the message to all the user\'s bot';
protected $usage = '/sendall <message to send>';
protected $usage = '/sendtoall <message to send>';
protected $version = '1.2.1';
protected $public = true;
protected $need_mysql = true;
/**#@-*/
/**
* Execution if MySQL is required but not available
*
* @return boolean
*/
public function executeNoDB()
{
//Preparing message
$message = $this->getMessage();
$chat_id = $message->getChat()->getId();
$data = [
'chat_id' => $chat_id,
'text' => 'Sorry no database connection, unable to execute "' . $this->name . '" command.',
];
return Request::sendMessage($data)->isOk();
}
/**
* Execute command
*
......@@ -60,9 +40,10 @@ class SendtoallCommand extends Command
$message = $this->getMessage();
$chat_id = $message->getChat()->getId();
$text = $message->getText(true);
if (empty($text)) {
$text = 'Write the message to send: /sendall <message>';
$text = 'Write the message to send: /sendtoall <message>';
} else {
$results = Request::sendToActiveChats(
'sendMessage', //callback function to execute (see Request.php methods)
......@@ -103,9 +84,10 @@ class SendtoallCommand extends Command
$text .= $tot . ') ' . $status . ' ' . $type . ' ' . $name . "\n";
}
$text .= 'Delivered: ' . ($tot - $fail) . '/' . $tot . "\n";
}
if ($tot === 0) {
$text = 'No users or chats found..';
if ($tot === 0) {
$text = 'No users or chats found..';
}
}
$data = [
......
......@@ -8,15 +8,15 @@
* file that was distributed with this source code.
*/
namespace Longman\TelegramBot\Commands;
namespace Longman\TelegramBot\Commands\AdminCommands;
use Longman\TelegramBot\Command;
use Longman\TelegramBot\Commands\AdminCommand;
use Longman\TelegramBot\Request;
/**
* Admin "/sendtochannel" command
*/
class SendtochannelCommand extends Command
class SendtochannelCommand extends AdminCommand
{
/**#@+
* {@inheritdoc}
......@@ -25,7 +25,6 @@ class SendtochannelCommand extends Command
protected $description = 'Send message to a channel';
protected $usage = '/sendchannel <message to send>';
protected $version = '0.1.1';
protected $public = true;
protected $need_mysql = false;
/**#@-*/
......
<?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.
*/
namespace Longman\TelegramBot\Commands;
use Longman\TelegramBot\Telegram;
/**
* Abstract System Command Class
*/
abstract class SystemCommand extends Command
{
/**
* Constructor
*
* @param Telegram $telegram
*/
public function __construct(Telegram $telegram)
{
parent::__construct($telegram);
}
}
......@@ -8,21 +8,21 @@
* file that was distributed with this source code.
*/
namespace Longman\TelegramBot\Commands;
namespace Longman\TelegramBot\Commands\SystemCommands;
use Longman\TelegramBot\Command;
use Longman\TelegramBot\Commands\SystemCommand;
/**
* Channel chat created command
*/
class ChannelchatcreatedCommand extends Command
class ChannelchatcreatedCommand extends SystemCommand
{
/**#@+
* {@inheritdoc}
*/
protected $name = 'Channelchatcreated';
protected $name = 'Channelchatcreated';
protected $description = 'Channel chat created';
protected $version = '1.0.1';
protected $version = '1.0.1';
/**#@-*/
/**
......
......@@ -8,21 +8,21 @@
* file that was distributed with this source code.
*/
namespace Longman\TelegramBot\Commands;
namespace Longman\TelegramBot\Commands\SystemCommands;
use Longman\TelegramBot\Command;
use Longman\TelegramBot\Commands\SystemCommand;
/**
* Chosen inline result command
*/
class ChoseninlineresultCommand extends Command
class ChoseninlineresultCommand extends SystemCommand
{
/**#@+
* {@inheritdoc}
*/
protected $name = 'choseninlineresult';
protected $name = 'choseninlineresult';
protected $description = 'Chosen result query';
protected $version = '1.0.1';
protected $version = '1.0.1';
/**#@-*/
/**
......
......@@ -8,14 +8,14 @@
* file that was distributed with this source code.
*/
namespace Longman\TelegramBot\Commands;
namespace Longman\TelegramBot\Commands\SystemCommands;
use Longman\TelegramBot\Command;
use Longman\TelegramBot\Commands\SystemCommand;
/**
* Delete chat photo command
*/
class DeletechatphotoCommand extends Command
class DeletechatphotoCommand extends SystemCommand
{
/**#@+
* {@inheritdoc}
......
......@@ -8,15 +8,15 @@
* file that was distributed with this source code.
*/
namespace Longman\TelegramBot\Commands;
namespace Longman\TelegramBot\Commands\SystemCommands;
use Longman\TelegramBot\Command;
use Longman\TelegramBot\Commands\SystemCommand;
use Longman\TelegramBot\Request;
/**
* Generic command
*/
class GenericCommand extends Command
class GenericCommand extends SystemCommand
{
/**#@+
* {@inheritdoc}
......
......@@ -8,14 +8,14 @@
* file that was distributed with this source code.
*/
namespace Longman\TelegramBot\Commands;
namespace Longman\TelegramBot\Commands\SystemCommands;
use Longman\TelegramBot\Command;
use Longman\TelegramBot\Commands\SystemCommand;
/**
* Generic message command
*/
class GenericmessageCommand extends Command
class GenericmessageCommand extends SystemCommand
{
/**#@+
* {@inheritdoc}
......
......@@ -8,14 +8,14 @@
* file that was distributed with this source code.
*/
namespace Longman\TelegramBot\Commands;
namespace Longman\TelegramBot\Commands\SystemCommands;
use Longman\TelegramBot\Command;
use Longman\TelegramBot\Commands\SystemCommand;
/**
* Group chat created command
*/
class GroupchatcreatedCommand extends Command
class GroupchatcreatedCommand extends SystemCommand
{
/**#@+
* {@inheritdoc}
......
......@@ -8,16 +8,16 @@
* file that was distributed with this source code.
*/
namespace Longman\TelegramBot\Commands;
namespace Longman\TelegramBot\Commands\SystemCommands;
use Longman\TelegramBot\Command;
use Longman\TelegramBot\Commands\SystemCommand;
use Longman\TelegramBot\Entities\InlineQueryResultArticle;
use Longman\TelegramBot\Request;
/**
* Inline query command
*/
class InlinequeryCommand extends Command
class InlinequeryCommand extends SystemCommand
{
/**#@+
* {@inheritdoc}
......
......@@ -8,14 +8,14 @@
* file that was distributed with this source code.
*/
namespace Longman\TelegramBot\Commands;
namespace Longman\TelegramBot\Commands\SystemCommands;
use Longman\TelegramBot\Command;
use Longman\TelegramBot\Commands\SystemCommand;
/**
* Left chat participant command
*/
class LeftchatparticipantCommand extends Command
class LeftchatparticipantCommand extends SystemCommand
{
/**#@+
* {@inheritdoc}
......
......@@ -8,15 +8,15 @@
* file that was distributed with this source code.
*/
namespace Longman\TelegramBot\Commands;
namespace Longman\TelegramBot\Commands\SystemCommands;
use Longman\TelegramBot\Command;
use Longman\TelegramBot\Commands\SystemCommand;
use Longman\TelegramBot\Request;
/**
* New chat participant command
*/
class NewchatparticipantCommand extends Command
class NewchatparticipantCommand extends SystemCommand
{
/**#@+
* {@inheritdoc}
......
......@@ -8,14 +8,14 @@
* file that was distributed with this source code.
*/
namespace Longman\TelegramBot\Commands;
namespace Longman\TelegramBot\Commands\SystemCommands;
use Longman\TelegramBot\Command;
use Longman\TelegramBot\Commands\SystemCommand;
/**
* New chat title command
*/
class NewchattitleCommand extends Command
class NewchattitleCommand extends SystemCommand
{
/**#@+
* {@inheritdoc}
......
......@@ -8,15 +8,15 @@
* file that was distributed with this source code.
*/
namespace Longman\TelegramBot\Commands;
namespace Longman\TelegramBot\Commands\SystemCommands;
use Longman\TelegramBot\Command;
use Longman\TelegramBot\Commands\SystemCommand;
use Longman\TelegramBot\Request;
/**
* Super group chat created command
*/
class SupergroupchatcreatedCommand extends Command
class SupergroupchatcreatedCommand extends SystemCommand
{
/**#@+
* {@inheritdoc}
......
<?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.
*/
namespace Longman\TelegramBot\Commands;
use Longman\TelegramBot\Telegram;
/**
* Abstract User Command Class
*/
abstract class UserCommand extends Command
{
/**
* Constructor
*
* @param Telegram $telegram
*/
public function __construct(Telegram $telegram)
{
parent::__construct($telegram);
}
}
......@@ -8,16 +8,16 @@
* file that was distributed with this source code.
*/
namespace Longman\TelegramBot\Commands;
namespace Longman\TelegramBot\Commands\UserCommands;
use Longman\TelegramBot\Command;
use Longman\TelegramBot\Commands\UserCommand;
use Longman\TelegramBot\Exception\TelegramException;
use Longman\TelegramBot\Request;
/**
* User "/date" command
*/
class DateCommand extends Command
class DateCommand extends UserCommand
{
/**#@+
* {@inheritdoc}
......
......@@ -8,15 +8,15 @@
* file that was distributed with this source code.
*/
namespace Longman\TelegramBot\Commands;
namespace Longman\TelegramBot\Commands\UserCommands;
use Longman\TelegramBot\Command;
use Longman\TelegramBot\Commands\UserCommand;
use Longman\TelegramBot\Request;
/**
* User "/echo" command
*/
class EchoCommand extends Command
class EchoCommand extends UserCommand
{
/**#@+
* {@inheritdoc}
......@@ -25,7 +25,6 @@ class EchoCommand extends Command
protected $description = 'Show text';
protected $usage = '/echo <text>';
protected $version = '1.0.1';
protected $public = true;
/**#@-*/
/**
......
......@@ -8,15 +8,15 @@
* file that was distributed with this source code.
*/
namespace Longman\TelegramBot\Commands;
namespace Longman\TelegramBot\Commands\UserCommands;
use Longman\TelegramBot\Command;
use Longman\TelegramBot\Commands\UserCommand;
use Longman\TelegramBot\Request;
/**
* User "/help" command
*/
class HelpCommand extends Command
class HelpCommand extends UserCommand
{
/**#@+
* {@inheritdoc}
......@@ -25,7 +25,6 @@ class HelpCommand extends Command
protected $description = 'Show bot commands help';
protected $usage = '/help or /help <command>';
protected $version = '1.0.1';
protected $public = true;
/**#@-*/
/**
......@@ -36,8 +35,8 @@ class HelpCommand extends Command
public function execute()
{
$message = $this->getMessage();
$chat_id = $message->getChat()->getId();
$message_id = $message->getMessageId();
$text = $message->getText(true);
......@@ -51,9 +50,6 @@ class HelpCommand extends Command
if (!$command->isEnabled()) {
continue;
}
if (!$command->isPublic()) {
continue;
}
$msg .= '/' . $command->getName() . ' - ' . $command->getDescription() . "\n";
}
......@@ -64,7 +60,7 @@ class HelpCommand extends Command
$text = str_replace('/', '', $text);
if (isset($commands[$text])) {
$command = $commands[$text];
if (!$command->isEnabled() || !$command->isPublic()) {
if (!$command->isEnabled()) {
$msg = 'Command ' . $text . ' not found';
} else {
$msg = 'Command: ' . $command->getName() . ' v' . $command->getVersion() . "\n";
......
......@@ -8,15 +8,15 @@
* file that was distributed with this source code.
*/
namespace Longman\TelegramBot\Commands;
namespace Longman\TelegramBot\Commands\UserCommands;
use Longman\TelegramBot\Command;
use Longman\TelegramBot\Commands\UserCommand;
use Longman\TelegramBot\Request;
/**
* User "/slap" command
*/
class SlapCommand extends Command
class SlapCommand extends UserCommand
{
/**#@+
* {@inheritdoc}
......
......@@ -8,15 +8,15 @@
* file that was distributed with this source code.
*/
namespace Longman\TelegramBot\Commands;
namespace Longman\TelegramBot\Commands\UserCommands;
use Longman\TelegramBot\Command;
use Longman\TelegramBot\Commands\UserCommand;
use Longman\TelegramBot\Request;
/**
* Start command
*/
class StartCommand extends Command
class StartCommand extends UserCommand
{
/**#@+
* {@inheritdoc}
......
......@@ -8,15 +8,15 @@
* file that was distributed with this source code.
*/
namespace Longman\TelegramBot\Commands;
namespace Longman\TelegramBot\Commands\UserCommands;
use Longman\TelegramBot\Command;
use Longman\TelegramBot\Commands\UserCommand;
use Longman\TelegramBot\Request;
/**
* User "/weather" command
*/
class WeatherCommand extends Command
class WeatherCommand extends UserCommand
{
/**#@+
* {@inheritdoc}
......
......@@ -10,16 +10,16 @@
* Written by Marco Boretto <marco.bore@gmail.com>
*/
namespace Longman\TelegramBot\Commands;
namespace Longman\TelegramBot\Commands\UserCommands;
use Longman\TelegramBot\Command;
use Longman\TelegramBot\Commands\UserCommand;
use Longman\TelegramBot\Entities\File;
use Longman\TelegramBot\Request;
/**
* User "/whoami" command
*/
class WhoamiCommand extends Command
class WhoamiCommand extends UserCommand
{
/**#@+
* {@inheritdoc}
......
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