Add SendPhoto method for sending photos in TgBotLib
This commit is contained in:
parent
294b25ad26
commit
a9a6d5a2b3
6 changed files with 182 additions and 23 deletions
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
{
|
||||
|
|
|
@ -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),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
109
src/TgBotLib/Methods/SendPhoto.php
Normal file
109
src/TgBotLib/Methods/SendPhoto.php
Normal file
|
@ -0,0 +1,109 @@
|
|||
<?php
|
||||
|
||||
namespace TgBotLib\Methods;
|
||||
|
||||
use TgBotLib\Abstracts\Method;
|
||||
use TgBotLib\Bot;
|
||||
use TgBotLib\Enums\Methods;
|
||||
use TgBotLib\Enums\Types\ParseMode;
|
||||
use TgBotLib\Exceptions\TelegramException;
|
||||
use TgBotLib\Interfaces\ObjectTypeInterface;
|
||||
use TgBotLib\Objects\Message;
|
||||
use TgBotLib\Objects\MessageEntity;
|
||||
use TgBotLib\Objects\ReplyParameters;
|
||||
|
||||
class SendPhoto extends Method
|
||||
{
|
||||
/**
|
||||
* Use this method to send photos. On success, the sent Message is returned.
|
||||
*
|
||||
* @param Bot $bot
|
||||
* @param array $parameters
|
||||
* @return Message
|
||||
* @throws TelegramException
|
||||
*/
|
||||
public static function execute(Bot $bot, array $parameters=[]): Message
|
||||
{
|
||||
if(isset($parameters['parse_mode']) && $parameters['parse_mode'] instanceof ParseMode)
|
||||
{
|
||||
$parameters['parse_mode'] = $parameters['parse_mode']->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'
|
||||
];
|
||||
}
|
||||
}
|
58
tests/TgBotLib/Methods/SendPhotoTest.php
Normal file
58
tests/TgBotLib/Methods/SendPhotoTest.php
Normal file
|
@ -0,0 +1,58 @@
|
|||
<?php
|
||||
|
||||
namespace TgBotLib\Methods;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use TgBotLib\Bot;
|
||||
use TgBotLib\Enums\Types\ParseMode;
|
||||
use TgBotLib\Objects\LinkPreviewOptions;
|
||||
use TgBotLib\Objects\Message;
|
||||
|
||||
class SendPhotoTest extends TestCase
|
||||
{
|
||||
const string TEST_IMAGE_PATH = __DIR__ . DIRECTORY_SEPARATOR . 'sample' . DIRECTORY_SEPARATOR . 'oj_simpson.png';
|
||||
const string TEST_IMAGE_URL = 'https://avatars.githubusercontent.com/u/22669599';
|
||||
private static Bot $bot;
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public static function setUpBeforeClass(): void
|
||||
{
|
||||
self::$bot = new Bot(BOT_TOKEN);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the `sendMessage` function of the bot instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testSendPhoto(): void
|
||||
{
|
||||
$result = self::$bot->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());
|
||||
}
|
||||
}
|
BIN
tests/TgBotLib/Methods/sample/oj_simpson.png
Normal file
BIN
tests/TgBotLib/Methods/sample/oj_simpson.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 289 KiB |
Loading…
Add table
Reference in a new issue