diff --git a/CHANGELOG.md b/CHANGELOG.md index 61264003b..0abf0b163 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c ### Added - New funding and support details. - Custom issue templates. (#972) +- Bot API 4.3 (Seamless Telegram Login, `LoginUrl`) +- `reply_markup` field to `Message` entity. ### Changed - Use PSR-12 for code style. - Some general housekeeping. (#972) diff --git a/src/DB.php b/src/DB.php index 92515b70a..c06d1224e 100644 --- a/src/DB.php +++ b/src/DB.php @@ -926,7 +926,7 @@ public static function insertMessageRequest(Message $message) `location`, `venue`, `poll`, `new_chat_members`, `left_chat_member`, `new_chat_title`, `new_chat_photo`, `delete_chat_photo`, `group_chat_created`, `supergroup_chat_created`, `channel_chat_created`, `migrate_to_chat_id`, `migrate_from_chat_id`, - `pinned_message`, `invoice`, `successful_payment`, `connected_website`, `passport_data` + `pinned_message`, `invoice`, `successful_payment`, `connected_website`, `passport_data`, `reply_markup` ) VALUES ( :message_id, :user_id, :chat_id, :date, :forward_from, :forward_from_chat, :forward_from_message_id, :forward_signature, :forward_sender_name, :forward_date, @@ -935,7 +935,7 @@ public static function insertMessageRequest(Message $message) :location, :venue, :poll, :new_chat_members, :left_chat_member, :new_chat_title, :new_chat_photo, :delete_chat_photo, :group_chat_created, :supergroup_chat_created, :channel_chat_created, :migrate_to_chat_id, :migrate_from_chat_id, - :pinned_message, :invoice, :successful_payment, :connected_website, :passport_data + :pinned_message, :invoice, :successful_payment, :connected_website, :passport_data, :reply_markup ) '); @@ -1007,6 +1007,7 @@ public static function insertMessageRequest(Message $message) $sth->bindValue(':successful_payment', $message->getSuccessfulPayment()); $sth->bindValue(':connected_website', $message->getConnectedWebsite()); $sth->bindValue(':passport_data', $message->getPassportData()); + $sth->bindValue(':reply_markup', $message->getReplyMarkup()); return $sth->execute(); } catch (PDOException $e) { diff --git a/src/Entities/InlineKeyboardButton.php b/src/Entities/InlineKeyboardButton.php index 492aa4280..004a7ed21 100644 --- a/src/Entities/InlineKeyboardButton.php +++ b/src/Entities/InlineKeyboardButton.php @@ -20,6 +20,7 @@ * * @method string getText() Label text on the button * @method string getUrl() Optional. HTTP url to be opened when button is pressed + * @method LoginUrl getLoginUrl() Optional. An HTTP URL used to automatically authorize the user. Can be used as a replacement for the Telegram Login Widget. * @method string getCallbackData() Optional. Data to be sent in a callback query to the bot when button is pressed, 1-64 bytes * @method string getSwitchInlineQuery() 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. Can be empty, in which case just the bot’s username will be inserted. * @method string getSwitchInlineQueryCurrentChat() Optional. If set, pressing the button will insert the bot‘s username and the specified inline query in the current chat's input field. Can be empty, in which case only the bot’s username will be inserted. @@ -28,6 +29,7 @@ * * @method $this setText(string $text) Label text on the button * @method $this setUrl(string $url) Optional. HTTP url to be opened when button is pressed + * @method $this setLoginUrl(LoginUrl $login_url) Optional. HTTP url to be opened when button is pressed * @method $this setCallbackData(string $callback_data) Optional. Data to be sent in a callback query to the bot when button is pressed, 1-64 bytes * @method $this setSwitchInlineQuery(string $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. Can be empty, in which case just the bot’s username will be inserted. * @method $this setSwitchInlineQueryCurrentChat(string $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. Can be empty, in which case only the bot’s username will be inserted. @@ -48,6 +50,7 @@ public static function couldBe($data) return is_array($data) && array_key_exists('text', $data) && ( array_key_exists('url', $data) || + array_key_exists('login_url', $data) || array_key_exists('callback_data', $data) || array_key_exists('switch_inline_query', $data) || array_key_exists('switch_inline_query_current_chat', $data) || @@ -67,7 +70,7 @@ protected function validate() $num_params = 0; - foreach (['url', 'callback_data', 'callback_game', 'pay'] as $param) { + foreach (['url', 'login_url', 'callback_data', 'callback_game', 'pay'] as $param) { if ($this->getProperty($param, '') !== '') { $num_params++; } @@ -80,7 +83,7 @@ protected function validate() } if ($num_params !== 1) { - throw new TelegramException('You must use only one of these fields: url, callback_data, switch_inline_query, switch_inline_query_current_chat, callback_game, pay!'); + throw new TelegramException('You must use only one of these fields: url, login_url, callback_data, switch_inline_query, switch_inline_query_current_chat, callback_game, pay!'); } } @@ -90,8 +93,8 @@ protected function validate() public function __call($method, $args) { // Only 1 of these can be set, so clear the others when setting a new one. - if (in_array($method, ['setUrl', 'setCallbackData', 'setSwitchInlineQuery', 'setSwitchInlineQueryCurrentChat', 'setCallbackGame', 'setPay'], true)) { - unset($this->url, $this->callback_data, $this->switch_inline_query, $this->switch_inline_query_current_chat, $this->callback_game, $this->pay); + if (in_array($method, ['setUrl', 'setLoginUrl', 'setCallbackData', 'setSwitchInlineQuery', 'setSwitchInlineQueryCurrentChat', 'setCallbackGame', 'setPay'], true)) { + unset($this->url, $this->login_url, $this->callback_data, $this->switch_inline_query, $this->switch_inline_query_current_chat, $this->callback_game, $this->pay); } return parent::__call($method, $args); diff --git a/src/Entities/LoginUrl.php b/src/Entities/LoginUrl.php new file mode 100644 index 000000000..f5da5f216 --- /dev/null +++ b/src/Entities/LoginUrl.php @@ -0,0 +1,28 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Longman\TelegramBot\Entities; + +/** + * Class LoginUrl + * + * This object represents a parameter of the inline keyboard button used to automatically authorize a user. + * + * @link https://core.telegram.org/bots/api#loginurl + * + * @method string getUrl() An HTTP URL to be opened with user authorization data added to the query string when the button is pressed. If the user refuses to provide authorization data, the original URL without information about the user will be opened. The data added is the same as described in Receiving authorization data. + * @method string getForwardText() Optional. New text of the button in forwarded messages. + * @method string getBotUsername() Optional. Username of a bot, which will be used for user authorization. See Setting up a bot for more details. If not specified, the current bot's username will be assumed. The url's domain must be the same as the domain linked with the bot. See Linking your domain to the bot for more details. + * @method bool getRequestWriteAccess() Optional. Pass True to request the permission for your bot to send messages to the user. + */ +class LoginUrl extends Entity +{ + +} diff --git a/src/Entities/Message.php b/src/Entities/Message.php index 69e468b1d..62fc34fe8 100644 --- a/src/Entities/Message.php +++ b/src/Entities/Message.php @@ -65,6 +65,7 @@ * @method SuccessfulPayment getSuccessfulPayment() Optional. Message is a service message about a successful payment, information about the payment. * @method string getConnectedWebsite() Optional. The domain name of the website on which the user has logged in. * @method PassportData getPassportData() Optional. Telegram Passport data + * @method InlineKeyboard getReplyMarkup() Optional. Inline keyboard attached to the message. login_url buttons are represented as ordinary url buttons. */ class Message extends Entity { @@ -101,6 +102,7 @@ protected function subEntities() 'invoice' => Invoice::class, 'successful_payment' => SuccessfulPayment::class, 'passport_data' => PassportData::class, + 'reply_markup' => InlineKeyboard::class, ]; } @@ -229,6 +231,7 @@ public function getType() 'invoice', 'successful_payment', 'passport_data', + 'reply_markup', ]; $is_command = strlen($this->getCommand()) > 0; diff --git a/structure.sql b/structure.sql index 10ce91573..2b7b6628a 100644 --- a/structure.sql +++ b/structure.sql @@ -115,6 +115,7 @@ CREATE TABLE IF NOT EXISTS `message` ( `successful_payment` TEXT NULL COMMENT 'Message is a service message about a successful payment, information about the payment', `connected_website` TEXT NULL COMMENT 'The domain name of the website on which the user has logged in.', `passport_data` TEXT NULL COMMENT 'Telegram Passport data', + `reply_markup` TEXT NULL COMMENT 'Inline keyboard attached to the message', PRIMARY KEY (`chat_id`, `id`), KEY `user_id` (`user_id`), diff --git a/tests/unit/Entities/InlineKeyboardButtonTest.php b/tests/unit/Entities/InlineKeyboardButtonTest.php index 20b9e0f6b..71f00f9b4 100644 --- a/tests/unit/Entities/InlineKeyboardButtonTest.php +++ b/tests/unit/Entities/InlineKeyboardButtonTest.php @@ -33,7 +33,7 @@ public function testInlineKeyboardButtonNoTextFail() /** * @expectedException \Longman\TelegramBot\Exception\TelegramException - * @expectedExceptionMessage You must use only one of these fields: url, callback_data, switch_inline_query, switch_inline_query_current_chat, callback_game, pay! + * @expectedExceptionMessage You must use only one of these fields: url, login_url, callback_data, switch_inline_query, switch_inline_query_current_chat, callback_game, pay! */ public function testInlineKeyboardButtonNoParameterFail() { @@ -42,7 +42,7 @@ public function testInlineKeyboardButtonNoParameterFail() /** * @expectedException \Longman\TelegramBot\Exception\TelegramException - * @expectedExceptionMessage You must use only one of these fields: url, callback_data, switch_inline_query, switch_inline_query_current_chat, callback_game, pay! + * @expectedExceptionMessage You must use only one of these fields: url, login_url, callback_data, switch_inline_query, switch_inline_query_current_chat, callback_game, pay! */ public function testInlineKeyboardButtonTooManyParametersFail() { diff --git a/utils/db-schema-update/unreleased.sql b/utils/db-schema-update/unreleased.sql index e69de29bb..9d48cd8e7 100644 --- a/utils/db-schema-update/unreleased.sql +++ b/utils/db-schema-update/unreleased.sql @@ -0,0 +1 @@ +ALTER TABLE `message` ADD COLUMN `reply_markup` TEXT NULL COMMENT 'Inline keyboard attached to the message' AFTER `passport_data`;