Routing Reference
Routing Reference
Section titled “Routing Reference”This section contains a complete list of all available routing methods from the TelegramRouter facade. Each method describes what parameters it accepts, which Telegram events it responds to, and what data is passed to the handler.
Note: If you prefer a more modern approach, you can also define routes using PHP 8 attributes. See PHP Attributes Routing for an alternative approach to route registration.
Common Parameters
Section titled “Common Parameters”Most routing methods accept the following common parameters:
$action(array|string|\Closure) — route handler. Can be:- Closure/function
- String in format
'Controller@method' - Array
[Controller::class, 'method']
$botId(string, default'*') — ID of the bot for which the route is registered.'*'means all bots$pattern(\Closure|string|null) — pattern for filtering. Can be:- String with
*support (wildcard) - Closure for custom validation
nullor'*'to handle all events of this type
- String with
All handlers receive a data object that inherits from AbstractRouteData (namespace HybridGram\Core\Routing\RouteData) and contains:
$data->update— fullUpdateobject from Telegram$data->botId— bot ID$data->getChat()— method to getChatobject$data->getUser()— method to getUserobject$data->getChatId()— chat ID (ornull)$data->getUserId()— user ID (ornull)
Commands and Messages
Section titled “Commands and Messages”onCommand()
Section titled “onCommand()”Handles commands starting with /.
Parameters:
$action— handler$botId(default'*') — bot ID$pattern(defaultnull) — command pattern (e.g.,'/start','/user:*')$commandParamOptions(?\Closure) — optional filter by command parameters
Event: message.text starts with /
Data: CommandData
$data->command— command name (without/)$data->commandParams— array of arguments after command$data->update$data->botId$data->getChat()$data->getUser()
Example:
TelegramRouter::onCommand('/start', function(CommandData $data) { // Handle /start command});
TelegramRouter::onCommand('/user:*', function(CommandData $data) { $userId = $data->commandParams[0] ?? null;});onMessage()
Section titled “onMessage()”Handles text messages.
Parameters:
$action— handler$botId(default'*') — bot ID$pattern(defaultnull) — pattern for message text
Event: message.text present
Data: TextMessageData (HybridGram\Core\Routing\RouteData\TextMessageData)
$data->text— message text (string)$data->message— fullMessageobject$data->update$data->botId$data->getChat()$data->getUser()
Example:
use HybridGram\Core\Routing\RouteData\TextMessageData;
TelegramRouter::onTextMessage(function(TextMessageData $data) { // Handle all messages; text is in $data->text});
TelegramRouter::onTextMessage(function(TextMessageData $data) { // Handle messages with pattern}, '*', 'hello*');Media Content
Section titled “Media Content”onPhoto()
Section titled “onPhoto()”Handles sent photos.
Parameters:
$action— handler$botId(default'*') — bot ID$pattern(\Closure|null, defaultnull) — pattern for caption
Event: message.photo present
Data: PhotoData
$data->photoSizes— array ofPhotoSizeobjects$data->update$data->botId$data->getChat()$data->getUser()
onDocument()
Section titled “onDocument()”Handles sent documents.
Parameters:
$action— handler$botId(default'*') — bot ID$pattern(defaultnull) — pattern for caption$documentOptions(?array<MimeType|string>) — optional filter by MIME types
Event: message.document present
Data: DocumentData
$data->document—Documentobject$data->update$data->botId$data->getChat()$data->getUser()
Example:
use HybridGram\Telegram\Document\MimeType;
TelegramRouter::onDocument(function(DocumentData $data) { // Handle all documents}, '*', null, [MimeType::PDF, MimeType::JSON]);onAudio()
Section titled “onAudio()”Handles audio files.
Event: message.audio present
Data: AudioData
$data->audio—Audioobject
onSticker()
Section titled “onSticker()”Handles stickers.
Event: message.sticker present
Data: StickerData
$data->sticker—Stickerobject
onVoice()
Section titled “onVoice()”Handles voice messages.
Event: message.voice present
Data: VoiceData
$data->voice—Voiceobject
onVideoNote()
Section titled “onVideoNote()”Handles video messages (round videos).
Event: message.videoNote present
Data: VideoNoteData
$data->videoNote—VideoNoteobject
Geolocation and Contacts
Section titled “Geolocation and Contacts”onLocation()
Section titled “onLocation()”Handles geolocation sending.
Event: message.location present
Data: LocationData
$data->location—Locationobject
onContact()
Section titled “onContact()”Handles contact sending.
Event: message.contact present
Data: ContactData
$data->contact—Contactobject
onPoll()
Section titled “onPoll()”Handles poll creation.
Parameters:
$action— handler$botId(default'*') — bot ID$pattern(\Closure|null, defaultnull) — optional pattern$isAnonymous(?bool) — filter by poll anonymity$pollType(?PollType) — filter by poll type (PollType::REGULARorPollType::QUIZ)
Event: message.poll present
Data: PollData
$data->poll—Pollobject
Example:
use HybridGram\Telegram\Poll\PollType;
TelegramRouter::onPoll(function(PollData $data) { // Handle only quizzes}, '*', null, false, PollType::QUIZ);Callback Query and Inline Query
Section titled “Callback Query and Inline Query”onCallbackQuery()
Section titled “onCallbackQuery()”Handles inline button presses.
Parameters:
$action— handler$botId(default'*') — bot ID$pattern(default'*') — pattern for action (e.g.,'menu:*')$queryParams(?array<string, string|null>|array<int, QueryParamInterface>) — optional query parameter filters
Event: callbackQuery present
Data: CallbackQueryData
$data->action— action string from callback data$data->params— parameter array from callback data$data->query—CallbackQueryobject$data->update$data->botId$data->getChat()$data->getUser()
onInlineQuery()
Section titled “onInlineQuery()”Handles inline queries.
Event: inlineQuery present
Data: InlineQueryData
$data->inlineQuery—InlineQueryobject
Chat Events
Section titled “Chat Events”onNewChatMembers()
Section titled “onNewChatMembers()”Handles adding new members to chat.
Data: NewChatMembersData
$data->newChatMembers— array of new members
onLeftChatMember()
Section titled “onLeftChatMember()”Handles member leaving chat.
Data: LeftChatMemberData
$data->leftChatMember— left member object
onMyChatMember()
Section titled “onMyChatMember()”Handles bot status changes in chat.
Event: myChatMember present
Data: ChatMemberUpdatedData
$data->chatMemberUpdated—ChatMemberUpdatedobject
Note: Works in all chat types by default (PRIVATE, GROUP, SUPERGROUP, CHANNEL).
onChatMember()
Section titled “onChatMember()”Handles chat member status changes.
Event: chatMember present
Data: ChatMemberUpdatedData
Universal Routes
Section titled “Universal Routes”onAny()
Section titled “onAny()”Handles any updates.
Data: AnyData
$data->update$data->botId
onFallback()
Section titled “onFallback()”Handles updates for which no suitable route was found.
Data: FallbackData
Helper Methods
Section titled “Helper Methods”forBot()
Section titled “forBot()”Returns route builder for specific bot.
TelegramRouter::forBot('main') ->onCommand('/start', function(CommandData $data) { // Route only for 'main' bot });group()
Section titled “group()”Groups routes with common attributes.
Attributes:
for_bot(string) — bot IDchat_type(ChatType|ChatType[]|null) — chat type or array of typesmiddlewares(array) — array of middleware classesfrom_state(array) — array of states from which transition should occurto_state(string) — state to transition to
TelegramRouter::group([ 'for_bot' => 'main', 'chat_type' => ChatType::GROUP, 'middlewares' => [AuthTelegramRouteMiddleware::class],], function($router) { $router->onCommand('/admin', function(CommandData $data) { // Route with group's common attributes });});chatType() and chatTypes()
Section titled “chatType() and chatTypes()”Set chat types for route.
// Single typeTelegramRouter::forBot('main') ->chatType(ChatType::PRIVATE) ->onCommand('/start', function(CommandData $data) { // ... });
// Multiple typesTelegramRouter::forBot('main') ->chatTypes([ChatType::PRIVATE, ChatType::GROUP]) ->onCommand('/help', function(CommandData $data) { // ... });Data Types for Filtering
Section titled “Data Types for Filtering”ChatType
Section titled “ChatType”Enum for filtering routes by chat type:
use HybridGram\Core\Routing\ChatType;
ChatType::PRIVATE // Private chatsChatType::GROUP // GroupsChatType::SUPERGROUP // SupergroupsChatType::CHANNEL // ChannelsMimeType
Section titled “MimeType”Enum for filtering documents by MIME type:
use HybridGram\Telegram\Document\MimeType;
MimeType::PNGMimeType::JPEGMimeType::PDFMimeType::JSON// ... and othersPollType
Section titled “PollType”Enum for filtering polls by type:
use HybridGram\Telegram\Poll\PollType;
PollType::REGULAR // Regular pollPollType::QUIZ // QuizChatMemberStatus
Section titled “ChatMemberStatus”Enum for filtering by chat member status:
use HybridGram\Telegram\ChatMember\ChatMemberStatus;
ChatMemberStatus::CREATORChatMemberStatus::ADMINISTRATORChatMemberStatus::MEMBERChatMemberStatus::RESTRICTEDChatMemberStatus::LEFTChatMemberStatus::KICKED