Add support for handling callback query events
This commit is contained in:
parent
f9741c250d
commit
c52d2f3601
3 changed files with 49 additions and 9 deletions
|
@ -19,6 +19,7 @@
|
||||||
use TgBotLib\Enums\Types\ChatActionType;
|
use TgBotLib\Enums\Types\ChatActionType;
|
||||||
use TgBotLib\Enums\Types\ParseMode;
|
use TgBotLib\Enums\Types\ParseMode;
|
||||||
use TgBotLib\Enums\Types\StickerFormat;
|
use TgBotLib\Enums\Types\StickerFormat;
|
||||||
|
use TgBotLib\Events\CallbackQueryEvent;
|
||||||
use TgBotLib\Events\CommandEvent;
|
use TgBotLib\Events\CommandEvent;
|
||||||
use TgBotLib\Events\UpdateEvent;
|
use TgBotLib\Events\UpdateEvent;
|
||||||
use TgBotLib\Exceptions\TelegramException;
|
use TgBotLib\Exceptions\TelegramException;
|
||||||
|
@ -375,6 +376,26 @@
|
||||||
return $results;
|
return $results;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @method array getEventHandlersByCallbackQuery(string $callbackData) Retrieves an array of event handlers whose callback data matches the given parameter.
|
||||||
|
* @param string $callbackData The callback data to match against the registered event handlers.
|
||||||
|
* @return array An array of matching event handler instances.
|
||||||
|
*/
|
||||||
|
public function getEventHandlersByCallbackQuery(string $callbackData): array
|
||||||
|
{
|
||||||
|
$results = [];
|
||||||
|
/** @var UpdateEvent $eventHandler */
|
||||||
|
foreach($this->eventHandlers as $eventHandler)
|
||||||
|
{
|
||||||
|
if(method_exists($eventHandler, 'getCallbackData') && $eventHandler::getCallbackData() === $callbackData)
|
||||||
|
{
|
||||||
|
$results[] = $eventHandler;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $results;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes all event handlers.
|
* Removes all event handlers.
|
||||||
*
|
*
|
||||||
|
@ -429,7 +450,6 @@
|
||||||
$command = $update?->getAnyMessage()?->getCommand();
|
$command = $update?->getAnyMessage()?->getCommand();
|
||||||
if($command !== null)
|
if($command !== null)
|
||||||
{
|
{
|
||||||
$commandExecuted = false;
|
|
||||||
Logger::getLogger()->debug(sprintf('Executing command %s for update %s', $command, $update->getUpdateId()));
|
Logger::getLogger()->debug(sprintf('Executing command %s for update %s', $command, $update->getUpdateId()));
|
||||||
|
|
||||||
/** @var CommandEvent $eventHandler */
|
/** @var CommandEvent $eventHandler */
|
||||||
|
@ -438,7 +458,6 @@
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
(new $eventHandler($update))->handle($this);
|
(new $eventHandler($update))->handle($this);
|
||||||
$commandExecuted = true;
|
|
||||||
}
|
}
|
||||||
catch(TelegramException $e)
|
catch(TelegramException $e)
|
||||||
{
|
{
|
||||||
|
@ -449,10 +468,28 @@
|
||||||
Logger::getLogger()->error(sprintf('Exception occurred: %s', $e->getMessage()), $e);
|
Logger::getLogger()->error(sprintf('Exception occurred: %s', $e->getMessage()), $e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if($commandExecuted)
|
$callbackData = $update?->getCallbackQuery()?->getData();
|
||||||
|
if($callbackData !== null)
|
||||||
{
|
{
|
||||||
return;
|
Logger::getLogger()->debug(sprintf('Executing callback query %s for update %s', $callbackData, $update->getUpdateId()));
|
||||||
|
|
||||||
|
/** @var CallbackQueryEvent $eventHandler */
|
||||||
|
foreach($this->getEventHandlersByCallbackQuery($callbackData) as $eventHandler)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
(new $eventHandler($update))->handle($this);
|
||||||
|
}
|
||||||
|
catch(TelegramException $e)
|
||||||
|
{
|
||||||
|
Logger::getLogger()->error(sprintf('Telegram exception occurred: %s', $e->getMessage()), $e);
|
||||||
|
}
|
||||||
|
catch(Exception $e)
|
||||||
|
{
|
||||||
|
Logger::getLogger()->error(sprintf('Exception occurred: %s', $e->getMessage()), $e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -115,11 +115,6 @@
|
||||||
return EventType::SHIPPING_QUERY;
|
return EventType::SHIPPING_QUERY;
|
||||||
}
|
}
|
||||||
|
|
||||||
if($update->getCallbackQuery() !== null)
|
|
||||||
{
|
|
||||||
return EventType::CALLBACK_QUERY;
|
|
||||||
}
|
|
||||||
|
|
||||||
if($update->getChosenInlineResult() !== null)
|
if($update->getChosenInlineResult() !== null)
|
||||||
{
|
{
|
||||||
return EventType::CHOSEN_INLINE_RESULT;
|
return EventType::CHOSEN_INLINE_RESULT;
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
namespace TgBotLib\Events;
|
namespace TgBotLib\Events;
|
||||||
|
|
||||||
|
use LogicException;
|
||||||
use TgBotLib\Enums\EventType;
|
use TgBotLib\Enums\EventType;
|
||||||
use TgBotLib\Objects\CallbackQuery;
|
use TgBotLib\Objects\CallbackQuery;
|
||||||
|
|
||||||
|
@ -15,6 +16,13 @@
|
||||||
return EventType::CALLBACK_QUERY;
|
return EventType::CALLBACK_QUERY;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves data associated with the callback.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public abstract static function getCallbackData(): string;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* New incoming callback query
|
* New incoming callback query
|
||||||
*
|
*
|
||||||
|
|
Loading…
Add table
Reference in a new issue