diff --git a/src/TgBotLib/Abstracts/Method.php b/src/TgBotLib/Abstracts/Method.php index f10ca66..7363983 100644 --- a/src/TgBotLib/Abstracts/Method.php +++ b/src/TgBotLib/Abstracts/Method.php @@ -44,17 +44,13 @@ * @param array|null $parameters An array of parameters to be sent in the POST request. * @return CurlHandle The configured cURL handle ready for execution. */ - protected static function buildPost(Bot $bot, string $method, ?array $parameters=null): CurlHandle - { + protected static function buildPost(Bot $bot, string $method, ?array $parameters = null): CurlHandle { $curl = curl_init(sprintf('%s/bot%s/%s', $bot->getEndpoint(), $bot->getToken(), $method)); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); - if($parameters === null) - { + if ($parameters === null) { curl_setopt($curl, CURLOPT_POST, false); - } - else - { + } else { curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($parameters)); diff --git a/src/TgBotLib/Bot.php b/src/TgBotLib/Bot.php index b8ae01f..67eb850 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\MessageId; use TgBotLib\Objects\ReplyKeyboardMarkup; use TgBotLib\Objects\ReplyKeyboardRemove; use TgBotLib\Objects\ReplyParameters; @@ -30,6 +31,7 @@ * @method bool close() Use this method to close the bot instance before moving it from one local server to another. You need to delete the webhook before calling this method to ensure that the bot isn't launched again after server restart. The method will return error 429 in the first 10 minutes after the bot is launched. Returns True on success. Requires no parameters. * @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. */ class Bot { diff --git a/src/TgBotLib/Enums/Methods.php b/src/TgBotLib/Enums/Methods.php index 80f669e..88c227b 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\ForwardMessage; + use TgBotLib\Methods\ForwardMessages; use TgBotLib\Methods\GetMe; use TgBotLib\Methods\Logout; use TgBotLib\Methods\SendMessage; @@ -18,6 +19,7 @@ case CLOSE = 'close'; case SEND_MESSAGE = 'sendMessage'; case FORWARD_MESSAGE = 'forwardMessage'; + case FORWARD_MESSAGES = 'forwardMessages'; /** * Executes a command on the provided bot with the given parameters. @@ -36,6 +38,7 @@ self::CLOSE => Close::execute($bot, $parameters), self::SEND_MESSAGE => SendMessage::execute($bot, $parameters), self::FORWARD_MESSAGE => ForwardMessage::execute($bot, $parameters), + self::FORWARD_MESSAGES => ForwardMessages::execute($bot, $parameters), }; } } diff --git a/src/TgBotLib/Methods/ForwardMessages.php b/src/TgBotLib/Methods/ForwardMessages.php new file mode 100644 index 0000000..ba52dbb --- /dev/null +++ b/src/TgBotLib/Methods/ForwardMessages.php @@ -0,0 +1,53 @@ + MessageId::fromArray($messageId), + self::executeCurl(self::buildPost($bot, Methods::FORWARD_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' + ]; + } +} \ No newline at end of file diff --git a/src/TgBotLib/Objects/MessageId.php b/src/TgBotLib/Objects/MessageId.php new file mode 100644 index 0000000..14364de --- /dev/null +++ b/src/TgBotLib/Objects/MessageId.php @@ -0,0 +1,46 @@ +message_id; + } + + /** + * @inheritDoc + */ + public function toArray(): ?array + { + return [ + 'message_id' => $this->message_id + ]; + } + + /** + * @inheritDoc + */ + public static function fromArray(?array $data): ?MessageId + { + if (empty($data)) + { + return null; + } + + $object = new self(); + $object->message_id = $data['message_id']; + + return $object; + } + } \ No newline at end of file diff --git a/tests/TgBotLib/Methods/ForwardMessagesTest.php b/tests/TgBotLib/Methods/ForwardMessagesTest.php new file mode 100644 index 0000000..895110a --- /dev/null +++ b/tests/TgBotLib/Methods/ForwardMessagesTest.php @@ -0,0 +1,55 @@ +sendMessage( + chat_id: self::TEST_CHAT_ID, + text: 'This is a test message.' + ); + + $message2 = self::$bot->sendMessage( + chat_id: self::TEST_CHAT_ID, + text: 'This is another test message.' + ); + + $result = self::$bot->forwardMessages( + chat_id: self::TEST_CHAT_ID, + from_chat_id: self::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); + } + } +}