Added event handlers

This commit is contained in:
Netkas 2023-02-19 21:58:08 -05:00
parent 2362365865
commit 11e60c56c4
3 changed files with 226 additions and 1 deletions

View file

@ -0,0 +1,40 @@
<?php
namespace TgBotLib\Abstracts;
abstract class EventType
{
const GenericUpdate = 'generic_update';
const Message = 'message';
const EditedMessage = 'edited_message';
const GenericCommandMessage = 'generic_command_message';
const ChatMemberJoined = 'chat_member_joined';
const ChatMemberLeft = 'chat_member_left';
const ChatMemberKicked = 'chat_member_kicked';
const ChatMemberBanned = 'chat_member_banned';
const ChatMemberUnbanned = 'chat_member_unbanned';
const ChatMemberPromoted = 'chat_member_promoted';
const ChatMemberDemoted = 'chat_member_demoted';
const ChatMemberRestricted = 'chat_member_restricted';
const ChatMemberUnrestricted = 'chat_member_unrestricted';
const ChatMemberChanged = 'chat_member_changed';
const ChatTitleChanged = 'chat_title_changed';
const ChatPhotoChanged = 'chat_photo_changed';
const ChatDescriptionChanged = 'chat_description_changed';
}

View file

@ -6,8 +6,12 @@
use CURLFile; use CURLFile;
use CurlHandle; use CurlHandle;
use InvalidArgumentException;
use TgBotLib\Abstracts\ChatMemberStatus;
use TgBotLib\Abstracts\EventType;
use TgBotLib\Exceptions\TelegramException; use TgBotLib\Exceptions\TelegramException;
use TgBotLib\Interfaces\CommandInterface; use TgBotLib\Interfaces\CommandInterface;
use TgBotLib\Interfaces\EventInterface;
use TgBotLib\Interfaces\ObjectTypeInterface; use TgBotLib\Interfaces\ObjectTypeInterface;
use TgBotLib\Objects\Telegram\BotCommandScope; use TgBotLib\Objects\Telegram\BotCommandScope;
use TgBotLib\Objects\Telegram\Chat; use TgBotLib\Objects\Telegram\Chat;
@ -57,6 +61,11 @@
*/ */
private $command_handlers; private $command_handlers;
/**
* @var EventInterface[]
*/
private $event_handlers;
/** /**
* Public Constructor * Public Constructor
* *
@ -69,6 +78,7 @@
$this->ssl = true; $this->ssl = true;
$this->last_update_id = 0; $this->last_update_id = 0;
$this->command_handlers = []; $this->command_handlers = [];
$this->event_handlers = [];
} }
/** /**
@ -160,7 +170,7 @@
]); ]);
$response = curl_exec($ch); $response = curl_exec($ch);
print_r($response); print_r($response . PHP_EOL);
if ($response === false) if ($response === false)
throw new TelegramException('curl error: ' . curl_error($ch), curl_errno($ch)); throw new TelegramException('curl error: ' . curl_error($ch), curl_errno($ch));
@ -224,6 +234,30 @@
$this->command_handlers[$command] = $handler; $this->command_handlers[$command] = $handler;
} }
/**
* Sets an event handler for the specified event
*
* @param string $event
* @param EventInterface $handler
* @return void
* @noinspection PhpUnused
*/
public function setEventHandler(string $event, EventInterface $handler): void
{
switch($event)
{
case EventType::GenericUpdate:
case EventType::Message:
case EventType::EditedMessage:
break;
default:
throw new InvalidArgumentException('Invalid event type');
}
$this->event_handlers[$event] = $handler;
}
/** /**
* Removes the command handler for the specified command * Removes the command handler for the specified command
* *
@ -236,6 +270,18 @@
unset($this->command_handlers[$command]); unset($this->command_handlers[$command]);
} }
/**
* Removes the event handler for the specified event
*
* @param string $event
* @return void
* @noinspection PhpUnused
*/
public function removeEventHandler(string $event): void
{
unset($this->event_handlers[$event]);
}
/** /**
* Handles a single update object * Handles a single update object
* *
@ -244,6 +290,125 @@
*/ */
public function handleUpdate(Update $update): void public function handleUpdate(Update $update): void
{ {
// Process event handlers
foreach($this->event_handlers as $event => $handler)
{
switch($event)
{
case EventType::GenericUpdate:
$handler->handle($this, $update);
break;
case EventType::Message:
if(($update->getMessage() ?? null) !== null)
$handler->handle($this, $update);
break;
case EventType::EditedMessage:
if(($update->getEditedMessage() ?? null) !== null)
$handler->handle($this, $update);
break;
case EventType::GenericCommandMessage:
if(($update->getMessage() ?? null) !== null && ($update->getMessage()->getText() ?? null) !== null)
{
$text = $update->getMessage()->getText();
if(str_starts_with($text, '/'))
{
$handler->handle($this, $update);
}
}
break;
case EventType::ChatMemberJoined:
if(($update->getMessage() ?? null) !== null && ($update->getMessage()->getNewChatMembers() ?? null) !== null)
{
$handler->handle($this, $update);
}
break;
case EventType::ChatMemberLeft:
if(($update->getMessage() ?? null) !== null && ($update->getMessage()->getLeftChatMember() ?? null) !== null)
{
$handler->handle($this, $update);
}
break;
case EventType::ChatMemberKicked:
if(($update->getMyChatMember() ?? null) !== null && ($update->getMyChatMember()->getNewChatMember() ?? null) !== null)
{
if(
$update->getMyChatMember()->getNewChatMember()->getStatus() === ChatMemberStatus::Kicked &&
$update->getMyChatMember()->getNewChatMember()->getUntilDate() === null
)
{
$handler->handle($this, $update);
}
}
break;
case EventType::ChatMemberBanned:
if(($update->getMyChatMember() ?? null) !== null && ($update->getMyChatMember()->getNewChatMember() ?? null) !== null)
{
if(
$update->getMyChatMember()->getNewChatMember()->getStatus() === ChatMemberStatus::Kicked &&
$update->getMyChatMember()->getNewChatMember()->getUntilDate() !== null
)
{
$handler->handle($this, $update);
}
}
break;
case EventType::ChatMemberUnrestricted:
case EventType::ChatMemberDemoted:
case EventType::ChatMemberUnbanned:
if(($update->getMyChatMember() ?? null) !== null && ($update->getMyChatMember()->getNewChatMember() ?? null) !== null)
{
if($update->getMyChatMember()->getNewChatMember()->getStatus() === ChatMemberStatus::Member)
{
$handler->handle($this, $update);
}
}
break;
case EventType::ChatMemberPromoted:
if(($update->getMyChatMember() ?? null) !== null && ($update->getMyChatMember()->getNewChatMember() ?? null) !== null)
{
if($update->getMyChatMember()->getNewChatMember()->getStatus() === ChatMemberStatus::Administrator)
{
$handler->handle($this, $update);
}
}
break;
case EventType::ChatMemberRestricted:
if(($update->getMyChatMember() ?? null) !== null && ($update->getMyChatMember()->getNewChatMember() ?? null) !== null)
{
if($update->getMyChatMember()->getNewChatMember()->getStatus() === ChatMemberStatus::Restricted)
{
$handler->handle($this, $update);
}
}
break;
case EventType::ChatTitleChanged:
if(($update->getMessage() ?? null) !== null && ($update->getMessage()->getNewChatTitle() ?? null) !== null)
{
$handler->handle($this, $update);
}
break;
case EventType::ChatPhotoChanged:
if(($update->getMessage() ?? null) !== null && ($update->getMessage()->getNewChatPhoto() ?? null) !== null)
{
$handler->handle($this, $update);
}
break;
}
}
// Process command handlers
if(($update->getMessage() ?? null) !== null && ($update->getMessage()->getText() ?? null) !== null) if(($update->getMessage() ?? null) !== null && ($update->getMessage()->getText() ?? null) !== null)
{ {
$text = $update->getMessage()->getText(); $text = $update->getMessage()->getText();
@ -256,6 +421,8 @@
} }
} }
} }
} }
/** /**

View file

@ -0,0 +1,18 @@
<?php
namespace TgBotLib\Interfaces;
use TgBotLib\Bot;
use TgBotLib\Objects\Telegram\Update;
interface EventInterface
{
/**
* Execute the command
*
* @param Bot $bot
* @param Update $update
* @return void
*/
public function handle(Bot $bot, Update $update): void;
}