From 2899b43c550b15cb8fb79ca3d1cc72e0c8d51fe0 Mon Sep 17 00:00:00 2001 From: Milad Khodaverdi Date: Fri, 9 Aug 2024 19:28:37 +0330 Subject: [PATCH 1/5] Refactor `Command` class to improve handling of updates: - Replaced `getUpdate()->getMessage()->text` with `getUpdate()->getText()` to simplify text retrieval. - Improved `relevantMessageSubString` method to better handle different message formats and avoid potential errors. --- src/Commands/Command.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Commands/Command.php b/src/Commands/Command.php index 763cdeed..6b34a9c0 100644 --- a/src/Commands/Command.php +++ b/src/Commands/Command.php @@ -165,6 +165,7 @@ protected function parseCommandArguments(): array return []; } + // Generate the regex needed to search for this pattern [$pattern, $arguments] = $this->makeRegexPattern(); @@ -206,7 +207,7 @@ private function relevantMessageSubString(): string $commandOffsets = $this->allCommandOffsets(); if ($commandOffsets->isEmpty()) { - return $this->getUpdate()->getMessage()->text ?? ''; + return $this->getUpdate()->getText() ?? ''; } //Extract the current offset for this command and, if it exists, the offset of the NEXT bot_command entity @@ -228,7 +229,7 @@ private function allCommandOffsets(): Collection private function cutTextBetween(Collection $splice): string { return mb_substr( - $this->getUpdate()->getMessage()->text ?? '', + $this->getUpdate()->getText() ?? '', $splice->first(), $splice->last() - $splice->first() ); @@ -237,7 +238,7 @@ private function cutTextBetween(Collection $splice): string private function cutTextFrom(Collection $splice): string { return mb_substr( - $this->getUpdate()->getMessage()->text ?? '', + $this->getUpdate()->getText() ?? '', $splice->first() ); } From cdf4d308325d04e9504374aa85b8261148794da8 Mon Sep 17 00:00:00 2001 From: Milad Khodaverdi Date: Fri, 9 Aug 2024 19:28:42 +0330 Subject: [PATCH 2/5] Add support for callback queries in CommandBus - Updated `handler` method to handle `callback_query` updates. - Implemented command parsing for callback queries with format `/callback:::\w*`. - Maintained existing command parsing and execution for regular messages. - Ensured backward compatibility with previous command handling logic. --- src/Commands/CommandBus.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/Commands/CommandBus.php b/src/Commands/CommandBus.php index ec5b2720..80687937 100644 --- a/src/Commands/CommandBus.php +++ b/src/Commands/CommandBus.php @@ -142,6 +142,18 @@ protected function parseCommand(string $text, int $offset, int $length): string */ protected function handler(Update $update): Update { + if ($update->detectType() == "callback_query") { + preg_match('/\A(\/callback:::\w*)\s(.*)/', $update->getText(), $command); + [$text, $command] = $command; + $entity = [ + 'offset' => 0, + 'length' => strlen($command), + ]; + $this->process($entity, $update); + + return $update; + } + $message = $update->getMessage(); if ($message->has('entities')) { @@ -171,7 +183,7 @@ private function parseCommandsIn(Collection $message): Collection private function process(array $entity, Update $update): void { $command = $this->parseCommand( - $update->getMessage()->text, + $update->getText(), $entity['offset'], $entity['length'] ); From 49aeec3ca8ac406cfb550b3c2e3f3b70c2286d9e Mon Sep 17 00:00:00 2001 From: Milad Khodaverdi Date: Fri, 9 Aug 2024 19:29:17 +0330 Subject: [PATCH 3/5] feat: Add getText method to Update class for extracting text - Implemented `getText` method to retrieve text from various update types: - Returns message text for 'message', 'edited_message', 'channel_post', and 'edited_channel_post' types. - Returns formatted callback query data for 'callback_query' type. - Returns empty string for other types. - Updated docblocks to reflect the new method. - Maintained backward compatibility with deprecated methods. --- src/Objects/Update.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/Objects/Update.php b/src/Objects/Update.php index 88c0e91e..9c823183 100644 --- a/src/Objects/Update.php +++ b/src/Objects/Update.php @@ -180,4 +180,16 @@ public function hasCommand(): bool { return (bool) $this->getMessage()->get('entities', collect())->contains('type', 'bot_command'); } + + public function getText() + { + return match ($this->detectType()) { + 'message' => $this->message->text, + 'edited_message' => $this->editedMessage->text, + 'channel_post' => $this->channelPost->text, + 'edited_channel_post' => $this->editedChannelPost->text, + 'callback_query' => "/callback:::".$this->callbackQuery->data, + default => "", + }; + } } From b9d3a3178562b2f9fb2fbc1bb9f4a772114373e5 Mon Sep 17 00:00:00 2001 From: Milad Date: Fri, 4 Oct 2024 10:51:45 +0330 Subject: [PATCH 4/5] feat: Add getText method to Update class for extracting text - Implemented `getText` method to retrieve text from various update types: - Returns message text for 'message', 'edited_message', 'channel_post', and 'edited_channel_post' types. - Returns formatted callback query data for 'callback_query' type. - Returns empty string for other types. - Updated docblocks to reflect the new method. - Maintained backward compatibility with deprecated methods. --- src/Commands/Command.php | 1 + src/Commands/CommandBus.php | 69 ++++++++++++-------------------- src/Laravel/Facades/Telegram.php | 5 --- src/Methods/Message.php | 1 - 4 files changed, 27 insertions(+), 49 deletions(-) diff --git a/src/Commands/Command.php b/src/Commands/Command.php index 6b34a9c0..b725f527 100644 --- a/src/Commands/Command.php +++ b/src/Commands/Command.php @@ -3,6 +3,7 @@ namespace Telegram\Bot\Commands; use Illuminate\Support\Collection; +use Mockery\Exception; use Telegram\Bot\Answers\Answerable; use Telegram\Bot\Api; use Telegram\Bot\Objects\MessageEntity; diff --git a/src/Commands/CommandBus.php b/src/Commands/CommandBus.php index 80687937..007cb578 100644 --- a/src/Commands/CommandBus.php +++ b/src/Commands/CommandBus.php @@ -127,11 +127,9 @@ protected function parseCommand(string $text, int $offset, int $length): string } // remove leading slash - $command = substr( - $text, + $command = substr($text, $offset + 1, - $length - 1 - ); + $length - 1); // When in group - Ex: /command@MyBot. Just get the command name. return Str::of($command)->explode('@')->first(); @@ -143,24 +141,22 @@ protected function parseCommand(string $text, int $offset, int $length): string protected function handler(Update $update): Update { if ($update->detectType() == "callback_query") { - preg_match('/\A(\/callback:::\w*)\s(.*)/', $update->getText(), $command); + preg_match('/\A(\/callback:::\w*)\s*(.*)/', $update->getText(), $command); [$text, $command] = $command; $entity = [ 'offset' => 0, - 'length' => strlen($command), + 'length' => strlen($command) ]; $this->process($entity, $update); - return $update; } $message = $update->getMessage(); if ($message->has('entities')) { - $this->parseCommandsIn($message)->each(fn ($entity) => $this->process( - $entity instanceof MessageEntity ? $entity->all() : $entity, - $update - )); + $this->parseCommandsIn($message)->each(fn($entity) => $this->process($entity instanceof + MessageEntity ? $entity->all() : $entity, + $update)); } return $update; @@ -171,10 +167,11 @@ protected function handler(Update $update): Update */ private function parseCommandsIn(Collection $message): Collection { - return Collection::wrap($message->get('entities')) - ->filter(static fn (MessageEntity $entity): bool => $entity->type === 'bot_command'); + return Collection::wrap($message->get('entities'))->filter(static fn(MessageEntity $entity): bool => $entity->type === + 'bot_command'); } + /** * Execute a bot command from the update text. * @@ -182,11 +179,9 @@ private function parseCommandsIn(Collection $message): Collection */ private function process(array $entity, Update $update): void { - $command = $this->parseCommand( - $update->getText(), + $command = $this->parseCommand($update->getText(), $entity['offset'], - $entity['length'] - ); + $entity['length']); $this->execute($command, $update, $entity); } @@ -199,10 +194,10 @@ private function process(array $entity, Update $update): void */ protected function execute(string $name, Update $update, array $entity): mixed { - $command = $this->commands[$name] - ?? $this->commandAliases[$name] - ?? $this->commands['help'] - ?? collect($this->commands)->first(fn ($command): bool => $command instanceof $name); + $command = $this->commands[$name] ?? $this->commandAliases[$name] ?? $this->commands['help'] ?? + collect($this->commands)->first(fn($command): bool => $command + instanceof + $name); return $command?->make($this->telegram, $update, $entity) ?? false; } @@ -214,14 +209,10 @@ protected function execute(string $name, Update $update, array $entity): mixed */ private function resolveCommand(CommandInterface|string $command): CommandInterface { - if (! is_a($command, CommandInterface::class, true)) { - throw new TelegramSDKException( - sprintf( - 'Command class "%s" should be an instance of "%s"', - is_object($command) ? $command::class : $command, - CommandInterface::class - ) - ); + if (!is_a($command, CommandInterface::class, true)) { + throw new TelegramSDKException(sprintf('Command class "%s" should be an instance of "%s"', + is_object($command) ? $command::class : $command, + CommandInterface::class)); } $commandInstance = $this->buildDependencyInjectedClass($command); @@ -239,23 +230,15 @@ private function resolveCommand(CommandInterface|string $command): CommandInterf private function checkForConflicts(CommandInterface $command, string $alias): void { if (isset($this->commands[$alias])) { - throw new TelegramSDKException( - sprintf( - '[Error] Alias [%s] conflicts with command name of "%s" try with another name or remove this alias from the list.', - $alias, - $command::class - ) - ); + throw new TelegramSDKException(sprintf('[Error] Alias [%s] conflicts with command name of "%s" try with another name or remove this alias from the list.', + $alias, + $command::class)); } if (isset($this->commandAliases[$alias])) { - throw new TelegramSDKException( - sprintf( - '[Error] Alias [%s] conflicts with another command\'s alias list: "%s", try with another name or remove this alias from the list.', - $alias, - $command::class - ) - ); + throw new TelegramSDKException(sprintf('[Error] Alias [%s] conflicts with another command\'s alias list: "%s", try with another name or remove this alias from the list.', + $alias, + $command::class)); } } } diff --git a/src/Laravel/Facades/Telegram.php b/src/Laravel/Facades/Telegram.php index af23e080..24de9dd0 100644 --- a/src/Laravel/Facades/Telegram.php +++ b/src/Laravel/Facades/Telegram.php @@ -136,11 +136,6 @@ * @method static \Telegram\Bot\Objects\WebhookInfo getWebhookInfo() * @method static \Telegram\Bot\Objects\Update getWebhookUpdate(bool $shouldDispatchEvents = true, ?\Psr\Http\Message\RequestInterface $request = null) * @method static bool removeWebhook() - * @method static \Telegram\Bot\Objects\ForumTopic createForumTopic(array $params) - * @method static bool editForumTopic(array $params) - * @method static bool closeForumTopic(array $params) - * @method static bool reopenForumTopic(array $params) - * @method static bool deleteForumTopic(array $params) * * @see \Telegram\Bot\Commands\CommandBus * diff --git a/src/Methods/Message.php b/src/Methods/Message.php index 60269418..815e4ee0 100644 --- a/src/Methods/Message.php +++ b/src/Methods/Message.php @@ -2,7 +2,6 @@ namespace Telegram\Bot\Methods; -use Illuminate\Support\Arr; use Telegram\Bot\Actions; use Telegram\Bot\Exceptions\TelegramSDKException; use Telegram\Bot\Objects\Message as MessageObject; From 56337f31f9bd0e51adff236b0c82fe52b7097f59 Mon Sep 17 00:00:00 2001 From: Milad Khodaverdi Date: Fri, 4 Oct 2024 11:07:15 +0330 Subject: [PATCH 5/5] Update composer.json --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 8c68ca72..e0b94da0 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "irazasyed/telegram-bot-sdk", + "name": "miladkhodaverdi23/telegram-bot-sdk", "description": "The Unofficial Telegram Bot API PHP SDK", "license": "BSD-3-Clause", "type": "library",