Add SendGift method to send gifts to users

This commit is contained in:
netkas 2024-11-28 15:38:35 -05:00
parent 939c736e1e
commit 8aac6544a6
3 changed files with 71 additions and 0 deletions

View file

@ -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

View file

@ -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),

View file

@ -0,0 +1,67 @@
<?php
namespace TgBotLib\Methods;
use TgBotLib\Abstracts\Method;
use TgBotLib\Bot;
use TgBotLib\Enums\Methods;
use TgBotLib\Enums\Types\ParseMode;
use TgBotLib\Interfaces\ObjectTypeInterface;
class SendGift extends Method
{
/**
* @inheritDoc
*/
public static function execute(Bot $bot, array $parameters = []): true
{
if(isset($parameters['text_parse_mode']) && $parameters['text_parse_mode'] instanceof ParseMode)
{
$parameters['text_parse_mode'] = $parameters['text_parse_mode']->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'
];
}
}