From 8aac6544a6e9fadeab2caa4de0d22a2a67ee5090 Mon Sep 17 00:00:00 2001 From: netkas Date: Thu, 28 Nov 2024 15:38:35 -0500 Subject: [PATCH] Add SendGift method to send gifts to users --- src/TgBotLib/Bot.php | 1 + src/TgBotLib/Enums/Methods.php | 3 ++ src/TgBotLib/Methods/SendGift.php | 67 +++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 src/TgBotLib/Methods/SendGift.php diff --git a/src/TgBotLib/Bot.php b/src/TgBotLib/Bot.php index 72dcdf7..36e60aa 100644 --- a/src/TgBotLib/Bot.php +++ b/src/TgBotLib/Bot.php @@ -171,6 +171,7 @@ * @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. + * @method true sendGift(int $user_id, string $gift_id, ?string $text=null, ?ParseMode $text_parse_mode=null, MessageEntity[]|null $text_entities=null) Sends a gift to the given user. The gift can't be converted to Telegram Stars by the user. Returns True on success. * @throws TelegramException if the method execution fails. */ class Bot diff --git a/src/TgBotLib/Enums/Methods.php b/src/TgBotLib/Enums/Methods.php index 4153afe..742df2a 100644 --- a/src/TgBotLib/Enums/Methods.php +++ b/src/TgBotLib/Enums/Methods.php @@ -87,6 +87,7 @@ use TgBotLib\Methods\SendDice; use TgBotLib\Methods\SendDocument; use TgBotLib\Methods\SendGame; + use TgBotLib\Methods\SendGift; use TgBotLib\Methods\SendInvoice; use TgBotLib\Methods\SendLocation; use TgBotLib\Methods\SendMediaGroup; @@ -248,6 +249,7 @@ case SET_CUSTOM_EMOJI_STICKER_SET_THUMBNAIL = 'setCustomEmojiStickerSetThumbnail'; case DELETE_STICKER_SET = 'deleteStickerSet'; case GET_AVAILABLE_GIFTS = 'getAvailableGifts'; + case SEND_GIFT = 'sendGift'; case ANSWER_INLINE_QUERY = 'answerInlineQuery'; case ANSWER_WEB_APP_QUERY = 'answerWebAppQuery'; case SAVE_PREPARED_INLINE_MESSAGE = 'savePreparedInlineMessage'; @@ -387,6 +389,7 @@ 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::SEND_GIFT => SendGift::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/SendGift.php b/src/TgBotLib/Methods/SendGift.php new file mode 100644 index 0000000..48fb035 --- /dev/null +++ b/src/TgBotLib/Methods/SendGift.php @@ -0,0 +1,67 @@ +value; + } + + if(isset($parameters['text_entities'])) + { + $textEntities = []; + foreach($parameters['text_entities'] as $textEntity) + { + if($textEntity instanceof ObjectTypeInterface) + { + $textEntities[] = $textEntity->toArray(); + } + + if(is_array($textEntity)) + { + $textEntities[] = $textEntity; + } + } + + $parameters['text_entities'] = json_encode($textEntities); + } + + return (bool)self::executeCurl(self::buildPost($bot, Methods::SEND_GIFT->value, $parameters)); + } + + /** + * @inheritDoc + */ + public static function getRequiredParameters(): ?array + { + return [ + 'user_id', + 'gift_id' + ]; + } + + /** + * @inheritDoc + */ + public static function getOptionalParameters(): ?array + { + return [ + 'text', + 'text_parse_mode', + 'text_entities' + ]; + } + } \ No newline at end of file