Skip to content

Commit 656f9e7

Browse files
committed
Accept default update types sent by Telegram, if not explicitly specified.
1 parent 2c193f5 commit 656f9e7

File tree

2 files changed

+30
-26
lines changed

2 files changed

+30
-26
lines changed

README.md

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -341,30 +341,19 @@ $telegram->useGetUpdatesWithoutDatabase();
341341

342342
## Filter Update
343343

344-
Update processing can be allowed or denied by defining a custom update filter.
344+
:exclamation: Note that by default, Telegram will send any new update types that may be added in the future. This may cause commands that don't take this into account to break!
345345

346-
Let's say we only want to allow messages from a user with ID `428`, we can do the following before handling the request:
346+
It is suggested that you specifically define which update types your bot can receive and handle correctly.
347347

348-
```php
349-
$telegram->setUpdateFilter(function (Update $update, Telegram $telegram, &$reason = 'Update denied by update_filter') {
350-
$user_id = $update->getMessage()->getFrom()->getId();
351-
if ($user_id === 428) {
352-
return true;
353-
}
354-
355-
$reason = "Invalid user with ID {$user_id}";
356-
return false;
357-
});
358-
```
359-
360-
The reason for denying an update can be defined with the `$reason` parameter. This text gets written to the debug log.
361-
362-
Alternatively, you can define which update types are handled by your bot, by defining them when setting the [webhook](#webhook-installation) or passing an array of allowed types when using [getUpdates](#getupdates-installation).
348+
You can define which update types are sent to your bot by defining them when setting the [webhook](#webhook-installation) or passing an array of allowed types when using [getUpdates](#getupdates-installation).
363349

364350
```php
365351
use Longman\TelegramBot\Entities\Update;
366352

367-
// Define the list of allowed Update types here.
353+
// For all update types currently implemented in this library:
354+
// $allowed_updates = Update::getUpdateTypes();
355+
356+
// Define the list of allowed Update types manually:
368357
$allowed_updates = [
369358
Update::TYPE_MESSAGE,
370359
Update::TYPE_CHANNEL_POST,
@@ -378,6 +367,24 @@ $telegram->setWebhook($hook_url, ['allowed_updates' => $allowed_updates]);
378367
$telegram->handleGetUpdates(['allowed_updates' => $allowed_updates]);
379368
```
380369

370+
Alternatively, Update processing can be allowed or denied by defining a custom update filter.
371+
372+
Let's say we only want to allow messages from a user with ID `428`, we can do the following before handling the request:
373+
374+
```php
375+
$telegram->setUpdateFilter(function (Update $update, Telegram $telegram, &$reason = 'Update denied by update_filter') {
376+
$user_id = $update->getMessage()->getFrom()->getId();
377+
if ($user_id === 428) {
378+
return true;
379+
}
380+
381+
$reason = "Invalid user with ID {$user_id}";
382+
return false;
383+
});
384+
```
385+
386+
The reason for denying an update can be defined with the `$reason` parameter. This text gets written to the debug log.
387+
381388
## Support
382389

383390
### Types

src/Telegram.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -415,9 +415,11 @@ public function handleGetUpdates($data = null, ?int $timeout = null): ServerResp
415415
);
416416
}
417417

418-
$offset = 0;
419-
$limit = null;
420-
$allowed_updates = null;
418+
$offset = 0;
419+
$limit = null;
420+
421+
// By default, get update types sent by Telegram.
422+
$allowed_updates = [];
421423

422424
// @todo Backwards compatibility for old signature, remove in next version.
423425
if (!is_array($data)) {
@@ -434,11 +436,6 @@ public function handleGetUpdates($data = null, ?int $timeout = null): ServerResp
434436
$allowed_updates = $data['allowed_updates'] ?? $allowed_updates;
435437
}
436438

437-
// By default, allow ALL known update types.
438-
if ($allowed_updates === null) {
439-
$allowed_updates = Update::getUpdateTypes();
440-
}
441-
442439
// Take custom input into account.
443440
if ($custom_input = $this->getCustomInput()) {
444441
try {

0 commit comments

Comments
 (0)