@@ -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,47 +457,58 @@ 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- }
465-
466- $ text = (is_null ($ text )?'' :$ text );
467-
468- $ output .= sprintf (
469- '%d: %s ' . PHP_EOL ,
470- $ chat_id ,
471- preg_replace ('/\s+/ ' , ' ' , trim ($ text ))
472- );
473- }
460+ $ get_updates_response = $ this ->telegram ->handleGetUpdates ();
461+
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 ));
474465 } else {
475- $ output .= sprintf ( ' Failed to fetch updates: %s ' . PHP_EOL , $ response -> printError ( ));
466+ $ this -> handleOutput ( $ this -> defaultGetUpdatesCallback ( $ get_updates_response ));
476467 }
477468
478- $ this ->handleOutput ($ output );
479-
480469 return $ this ;
481470 }
482471
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+ $ results = array_filter ((array ) $ get_updates_response ->getResult ());
482+
483+ $ output = date ('Y-m-d H:i:s ' ) . ' - ' ;
484+ $ output .= sprintf ('Updates processed: %d ' . PHP_EOL , count ($ results ));
485+
486+ /** @var Entities\Update $result */
487+ foreach ($ results as $ result ) {
488+ $ chat_id = 0 ;
489+ $ text = 'Nothing ' ;
490+
491+ $ update_content = $ result ->getUpdateContent ();
492+ if ($ update_content instanceof Entities \Message) {
493+ $ chat_id = $ update_content ->getFrom ()->getId ();
494+ $ text = $ update_content ->getType ();
495+ } elseif ($ update_content instanceof Entities \InlineQuery ||
496+ $ update_content instanceof Entities \ChosenInlineResult
497+ ) {
498+ $ chat_id = $ update_content ->getFrom ()->getId ();
499+ $ text = $ update_content ->getQuery ();
500+ }
501+
502+ $ output .= sprintf (
503+ '%d: %s ' . PHP_EOL ,
504+ $ chat_id ,
505+ preg_replace ('/\s+/ ' , ' ' , trim ($ text ))
506+ );
507+ }
508+
509+ return $ output ;
510+ }
511+
483512 /**
484513 * Handle the updates using the Webhook method.
485514 *
0 commit comments