@@ -45,6 +45,11 @@ class BotManager
4545 */
4646 private $ action ;
4747
48+ /**
49+ * @var callable
50+ */
51+ private $ custom_get_updates_callback ;
52+
4853 /**
4954 * BotManager constructor.
5055 *
@@ -431,6 +436,19 @@ public function handleGetUpdatesLoop(int $loop_time_in_seconds, int $loop_interv
431436 return $ this ;
432437 }
433438
439+ /**
440+ * Set a custom callback for handling the output of the getUpdates results.
441+ *
442+ * @param callable $callback
443+ *
444+ * @return \TelegramBot\TelegramBotManager\BotManager
445+ */
446+ public function setCustomGetUpdatesCallback (callable $ callback ): BotManager
447+ {
448+ $ this ->custom_get_updates_callback = $ callback ;
449+ return $ this ;
450+ }
451+
434452 /**
435453 * Handle the updates using the getUpdates method.
436454 *
@@ -439,45 +457,61 @@ public function handleGetUpdatesLoop(int $loop_time_in_seconds, int $loop_interv
439457 */
440458 public function handleGetUpdates (): self
441459 {
442- $ output = date ('Y-m-d H:i:s ' , time ()) . ' - ' ;
443-
444- $ response = $ this ->telegram ->handleGetUpdates ();
445- if ($ response ->isOk ()) {
446- $ results = array_filter ((array ) $ response ->getResult ());
447-
448- $ output .= sprintf ('Updates processed: %d ' . PHP_EOL , count ($ results ));
449-
450- /** @var Entities\Update $result */
451- foreach ($ results as $ result ) {
452- $ chat_id = 0 ;
453- $ text = 'Nothing ' ;
454-
455- $ update_content = $ result ->getUpdateContent ();
456- if ($ update_content instanceof Entities \Message) {
457- $ chat_id = $ update_content ->getFrom ()->getId ();
458- $ text = $ update_content ->getText ();
459- } elseif ($ update_content instanceof Entities \InlineQuery ||
460- $ update_content instanceof Entities \ChosenInlineResult
461- ) {
462- $ chat_id = $ update_content ->getFrom ()->getId ();
463- $ text = $ update_content ->getQuery ();
464- }
460+ $ get_updates_response = $ this ->telegram ->handleGetUpdates ();
465461
466- $ output .= sprintf (
467- '%d: %s ' . PHP_EOL ,
468- $ chat_id ,
469- preg_replace ('/\s+/ ' , ' ' , trim ($ text ))
470- );
471- }
462+ // Check if the user has set a custom callback for handling the response.
463+ if ($ this ->custom_get_updates_callback !== null ) {
464+ $ this ->handleOutput (call_user_func ($ this ->custom_get_updates_callback , $ get_updates_response ));
472465 } else {
473- $ output .= sprintf ( ' Failed to fetch updates: %s ' . PHP_EOL , $ response -> printError ( ));
466+ $ this -> handleOutput ( $ this -> defaultGetUpdatesCallback ( $ get_updates_response ));
474467 }
475468
476- $ this ->handleOutput ($ output );
477-
478469 return $ this ;
479470 }
480471
472+ /**
473+ * Return the default output for getUpdates handling.
474+ *
475+ * @param Entities\ServerResponse $get_updates_response
476+ *
477+ * @return string
478+ */
479+ protected function defaultGetUpdatesCallback ($ get_updates_response ): string
480+ {
481+ /** @var Entities\Update[] $results */
482+ $ results = array_filter ((array ) $ get_updates_response ->getResult ());
483+
484+ $ output = sprintf (
485+ '%s - Updates processed: %d ' . PHP_EOL ,
486+ date ('Y-m-d H:i:s ' ),
487+ count ($ results )
488+ );
489+
490+ foreach ($ results as $ result ) {
491+ $ chat_id = 0 ;
492+ $ text = '<n/a> ' ;
493+
494+ $ update_content = $ result ->getUpdateContent ();
495+ if ($ update_content instanceof Entities \Message) {
496+ $ chat_id = $ update_content ->getFrom ()->getId ();
497+ $ text = sprintf ('<%s> ' , $ update_content ->getType ());
498+ } elseif ($ update_content instanceof Entities \InlineQuery ||
499+ $ update_content instanceof Entities \ChosenInlineResult
500+ ) {
501+ $ chat_id = $ update_content ->getFrom ()->getId ();
502+ $ text = sprintf ('<query> %s ' , $ update_content ->getQuery ());
503+ }
504+
505+ $ output .= sprintf (
506+ '%d: %s ' . PHP_EOL ,
507+ $ chat_id ,
508+ preg_replace ('/\s+/ ' , ' ' , trim ($ text ))
509+ );
510+ }
511+
512+ return $ output ;
513+ }
514+
481515 /**
482516 * Handle the updates using the Webhook method.
483517 *
0 commit comments