Add SetChatPhoto method to handle chat photo updates

This commit is contained in:
netkas 2024-11-06 15:42:43 -05:00
parent 9889abf149
commit 17f1599797
2 changed files with 56 additions and 0 deletions

View file

@ -47,6 +47,7 @@
use TgBotLib\Methods\SendVoice;
use TgBotLib\Methods\SetChatAdministratorCustomTitle;
use TgBotLib\Methods\SetChatPermissions;
use TgBotLib\Methods\SetChatPhoto;
use TgBotLib\Methods\SetMessageReaction;
use TgBotLib\Methods\SetWebhook;
use TgBotLib\Methods\UnbanChatMember;
@ -100,6 +101,7 @@
case REVOKE_CHAT_INVITE_LINK = 'revokeChatInviteLink';
case APPROVE_CHAT_JOIN_REQUEST = 'approveChatJoinRequest';
case DECLINE_CHAT_JOIN_REQUEST = 'declineChatJoinRequest';
case SET_CHAT_PHOTO = 'setChatPhoto';
/**
* Executes a command on the provided bot with the given parameters.
@ -159,6 +161,7 @@
self::REVOKE_CHAT_INVITE_LINK => RevokeChatInviteLink::execute($bot, $parameters),
self::APPROVE_CHAT_JOIN_REQUEST => ApproveChatJoinRequest::execute($bot, $parameters),
self::DECLINE_CHAT_JOIN_REQUEST => DeclineChatJoinRequest::execute($bot, $parameters),
self::SET_CHAT_PHOTO => SetChatPhoto::execute($bot, $parameters),
};
}
}

View file

@ -0,0 +1,53 @@
<?php
namespace TgBotLib\Methods;
use TgBotLib\Abstracts\Method;
use TgBotLib\Bot;
use TgBotLib\Enums\Methods;
use TgBotLib\Objects\Message;
class SetChatPhoto extends Method
{
/**
* @inheritDoc
*/
public static function execute(Bot $bot, array $parameters = []): true
{
// 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::SET_CHAT_PHOTO->value, 'photo', $photo, array_diff_key($parameters, ['photo' => null]));
return (bool)(self::executeCurl($curl));
}
}
// If photo is a file_id or URL, use regular POST method
return (bool)(self::executeCurl(self::buildPost($bot, Methods::SET_CHAT_PHOTO->value, $parameters)));
}
/**
* @inheritDoc
*/
public static function getRequiredParameters(): ?array
{
return [
'chat_id',
'photo'
];
}
/**
* @inheritDoc
*/
public static function getOptionalParameters(): ?array
{
return null;
}
}