Added CommandHandler

This commit is contained in:
Netkas 2023-02-19 17:25:59 -05:00
parent eacc106666
commit 2362365865
5 changed files with 134 additions and 3 deletions

View file

@ -0,0 +1,12 @@
<?php
namespace TgBotLib\Abstracts;
abstract class UpdateEventType
{
const GenericUpdate = 'generic_update';
const Message = 'message';
const EditedMessage = 'edited_message';
}

View file

@ -7,6 +7,7 @@
use CURLFile; use CURLFile;
use CurlHandle; use CurlHandle;
use TgBotLib\Exceptions\TelegramException; use TgBotLib\Exceptions\TelegramException;
use TgBotLib\Interfaces\CommandInterface;
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;
@ -51,6 +52,11 @@
*/ */
private $curl_handle; private $curl_handle;
/**
* @var CommandInterface[]
*/
private $command_handlers;
/** /**
* Public Constructor * Public Constructor
* *
@ -62,6 +68,7 @@
$this->host = 'api.telegram.org'; $this->host = 'api.telegram.org';
$this->ssl = true; $this->ssl = true;
$this->last_update_id = 0; $this->last_update_id = 0;
$this->command_handlers = [];
} }
/** /**
@ -204,6 +211,71 @@
return $parsed['result']; return $parsed['result'];
} }
/**
* Sets a command handler for the specified command
*
* @param string $command
* @param CommandInterface $handler
* @return void
* @noinspection PhpUnused
*/
public function setCommandHandler(string $command, CommandInterface $handler): void
{
$this->command_handlers[$command] = $handler;
}
/**
* Removes the command handler for the specified command
*
* @param string $command
* @return void
* @noinspection PhpUnused
*/
public function removeCommandHandler(string $command): void
{
unset($this->command_handlers[$command]);
}
/**
* Handles a single update object
*
* @param Update $update
* @return void
*/
public function handleUpdate(Update $update): void
{
if(($update->getMessage() ?? null) !== null && ($update->getMessage()->getText() ?? null) !== null)
{
$text = $update->getMessage()->getText();
if(strpos($text, '/') === 0)
{
$command = explode(' ', substr($text, 1))[0];
if(isset($this->command_handlers[$command]))
{
$this->command_handlers[$command]->handle($this, $update);
}
}
}
}
/**
* Handles all updates received from the Telegram API using long polling
*
* @param bool $run_forever
* @return void
* @throws TelegramException
*/
public function handleGetUpdates(bool $run_forever = false): void
{
do
{
$updates = $this->getUpdates();
foreach($updates as $update)
{
$this->handleUpdate($update);
}
} while($run_forever);
}
/** /**
* Use this method to receive incoming updates using long polling (wiki). Returns an Array of Update objects. * Use this method to receive incoming updates using long polling (wiki). Returns an Array of Update objects.
@ -348,7 +420,7 @@
* @link https://core.telegram.org/bots/api#sendmessage * @link https://core.telegram.org/bots/api#sendmessage
* @noinspection PhpUnused * @noinspection PhpUnused
*/ */
public function sendMessage(string|int $chat_id, string $text, array $options): Message public function sendMessage(string|int $chat_id, string $text, array $options=[]): Message
{ {
return Message::fromArray($this->sendRequest('sendMessage', array_merge($options, [ return Message::fromArray($this->sendRequest('sendMessage', array_merge($options, [
'chat_id' => $chat_id, 'chat_id' => $chat_id,

View file

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

View file

@ -0,0 +1,23 @@
<?php
namespace commands;
use TgBotLib\Bot;
use TgBotLib\Exceptions\TelegramException;
use TgBotLib\Interfaces\CommandInterface;
use TgBotLib\Objects\Telegram\Update;
class StartCommand implements CommandInterface
{
/**
* @param Bot $bot
* @param Update $update
* @return void
* @throws TelegramException
*/
public function handle(Bot $bot, Update $update): void
{
// reply to the incoming message
$bot->sendMessage($update->getMessage()->getChat()->getId(), 'Hello, ' . $update->getMessage()->getFrom()->getFirstName());
}
}

View file

@ -1,7 +1,13 @@
<?php <?php
use commands\StartCommand;
require 'ncc'; require 'ncc';
import('net.nosial.tgbotlib'); import('net.nosial.tgbotlib');
$bot = new TgBotLib\Bot('5126030313:AAEn3QcwSvTJ2eAKUnSb_MkC5U0tlqkM1xw'); require 'commands/StartCommand.php';
$bot->sendMessage(chat_id: 570787098, text: 'Hello world!');
$bot = new TgBotLib\Bot('YOUR_BOT_TOKEN');
$bot->setCommandHandler('start', new StartCommand());
$bot->handleGetUpdates(true);