From 939c736e1e5c831720c76bdbd01b4b54f36f960a Mon Sep 17 00:00:00 2001 From: netkas Date: Thu, 28 Nov 2024 15:33:04 -0500 Subject: [PATCH] Add support for retrieving available gifts --- src/TgBotLib/Bot.php | 2 + src/TgBotLib/Enums/Methods.php | 3 + src/TgBotLib/Methods/GetAvailableGifts.php | 36 +++++++ src/TgBotLib/Objects/Gift.php | 106 +++++++++++++++++++++ src/TgBotLib/Objects/Gifts.php | 56 +++++++++++ 5 files changed, 203 insertions(+) create mode 100644 src/TgBotLib/Methods/GetAvailableGifts.php create mode 100644 src/TgBotLib/Objects/Gift.php create mode 100644 src/TgBotLib/Objects/Gifts.php diff --git a/src/TgBotLib/Bot.php b/src/TgBotLib/Bot.php index 09713c5..72dcdf7 100644 --- a/src/TgBotLib/Bot.php +++ b/src/TgBotLib/Bot.php @@ -36,6 +36,7 @@ use TgBotLib\Objects\File; use TgBotLib\Objects\ForceReply; use TgBotLib\Objects\ForumTopic; + use TgBotLib\Objects\Gifts; use TgBotLib\Objects\InlineKeyboardMarkup; use TgBotLib\Objects\InputMedia; 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 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 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. */ class Bot diff --git a/src/TgBotLib/Enums/Methods.php b/src/TgBotLib/Enums/Methods.php index 5f984f1..4153afe 100644 --- a/src/TgBotLib/Enums/Methods.php +++ b/src/TgBotLib/Enums/Methods.php @@ -46,6 +46,7 @@ use TgBotLib\Methods\ExportChatInviteLink; use TgBotLib\Methods\ForwardMessage; use TgBotLib\Methods\ForwardMessages; + use TgBotLib\Methods\GetAvailableGifts; use TgBotLib\Methods\GetBusinessConnection; use TgBotLib\Methods\GetChat; use TgBotLib\Methods\GetChatAdministrators; @@ -246,6 +247,7 @@ case SET_STICKER_SET_THUMBNAIL = 'setStickerSetThumbnail'; case SET_CUSTOM_EMOJI_STICKER_SET_THUMBNAIL = 'setCustomEmojiStickerSetThumbnail'; case DELETE_STICKER_SET = 'deleteStickerSet'; + case GET_AVAILABLE_GIFTS = 'getAvailableGifts'; case ANSWER_INLINE_QUERY = 'answerInlineQuery'; case ANSWER_WEB_APP_QUERY = 'answerWebAppQuery'; case SAVE_PREPARED_INLINE_MESSAGE = 'savePreparedInlineMessage'; @@ -384,6 +386,7 @@ self::SET_STICKER_SET_THUMBNAIL => SetStickerSetThumbnail::execute($bot, $parameters), self::SET_CUSTOM_EMOJI_STICKER_SET_THUMBNAIL => SetCustomEmojiStickerSetThumbnail::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_WEB_APP_QUERY => AnswerWebAppQuery::execute($bot, $parameters), self::SAVE_PREPARED_INLINE_MESSAGE => SavePreparedInlineMessage::execute($bot, $parameters), diff --git a/src/TgBotLib/Methods/GetAvailableGifts.php b/src/TgBotLib/Methods/GetAvailableGifts.php new file mode 100644 index 0000000..14f8a5f --- /dev/null +++ b/src/TgBotLib/Methods/GetAvailableGifts.php @@ -0,0 +1,36 @@ +value))); + } + + /** + * @inheritDoc + */ + public static function getRequiredParameters(): ?array + { + return null; + } + + /** + * @inheritDoc + */ + public static function getOptionalParameters(): ?array + { + return null; + } + } \ No newline at end of file diff --git a/src/TgBotLib/Objects/Gift.php b/src/TgBotLib/Objects/Gift.php new file mode 100644 index 0000000..15bd31d --- /dev/null +++ b/src/TgBotLib/Objects/Gift.php @@ -0,0 +1,106 @@ +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); + } + } \ No newline at end of file diff --git a/src/TgBotLib/Objects/Gifts.php b/src/TgBotLib/Objects/Gifts.php new file mode 100644 index 0000000..84f6b48 --- /dev/null +++ b/src/TgBotLib/Objects/Gifts.php @@ -0,0 +1,56 @@ +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); + } + } \ No newline at end of file