Skip to content

Commit

Permalink
Merge pull request #1231 from php-telegram-bot/1228-bot-api-5.3-tweaks
Browse files Browse the repository at this point in the history
Bot API 5.3 tweaks
  • Loading branch information
noplanman authored Jun 26, 2021
2 parents 97bc2c8 + 5f552a7 commit a826542
Show file tree
Hide file tree
Showing 21 changed files with 45 additions and 85 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c
### Notes
- [:ledger: View file changes][Unreleased][:page_with_curl: DB migration script][unreleased-sql-migration]
### Added
- Bot API 5.3 (Personalized Commands, Keyboard Placeholders). (@TiiFuchs, @noplanman) (#1229, #1231)
### Changed
### Deprecated
### Removed
Expand Down
2 changes: 0 additions & 2 deletions src/Entities/BotCommandScope/BotCommandScope.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<?php


namespace Longman\TelegramBot\Entities\BotCommandScope;


interface BotCommandScope
{

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<?php


namespace Longman\TelegramBot\Entities\BotCommandScope;


use Longman\TelegramBot\Entities\Entity;

/**
Expand Down
2 changes: 0 additions & 2 deletions src/Entities/BotCommandScope/BotCommandScopeAllGroupChats.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<?php


namespace Longman\TelegramBot\Entities\BotCommandScope;


use Longman\TelegramBot\Entities\Entity;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<?php


namespace Longman\TelegramBot\Entities\BotCommandScope;


use Longman\TelegramBot\Entities\Entity;

/**
Expand Down
2 changes: 0 additions & 2 deletions src/Entities/BotCommandScope/BotCommandScopeChat.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<?php


namespace Longman\TelegramBot\Entities\BotCommandScope;


use Longman\TelegramBot\Entities\Entity;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<?php


namespace Longman\TelegramBot\Entities\BotCommandScope;


use Longman\TelegramBot\Entities\Entity;

/**
Expand Down
2 changes: 0 additions & 2 deletions src/Entities/BotCommandScope/BotCommandScopeChatMember.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<?php


namespace Longman\TelegramBot\Entities\BotCommandScope;


use Longman\TelegramBot\Entities\Entity;

/**
Expand Down
2 changes: 0 additions & 2 deletions src/Entities/BotCommandScope/BotCommandScopeDefault.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<?php


namespace Longman\TelegramBot\Entities\BotCommandScope;


use Longman\TelegramBot\Entities\Entity;
use Longman\TelegramBot\Entities\InputMedia\InputMediaAnimation;

Expand Down
3 changes: 0 additions & 3 deletions src/Entities/ChatMember/ChatMemberAdministrator.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<?php


namespace Longman\TelegramBot\Entities\ChatMember;


use Longman\TelegramBot\Entities\Entity;
use Longman\TelegramBot\Entities\User;

Expand Down Expand Up @@ -39,5 +37,4 @@ protected function subEntities(): array
'user' => User::class,
];
}

}
3 changes: 0 additions & 3 deletions src/Entities/ChatMember/ChatMemberBanned.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<?php


namespace Longman\TelegramBot\Entities\ChatMember;


use Longman\TelegramBot\Entities\Entity;
use Longman\TelegramBot\Entities\User;

Expand All @@ -27,5 +25,4 @@ protected function subEntities(): array
'user' => User::class
];
}

}
3 changes: 0 additions & 3 deletions src/Entities/ChatMember/ChatMemberLeft.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<?php


namespace Longman\TelegramBot\Entities\ChatMember;


use Longman\TelegramBot\Entities\Entity;
use Longman\TelegramBot\Entities\User;

Expand All @@ -26,5 +24,4 @@ protected function subEntities(): array
'user' => User::class
];
}

}
3 changes: 0 additions & 3 deletions src/Entities/ChatMember/ChatMemberMember.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<?php


namespace Longman\TelegramBot\Entities\ChatMember;


use Longman\TelegramBot\Entities\Entity;
use Longman\TelegramBot\Entities\User;

Expand All @@ -26,5 +24,4 @@ protected function subEntities(): array
'user' => User::class
];
}

}
2 changes: 0 additions & 2 deletions src/Entities/ChatMember/ChatMemberNotImplemented.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<?php


namespace Longman\TelegramBot\Entities\ChatMember;


use Longman\TelegramBot\Entities\Entity;
use Longman\TelegramBot\Entities\User;

Expand Down
3 changes: 0 additions & 3 deletions src/Entities/ChatMember/ChatMemberOwner.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<?php


namespace Longman\TelegramBot\Entities\ChatMember;


use Longman\TelegramBot\Entities\Entity;
use Longman\TelegramBot\Entities\User;

Expand All @@ -28,5 +26,4 @@ protected function subEntities(): array
'user' => User::class,
];
}

}
3 changes: 0 additions & 3 deletions src/Entities/ChatMember/ChatMemberRestricted.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<?php


namespace Longman\TelegramBot\Entities\ChatMember;


use Longman\TelegramBot\Entities\Entity;
use Longman\TelegramBot\Entities\User;

Expand Down Expand Up @@ -36,5 +34,4 @@ protected function subEntities(): array
'user' => User::class
];
}

}
3 changes: 1 addition & 2 deletions src/Entities/ChatMember/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Longman\TelegramBot\Entities\ChatMember;

use Longman\TelegramBot\Entities\Entity;
use Longman\TelegramBot\Exception\TelegramException;

class Factory extends \Longman\TelegramBot\Entities\Factory
{
Expand All @@ -18,7 +17,7 @@ public static function make(array $data, string $bot_username): Entity
'kicked' => ChatMemberBanned::class,
];

if (! isset($type[$data['status'] ?? ''])) {
if (!isset($type[$data['status'] ?? ''])) {
return new ChatMemberNotImplemented($data, $bot_username);
}

Expand Down
24 changes: 8 additions & 16 deletions src/Entities/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace Longman\TelegramBot\Entities;

use Exception;
use Longman\TelegramBot\Entities\InlineQuery\InlineEntity;
use Longman\TelegramBot\Entities\InputMedia\InputMedia;

Expand Down Expand Up @@ -165,28 +164,21 @@ public function __call($method, $args)
* mainly for PhotoSize and Entities object arrays.
*
* @param string $class
* @param string $property
* @param string $property_name
*
* @return array
*/
protected function makePrettyObjectArray(string $class, string $property): array
protected function makePrettyObjectArray(string $class, string $property_name): array
{
$new_objects = [];

$objects = [];
$bot_username = $this->getProperty('bot_username');
try {
if ($objects = $this->getProperty($property)) {
foreach ($objects as $object) {
if (!empty($object)) {
$new_objects[] = Factory::resolveEntityClass($class, $object, $bot_username);
}
}
}
} catch (Exception $e) {
$new_objects = [];

$properties = array_filter($this->getProperty($property_name) ?: []);
foreach ($properties as $property) {
$objects[] = Factory::resolveEntityClass($class, $property, $bot_username);
}

return $new_objects;
return $objects;
}

/**
Expand Down
14 changes: 8 additions & 6 deletions src/Entities/Keyboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@
*
* @link https://core.telegram.org/bots/api#replykeyboardmarkup
*
* @method bool getResizeKeyboard() Optional. Requests clients to resize the keyboard vertically for optimal fit (e.g., make the keyboard smaller if there are just two rows of buttons). Defaults to false, in which case the custom keyboard is always of the same height as the app's standard keyboard.
* @method bool getOneTimeKeyboard() Optional. Requests clients to remove the keyboard as soon as it's been used. The keyboard will still be available, but clients will automatically display the usual letter-keyboard in the chat – the user can press a special button in the input field to see the custom keyboard again. Defaults to false.
* @method bool getSelective() Optional. Use this parameter if you want to show the keyboard to specific users only. Targets: 1) users that are @mentioned in the text of the Message object; 2) if the bot's message is a reply (has reply_to_message_id), sender of the original message.
* @method bool getResizeKeyboard() Optional. Requests clients to resize the keyboard vertically for optimal fit (e.g., make the keyboard smaller if there are just two rows of buttons). Defaults to false, in which case the custom keyboard is always of the same height as the app's standard keyboard.
* @method bool getOneTimeKeyboard() Optional. Requests clients to remove the keyboard as soon as it's been used. The keyboard will still be available, but clients will automatically display the usual letter-keyboard in the chat – the user can press a special button in the input field to see the custom keyboard again. Defaults to false.
* @method string getInputFieldPlaceholder() Optional. The placeholder to be shown in the input field when the keyboard is active; 1-64 characters
* @method bool getSelective() Optional. Use this parameter if you want to show the keyboard to specific users only. Targets: 1) users that are @mentioned in the text of the Message object; 2) if the bot's message is a reply (has reply_to_message_id), sender of the original message.
*
* @method $this setResizeKeyboard(bool $resize_keyboard) Optional. Requests clients to resize the keyboard vertically for optimal fit (e.g., make the keyboard smaller if there are just two rows of buttons). Defaults to false, in which case the custom keyboard is always of the same height as the app's standard keyboard.
* @method $this setOneTimeKeyboard(bool $one_time_keyboard) Optional. Requests clients to remove the keyboard as soon as it's been used. The keyboard will still be available, but clients will automatically display the usual letter-keyboard in the chat – the user can press a special button in the input field to see the custom keyboard again. Defaults to false.
* @method $this setSelective(bool $selective) Optional. Use this parameter if you want to show the keyboard to specific users only. Targets: 1) users that are @mentioned in the text of the Message object; 2) if the bot's message is a reply (has reply_to_message_id), sender of the original message.
* @method $this setResizeKeyboard(bool $resize_keyboard) Optional. Requests clients to resize the keyboard vertically for optimal fit (e.g., make the keyboard smaller if there are just two rows of buttons). Defaults to false, in which case the custom keyboard is always of the same height as the app's standard keyboard.
* @method $this setOneTimeKeyboard(bool $one_time_keyboard) Optional. Requests clients to remove the keyboard as soon as it's been used. The keyboard will still be available, but clients will automatically display the usual letter-keyboard in the chat – the user can press a special button in the input field to see the custom keyboard again. Defaults to false.
* @method $this setInputFieldPlaceholder(string $input_field_placeholder) Optional. The placeholder to be shown in the input field when the keyboard is active; 1-64 characters
* @method $this setSelective(bool $selective) Optional. Use this parameter if you want to show the keyboard to specific users only. Targets: 1) users that are @mentioned in the text of the Message object; 2) if the bot's message is a reply (has reply_to_message_id), sender of the original message.
*/
class Keyboard extends Entity
{
Expand Down
35 changes: 17 additions & 18 deletions src/Entities/ServerResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,10 @@ public function printError($return = false)
* @param array $result
* @param string $bot_username
*
* @return Chat|ChatMember|File|Message|User|UserProfilePhotos|WebhookInfo|Entity
* @return Chat|ChatMember|File|Message|User|UserProfilePhotos|WebhookInfo
*/
private function createResultObject(array $result, string $bot_username)
private function createResultObject(array $result, string $bot_username): Entity
{
$action = Request::getCurrentAction();

// We don't need to save the raw_data of the response object!
$result['raw_data'] = null;

$result_object_types = [
'getChat' => Chat::class,
'getChatMember' => ChatMemberFactory::class,
Expand All @@ -125,40 +120,44 @@ private function createResultObject(array $result, string $bot_username)
'getWebhookInfo' => WebhookInfo::class,
];

$object_class = array_key_exists($action, $result_object_types) ? $result_object_types[$action] : Message::class;
$action = Request::getCurrentAction();
$object_class = $result_object_types[$action] ?? Message::class;

// We don't need to save the raw_data of the response object!
$result['raw_data'] = null;

return Factory::resolveEntityClass($object_class, $result, $bot_username);
}

/**
* Create and return the objects array of the received result
*
* @param array $result
* @param array $results
* @param string $bot_username
*
* @return BotCommand[]|ChatMember[]|GameHighScore[]|Message[]|Update[]
*/
private function createResultObjects(array $result, string $bot_username): array
private function createResultObjects(array $results, string $bot_username): array
{
$results = [];
$action = Request::getCurrentAction();

$result_object_types = [
'getMyCommands' => BotCommand::class,
'getChatAdministrators' => ChatMemberFactory::class,
'getGameHighScores' => GameHighScore::class,
'sendMediaGroup' => Message::class,
];

$object_class = array_key_exists($action, $result_object_types) ? $result_object_types[$action] : Update::class;
$action = Request::getCurrentAction();
$object_class = $result_object_types[$action] ?? Update::class;

$objects = [];

foreach ($result as $data) {
foreach ($results as $result) {
// We don't need to save the raw_data of the response object!
$data['raw_data'] = null;
$result['raw_data'] = null;

$results[] = Factory::resolveEntityClass($object_class, $data, $bot_username);
$objects[] = Factory::resolveEntityClass($object_class, $result, $bot_username);
}

return $results;
return $objects;
}
}
17 changes: 10 additions & 7 deletions tests/Unit/Entities/KeyboardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,22 +114,25 @@ public function testKeyboardWithButtonObjects(): void

public function testKeyboardWithDataArray(): void
{
$resize_keyboard = (bool) mt_rand(0, 1);
$one_time_keyboard = (bool) mt_rand(0, 1);
$selective = (bool) mt_rand(0, 1);
$resize_keyboard = (bool) mt_rand(0, 1);
$one_time_keyboard = (bool) mt_rand(0, 1);
$input_field_placeholder = 'placeholder';
$selective = (bool) mt_rand(0, 1);

$keyboard_obj = new Keyboard([
'resize_keyboard' => $resize_keyboard,
'one_time_keyboard' => $one_time_keyboard,
'selective' => $selective,
'keyboard' => [['Button Text 1']],
'resize_keyboard' => $resize_keyboard,
'one_time_keyboard' => $one_time_keyboard,
'input_field_placeholder' => $input_field_placeholder,
'selective' => $selective,
'keyboard' => [['Button Text 1']],
]);

$keyboard = $keyboard_obj->getProperty('keyboard');
self::assertSame('Button Text 1', $keyboard[0][0]->getText());

self::assertSame($resize_keyboard, $keyboard_obj->getResizeKeyboard());
self::assertSame($one_time_keyboard, $keyboard_obj->getOneTimeKeyboard());
self::assertSame($input_field_placeholder, $keyboard_obj->getInputFieldPlaceholder());
self::assertSame($selective, $keyboard_obj->getSelective());
}

Expand Down

0 comments on commit a826542

Please sign in to comment.