From 7970313d888bde46fcf5015c75effaf4027c78eb Mon Sep 17 00:00:00 2001 From: Netkas Date: Sun, 23 Apr 2023 16:52:40 -0400 Subject: [PATCH] Added methods `\TgBotLib\Objects\Telegram\InlineKeyboardButton::setUrl`, `\TgBotLib\Objects\Telegram\InlineKeyboardButton::setText`, `\TgBotLib\Objects\Telegram\InlineKeyboardButton::setCallbackData`,`\TgBotLib\Objects\Telegram\InlineKeyboardButton::setWebApp`, `\TgBotLib\Objects\Telegram\InlineKeyboardButton::setLoginUrl`, `\TgBotLib\Objects\Telegram\InlineKeyboardButton::setSwitchInlineQuery`, `\TgBotLib\Objects\Telegram\InlineKeyboardButton::setSwitchInlineQueryCurrentChat` `\TgBotLib\Objects\Telegram\InlineKeyboardButton::setCallbackGame`, `\TgBotLib\Objects\Telegram\InlineKeyboardButton::setPay` to `\TgBotLib\Objects\Telegram\InlineKeyboardButton` --- CHANGELOG.md | 6 +- .../Objects/Telegram/InlineKeyboardButton.php | 146 ++++++++++++++++++ 2 files changed, 151 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 08dcb18..ac294d3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,7 +19,11 @@ This update accompanies the release of the [Telegram Bot API 6.7](https://core.t ### Changed * Refactored InputMessageContent types to its own namespace so InputMessageContent can always return the correct InputMessageContent object type when calling `fromArray()` - + * Added methods `\TgBotLib\Objects\Telegram\InlineKeyboardButton::setUrl`, `\TgBotLib\Objects\Telegram\InlineKeyboardButton::setText`, `\TgBotLib\Objects\Telegram\InlineKeyboardButton::setCallbackData`, + `\TgBotLib\Objects\Telegram\InlineKeyboardButton::setWebApp`, `\TgBotLib\Objects\Telegram\InlineKeyboardButton::setLoginUrl`, + `\TgBotLib\Objects\Telegram\InlineKeyboardButton::setSwitchInlineQuery`, `\TgBotLib\Objects\Telegram\InlineKeyboardButton::setSwitchInlineQueryCurrentChat`, + `\TgBotLib\Objects\Telegram\InlineKeyboardButton::setCallbackGame`, `\TgBotLib\Objects\Telegram\InlineKeyboardButton::setPay` + to `\TgBotLib\Objects\Telegram\InlineKeyboardButton` ## [6.6.0] - 2023-04-10 diff --git a/src/TgBotLib/Objects/Telegram/InlineKeyboardButton.php b/src/TgBotLib/Objects/Telegram/InlineKeyboardButton.php index c6144eb..b2b4860 100644 --- a/src/TgBotLib/Objects/Telegram/InlineKeyboardButton.php +++ b/src/TgBotLib/Objects/Telegram/InlineKeyboardButton.php @@ -4,6 +4,8 @@ namespace TgBotLib\Objects\Telegram; + use InvalidArgumentException; + use TgBotLib\Classes\Validate; use TgBotLib\Interfaces\ObjectTypeInterface; class InlineKeyboardButton implements ObjectTypeInterface @@ -63,6 +65,18 @@ return $this->text; } + /** + * Label text on the button + * + * @param string $text + * @return $this + */ + public function setText(string $text): self + { + $this->text = $text; + return $this; + } + /** * Optional. HTTP or tg:// URL to be opened when the button is pressed. Links tg://user?id= can be * used to mention a user by their ID without using a username, if this is allowed by their privacy settings. @@ -74,6 +88,22 @@ return $this->url; } + /** + * Optional. HTTP or tg:// URL to be opened when the button is pressed. Links tg://user?id= can be used + * to mention a user by their ID without using a username, if this is allowed by their privacy settings. + * + * @param string|null $url + * @return $this + */ + public function setUrl(?string $url): self + { + if(!Validate::url($url)) + throw new InvalidArgumentException(sprintf('Invalid url: %s', $url)); + + $this->url = $url; + return $this; + } + /** * Optional. Data to be sent in a callback query to the bot when button is pressed, 1-64 bytes * @@ -85,6 +115,27 @@ return $this->callback_data; } + /** + * Optional. Data to be sent in a callback query to the bot when button is pressed, 1-64 bytes + * + * @param string|null $callbackData + * @return $this + */ + public function setCallbackData(?string $callbackData): self + { + if($callbackData == null) + { + $this->callback_data = null; + return $this; + } + + if(!Validate::length($callbackData, 1, 64)) + throw new InvalidArgumentException(sprintf('Invalid callback data length: %s', $callbackData)); + + $this->callback_data = $callbackData; + return $this; + } + /** * Optional. Description of the Web App that will be launched when the user presses the button. The Web App will * be able to send an arbitrary message on behalf of the user using the method answerWebAppQuery. Available only @@ -97,6 +148,20 @@ return $this->web_app; } + /** + * Optional. Description of the Web App that will be launched when the user presses the button. The Web App will + * be able to send an arbitrary message on behalf of the user using the method answerWebAppQuery. Available only + * in private chats between a user and the bot. + * + * @param WebAppInfo|null $webApp + * @return $this + */ + public function setWebApp(?WebAppInfo $webApp): self + { + $this->web_app = $webApp; + return $this; + } + /** * Optional. An HTTPS URL used to automatically authorize the user. Can be used as a replacement for the * Telegram Login Widget. @@ -108,6 +173,25 @@ return $this->login_url; } + /** + * Optional. An HTTPS URL used to automatically authorize the user. Can be used as a replacement for the + * Telegram Login Widget. + * + * @param LoginUrl|null $loginUrl + * @return $this + */ + public function setLoginUrl(?LoginUrl $loginUrl): self + { + if(!Validate::url($loginUrl->getUrl())) + throw new InvalidArgumentException(sprintf('Invalid login url: %s', $loginUrl->getUrl())); + + if(!Validate::isHttps($loginUrl->getUrl())) + throw new InvalidArgumentException(sprintf('The login url must be https: %s', $loginUrl->getUrl())); + + $this->login_url = $loginUrl; + return $this; + } + /** * Optional. If set, pressing the button will prompt the user to select one of their chats, open that chat and * insert the bot's username and the specified inline query in the input field. May be empty, in which case just @@ -125,6 +209,24 @@ return $this->switch_inline_query; } + /** + * Optional. If set, pressing the button will prompt the user to select one of their chats, open that chat and + * insert the bot's username and the specified inline query in the input field. May be empty, in which case just + * the bot's username will be inserted. + * + * Note: This offers an easy way for users to start using your bot in inline mode when they are currently in a + * private chat with it. Especially useful when combined with switch_pm… actions - in this case the user will be + * automatically returned to the chat they switched from, skipping the chat selection screen. + * + * @param string|null $switchInlineQuery + * @return $this + */ + public function setSwitchInlineQuery(?string $switchInlineQuery): self + { + $this->switch_inline_query = $switchInlineQuery; + return $this; + } + /** * Optional. If set, pressing the button will insert the bot's username and the specified inline query in * the current chat's input field. May be empty, in which case only the bot's username will be inserted. @@ -139,6 +241,22 @@ return $this->switch_inline_query_current_chat; } + /** + * Optional. If set, pressing the button will insert the bot's username and the specified inline query in the + * current chat's input field. May be empty, in which case only the bot's username will be inserted. + * + * This offers a quick way for the user to open your bot in inline mode in the same chat - good for selecting + * something from multiple options. + * + * @param string|null $switchInlineQueryCurrentChat + * @return $this + */ + public function setSwitchInlineQueryCurrentChat(?string $switchInlineQueryCurrentChat): self + { + $this->switch_inline_query_current_chat = $switchInlineQueryCurrentChat; + return $this; + } + /** * Optional. Description of the game that will be launched when the user presses the button. * NOTE: This type of button must always be the first button in the first row. @@ -150,6 +268,19 @@ return $this->callback_game; } + /* + * Optional. If set, pressing the button will insert the bot's username and the specified inline query in the + * current chat's input field. May be empty, in which case only the bots username will be inserted. + * + * This offers a quick way for the user to open your bot in inline mode in the same chat - + * good for selecting something from multiple options. + */ + public function setCallbackGame(?CallbackGame $callbackGame): self + { + $this->callback_game = $callbackGame; + return $this; + } + /** * Optional. Specify True, to send a Pay button. * NOTE: This type of button must always be the first button in the first row and can only be used in invoice messages. @@ -162,6 +293,21 @@ return $this->pay; } + /** + * Optional. Specify True, to send a Pay button. + * + * NOTE: This type of button must always be the first button in the first row and can only be used in invoice + * messages. + * + * @param bool $pay + * @return $this + */ + public function setPay(bool $pay): self + { + $this->pay = $pay; + return $this; + } + /** * Returns an array representation of the object *