Skip to content

Commit d49436d

Browse files
committed
Merge pull request #103 from noplanman/new_command_structure_alpha
New command structure alpha
2 parents 7147479 + bcd749e commit d49436d

36 files changed

+1102
-677
lines changed

src/Commands/AdminCommand.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
/**
3+
* This file is part of the TelegramBot package.
4+
*
5+
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace Longman\TelegramBot\Commands;
12+
13+
/**
14+
* Abstract Admin Command Class
15+
*/
16+
abstract class AdminCommand extends Command
17+
{
18+
19+
}

src/Admin/ChatsCommand.php renamed to src/Commands/AdminCommands/ChatsCommand.php

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@
88
* file that was distributed with this source code.
99
*/
1010

11-
namespace Longman\TelegramBot\Commands;
11+
namespace Longman\TelegramBot\Commands\AdminCommands;
1212

13-
use Longman\TelegramBot\Command;
13+
use Longman\TelegramBot\Commands\AdminCommand;
1414
use Longman\TelegramBot\DB;
1515
use Longman\TelegramBot\Entities\Chat;
1616
use Longman\TelegramBot\Request;
1717

1818
/**
1919
* Admin "/chats" command
2020
*/
21-
class ChatsCommand extends Command
21+
class ChatsCommand extends AdminCommand
2222
{
2323
/**#@+
2424
* {@inheritdoc}
@@ -27,33 +27,11 @@ class ChatsCommand extends Command
2727
protected $description = 'List all chats stored by the bot';
2828
protected $usage = '/chats';
2929
protected $version = '1.0.1';
30-
protected $public = true;
3130
protected $need_mysql = false;
3231
/**#@-*/
3332

3433
/**
35-
* Execution if MySQL is required but not available
36-
*
37-
* @return boolean
38-
*/
39-
public function executeNoDB()
40-
{
41-
//Preparing message
42-
$message = $this->getMessage();
43-
$chat_id = $message->getChat()->getId();
44-
45-
$data = [
46-
'chat_id' => $chat_id,
47-
'text' => 'Sorry no database connection, unable to execute "' . $this->name . '" command.',
48-
];
49-
50-
return Request::sendMessage($data)->isOk();
51-
}
52-
53-
/**
54-
* Execute command
55-
*
56-
* @return boolean
34+
* {@inheritdoc}
5735
*/
5836
public function execute()
5937
{
@@ -105,6 +83,6 @@ public function execute()
10583
'text' => $text,
10684
];
10785

108-
return Request::sendMessage($data)->isOk();
86+
return Request::sendMessage($data);
10987
}
11088
}

src/Admin/SendtoallCommand.php renamed to src/Commands/AdminCommands/SendtoallCommand.php

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,46 +8,26 @@
88
* file that was distributed with this source code.
99
*/
1010

11-
namespace Longman\TelegramBot\Commands;
11+
namespace Longman\TelegramBot\Commands\AdminCommands;
1212

13-
use Longman\TelegramBot\Command;
13+
use Longman\TelegramBot\Commands\AdminCommand;
1414
use Longman\TelegramBot\Request;
1515

1616
/**
1717
* Admin "/sendtoall" command
1818
*/
19-
class SendtoallCommand extends Command
19+
class SendtoallCommand extends AdminCommand
2020
{
2121
/**#@+
2222
* {@inheritdoc}
2323
*/
2424
protected $name = 'sendtoall';
2525
protected $description = 'Send the message to all the user\'s bot';
26-
protected $usage = '/sendall <message to send>';
26+
protected $usage = '/sendtoall <message to send>';
2727
protected $version = '1.2.1';
28-
protected $public = true;
2928
protected $need_mysql = true;
3029
/**#@-*/
3130

32-
/**
33-
* Execution if MySQL is required but not available
34-
*
35-
* @return boolean
36-
*/
37-
public function executeNoDB()
38-
{
39-
//Preparing message
40-
$message = $this->getMessage();
41-
$chat_id = $message->getChat()->getId();
42-
43-
$data = [
44-
'chat_id' => $chat_id,
45-
'text' => 'Sorry no database connection, unable to execute "' . $this->name . '" command.',
46-
];
47-
48-
return Request::sendMessage($data)->isOk();
49-
}
50-
5131
/**
5232
* Execute command
5333
*
@@ -60,9 +40,10 @@ public function execute()
6040
$message = $this->getMessage();
6141

6242
$chat_id = $message->getChat()->getId();
43+
$text = $message->getText(true);
6344

6445
if (empty($text)) {
65-
$text = 'Write the message to send: /sendall <message>';
46+
$text = 'Write the message to send: /sendtoall <message>';
6647
} else {
6748
$results = Request::sendToActiveChats(
6849
'sendMessage', //callback function to execute (see Request.php methods)
@@ -103,16 +84,17 @@ public function execute()
10384
$text .= $tot . ') ' . $status . ' ' . $type . ' ' . $name . "\n";
10485
}
10586
$text .= 'Delivered: ' . ($tot - $fail) . '/' . $tot . "\n";
106-
}
107-
if ($tot === 0) {
108-
$text = 'No users or chats found..';
87+
88+
if ($tot === 0) {
89+
$text = 'No users or chats found..';
90+
}
10991
}
11092

11193
$data = [
11294
'chat_id' => $chat_id,
11395
'text' => $text,
11496
];
11597

116-
return Request::sendMessage($data)->isOk();
98+
return Request::sendMessage($data);
11799
}
118100
}

src/Admin/SendtochannelCommand.php renamed to src/Commands/AdminCommands/SendtochannelCommand.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,23 @@
88
* file that was distributed with this source code.
99
*/
1010

11-
namespace Longman\TelegramBot\Commands;
11+
namespace Longman\TelegramBot\Commands\AdminCommands;
1212

13-
use Longman\TelegramBot\Command;
13+
use Longman\TelegramBot\Commands\AdminCommand;
1414
use Longman\TelegramBot\Request;
1515

1616
/**
1717
* Admin "/sendtochannel" command
1818
*/
19-
class SendtochannelCommand extends Command
19+
class SendtochannelCommand extends AdminCommand
2020
{
2121
/**#@+
2222
* {@inheritdoc}
2323
*/
2424
protected $name = 'sendtochannel';
2525
protected $description = 'Send message to a channel';
26-
protected $usage = '/sendchannel <message to send>';
26+
protected $usage = '/sendtochannel <message to send>';
2727
protected $version = '0.1.1';
28-
protected $public = true;
2928
protected $need_mysql = false;
3029
/**#@-*/
3130

@@ -53,8 +52,7 @@ public function execute()
5352
'text' => $text,
5453
];
5554

56-
$result = Request::sendMessage($data);
57-
if ($result->isOk()) {
55+
if (Request::sendMessage($data)->isOk()) {
5856
$text_back = 'Message sent succesfully to: ' . $your_channel;
5957
} else {
6058
$text_back = 'Sorry message not sent to: ' . $your_channel;
@@ -66,6 +64,6 @@ public function execute()
6664
'text' => $text_back,
6765
];
6866

69-
return Request::sendMessage($data)->isOk();
67+
return Request::sendMessage($data);
7068
}
7169
}

0 commit comments

Comments
 (0)