diff --git a/src/TgBotLib/Abstracts/UpdateEventType.php b/src/TgBotLib/Abstracts/UpdateEventType.php new file mode 100644 index 0000000..c3e849b --- /dev/null +++ b/src/TgBotLib/Abstracts/UpdateEventType.php @@ -0,0 +1,12 @@ +host = 'api.telegram.org'; $this->ssl = true; $this->last_update_id = 0; + $this->command_handlers = []; } /** @@ -204,6 +211,71 @@ 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. @@ -348,7 +420,7 @@ * @link https://core.telegram.org/bots/api#sendmessage * @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, [ 'chat_id' => $chat_id, diff --git a/src/TgBotLib/Interfaces/CommandInterface.php b/src/TgBotLib/Interfaces/CommandInterface.php new file mode 100644 index 0000000..93f3328 --- /dev/null +++ b/src/TgBotLib/Interfaces/CommandInterface.php @@ -0,0 +1,18 @@ +sendMessage($update->getMessage()->getChat()->getId(), 'Hello, ' . $update->getMessage()->getFrom()->getFirstName()); + } + } \ No newline at end of file diff --git a/tests/get_updates.php b/tests/get_updates.php index da40fa5..600b501 100644 --- a/tests/get_updates.php +++ b/tests/get_updates.php @@ -1,7 +1,13 @@ sendMessage(chat_id: 570787098, text: 'Hello world!'); \ No newline at end of file + require 'commands/StartCommand.php'; + + $bot = new TgBotLib\Bot('YOUR_BOT_TOKEN'); + $bot->setCommandHandler('start', new StartCommand()); + + $bot->handleGetUpdates(true); \ No newline at end of file