Skip to content

API 4.3 - Seamless Telegram Login #957

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions src/DB.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
)
');

Expand Down Expand Up @@ -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) {
Expand Down
11 changes: 7 additions & 4 deletions src/Entities/InlineKeyboardButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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) ||
Expand All @@ -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++;
}
Expand All @@ -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!');
}
}

Expand All @@ -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);
Expand Down
28 changes: 28 additions & 0 deletions src/Entities/LoginUrl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* This file is part of the TelegramBot package.
*
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
*
* 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
{

}
3 changes: 3 additions & 0 deletions src/Entities/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -101,6 +102,7 @@ protected function subEntities()
'invoice' => Invoice::class,
'successful_payment' => SuccessfulPayment::class,
'passport_data' => PassportData::class,
'reply_markup' => InlineKeyboard::class,
];
}

Expand Down Expand Up @@ -229,6 +231,7 @@ public function getType()
'invoice',
'successful_payment',
'passport_data',
'reply_markup',
];

$is_command = strlen($this->getCommand()) > 0;
Expand Down
1 change: 1 addition & 0 deletions structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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`),
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/Entities/InlineKeyboardButtonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -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()
{
Expand Down
1 change: 1 addition & 0 deletions utils/db-schema-update/unreleased.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE `message` ADD COLUMN `reply_markup` TEXT NULL COMMENT 'Inline keyboard attached to the message' AFTER `passport_data`;