Skip to content

Commit 1bfd0e9

Browse files
committed
Add new LoginUrl entity and login_url field for InlineKeyboard.
1 parent 1785d98 commit 1bfd0e9

File tree

4 files changed

+38
-6
lines changed

4 files changed

+38
-6
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c
55

66
## [Unreleased]
77
### Added
8+
- Bot API 4.3 (Seamless Telegram Login, `LoginUrl`)
89
### Changed
910
### Deprecated
1011
### Removed

src/Entities/InlineKeyboardButton.php

+7-4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
*
2121
* @method string getText() Label text on the button
2222
* @method string getUrl() Optional. HTTP url to be opened when button is pressed
23+
* @method LoginUrl getLoginUrl() Optional. An HTTP URL used to automatically authorize the user. Can be used as a replacement for the Telegram Login Widget.
2324
* @method string getCallbackData() Optional. Data to be sent in a callback query to the bot when button is pressed, 1-64 bytes
2425
* @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.
2526
* @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 @@
2829
*
2930
* @method $this setText(string $text) Label text on the button
3031
* @method $this setUrl(string $url) Optional. HTTP url to be opened when button is pressed
32+
* @method $this setLoginUrl(LoginUrl $login_url) Optional. HTTP url to be opened when button is pressed
3133
* @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
3234
* @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.
3335
* @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)
4850
return is_array($data) &&
4951
array_key_exists('text', $data) && (
5052
array_key_exists('url', $data) ||
53+
array_key_exists('login_url', $data) ||
5154
array_key_exists('callback_data', $data) ||
5255
array_key_exists('switch_inline_query', $data) ||
5356
array_key_exists('switch_inline_query_current_chat', $data) ||
@@ -67,7 +70,7 @@ protected function validate()
6770

6871
$num_params = 0;
6972

70-
foreach (['url', 'callback_data', 'callback_game', 'pay'] as $param) {
73+
foreach (['url', 'login_url', 'callback_data', 'callback_game', 'pay'] as $param) {
7174
if ($this->getProperty($param, '') !== '') {
7275
$num_params++;
7376
}
@@ -80,7 +83,7 @@ protected function validate()
8083
}
8184

8285
if ($num_params !== 1) {
83-
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!');
86+
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!');
8487
}
8588
}
8689

@@ -90,8 +93,8 @@ protected function validate()
9093
public function __call($method, $args)
9194
{
9295
// Only 1 of these can be set, so clear the others when setting a new one.
93-
if (in_array($method, ['setUrl', 'setCallbackData', 'setSwitchInlineQuery', 'setSwitchInlineQueryCurrentChat', 'setCallbackGame', 'setPay'], true)) {
94-
unset($this->url, $this->callback_data, $this->switch_inline_query, $this->switch_inline_query_current_chat, $this->callback_game, $this->pay);
96+
if (in_array($method, ['setUrl', 'setLoginUrl', 'setCallbackData', 'setSwitchInlineQuery', 'setSwitchInlineQueryCurrentChat', 'setCallbackGame', 'setPay'], true)) {
97+
unset($this->url, $this->login_url, $this->callback_data, $this->switch_inline_query, $this->switch_inline_query_current_chat, $this->callback_game, $this->pay);
9598
}
9699

97100
return parent::__call($method, $args);

src/Entities/LoginUrl.php

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
/**
3+
* This file is part of the TelegramBot package.
4+
*
5+
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace Longman\TelegramBot\Entities;
12+
13+
/**
14+
* Class LoginUrl
15+
*
16+
* This object represents a parameter of the inline keyboard button used to automatically authorize a user.
17+
*
18+
* @link https://core.telegram.org/bots/api#loginurl
19+
*
20+
* @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.
21+
* @method string getForwardText() Optional. New text of the button in forwarded messages.
22+
* @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.
23+
* @method bool getRequestWriteAccess() Optional. Pass True to request the permission for your bot to send messages to the user.
24+
*/
25+
class LoginUrl extends Entity
26+
{
27+
28+
}

tests/unit/Entities/InlineKeyboardButtonTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function testInlineKeyboardButtonNoTextFail()
3333

3434
/**
3535
* @expectedException \Longman\TelegramBot\Exception\TelegramException
36-
* @expectedExceptionMessage You must use only one of these fields: url, callback_data, switch_inline_query, switch_inline_query_current_chat, callback_game, pay!
36+
* @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!
3737
*/
3838
public function testInlineKeyboardButtonNoParameterFail()
3939
{
@@ -42,7 +42,7 @@ public function testInlineKeyboardButtonNoParameterFail()
4242

4343
/**
4444
* @expectedException \Longman\TelegramBot\Exception\TelegramException
45-
* @expectedExceptionMessage You must use only one of these fields: url, callback_data, switch_inline_query, switch_inline_query_current_chat, callback_game, pay!
45+
* @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!
4646
*/
4747
public function testInlineKeyboardButtonTooManyParametersFail()
4848
{

0 commit comments

Comments
 (0)