From 7cfd6c0c5fec824ce3c8bf50260433308fb57eca Mon Sep 17 00:00:00 2001 From: netkas Date: Mon, 4 Nov 2024 01:16:55 -0500 Subject: [PATCH] Add methods to retrieve text and commands from messages --- src/TgBotLib/Objects/Message.php | 33 ++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/TgBotLib/Objects/Message.php b/src/TgBotLib/Objects/Message.php index fb10119..2f5714f 100644 --- a/src/TgBotLib/Objects/Message.php +++ b/src/TgBotLib/Objects/Message.php @@ -991,6 +991,39 @@ return $this->reply_markup; } + /** + * Retrieves any available text, prioritizing 'text' over 'caption'. + * + * @return string|null + */ + public function getAnyText(): ?string + { + return $this->text ?? $this->caption; + } + + /** + * Retrieves the command from the text if it exists. + * + * @return string|null + */ + public function getCommand(): ?string + { + if ($this->text === null) + { + return null; + } + + $text = trim($this->text); + + if (!str_starts_with($text, '/')) + { + return null; + } + + $parts = explode(' ', $text); + return ltrim($parts[0], '/'); + } + /** * @inheritDoc */