diff --git a/src/TgBotLib/Bot.php b/src/TgBotLib/Bot.php index 67eb850..8662cd4 100644 --- a/src/TgBotLib/Bot.php +++ b/src/TgBotLib/Bot.php @@ -19,6 +19,7 @@ use TgBotLib\Objects\InlineKeyboardMarkup; use TgBotLib\Objects\LinkPreviewOptions; use TgBotLib\Objects\Message; + use TgBotLib\Objects\MessageEntity; use TgBotLib\Objects\MessageId; use TgBotLib\Objects\ReplyKeyboardMarkup; use TgBotLib\Objects\ReplyKeyboardRemove; @@ -32,6 +33,7 @@ * @method Message sendMessage(string|int $chat_id, string $text, ?string $business_connection_id=null, ?int $message_thread_id=null, string|ParseMode|null $parse_mode=null, ?array $entities=null, ?LinkPreviewOptions $link_preview_options=null, ?bool $disable_notification=null, ?bool $protect_content=null, ?string $message_effect_id=null, ?ReplyParameters $reply_parameters=null, InlineKeyboardMarkup|ReplyKeyboardMarkup|ReplyKeyboardRemove|ForceReply|null $reply_markup=null) Use this method to send text messages. On success, the sent Message is returned. * @method Message forwardMessage(string|int $chat_id, string|int $from_chat_id, int $message_id, ?int $message_thread_id=null, ?bool $disable_notification=null, ?bool $protect_content=null) Use this method to forward messages of any kind. Service messages and messages with protected content can't be forwarded. On success, the sent Message is returned. * @method MessageId[] forwardMessages(string|int $chat_id, string|int $from_chat_id, int[] $message_ids, ?int $message_thread_id=null, ?bool $disable_notification=null, ?bool $protect_content=null) Use this method to forward multiple messages of any kind. If some of the specified messages can't be found or forwarded, they are skipped. Service messages and messages with protected content can't be forwarded. Album grouping is kept for forwarded messages. On success, an array of MessageId of the sent messages is returned. + * @method MessageId copyMessage(string|int $chat_id, string|int $from_chat_id, int $message_id, ?int $message_thread_id=null, ?string $caption=null, ?string|ParseMode $parse_mode=null, ?MessageEntity[] $caption_entities=null, ?bool $show_caption_above_media=null, ?bool $disable_notification=null, ?bool $protect_content=null, ?ReplyParameters $reply_parameters=null, InlineKeyboardMarkup|ReplyKeyboardMarkup|ReplyKeyboardRemove|ForceReply|null $reply_markup=null) Use this method to copy messages of any kind. Service messages, paid media messages, giveaway messages, giveaway winners messages, and invoice messages can't be copied. A quiz poll can be copied only if the value of the field correct_option_id is known to the bot. The method is analogous to the method forwardMessage, but the copied message doesn't have a link to the original message. Returns the MessageId of the sent message on success. */ class Bot { diff --git a/src/TgBotLib/Enums/Methods.php b/src/TgBotLib/Enums/Methods.php index 88c227b..4919db0 100644 --- a/src/TgBotLib/Enums/Methods.php +++ b/src/TgBotLib/Enums/Methods.php @@ -6,6 +6,7 @@ use TgBotLib\Exceptions\TelegramException; use TgBotLib\Interfaces\ObjectTypeInterface; use TgBotLib\Methods\Close; + use TgBotLib\Methods\CopyMessage; use TgBotLib\Methods\ForwardMessage; use TgBotLib\Methods\ForwardMessages; use TgBotLib\Methods\GetMe; @@ -20,6 +21,7 @@ case SEND_MESSAGE = 'sendMessage'; case FORWARD_MESSAGE = 'forwardMessage'; case FORWARD_MESSAGES = 'forwardMessages'; + case COPY_MESSAGE = 'copyMessage'; /** * Executes a command on the provided bot with the given parameters. @@ -39,6 +41,7 @@ self::SEND_MESSAGE => SendMessage::execute($bot, $parameters), self::FORWARD_MESSAGE => ForwardMessage::execute($bot, $parameters), self::FORWARD_MESSAGES => ForwardMessages::execute($bot, $parameters), + self::COPY_MESSAGE => CopyMessage::execute($bot, $parameters), }; } } diff --git a/src/TgBotLib/Methods/CopyMessage.php b/src/TgBotLib/Methods/CopyMessage.php new file mode 100644 index 0000000..dfdaf88 --- /dev/null +++ b/src/TgBotLib/Methods/CopyMessage.php @@ -0,0 +1,99 @@ +value; + } + + if(isset($parameters['caption_entities']) && is_array($parameters['caption_entities'])) + { + $entities = []; + + foreach($parameters['caption_entities'] as $entity) + { + if($entity instanceof MessageEntity) + { + $entities[] = $entity->toArray(); + } + else + { + $entities[] = $entity; + } + } + + $parameters['caption_entities'] = $entities; + } + + if(isset($parameters['reply_parameters']) && $parameters['reply_parameters'] instanceof ReplyParameters) + { + $parameters['reply_parameters'] = $parameters['reply_parameters']->toArray(); + } + + if(isset($parameters['reply_markup']) && $parameters['reply_markup'] instanceof ObjectTypeInterface) + { + $parameters['reply_markup'] = $parameters['reply_markup']->toArray(); + } + + + return MessageId::fromArray(self::executeCurl(self::buildPost($bot, Methods::COPY_MESSAGE->value, $parameters))); + } + + /** + * @inheritDoc + */ + public static function getRequiredParameters(): ?array + { + return [ + 'chat_id', + 'from_chat_id', + 'message_id' + ]; + } + + /** + * @inheritDoc + */ + public static function getOptionalParameters(): ?array + { + return [ + 'message_thread_id', + 'caption', + 'parse_mode', + 'caption_entities', + 'show_caption_above_media', + 'disable_notification', + 'protect_content', + 'reply_parameters', + 'reply_markup' + ]; + } + } \ No newline at end of file diff --git a/tests/TgBotLib/Methods/CopyMessageTest.php b/tests/TgBotLib/Methods/CopyMessageTest.php new file mode 100644 index 0000000..f9f0471 --- /dev/null +++ b/tests/TgBotLib/Methods/CopyMessageTest.php @@ -0,0 +1,48 @@ +sendMessage( + chat_id: TEST_CHAT_ID, + text: 'Test Unit: testCopyMessage' + ); + + $this->assertInstanceOf(Message::class, $result); + $this->assertEquals(TEST_CHAT_ID, $result->getChat()->getId()); + $this->assertEquals('Test Unit: testCopyMessage', $result->getText()); + + $result = self::$bot->copyMessage( + chat_id: TEST_CHAT_ID, + from_chat_id: TEST_CHAT_ID, + message_id: $result->getMessageId() + ); + + $this->assertInstanceOf(MessageId::class, $result); + } +}