Skip to content

Commit 285a64c

Browse files
authored
Merge pull request #31 from php-telegram-bot/develop
Version 1.1.0
2 parents eb794c1 + c3cd3fc commit 285a64c

File tree

6 files changed

+117
-40
lines changed

6 files changed

+117
-40
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c
1111
### Fixed
1212
### Security
1313

14+
## [1.1.0] - 2017-05-23
15+
### Added
16+
- `webhookinfo` action to get result from `getWebhookInfo`.
17+
### Changed
18+
- Clean up and refactor some methods.
19+
### Fixed
20+
- Passing an empty array to `webhook.allowed_updates` parameter now correctly resets to defaults.
21+
1422
## [1.0.1] - 2017-05-09
1523
### Changed
1624
- Use more stable `longman/ip-tools` for IP matching.

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ There are a few parameters available to get things rolling:
5959
| --------- | ----------- |
6060
| s | **s**ecret: This is a special secret value defined in the main `manager.php` file. |
6161
| | This parameter is required to call the script via browser! |
62-
| a | **a**ction: The actual action to perform. (handle (default), cron, set, unset, reset) |
63-
| | **handle** executes the `getUpdates` method; **cron** executes cron commands; **set** / **unset** / **reset** the webhook. |
62+
| a | **a**ction: The actual action to perform. (handle (default), webhookinfo, cron, set, unset, reset) |
63+
| | **handle** executes the `getUpdates` method; **webhookinfo** to get result from `getWebhookInfo`, **cron** executes cron commands; **set** / **unset** / **reset** the webhook. |
6464
| l | **l**oop: Number of seconds to loop the script for (used for getUpdates method). |
6565
| | This would be used mainly via CLI, to continually get updates for a certain period. |
6666
| i | **i**nterval: Number of seconds to wait between getUpdates requests (used for getUpdates method, default is 2). |
@@ -180,7 +180,8 @@ The `secret` is a user-defined key that is required to execute any of the librar
180180
Best make it long, random and very unique!
181181

182182
For 84 random characters:
183-
- If you have `pwgen` installed, just execute `pwgen 84` and choose any one.
183+
- If you have `pwgen` installed, just execute `pwgen 84 1` and copy the output.
184+
- If you have `openssl` installed, use `openssl rand -hex 84`.
184185
- Or just go [here][random-characters] and put all the output onto a single line.
185186

186187
(You get 2 guesses why 84 is a good number :wink:)

src/Action.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class Action
2323
'reset',
2424
'handle',
2525
'cron',
26+
'webhookinfo',
2627
];
2728

2829
/**

src/BotManager.php

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -122,18 +122,22 @@ public function run(): self
122122
{
123123
// Make sure this is a valid call.
124124
$this->validateSecret();
125+
$this->validateRequest();
125126

126-
if (!$this->isValidRequest()) {
127-
throw new InvalidAccessException('Invalid access');
127+
if ($this->action->isAction('webhookinfo')) {
128+
$webhookinfo = Request::getWebhookInfo();
129+
print_r($webhookinfo->getResult() ?: $webhookinfo->printError());
130+
return $this;
128131
}
129-
130132
if ($this->action->isAction(['set', 'unset', 'reset'])) {
131-
$this->validateAndSetWebhook();
132-
} elseif ($this->action->isAction('handle')) {
133-
$this->setBotExtras();
133+
return $this->validateAndSetWebhook();
134+
}
135+
136+
$this->setBotExtras();
137+
138+
if ($this->action->isAction('handle')) {
134139
$this->handleRequest();
135140
} elseif ($this->action->isAction('cron')) {
136-
$this->setBotExtras();
137141
$this->handleCron();
138142
}
139143

@@ -206,7 +210,13 @@ public function validateAndSetWebhook(): self
206210
'certificate' => $webhook['certificate'] ?? null,
207211
'max_connections' => $webhook['max_connections'] ?? null,
208212
'allowed_updates' => $webhook['allowed_updates'] ?? null,
209-
]);
213+
], function ($v, $k) {
214+
if ($k === 'allowed_updates') {
215+
// Special case for allowed_updates, which can be an empty array.
216+
return is_array($v);
217+
}
218+
return !empty($v);
219+
}, ARRAY_FILTER_USE_BOTH);
210220

211221
$this->handleOutput(
212222
$this->telegram->setWebhook(
@@ -328,17 +338,15 @@ protected function setBotExtrasRequest(): self
328338
*/
329339
public function handleRequest(): self
330340
{
331-
if (empty($this->params->getBotParam('webhook.url'))) {
332-
if ($loop_time = $this->getLoopTime()) {
333-
$this->handleGetUpdatesLoop($loop_time, $this->getLoopInterval());
334-
} else {
335-
$this->handleGetUpdates();
336-
}
337-
} else {
338-
$this->handleWebhook();
341+
if ($this->params->getBotParam('webhook.url')) {
342+
return $this->handleWebhook();
339343
}
340344

341-
return $this;
345+
if ($loop_time = $this->getLoopTime()) {
346+
return $this->handleGetUpdatesLoop($loop_time, $this->getLoopInterval());
347+
}
348+
349+
return $this->handleGetUpdates();
342350
}
343351

344352
/**
@@ -353,9 +361,9 @@ public function handleCron(): self
353361

354362
$commands = [];
355363
foreach ($groups as $group) {
356-
$commands = array_merge($commands, $this->params->getBotParam('cron.groups.' . $group, []));
364+
$commands[] = $this->params->getBotParam('cron.groups.' . $group, []);
357365
}
358-
$this->telegram->runCommands($commands);
366+
$this->telegram->runCommands(array_merge(...$commands));
359367

360368
return $this;
361369
}
@@ -524,4 +532,16 @@ public function isValidRequest(): bool
524532
(array) $this->params->getBotParam('valid_ips', [])
525533
));
526534
}
535+
536+
/**
537+
* Make sure this is a valid request.
538+
*
539+
* @throws \TelegramBot\TelegramBotManager\Exception\InvalidAccessException
540+
*/
541+
private function validateRequest()
542+
{
543+
if (!$this->isValidRequest()) {
544+
throw new InvalidAccessException('Invalid access');
545+
}
546+
}
527547
}

