From 294b25ad26bf97a1bf59ccf13feddbba630d60ca Mon Sep 17 00:00:00 2001 From: netkas Date: Tue, 8 Oct 2024 12:32:47 -0400 Subject: [PATCH] Add copyMessages method for bulk message copying --- src/TgBotLib/Bot.php | 1 + src/TgBotLib/Enums/Methods.php | 3 + src/TgBotLib/Methods/CopyMessages.php | 61 +++++++++++++++++++++ tests/TgBotLib/Methods/CopyMessagesTest.php | 55 +++++++++++++++++++ 4 files changed, 120 insertions(+) create mode 100644 src/TgBotLib/Methods/CopyMessages.php create mode 100644 tests/TgBotLib/Methods/CopyMessagesTest.php diff --git a/src/TgBotLib/Bot.php b/src/TgBotLib/Bot.php index 8662cd4..d81cff0 100644 --- a/src/TgBotLib/Bot.php +++ b/src/TgBotLib/Bot.php @@ -34,6 +34,7 @@ * @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. + * @method MessageId[] copyMessages(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, ?bool $remove_caption=null) Use this method to copy messages of any kind. If some of the specified messages can't be found or copied, they are skipped. 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 forwardMessages, but the copied messages don't have a link to the original message. Album grouping is kept for copied messages. On success, an array of MessageId of the sent messages is returned. */ class Bot { diff --git a/src/TgBotLib/Enums/Methods.php b/src/TgBotLib/Enums/Methods.php index 4919db0..8a9a884 100644 --- a/src/TgBotLib/Enums/Methods.php +++ b/src/TgBotLib/Enums/Methods.php @@ -7,6 +7,7 @@ use TgBotLib\Interfaces\ObjectTypeInterface; use TgBotLib\Methods\Close; use TgBotLib\Methods\CopyMessage; + use TgBotLib\Methods\CopyMessages; use TgBotLib\Methods\ForwardMessage; use TgBotLib\Methods\ForwardMessages; use TgBotLib\Methods\GetMe; @@ -22,6 +23,7 @@ case FORWARD_MESSAGE = 'forwardMessage'; case FORWARD_MESSAGES = 'forwardMessages'; case COPY_MESSAGE = 'copyMessage'; + case COPY_MESSAGES = 'copyMessages'; /** * Executes a command on the provided bot with the given parameters. @@ -42,6 +44,7 @@ self::FORWARD_MESSAGE => ForwardMessage::execute($bot, $parameters), self::FORWARD_MESSAGES => ForwardMessages::execute($bot, $parameters), self::COPY_MESSAGE => CopyMessage::execute($bot, $parameters), + self::COPY_MESSAGES => CopyMessages::execute($bot, $parameters) }; } } diff --git a/src/TgBotLib/Methods/CopyMessages.php b/src/TgBotLib/Methods/CopyMessages.php new file mode 100644 index 0000000..1c9bca2 --- /dev/null +++ b/src/TgBotLib/Methods/CopyMessages.php @@ -0,0 +1,61 @@ + MessageId::fromArray($message), + self::executeCurl(self::buildPost($bot, Methods::COPY_MESSAGES->value, $parameters)) + ); + } + + /** + * @inheritDoc + */ + public static function getRequiredParameters(): ?array + { + return [ + 'chat_id', + 'from_chat_id', + 'message_ids' + ]; + } + + /** + * @inheritDoc + */ + public static function getOptionalParameters(): ?array + { + return [ + 'message_thread_id', + 'disable_notification', + 'protect_content', + 'remove_caption' + ]; + } + } \ No newline at end of file diff --git a/tests/TgBotLib/Methods/CopyMessagesTest.php b/tests/TgBotLib/Methods/CopyMessagesTest.php new file mode 100644 index 0000000..12c792e --- /dev/null +++ b/tests/TgBotLib/Methods/CopyMessagesTest.php @@ -0,0 +1,55 @@ +sendMessage( + chat_id: TEST_CHAT_ID, + text: "Test Message 1" + ); + + $message2 = self::$bot->sendMessage( + chat_id: TEST_CHAT_ID, + text: "Test Message 2" + ); + + $result = self::$bot->copyMessages( + chat_id: TEST_CHAT_ID, + from_chat_id: TEST_CHAT_ID, + message_ids: [$message1->getMessageId(), $message2->getMessageId()] + ); + + $this->assertIsArray($result); + $this->assertCount(2, $result); + foreach ($result as $messageId) + { + $this->assertInstanceOf(MessageId::class, $messageId); + } + + } +}