Skip to content

New command structure alpha #103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
1968e93
Restructure command files.
noplanman Feb 18, 2016
c914984
Rewrite command handling.
noplanman Feb 18, 2016
2c8cf26
Clear out abstract class constructors that aren't needed.
noplanman Feb 18, 2016
02e690e
Move command files around and put abstract command classes files toge…
noplanman Feb 19, 2016
24067d7
Move command files around and put abstract command classes files toge…
noplanman Feb 19, 2016
675ab65
fix use command
MBoretto Feb 19, 2016
0a90b0a
Merge pull request #1 from MBoretto/new_command_structure_alpha
noplanman Feb 19, 2016
168e733
Add methods to determine command type.
noplanman Feb 19, 2016
485743d
Add parameter to handle() method which allows for test input.
noplanman Feb 19, 2016
b65d7da
Rename all "path" related variables and documentation.
noplanman Feb 19, 2016
bd355a3
Fix command config method.
noplanman Feb 19, 2016
d206fd5
Message type array removed because it's not needed.
noplanman Feb 20, 2016
884830e
Set update object on class property level to provide easy access.
noplanman Feb 20, 2016
d1a5946
Rewrite help command to reflect changes in the command logic.
noplanman Feb 20, 2016
24cb59c
Reformat "Command not found" error output.
noplanman Feb 20, 2016
39b499e
Add error message to /echo command if no input.
noplanman Feb 20, 2016
f6cf49f
Set command config to default empty array.
noplanman Feb 20, 2016
2d32bec
Fix a few base test classes.
noplanman Feb 20, 2016
08f4756
Add first command tests.
noplanman Feb 20, 2016
1e325d8
Ok, so PHP7 handles exceptions a bit differently.
noplanman Feb 20, 2016
cc26b29
Command getConfig should return "null" if no config key is found.
noplanman Feb 20, 2016
cbaab2c
Allow Update object to be set to the command at construction.
noplanman Feb 20, 2016
8285b28
Enable method-chaining for "enableMySQL" method.
noplanman Feb 20, 2016
d2c1fff
Introduce new getLastCommandResponse() method to retrieve the last ex…
noplanman Feb 20, 2016
4fbb6a5
Add failsafe if Generic command can't be found, to prevent an infinit…
noplanman Feb 20, 2016
4ed9660
ALL commands now return a ServerResponse object instead of a bool.
noplanman Feb 20, 2016
1408ba1
Make preExecute more logical regarding MySQL requirement.
noplanman Feb 20, 2016
b24bcfc
Add first few command tests for "help" and "echo" command.
noplanman Feb 21, 2016
980dcd4
Correct usage text for /sendtochannel command.
noplanman Feb 21, 2016
bcd749e
Fix return value to return a ServerResponse.
noplanman Feb 23, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/Commands/AdminCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
/**
* This file is part of the TelegramBot package.
*
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Longman\TelegramBot\Commands;

/**
* Abstract Admin Command Class
*/
abstract class AdminCommand extends Command
{

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@
* 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;

/**
* Admin "/chats" command
*/
class ChatsCommand extends Command
class ChatsCommand extends AdminCommand
{
/**#@+
* {@inheritdoc}
Expand All @@ -27,33 +27,11 @@ 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
*
* @return boolean
* {@inheritdoc}
*/
public function execute()
{
Expand Down Expand Up @@ -105,6 +83,6 @@ public function execute()
'text' => $text,
];

return Request::sendMessage($data)->isOk();
return Request::sendMessage($data);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -60,9 +40,10 @@ public function execute()
$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)
Expand Down Expand Up @@ -103,16 +84,17 @@ public function execute()
$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 = [
'chat_id' => $chat_id,
'text' => $text,
];

return Request::sendMessage($data)->isOk();
return Request::sendMessage($data);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,23 @@
* 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}
*/
protected $name = 'sendtochannel';
protected $description = 'Send message to a channel';
protected $usage = '/sendchannel <message to send>';
protected $usage = '/sendtochannel <message to send>';
protected $version = '0.1.1';
protected $public = true;
protected $need_mysql = false;
/**#@-*/

Expand Down Expand Up @@ -53,8 +52,7 @@ public function execute()
'text' => $text,
];

$result = Request::sendMessage($data);
if ($result->isOk()) {
if (Request::sendMessage($data)->isOk()) {
$text_back = 'Message sent succesfully to: ' . $your_channel;
} else {
$text_back = 'Sorry message not sent to: ' . $your_channel;
Expand All @@ -66,6 +64,6 @@ public function execute()
'text' => $text_back,
];

return Request::sendMessage($data)->isOk();
return Request::sendMessage($data);
}
}
Loading