Add support for retrieving available gifts

This commit is contained in:
netkas 2024-11-28 15:33:04 -05:00
parent 2cdd327abf
commit 939c736e1e
5 changed files with 203 additions and 0 deletions

View file

@ -36,6 +36,7 @@
use TgBotLib\Objects\File; use TgBotLib\Objects\File;
use TgBotLib\Objects\ForceReply; use TgBotLib\Objects\ForceReply;
use TgBotLib\Objects\ForumTopic; use TgBotLib\Objects\ForumTopic;
use TgBotLib\Objects\Gifts;
use TgBotLib\Objects\InlineKeyboardMarkup; use TgBotLib\Objects\InlineKeyboardMarkup;
use TgBotLib\Objects\InputMedia; use TgBotLib\Objects\InputMedia;
use TgBotLib\Objects\InputPollOption; use TgBotLib\Objects\InputPollOption;
@ -169,6 +170,7 @@
* @method true setStickerSetThumbnail(string $name, int $user_id, string $format, ?string $thumbnail=null) Use this method to set the thumbnail of a regular or mask sticker set. The format of the thumbnail file must be the format of the stickers in the set. Returns True on success. * @method true setStickerSetThumbnail(string $name, int $user_id, string $format, ?string $thumbnail=null) Use this method to set the thumbnail of a regular or mask sticker set. The format of the thumbnail file must be the format of the stickers in the set. Returns True on success.
* @method true setCustomEmojiStickerSetThumbnail(string $name, ?string $custom_emoji_id=null) Use this method to set the thumbnail of a custom emoji sticker set. Returns True on success. * @method true setCustomEmojiStickerSetThumbnail(string $name, ?string $custom_emoji_id=null) Use this method to set the thumbnail of a custom emoji sticker set. Returns True on success.
* @method true deleteStickerSet(string $name) Use this method to delete a sticker set that was created by the bot. Returns True on success. * @method true deleteStickerSet(string $name) Use this method to delete a sticker set that was created by the bot. Returns True on success.
* @method Gifts getAvailableGifts() Returns the list of gifts that can be sent by the bot to users. Requires no parameters. Returns a Gifts object.
* @throws TelegramException if the method execution fails. * @throws TelegramException if the method execution fails.
*/ */
class Bot class Bot

View file

@ -46,6 +46,7 @@
use TgBotLib\Methods\ExportChatInviteLink; use TgBotLib\Methods\ExportChatInviteLink;
use TgBotLib\Methods\ForwardMessage; use TgBotLib\Methods\ForwardMessage;
use TgBotLib\Methods\ForwardMessages; use TgBotLib\Methods\ForwardMessages;
use TgBotLib\Methods\GetAvailableGifts;
use TgBotLib\Methods\GetBusinessConnection; use TgBotLib\Methods\GetBusinessConnection;
use TgBotLib\Methods\GetChat; use TgBotLib\Methods\GetChat;
use TgBotLib\Methods\GetChatAdministrators; use TgBotLib\Methods\GetChatAdministrators;
@ -246,6 +247,7 @@
case SET_STICKER_SET_THUMBNAIL = 'setStickerSetThumbnail'; case SET_STICKER_SET_THUMBNAIL = 'setStickerSetThumbnail';
case SET_CUSTOM_EMOJI_STICKER_SET_THUMBNAIL = 'setCustomEmojiStickerSetThumbnail'; case SET_CUSTOM_EMOJI_STICKER_SET_THUMBNAIL = 'setCustomEmojiStickerSetThumbnail';
case DELETE_STICKER_SET = 'deleteStickerSet'; case DELETE_STICKER_SET = 'deleteStickerSet';
case GET_AVAILABLE_GIFTS = 'getAvailableGifts';
case ANSWER_INLINE_QUERY = 'answerInlineQuery'; case ANSWER_INLINE_QUERY = 'answerInlineQuery';
case ANSWER_WEB_APP_QUERY = 'answerWebAppQuery'; case ANSWER_WEB_APP_QUERY = 'answerWebAppQuery';
case SAVE_PREPARED_INLINE_MESSAGE = 'savePreparedInlineMessage'; case SAVE_PREPARED_INLINE_MESSAGE = 'savePreparedInlineMessage';
@ -384,6 +386,7 @@
self::SET_STICKER_SET_THUMBNAIL => SetStickerSetThumbnail::execute($bot, $parameters), self::SET_STICKER_SET_THUMBNAIL => SetStickerSetThumbnail::execute($bot, $parameters),
self::SET_CUSTOM_EMOJI_STICKER_SET_THUMBNAIL => SetCustomEmojiStickerSetThumbnail::execute($bot, $parameters), self::SET_CUSTOM_EMOJI_STICKER_SET_THUMBNAIL => SetCustomEmojiStickerSetThumbnail::execute($bot, $parameters),
self::DELETE_STICKER_SET => DeleteStickerSet::execute($bot, $parameters), self::DELETE_STICKER_SET => DeleteStickerSet::execute($bot, $parameters),
self::GET_AVAILABLE_GIFTS => GetAvailableGifts::execute($bot, $parameters),
self::ANSWER_INLINE_QUERY => AnswerInlineQuery::execute($bot, $parameters), self::ANSWER_INLINE_QUERY => AnswerInlineQuery::execute($bot, $parameters),
self::ANSWER_WEB_APP_QUERY => AnswerWebAppQuery::execute($bot, $parameters), self::ANSWER_WEB_APP_QUERY => AnswerWebAppQuery::execute($bot, $parameters),
self::SAVE_PREPARED_INLINE_MESSAGE => SavePreparedInlineMessage::execute($bot, $parameters), self::SAVE_PREPARED_INLINE_MESSAGE => SavePreparedInlineMessage::execute($bot, $parameters),

