2023-02-13 14:11:43 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
2024-10-03 21:18:35 -04:00
|
|
|
namespace TgBotLib\Objects;
|
2023-02-13 14:11:43 -05:00
|
|
|
|
2023-04-23 16:52:40 -04:00
|
|
|
use InvalidArgumentException;
|
|
|
|
use TgBotLib\Classes\Validate;
|
2023-02-13 14:11:43 -05:00
|
|
|
use TgBotLib\Interfaces\ObjectTypeInterface;
|
2024-10-04 15:12:35 -04:00
|
|
|
use TgBotLib\Objects\Games\CallbackGame;
|
2023-02-13 14:11:43 -05:00
|
|
|
|
|
|
|
class InlineKeyboardButton implements ObjectTypeInterface
|
|
|
|
{
|
2024-10-05 00:17:34 -04:00
|
|
|
private string $text;
|
|
|
|
private ?string $url;
|
|
|
|
private ?string $callback_data;
|
|
|
|
private ?WebAppInfo $web_app;
|
|
|
|
private ?LoginUrl $login_url;
|
|
|
|
private ?string $switch_inline_query;
|
|
|
|
private ?string $switch_inline_query_current_chat;
|
|
|
|
private ?CallbackGame $callback_game;
|
|
|
|
private bool $pay;
|
2023-02-13 14:11:43 -05:00
|
|
|
|
2024-10-09 17:38:13 -04:00
|
|
|
/**
|
|
|
|
* InlineKeyboardButton constructor.
|
|
|
|
*/
|
2024-10-09 14:07:41 -04:00
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->text = (string)null;
|
|
|
|
$this->url = null;
|
|
|
|
$this->callback_data = null;
|
|
|
|
$this->web_app = null;
|
|
|
|
$this->login_url = null;
|
|
|
|
$this->switch_inline_query = null;
|
|
|
|
$this->switch_inline_query_current_chat = null;
|
|
|
|
$this->callback_game = null;
|
|
|
|
$this->pay = false;
|
|
|
|
}
|
|
|
|
|
2023-02-13 14:11:43 -05:00
|
|
|
/**
|
|
|
|
* Label text on the button
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getText(): string
|
|
|
|
{
|
|
|
|
return $this->text;
|
|
|
|
}
|
|
|
|
|
2023-04-23 16:52:40 -04:00
|
|
|
/**
|
|
|
|
* Label text on the button
|
|
|
|
*
|
|
|
|
* @param string $text
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setText(string $text): self
|
|
|
|
{
|
|
|
|
$this->text = $text;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2023-02-13 14:11:43 -05:00
|
|
|
/**
|
|
|
|
* Optional. HTTP or tg:// URL to be opened when the button is pressed. Links tg://user?id=<user_id> can be
|
|
|
|
* used to mention a user by their ID without using a username, if this is allowed by their privacy settings.
|
|
|
|
*
|
|
|
|
* @return string|null
|
|
|
|
*/
|
|
|
|
public function getUrl(): ?string
|
|
|
|
{
|
|
|
|
return $this->url;
|
|
|
|
}
|
|
|
|
|
2023-04-23 16:52:40 -04:00
|
|
|
/**
|
|
|
|
* Optional. HTTP or tg:// URL to be opened when the button is pressed. Links tg://user?id=<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))
|
2024-10-09 17:38:13 -04:00
|
|
|
{
|
2023-04-23 16:52:40 -04:00
|
|
|
throw new InvalidArgumentException(sprintf('Invalid url: %s', $url));
|
2024-10-09 17:38:13 -04:00
|
|
|
}
|
2023-04-23 16:52:40 -04:00
|
|
|
|
|
|
|
$this->url = $url;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2023-02-13 14:11:43 -05:00
|
|
|
/**
|
|
|
|
* Optional. Data to be sent in a callback query to the bot when button is pressed, 1-64 bytes
|
|
|
|
*
|
|
|
|
* @see https://core.telegram.org/bots/api#callbackquery
|
|
|
|
* @return string|null
|
|
|
|
*/
|
|
|
|
public function getCallbackData(): ?string
|
|
|
|
{
|
|
|
|
return $this->callback_data;
|
|
|
|
}
|
|
|
|
|
2023-04-23 16:52:40 -04:00
|
|
|
/**
|
|
|
|
* 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))
|
2024-10-09 17:38:13 -04:00
|
|
|
{
|
2023-04-23 16:52:40 -04:00
|
|
|
throw new InvalidArgumentException(sprintf('Invalid callback data length: %s', $callbackData));
|
2024-10-09 17:38:13 -04:00
|
|
|
}
|
2023-04-23 16:52:40 -04:00
|
|
|
|
|
|
|
$this->callback_data = $callbackData;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2023-02-13 14:11:43 -05:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @return WebAppInfo|null
|
|
|
|
*/
|
|
|
|
public function getWebApp(): ?WebAppInfo
|
|
|
|
{
|
|
|
|
return $this->web_app;
|
|
|
|
}
|
|
|
|
|
2023-04-23 16:52:40 -04:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2023-02-13 14:11:43 -05:00
|
|
|
/**
|
|
|
|
* Optional. An HTTPS URL used to automatically authorize the user. Can be used as a replacement for the
|
|
|
|
* Telegram Login Widget.
|
|
|
|
*
|
|
|
|
* @return LoginUrl|null
|
|
|
|
*/
|
|
|
|
public function getLoginUrl(): ?LoginUrl
|
|
|
|
{
|
|
|
|
return $this->login_url;
|
|
|
|
}
|
|
|
|
|
2023-04-23 16:52:40 -04:00
|
|
|
/**
|
|
|
|
* 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()))
|
2024-10-09 17:38:13 -04:00
|
|
|
{
|
2023-04-23 16:52:40 -04:00
|
|
|
throw new InvalidArgumentException(sprintf('Invalid login url: %s', $loginUrl->getUrl()));
|
2024-10-09 17:38:13 -04:00
|
|
|
}
|
2023-04-23 16:52:40 -04:00
|
|
|
|
|
|
|
if(!Validate::isHttps($loginUrl->getUrl()))
|
2024-10-09 17:38:13 -04:00
|
|
|
{
|
2023-04-23 16:52:40 -04:00
|
|
|
throw new InvalidArgumentException(sprintf('The login url must be https: %s', $loginUrl->getUrl()));
|
2024-10-09 17:38:13 -04:00
|
|
|
}
|
2023-04-23 16:52:40 -04:00
|
|
|
|
|
|
|
$this->login_url = $loginUrl;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2023-02-13 14:11:43 -05:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @see https://core.telegram.org/bots/api#answerinlinequery
|
|
|
|
* @return string|null
|
|
|
|
*/
|
|
|
|
public function getSwitchInlineQuery(): ?string
|
|
|
|
{
|
|
|
|
return $this->switch_inline_query;
|
|
|
|
}
|
|
|
|
|
2023-04-23 16:52:40 -04:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2023-02-13 14:11:43 -05:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @return string|null
|
|
|
|
*/
|
|
|
|
public function getSwitchInlineQueryCurrentChat(): ?string
|
|
|
|
{
|
|
|
|
return $this->switch_inline_query_current_chat;
|
|
|
|
}
|
|
|
|
|
2023-04-23 16:52:40 -04:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2023-02-13 14:11:43 -05:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @return CallbackGame|null
|
|
|
|
*/
|
|
|
|
public function getCallbackGame(): ?CallbackGame
|
|
|
|
{
|
|
|
|
return $this->callback_game;
|
|
|
|
}
|
|
|
|
|
2023-04-23 16:52:40 -04:00
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2023-02-13 14:11:43 -05:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @see https://core.telegram.org/bots/api#payments
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isPay(): bool
|
|
|
|
{
|
|
|
|
return $this->pay;
|
|
|
|
}
|
|
|
|
|
2023-04-23 16:52:40 -04:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2023-02-13 14:11:43 -05:00
|
|
|
/**
|
2024-10-05 00:17:34 -04:00
|
|
|
* @inheritDoc
|
2023-02-13 14:11:43 -05:00
|
|
|
*/
|
|
|
|
public function toArray(): array
|
|
|
|
{
|
2024-10-09 14:07:41 -04:00
|
|
|
$array = ['text' => $this->text];
|
|
|
|
|
|
|
|
if ($this->url !== null)
|
|
|
|
{
|
|
|
|
$array['url'] = $this->url;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->callback_data !== null)
|
|
|
|
{
|
|
|
|
$array['callback_data'] = $this->callback_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->web_app !== null)
|
|
|
|
{
|
|
|
|
$array['web_app'] = $this->web_app->toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->login_url !== null)
|
|
|
|
{
|
|
|
|
$array['login_url'] = $this->login_url->toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->switch_inline_query !== null)
|
|
|
|
{
|
|
|
|
$array['switch_inline_query'] = $this->switch_inline_query;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->switch_inline_query_current_chat !== null)
|
|
|
|
{
|
|
|
|
$array['switch_inline_query_current_chat'] = $this->switch_inline_query_current_chat;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->callback_game !== null)
|
|
|
|
{
|
|
|
|
$array['callback_game'] = $this->callback_game->toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->pay)
|
|
|
|
{
|
|
|
|
$array['pay'] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $array;
|
2023-02-13 14:11:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-10-05 00:17:34 -04:00
|
|
|
* @inheritDoc
|
2023-02-13 14:11:43 -05:00
|
|
|
*/
|
2024-10-03 21:14:27 -04:00
|
|
|
public static function fromArray(?array $data): ?InlineKeyboardButton
|
2023-02-13 14:11:43 -05:00
|
|
|
{
|
2024-10-03 21:14:27 -04:00
|
|
|
if($data === null)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
2023-02-13 14:11:43 -05:00
|
|
|
|
2024-10-03 21:14:27 -04:00
|
|
|
$object = new self();
|
2023-02-13 14:11:43 -05:00
|
|
|
$object->text = $data['text'] ?? null;
|
|
|
|
$object->url = $data['url'] ?? null;
|
|
|
|
$object->callback_data = $data['callback_data'] ?? null;
|
2023-02-14 17:35:16 -05:00
|
|
|
$object->web_app = isset($data['web_app']) && is_array($data['web_app']) ? WebAppInfo::fromArray($data['web_app']) : null;
|
|
|
|
$object->login_url = isset($data['login_url']) && is_array($data['login_url']) ? LoginUrl::fromArray($data['login_url']) : null;
|
2023-02-13 14:11:43 -05:00
|
|
|
$object->switch_inline_query = $data['switch_inline_query'] ?? null;
|
|
|
|
$object->switch_inline_query_current_chat = $data['switch_inline_query_current_chat'] ?? null;
|
2023-02-14 17:35:16 -05:00
|
|
|
$object->callback_game = isset($data['callback_game']) && is_array($data['callback_game']) ? CallbackGame::fromArray($data['callback_game']) : null;
|
2024-10-09 14:07:41 -04:00
|
|
|
$object->pay = $data['pay'] ?? false;
|
2023-02-13 14:11:43 -05:00
|
|
|
|
|
|
|
return $object;
|
|
|
|
}
|
|
|
|
}
|