Skip to content

Commit b15a6cf

Browse files
committed
Allow cron execution by defining groups of commands that can be called individually.
1 parent da9458f commit b15a6cf

File tree

7 files changed

+69
-16
lines changed

7 files changed

+69
-16
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
44
## [Unreleased]
55
### Added
66
- Ability to define custom valid IPs to access webhook.
7+
- Execute commands via cron, using `cron` action and `g` parameter.
78
### Changed
89
- Remodelled the config array to a more flexible structure.
910
### Deprecated

README.md

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,20 @@ and then run `composer update`
4040

4141
What use would this library be if you couldn't perform any actions?!
4242

43-
There are 4 parameters available to get things rolling:
43+
There are a few parameters available to get things rolling:
4444

4545
| Parameter | Description |
4646
| --------- | ----------- |
4747
| s | **s**ecret: This is a special secret value defined in the main `manager.php` file. |
4848
| | This parameter is required to call the script via browser! |
49-
| a | **a**ction: The actual action to perform. (handle (default), set, unset, reset) |
50-
| | **handle** executes the `getUpdates` method; **set** / **unset** / **reset** the Webhook. |
49+
| a | **a**ction: The actual action to perform. (handle (default), cron, set, unset, reset) |
50+
| | **handle** executes the `getUpdates` method; **cron** executes cron commands; **set** / **unset** / **reset** the webhook. |
5151
| l | **l**oop: Number of seconds to loop the script for (used for getUpdates method). |
5252
| | This would be used mainly via CLI, to continually get updates for a certain period. |
53-
| i | **i**nterval: Number of seconds to wait between getUpdates requests (used for getUpdates method, default: 2). |
53+
| i | **i**nterval: Number of seconds to wait between getUpdates requests (used for getUpdates method, default is 2). |
5454
| | This would be used mainly via CLI, to continually get updates for a certain period, every **i** seconds. |
55+
| g | **g**roup: Commands group for cron (only used together with `cron` action, default group is `default`). |
56+
| | Define which group of commands to execute via cron. |
5557

5658
#### via browser
5759

@@ -102,7 +104,7 @@ Handle updates for 30 seconds, fetching every 5 seconds:
102104
You can name this file whatever you like, it just has to be somewhere inside your PHP project (preferably in the root folder to make things easier).
103105
(Let's assume our file is called `manager.php`)
104106

105-
Let's start off with a simple example that uses the Webhook method:
107+
Let's start off with a simple example that uses the webhook method:
106108
```php
107109
<?php
108110

@@ -256,6 +258,24 @@ $bot = new BotManager([
256258
],
257259
],
258260

261+
// (array) All options that have to do with cron.
262+
'cron' => [
263+
// (array) List of groups that contain the commands to execute.
264+
'groups' => [
265+
// Each group has a name and array of commands.
266+
// When no group is defined, the default group gets executed.
267+
'default' => [
268+
'/default_cron_command',
269+
],
270+
'maintenance' => [
271+
'/db_cleanup',
272+
'/db_repair',
273+
'/log_rotate',
274+
'/message_admins Maintenance completed',
275+
],
276+
],
277+
],
278+
259279
// (string) Override the custom input of your bot (mostly for testing purposes!).
260280
'custom_input' => '{"some":"raw", "json":"update"}',
261281
]);

src/Action.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class Action
2222
'unset',
2323
'reset',
2424
'handle',
25+
'cron',
2526
];
2627

2728
/**

src/BotManager.php

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,18 @@ public function run(): self
123123
// Make sure this is a valid call.
124124
$this->validateSecret();
125125

126+
if (!$this->isValidRequest()) {
127+
throw new InvalidAccessException('Invalid access');
128+
}
129+
126130
if ($this->action->isAction(['set', 'unset', 'reset'])) {
127131
$this->validateAndSetWebhook();
128132
} elseif ($this->action->isAction('handle')) {
129-
// Set any extras.
130133
$this->setBotExtras();
131134
$this->handleRequest();
135+
} elseif ($this->action->isAction('cron')) {
136+
$this->setBotExtras();
137+
$this->handleCron();
132138
}
133139

134140
return $this;
@@ -337,6 +343,21 @@ public function handleRequest(): self
337343
return $this;
338344
}
339345

346+
/**
347+
* Handle cron.
348+
*
349+
* @return \NPM\TelegramBotManager\BotManager
350+
* @throws \Longman\TelegramBot\Exception\TelegramException
351+
*/
352+
public function handleCron(): self
353+
{
354+
$group = $this->params->getScriptParam('g', 'default');
355+
$commands = $this->params->getBotParam('cron.groups.' . $group, []);
356+
$this->telegram->runCommands($commands);
357+
358+
return $this;
359+
}
360+
340361
/**
341362
* Get the number of seconds the script should loop.
342363
*
@@ -456,10 +477,6 @@ public function handleGetUpdates(): self
456477
*/
457478
public function handleWebhook(): self
458479
{
459-
if (!$this->isValidRequest()) {
460-
throw new InvalidAccessException('Invalid access');
461-
}
462-
463480
$this->telegram->handle();
464481

465482
return $this;

src/Params.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ class Params
1818
* @var array List of valid script parameters.
1919
*/
2020
private static $valid_script_params = [
21-
's',
22-
'a',
23-
'l',
24-
'i',
21+
's', // secret
22+
'a', // action
23+
'l', // loop
24+
'i', // interval
25+
'g', // group (for cron)
2526
];
2627

2728
/**
@@ -47,8 +48,8 @@ class Params
4748
'paths',
4849
'commands',
4950
'botan',
50-
'custom_input',
5151
'cron',
52+
'custom_input',
5253
];
5354

5455
/**

tests/TelegramBotManager/Tests/ActionTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public function testConstruct()
2020
self::assertEquals('unset', (new Action('unset'))->getAction());
2121
self::assertEquals('reset', (new Action('reset'))->getAction());
2222
self::assertEquals('handle', (new Action('handle'))->getAction());
23+
self::assertEquals('cron', (new Action('cron'))->getAction());
2324
}
2425

2526
/**

tests/TelegramBotManager/Tests/ParamsTest.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,19 @@ class ParamsTest extends \PHPUnit\Framework\TestCase
6363
'botan' => [
6464
'token' => 'botan_12345',
6565
],
66-
'custom_input' => '{"some":"raw", "json":"update"}',
66+
'cron' => [
67+
'groups' => [
68+
'default' => [
69+
'/default_cron_command',
70+
],
71+
'maintenance' => [
72+
'/db_cleanup',
73+
'/db_repair',
74+
'/log_rotate',
75+
],
76+
],
77+
],
78+
'custom_input' => '{"some":"raw", "json":"update"}',
6779
];
6880

6981
public function setUp()

0 commit comments

Comments
 (0)