View file

@ -0,0 +1,36 @@
<?php
namespace TgBotLib\Methods;
use TgBotLib\Abstracts\Method;
use TgBotLib\Bot;
use TgBotLib\Enums\Methods;
use TgBotLib\Objects\Gifts;
class GetAvailableGifts extends Method
{
/**
* @inheritDoc
*/
public static function execute(Bot $bot, array $parameters = []): Gifts
{
return Gifts::fromArray(self::executeCurl(self::buildPost($bot, Methods::GET_AVAILABLE_GIFTS->value)));
}
/**
* @inheritDoc
*/
public static function getRequiredParameters(): ?array
{
return null;
}
/**
* @inheritDoc
*/
public static function getOptionalParameters(): ?array
{
return null;
}
}

View file

@ -0,0 +1,106 @@
<?php
namespace TgBotLib\Objects;
use TgBotLib\Interfaces\ObjectTypeInterface;
use TgBotLib\Objects\Stickers\Sticker;
class Gift implements ObjectTypeInterface
{
private string $id;
private Sticker $sticker;
private int $starCount;
private ?int $totalCount;
private ?int $remainingCount;
/**
* This object represents a gift that can be sent by the bot.
*
* @param array|null $data
*/
public function __construct(?array $data=null)
{
if($data == null)
{
return;
}
$this->id = $data['id'];
$this->sticker = Sticker::fromArray($data['sticker']);
$this->starCount = $data['star_count'];
$this->totalCount = $data['total_count'] ?? null;
$this->remainingCount = $data['remaining_count'] ?? null;
}
/**
* Unique identifier of the gift
*
* @return string
*/
public function getId(): string
{
return $this->id;
}
/**
* The sticker that represents the gift
*
* @return Sticker
*/
public function getSticker(): Sticker
{
return $this->sticker;
}
/**
* The number of Telegram Stars that must be paid to send the sticker
*
* @return int
*/
public function getStarCount(): int
{
return $this->starCount;
}
/**
* Optional. The total number of the gifts of this type that can be sent; for limited gifts only
*
* @return int|null
*/
public function getTotalCount(): ?int
{
return $this->totalCount;
}
/**
* Optional. The number of remaining gifts of this type that can be sent; for limited gifts only
*
* @return int|null
*/
public function getRemainingCount(): ?int
{
return $this->remainingCount;
}
/**
* @inheritDoc
*/
public function toArray(): ?array
{
return [
'id' => $this->id,
'sticker' => $this->sticker->toArray(),
'star_count' => $this->starCount,
'total_count' => $this->totalCount,
'remaining_count' => $this->remainingCount
];
}
/**
* @inheritDoc
*/
public static function fromArray(?array $data): ?Gift
{
return new self($data);
}
}

View file

@ -0,0 +1,56 @@
<?php
namespace TgBotLib\Objects;
use TgBotLib\Interfaces\ObjectTypeInterface;
class Gifts implements ObjectTypeInterface
{
/**
* @var Gift[]
*/
private array $gifts;
/**
* This object represent a list of gifts.
*
* @param array|null $data
*/
public function __construct(?array $data=null)
{
if($data == null)
{
return;
}
$this->gifts = array_map(fn(array $items) => Gift::fromArray($items), $data['gifts'] ?? []);
}
/**
* The list of gifts
*
* @return Gift[]
*/
public function getGifts(): array
{
return $this->gifts;
}
/**
* @inheritDoc
*/
public function toArray(): ?array
{
return [
'gifts' => array_map(fn(Gift $item) => $item->toArray(), $this->gifts)
];
}
/**
* @inheritDoc
*/
public static function fromArray(?array $data): ?Gifts
{
return new self($data);
}
}