@@ -417,17 +417,24 @@ public function handleGetUpdates(int $limit = 0, int $timeout = 0): ServerRespon
417
417
418
418
//Take custom input into account.
419
419
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
+ }
421
429
} else {
422
430
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 );
426
433
$ this ->last_update_id = $ last_update ['id ' ] ?? null ;
427
434
}
428
435
429
436
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.
431
438
}
432
439
433
440
$ response = Request::getUpdates ([
@@ -438,12 +445,13 @@ public function handleGetUpdates(int $limit = 0, int $timeout = 0): ServerRespon
438
445
}
439
446
440
447
if ($ response ->isOk ()) {
441
- $ results = $ response ->getResult ();
448
+ // Log update.
449
+ TelegramLog::update ($ response ->toJson ());
442
450
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 );
447
455
}
448
456
449
457
if (!DB ::isDbConnected () && !$ custom_input && $ this ->last_update_id !== null && $ offset === 0 ) {
@@ -472,15 +480,17 @@ public function handle(): bool
472
480
throw new TelegramException ('Bot Username is not defined! ' );
473
481
}
474
482
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. ' );
479
486
}
480
487
481
- $ post = json_decode ($ this ->input , true );
488
+ // Log update.
489
+ TelegramLog::update ($ input );
490
+
491
+ $ post = json_decode ($ input , true );
482
492
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. ' );
484
494
}
485
495
486
496
if ($ response = $ this ->processUpdate (new Update ($ post , $ this ->bot_username ))) {
0 commit comments