Skip to content

Commit ac4c506

Browse files
authored
Merge pull request #957 from noplanman/955-bot_api_4.3
API 4.3 - Seamless Telegram Login
2 parents 2e57ef0 + d14fc93 commit ac4c506

File tree

8 files changed

+47
-8
lines changed

8 files changed

+47
-8
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c
77
### Added
88
- New funding and support details.
99
- Custom issue templates. (#972)
10+
- Bot API 4.3 (Seamless Telegram Login, `LoginUrl`)
11+
- `reply_markup` field to `Message` entity.
1012
### Changed
1113
- Use PSR-12 for code style.
1214
- Some general housekeeping. (#972)

src/DB.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -926,7 +926,7 @@ public static function insertMessageRequest(Message $message)
926926
`location`, `venue`, `poll`, `new_chat_members`, `left_chat_member`,
927927
`new_chat_title`, `new_chat_photo`, `delete_chat_photo`, `group_chat_created`,
928928
`supergroup_chat_created`, `channel_chat_created`, `migrate_to_chat_id`, `migrate_from_chat_id`,
929-
`pinned_message`, `invoice`, `successful_payment`, `connected_website`, `passport_data`
929+
`pinned_message`, `invoice`, `successful_payment`, `connected_website`, `passport_data`, `reply_markup`
930930
) VALUES (
931931
:message_id, :user_id, :chat_id, :date, :forward_from, :forward_from_chat, :forward_from_message_id,
932932
:forward_signature, :forward_sender_name, :forward_date,
@@ -935,7 +935,7 @@ public static function insertMessageRequest(Message $message)
935935
:location, :venue, :poll, :new_chat_members, :left_chat_member,
936936
:new_chat_title, :new_chat_photo, :delete_chat_photo, :group_chat_created,
937937
:supergroup_chat_created, :channel_chat_created, :migrate_to_chat_id, :migrate_from_chat_id,
938-
:pinned_message, :invoice, :successful_payment, :connected_website, :passport_data
938+
:pinned_message, :invoice, :successful_payment, :connected_website, :passport_data, :reply_markup
939939
)
940940
');
941941

@@ -1007,6 +1007,7 @@ public static function insertMessageRequest(Message $message)
10071007
$sth->bindValue(':successful_payment', $message->getSuccessfulPayment());
10081008
$sth->bindValue(':connected_website', $message->getConnectedWebsite());
10091009
$sth->bindValue(':passport_data', $message->getPassportData());
1010+
$sth->bindValue(':reply_markup', $message->getReplyMarkup());
10101011

10111012
return $sth->execute();
10121013
} catch (PDOException $e) {

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/#Url.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 <akalongman@gmail.com>
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+
}

src/Entities/Message.php

+3
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
* @method SuccessfulPayment getSuccessfulPayment() Optional. Message is a service message about a successful payment, information about the payment.
6666
* @method string getConnectedWebsite() Optional. The domain name of the website on which the user has logged in.
6767
* @method PassportData getPassportData() Optional. Telegram Passport data
68+
* @method InlineKeyboard getReplyMarkup() Optional. Inline keyboard attached to the message. login_url buttons are represented as ordinary url buttons.
6869
*/
6970
class Message extends Entity
7071
{
@@ -101,6 +102,7 @@ protected function subEntities()
101102
'invoice' => Invoice::class,
102103
'successful_payment' => SuccessfulPayment::class,
103104
'passport_data' => PassportData::class,
105+
'reply_markup' => InlineKeyboard::class,
104106
];
105107
}
106108

@@ -229,6 +231,7 @@ public function getType()
229231
'invoice',
230232
'successful_payment',
231233
'passport_data',
234+
'reply_markup',
232235
];
233236

234237
$is_command = strlen($this->getCommand()) > 0;

structure.sql

+1
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ CREATE TABLE IF NOT EXISTS `message` (
115115
`successful_payment` TEXT NULL COMMENT 'Message is a service message about a successful payment, information about the payment',
116116
`connected_website` TEXT NULL COMMENT 'The domain name of the website on which the user has logged in.',
117117
`passport_data` TEXT NULL COMMENT 'Telegram Passport data',
118+
`reply_markup` TEXT NULL COMMENT 'Inline keyboard attached to the message',
118119

119120
PRIMARY KEY (`chat_id`, `id`),
120121
KEY `user_id` (`user_id`),

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
{

utils/db-schema-update/unreleased.sql

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE `message` ADD COLUMN `reply_markup` TEXT NULL COMMENT 'Inline keyboard attached to the message' AFTER `passport_data`;

0 commit comments

Comments
 (0)