Botfire: getEvent Method Documentation
Overview
The getEvent method is a core component of the Botfire library for creating Telegram bots. It allows developers to process events received via Telegram webhooks, identifying the type of event and extracting relevant data for further handling.
Usage
To access the getEvent method, use the Bot class as follows:
use Botfire\Bot;
$event = Bot::getEvent();This method processes incoming webhook data from Telegram and provides a structured way to interact with the event.
Methods
The getEvent method returns an object with several methods to extract specific information about the event:
1. getName()
Returns the name of the event, which corresponds to one of the predefined event types.
Example
$event_name = Bot::getEvent()->getName();Possible Event Names
The event name can be one of the following constants:
message- A standard message sent to the bot.edited_message- An edited message.channel_post- A post in a channel.edited_channel_post- An edited channel post.business_connection- A business connection event.business_message- A message in a business context.edited_business_message- An edited business message.deleted_business_messages- Deleted business messages.message_reaction- A reaction to a message.message_reaction_count- The count of reactions to a message.inline_query- An inline query from a user.chosen_inline_result- A chosen inline query result.callback_query- A callback query from an inline button.shipping_query- A shipping query for payments.pre_checkout_query- A pre-checkout query for payments.purchased_paid_media- Purchased paid media.poll- A poll created in a chat.poll_answer- A user's answer to a poll.my_chat_member- Updates related to the bot's chat member status.chat_member- Updates related to a chat member's status.chat_join_request- A request to join a chat.chat_boost- A chat boost event.removed_chat_boost- A removed chat boost.
Example Usage
If a client sends a standard message to the bot, the following will return 'message':
$event_name = Bot::getEvent()->getName(); // Returns 'message'2. getType()
Returns the general type of the event. Some events with different names may share the same type. For example, both edited_message and channel_post are of type message.
Example
$type = Bot::getEvent()->getType(); // e.g., Returns 'message' for edited_message or channel_post3. Event Type Checks
The following methods check whether the message corresponds to a specific event type, based on the getEvent()->getName() method from the GetEvent class:
isCallbackQuery(): bool- Returnstrueif the event is a callback query.isEditedMessage(): bool- Returnstrueif the event is an edited message.isEditedChannelPost(): bool- Returnstrueif the event is an edited channel post.isMessage(): bool- Returnstrueif the event is a standard message and the message is valid.isChannelPost(): bool- Returnstrueif the event is a channel post.isBusinessMessage(): bool- Returnstrueif the event is a business message.isEditedBusinessMessage(): bool- Returnstrueif the event is an edited business message.
Example
To check if the message is a callback query:
if (Bot::getEvent()->isCallbackQuery()) {
// Handle callback query
}4. getBody()
Returns the full data payload of the event, regardless of its type. This method provides access to the detailed information contained within the Telegram webhook update.
Example
$event_data = Bot::getEvent()->getBody();5. getUpdateId()
Returns the unique identifier for the update, as described by the Telegram API:
The update's unique identifier. Update identifiers start from a certain positive number and increase sequentially. This identifier becomes especially handy if you're using webhooks, since it allows you to ignore repeated updates or to restore the correct update sequence, should they get out of order. If there are no new updates for at least a week, then the identifier of the next update will be chosen randomly instead of sequentially.
Example
$update_id = Bot::getEvent()->getUpdateId();Notes
- The
getEventmethod is designed to handle incoming Telegram webhook updates, making it essential for real-time bot interactions. - Use the
getName()method to identify the specific event and handle it accordingly in your bot logic. - The
getType()method is useful for grouping similar events (e.g., treatingedited_messageandchannel_postasmessagetypes). - The
getBody()method provides raw event data, which can be processed based on the event type.