src/Params.php

Lines changed: 64 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -110,37 +110,67 @@ public function __construct(array $params)
110110
/**
111111
* Validate and set up the vital and extra params.
112112
*
113-
* @param $params
113+
* @param array $params
114114
*
115115
* @return \TelegramBot\TelegramBotManager\Params
116116
* @throws \TelegramBot\TelegramBotManager\Exception\InvalidParamsException
117117
*/
118-
private function validateAndSetBotParams($params): self
118+
private function validateAndSetBotParams(array $params): self
119+
{
120+
$this->validateAndSetBotParamsVital($params);
121+
$this->validateAndSetBotParamsSpecial($params);
122+
$this->validateAndSetBotParamsExtra($params);
123+
124+
return $this;
125+
}
126+
127+
/**
128+
* Set all vital params.
129+
*
130+
* @param array $params
131+
*
132+
* @throws \TelegramBot\TelegramBotManager\Exception\InvalidParamsException
133+
*/
134+
private function validateAndSetBotParamsVital(array $params)
119135
{
120-
// Set all vital params.
121136
foreach (self::$valid_vital_bot_params as $vital_key) {
122137
if (!array_key_exists($vital_key, $params)) {
123138
throw new InvalidParamsException('Some vital info is missing: ' . $vital_key);
124139
}
125140

126141
$this->bot_params[$vital_key] = $params[$vital_key];
127142
}
143+
}
128144

145+
/**
146+
* Special case parameters.
147+
*
148+
* @param array $params
149+
*
150+
* @throws \TelegramBot\TelegramBotManager\Exception\InvalidParamsException
151+
*/
152+
private function validateAndSetBotParamsSpecial(array $params)
153+
{
129154
// Special case, where secret MUST be defined if we have a webhook.
130155
if (($params['webhook'] ?? null) && !($params['secret'] ?? null)) {
131156
throw new InvalidParamsException('Some vital info is missing: secret');
132157
}
158+
}
133159

134-
// Set all extra params.
160+
/**
161+
* Set all extra params.
162+
*
163+
* @param array $params
164+
*/
165+
private function validateAndSetBotParamsExtra(array $params)
166+
{
135167
foreach (self::$valid_extra_bot_params as $extra_key) {
136168
if (!array_key_exists($extra_key, $params)) {
137169
continue;
138170
}
139171

140172
$this->bot_params[$extra_key] = $params[$extra_key];
141173
}
142-
143-
return $this;
144174
}
145175

146176
/**
@@ -152,28 +182,44 @@ private function validateAndSetBotParams($params): self
152182
* @return \TelegramBot\TelegramBotManager\Params
153183
*/
154184
private function validateAndSetScriptParams(): self
185+
{
186+
$this->setScriptParams();
187+
$this->validateScriptParams();
188+
189+
return $this;
190+
}
191+
192+
/**
193+
* Set script parameters from query string or CLI.
194+
*/
195+
private function setScriptParams()
155196
{
156197
$this->script_params = $_GET;
157198

158-
// If we're running from CLI, properly set script parameters.
159-
if ('cli' === PHP_SAPI) {
160-
// We don't need the first arg (the file name).
161-
$args = array_slice($_SERVER['argv'], 1);
199+
// If we're not running from CLI, script parameters are already set from $_GET.
200+
if ('cli' !== PHP_SAPI) {
201+
return;
202+
}
162203

163-
/** @var array $args */
164-
foreach ($args as $arg) {
165-
@list($key, $val) = explode('=', $arg);
166-
isset($key, $val) && $this->script_params[$key] = $val;
167-
}
204+
// We don't need the first arg (the file name).
205+
$args = array_slice($_SERVER['argv'], 1);
206+
207+
/** @var array $args */
208+
foreach ($args as $arg) {
209+
@list($key, $val) = explode('=', $arg);
210+
isset($key, $val) && $this->script_params[$key] = $val;
168211
}
212+
}
169213

170-
// Keep only valid ones.
214+
/**
215+
* Keep only valid script parameters.
216+
*/
217+
private function validateScriptParams()
218+
{
171219
$this->script_params = array_intersect_key(
172220
$this->script_params,
173221
array_fill_keys(self::$valid_script_params, null)
174222
);
175-
176-
return $this;
177223
}
178224

179225
/**

tests/ActionTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public function testConstruct()
2121
self::assertEquals('reset', (new Action('reset'))->getAction());
2222
self::assertEquals('handle', (new Action('handle'))->getAction());
2323
self::assertEquals('cron', (new Action('cron'))->getAction());
24+
self::assertEquals('webhookinfo', (new Action('webhookinfo'))->getAction());
2425
}
2526

2627
/**

0 commit comments

Comments
 (0)