From 6c6d7abb56c91b947744df07cd986f16f95d3732 Mon Sep 17 00:00:00 2001 From: netkas Date: Tue, 5 Nov 2024 11:54:23 -0500 Subject: [PATCH] Add command stripping option to getText and getCaption --- src/TgBotLib/Objects/Message.php | 36 ++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/src/TgBotLib/Objects/Message.php b/src/TgBotLib/Objects/Message.php index cc5b2c3..82148ea 100644 --- a/src/TgBotLib/Objects/Message.php +++ b/src/TgBotLib/Objects/Message.php @@ -338,10 +338,26 @@ /** * Optional. For text messages, the actual UTF-8 text of the message * + * @param bool $includeCommand * @return string|null */ - public function getText(): ?string + public function getText(bool $includeCommand=true): ?string { + if($includeCommand) + { + return $this->text; + } + + if($this->text === null) + { + return null; + } + + if(preg_match('/^\/([a-zA-Z0-9_]+)(?:@[a-zA-Z0-9_]+)?/', $this->text, $matches)) + { + return str_replace($matches[0], '', $this->text); + } + return $this->text; } @@ -481,10 +497,26 @@ /** * Optional. Caption for the animation, audio, document, paid media, photo, video or voice * + * @param bool $includeCommand * @return string|null */ - public function getCaption(): ?string + public function getCaption(bool $includeCommand=false): ?string { + if($includeCommand) + { + return $this->caption; + } + + if($this->caption === null) + { + return null; + } + + if(preg_match('/^\/([a-zA-Z0-9_]+)(?:@[a-zA-Z0-9_]+)?/', $this->caption, $matches)) + { + return str_replace($matches[0], '', $this->caption); + } + return $this->caption; }