From 513344b4a63725872f1160b5982f5ea006be7592 Mon Sep 17 00:00:00 2001 From: netkas Date: Mon, 4 Nov 2024 01:17:03 -0500 Subject: [PATCH] Add command handling in PollingBot --- src/TgBotLib/PollingBot.php | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/TgBotLib/PollingBot.php b/src/TgBotLib/PollingBot.php index d883ed1..27b3e63 100644 --- a/src/TgBotLib/PollingBot.php +++ b/src/TgBotLib/PollingBot.php @@ -6,6 +6,8 @@ use TgBotLib\Abstracts\UpdateEvent; use TgBotLib\Classes\Logger; use TgBotLib\Enums\EventType; + use TgBotLib\Events\CommandEvent; + use TgBotLib\Objects\Update; use Throwable; /** @@ -228,10 +230,27 @@ */ private function processUpdates(array $updates): void { + /** @var Update $update */ foreach ($updates as $update) { - $updateByType = $this->getEventHandlersByType(EventType::determineEventType($update)); + // Check if the update contains a command + $command = $update->getAnyMessage()->getCommand(); + if ($command !== null) + { + $commandHandlers = $this->getEventHandlersByCommand($command); + foreach ($commandHandlers as $commandHandler) + { + /** @var CommandEvent $commandHandler */ + if ($commandHandler::getCommand() === $command) + { + (new $commandHandler($update))->handle($this); + continue 2; + } + } + } + // Check if the update contains a callback query + $updateByType = $this->getEventHandlersByType(EventType::determineEventType($update)); if (count($updateByType) === 0) { // If no event handlers are found appropriate for the update type, use the generic update event handler