Events
When telegram send any type of update, this library converts it to an instance of the Update object, an to listen to it you need to create a event of the update type
Event types
All the current types of supported events is defined in the enum Mateodioev\TgHandler\Events\EventType:
EventType::messageMessage sent by a userEventType::callback_queryUser press a inline buttonEventType::edited_messageEventType::channel_postEventType::edited_channel_postEventType::inline_queryEventType::chosen_inline_resultEventType::shipping_queryEventType::pollEventType::poll_answerEventType::my_chat_memberEventType::chat_join_requestEventType::noneJust for ignore an eventEventType::allSpecial type to listen any type of update
To see the description of every type go to telegram documentation
Creating a new event
use Mateodioev\TgHandler\Events\{EventType, abstractEvent};
// Event to hear any text message sent by a user, channel, etc
class MyEvent extends abstractEvent
{
public EventType $type = EventType::message;
public function execute(array $args = [])
{
// Event logic here
$this->api()->sendMessage(
chatID: 'YOUR TARGET ID',
text: 'Hello',
);
}
}
In this event we set the property $type to a specific event type. To listen more events just create another event and add it to the bot.
To access to the Update information see Context object
All event
This is a special event that serves to listen to any type of event.
use Mateodioev\TgHandler\Events\Types\AllEvent;
class GlobalListener extends AllEvent
{
public function execute(array $args = [])
{
$type = $this->ctx()->eventType()->prettyName();
$raw = json_encode($this->ctx()->getReduced(), JSON_PRETTY_PRINT) . PHP_EOL;
echo $raw;
}
}
Adding the event to the bot
$bot->onEvent(new MyEventName());
Stopping the execution of the event
Throw the special StopCommand exception
use Mateodioev\TgHandler\Commands\StopCommand;
public function execute(array $args = [])
{
throw new StopCommand();
}
Note
If you set a message, the bot will try to send the same message to the user
throw new StopCommand('You can\'t use this command');
Use html format.