Skip to content

Commit 4012ff0

Browse files
authored
Merge pull request #1164 from noplanman/input-improvements
Better update logging and error messages.
2 parents 2093cff + 60f705f commit 4012ff0

File tree

3 files changed

+31
-30
lines changed

3 files changed

+31
-30
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c
88
- [:ledger: View file changes][Unreleased]
99
### Added
1010
### Changed
11+
- Improved error messages for empty input.
12+
- Log update when processing it, not when fetching input.
1113
### Deprecated
1214
### Removed
1315
### Fixed

src/Request.php

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -293,21 +293,15 @@ public static function setClient(ClientInterface $client): void
293293
}
294294

295295
/**
296-
* Set input from custom input or stdin and return it
296+
* Get input from custom input or stdin and return it
297297
*
298298
* @return string
299299
*/
300300
public static function getInput(): string
301301
{
302302
// First check if a custom input has been set, else get the PHP input.
303-
$input = self::$telegram->getCustomInput();
304-
if (empty($input)) {
305-
$input = file_get_contents('php://input');
306-
}
307-
308-
TelegramLog::update($input);
309-
310-
return $input;
303+
return self::$telegram->getCustomInput()
304+
?: file_get_contents('php://input');
311305
}
312306

313307
/**
@@ -496,11 +490,6 @@ public static function execute(string $action, array $data = []): string
496490
$request_params
497491
);
498492
$result = (string) $response->getBody();
499-
500-
//Logging getUpdates Update
501-
if ($action === 'getUpdates') {
502-
TelegramLog::update($result);
503-
}
504493
} catch (RequestException $e) {
505494
$response = null;
506495
$result = $e->getResponse() ? (string) $e->getResponse()->getBody() : '';

src/Telegram.php

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -417,17 +417,24 @@ public function handleGetUpdates(int $limit = 0, int $timeout = 0): ServerRespon
417417

418418
//Take custom input into account.
419419
if ($custom_input = $this->getCustomInput()) {
420-
$response = new ServerResponse(json_decode($custom_input, true), $this->bot_username);
420+
try {
421+
$input = json_decode($this->input, true, 512, JSON_THROW_ON_ERROR);
422+
if (empty($input)) {
423+
throw new TelegramException('Custom input is empty');
424+
}
425+
$response = new ServerResponse($input, $this->bot_username);
426+
} catch (\Throwable $e) {
427+
throw new TelegramException('Invalid custom input JSON: ' . $e->getMessage());
428+
}
421429
} else {
422430
if (DB::isDbConnected() && $last_update = DB::selectTelegramUpdate(1)) {
423-
//Get last update id from the database
424-
$last_update = reset($last_update);
425-
431+
// Get last Update id from the database.
432+
$last_update = reset($last_update);
426433
$this->last_update_id = $last_update['id'] ?? null;
427434
}
428435

429436
if ($this->last_update_id !== null) {
430-
$offset = $this->last_update_id + 1; //As explained in the telegram bot API documentation
437+
$offset = $this->last_update_id + 1; // As explained in the telegram bot API documentation.
431438
}
432439

433440
$response = Request::getUpdates([
@@ -438,12 +445,13 @@ public function handleGetUpdates(int $limit = 0, int $timeout = 0): ServerRespon
438445
}
439446

440447
if ($response->isOk()) {
441-
$results = $response->getResult();
448+
// Log update.
449+
TelegramLog::update($response->toJson());
442450

443-
//Process all updates
444-
/** @var Update $result */
445-
foreach ($results as $result) {
446-
$this->processUpdate($result);
451+
// Process all updates
452+
/** @var Update $update */
453+
foreach ($response->getResult() as $update) {
454+
$this->processUpdate($update);
447455
}
448456

449457
if (!DB::isDbConnected() && !$custom_input && $this->last_update_id !== null && $offset === 0) {
@@ -472,15 +480,17 @@ public function handle(): bool
472480
throw new TelegramException('Bot Username is not defined!');
473481
}
474482

475-
$this->input = Request::getInput();
476-
477-
if (empty($this->input)) {
478-
throw new TelegramException('Input is empty!');
483+
$input = Request::getInput();
484+
if (empty($input)) {
485+
throw new TelegramException('Input is empty! The webhook must not be called manually, only by Telegram.');
479486
}
480487

481-
$post = json_decode($this->input, true);
488+
// Log update.
489+
TelegramLog::update($input);
490+
491+
$post = json_decode($input, true);
482492
if (empty($post)) {
483-
throw new TelegramException('Invalid JSON!');
493+
throw new TelegramException('Invalid input JSON! The webhook must not be called manually, only by Telegram.');
484494
}
485495

486496
if ($response = $this->processUpdate(new Update($post, $this->bot_username))) {

0 commit comments

Comments
 (0)