diff --git a/src/TgBotLib/Abstracts/Method.php b/src/TgBotLib/Abstracts/Method.php index 7363983..d8a78c1 100644 --- a/src/TgBotLib/Abstracts/Method.php +++ b/src/TgBotLib/Abstracts/Method.php @@ -48,9 +48,12 @@ $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)); @@ -70,31 +73,16 @@ */ protected static function buildUpload(Bot $bot, string $method, string $file_param, string $file_path, array $parameters): CurlHandle { - $curl = curl_init(sprintf('%s/%s?%s', $bot->getEndpoint(), $method, http_build_query($parameters))); - curl_setopt($curl, CURLOPT_POST, true); - curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); - curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: multipart/form-data']); - curl_setopt($curl, CURLOPT_POSTFIELDS, [ - $file_param => new CURLFile($file_path) - ]); - - return $curl; - } - - protected static function buildMultiUpload(Bot $bot, string $method, array $files, array $parameters): CurlHandle - { - $curl = curl_init(sprintf('%s/%s?%s', $bot->getEndpoint(), $method, http_build_query($parameters))); + $curl = curl_init(sprintf('%s/bot%s/%s', $bot->getEndpoint(), $bot->getToken(), $method)); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: multipart/form-data']); - $post_fields = []; - foreach($files as $file_param => $file_path) - { - $post_fields[$file_param] = new CURLFile($file_path); - } + // Merge file with other parameters + $parameters[$file_param] = new CURLFile($file_path); + + curl_setopt($curl, CURLOPT_POSTFIELDS, $parameters); - curl_setopt($curl, CURLOPT_POSTFIELDS, $post_fields); return $curl; } diff --git a/src/TgBotLib/Bot.php b/src/TgBotLib/Bot.php index d81cff0..fd22d13 100644 --- a/src/TgBotLib/Bot.php +++ b/src/TgBotLib/Bot.php @@ -35,6 +35,7 @@ * @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. + * @method Message sendPhoto(string|int $chat_id, string $photo, ?string $business_connection_id=null, ?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, ?string $message_effect_id=null, ?ReplyParameters $reply_parameters=null, InlineKeyboardMarkup|ReplyKeyboardMarkup|ReplyKeyboardRemove|ForceReply|null $reply_markup=null) Use this method to send photos. On success, the sent Message is returned. */ class Bot { diff --git a/src/TgBotLib/Enums/Methods.php b/src/TgBotLib/Enums/Methods.php index 8a9a884..1122cad 100644 --- a/src/TgBotLib/Enums/Methods.php +++ b/src/TgBotLib/Enums/Methods.php @@ -13,6 +13,7 @@ use TgBotLib\Methods\GetMe; use TgBotLib\Methods\Logout; use TgBotLib\Methods\SendMessage; + use TgBotLib\Methods\SendPhoto; enum Methods : string { @@ -24,6 +25,7 @@ case FORWARD_MESSAGES = 'forwardMessages'; case COPY_MESSAGE = 'copyMessage'; case COPY_MESSAGES = 'copyMessages'; + case SEND_PHOTO = 'sendPhoto'; /** * Executes a command on the provided bot with the given parameters. @@ -44,7 +46,8 @@ 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) + self::COPY_MESSAGES => CopyMessages::execute($bot, $parameters), + self::SEND_PHOTO => SendPhoto::execute($bot, $parameters), }; } } diff --git a/src/TgBotLib/Methods/SendPhoto.php b/src/TgBotLib/Methods/SendPhoto.php new file mode 100644 index 0000000..dbac51d --- /dev/null +++ b/src/TgBotLib/Methods/SendPhoto.php @@ -0,0 +1,109 @@ +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(); + } + + // Handle different photo input types + if (isset($parameters['photo'])) + { + $photo = $parameters['photo']; + + // If photo is a file path and exists locally + if (is_string($photo) && file_exists($photo) && is_file($photo)) + { + $curl = self::buildUpload($bot, Methods::SEND_PHOTO->value, 'photo', $photo, array_diff_key($parameters, ['photo' => null])); + return Message::fromArray(self::executeCurl($curl)); + } + } + + // If photo is a file_id or URL, use regular POST method + return Message::fromArray(self::executeCurl(self::buildPost($bot, Methods::SEND_PHOTO->value, $parameters))); + } + + /** + * @inheritDoc + */ + public static function getRequiredParameters(): ?array + { + return [ + 'chat_id', + 'photo' + ]; + } + + /** + * @inheritDoc + */ + public static function getOptionalParameters(): ?array + { + return [ + 'business_connection_id', + 'message_thread_id', + 'caption', + 'parse_mode', + 'caption_entities', + 'show_caption_above_media', + 'has_spoiler', + 'disable_notification', + 'protect_content', + 'message_effect_id', + 'reply_parameters', + 'reply_markup' + ]; + } + } \ No newline at end of file diff --git a/tests/TgBotLib/Methods/SendPhotoTest.php b/tests/TgBotLib/Methods/SendPhotoTest.php new file mode 100644 index 0000000..52e1149 --- /dev/null +++ b/tests/TgBotLib/Methods/SendPhotoTest.php @@ -0,0 +1,58 @@ +sendPhoto( + chat_id: TEST_CHAT_ID, + photo: self::TEST_IMAGE_PATH, + caption: 'Test Unit: testSendPhoto', + ); + + $this->assertInstanceOf(Message::class, $result); + $this->assertEquals('Test Unit: testSendPhoto', $result->getCaption()); + } + + /** + * Tests the `sendPhoto` function of the bot instance with a URL. + * + * @return void + */ + public function testSendPhotoWithUrl(): void + { + $result = self::$bot->sendPhoto( + chat_id: TEST_CHAT_ID, + photo: self::TEST_IMAGE_URL, + caption: 'Test Unit: testSendPhotoWithUrl', + ); + + $this->assertInstanceOf(Message::class, $result); + $this->assertEquals('Test Unit: testSendPhotoWithUrl', $result->getCaption()); + } +} diff --git a/tests/TgBotLib/Methods/sample/oj_simpson.png b/tests/TgBotLib/Methods/sample/oj_simpson.png new file mode 100644 index 0000000..8fb935f Binary files /dev/null and b/tests/TgBotLib/Methods/sample/oj_simpson.png differ