Skip to content

Commit 58b5ffa

Browse files
committed
Allow custom deny reason to be written to debug log, add short description to readme.
1 parent ee77f9a commit 58b5ffa

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ A Telegram Bot based on the official [Telegram Bot API]
4242
- [getUserProfilePhoto](#getuserprofilephoto)
4343
- [getFile and downloadFile](#getfile-and-downloadfile)
4444
- [Send message to all active chats](#send-message-to-all-active-chats)
45+
- [Filter Update](#filter-update)
4546
- [Utils](#utils)
4647
- [MySQL storage (Recommended)](#mysql-storage-recommended)
4748
- [External Database connection](#external-database-connection)
@@ -414,6 +415,25 @@ $results = Request::sendToActiveChats(
414415

415416
You can also broadcast a message to users, from the private chat with your bot. Take a look at the [admin commands](#admin-commands) below.
416417

418+
#### Filter Update
419+
420+
Update processing can be allowed or denied by defining a custom update filter.
421+
Let's say we only want to allow messages from a user with ID 428, we can do the following before handling the request:
422+
423+
```php
424+
$telegram->setUpdateFilter(function (Update $update, Telegram $telegram, &$reason = 'Update denied by update_filter') {
425+
$user_id = $update->getMessage()->getFrom()->getId();
426+
if ($user_id === 428) {
427+
return true;
428+
}
429+
430+
$reason = "Invalid user with ID {$user_id}";
431+
return false;
432+
});
433+
```
434+
435+
The reason for denying an update can be defined with the `$reason` parameter. This text gets written to the debug log.
436+
417437
## Utils
418438

419439
### MySQL storage (Recommended)

src/Telegram.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -464,14 +464,15 @@ public function processUpdate(Update $update)
464464
$this->last_update_id = $update->getUpdateId();
465465

466466
if (is_callable($this->update_filter)) {
467+
$reason = 'Update denied by update_filter';
467468
try {
468-
$allowed = (bool) call_user_func($this->update_filter, $update, $this);
469+
$allowed = (bool) call_user_func_array($this->update_filter, [$update, $this, &$reason]);
469470
} catch (\Exception $e) {
470471
$allowed = false;
471472
}
472473

473474
if (!$allowed) {
474-
TelegramLog::debug('Update denied by update_filter');
475+
TelegramLog::debug($reason);
475476
return new ServerResponse(['ok' => false, 'description' => 'denied'], null);
476477
}
477478
}

tests/unit/TelegramTest.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Longman\TelegramBot\Entities\Update;
1616
use Longman\TelegramBot\Exception\TelegramException;
1717
use Longman\TelegramBot\Telegram;
18+
use Longman\TelegramBot\TelegramLog;
1819

1920
/**
2021
* @package TelegramTest
@@ -171,14 +172,27 @@ public function testUpdateFilter()
171172
}
172173
}';
173174

175+
$debug_log_file = '/tmp/php-telegram-bot-update-filter-debug.log';
176+
TelegramLog::initialize(
177+
new \Monolog\Logger('bot_log', [
178+
(new \Monolog\Handler\StreamHandler($debug_log_file, \Monolog\Logger::DEBUG))->setFormatter(new \Monolog\Formatter\LineFormatter(null, null, true)),
179+
])
180+
);
181+
174182
$update = new Update(json_decode($rawUpdate, true), $this->telegram->getBotUsername());
175-
$this->telegram->setUpdateFilter(function ($update, $telegramInstance) {
176-
if ($update->getMessage()->getChat()->getId() == 313534466) {
183+
$this->telegram->setUpdateFilter(function (Update $update, Telegram $telegram, &$reason) {
184+
if ($update->getMessage()->getChat()->getId() === 313534466) {
185+
$reason = 'Invalid user, update denied.';
177186
return false;
178187
}
179188
return true;
180189
});
181190
$response = $this->telegram->processUpdate($update);
182191
$this->assertFalse($response->isOk());
192+
193+
// Check that the reason is written to the debug log.
194+
$debug_log = file_get_contents($debug_log_file);
195+
$this->assertStringContainsString('Invalid user, update denied.', $debug_log);
196+
file_exists($debug_log_file) && unlink($debug_log_file);
183197
}
184198
}

0 commit comments

Comments
 (0)