Skip to content

Commit a826542

Browse files
authored
Merge pull request #1231 from php-telegram-bot/1228-bot-api-5.3-tweaks
Bot API 5.3 tweaks
2 parents 97bc2c8 + 5f552a7 commit a826542

21 files changed

+45
-85
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c
77
### Notes
88
- [:ledger: View file changes][Unreleased][:page_with_curl: DB migration script][unreleased-sql-migration]
99
### Added
10+
- Bot API 5.3 (Personalized Commands, Keyboard Placeholders). (@TiiFuchs, @noplanman) (#1229, #1231)
1011
### Changed
1112
### Deprecated
1213
### Removed

src/Entities/BotCommandScope/BotCommandScope.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
<?php
22

3-
43
namespace Longman\TelegramBot\Entities\BotCommandScope;
54

6-
75
interface BotCommandScope
86
{
97

src/Entities/BotCommandScope/BotCommandScopeAllChatAdministrators.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
<?php
22

3-
43
namespace Longman\TelegramBot\Entities\BotCommandScope;
54

6-
75
use Longman\TelegramBot\Entities\Entity;
86

97
/**

src/Entities/BotCommandScope/BotCommandScopeAllGroupChats.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
<?php
22

3-
43
namespace Longman\TelegramBot\Entities\BotCommandScope;
54

6-
75
use Longman\TelegramBot\Entities\Entity;
86

97
/**

src/Entities/BotCommandScope/BotCommandScopeAllPrivateChats.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
<?php
22

3-
43
namespace Longman\TelegramBot\Entities\BotCommandScope;
54

6-
75
use Longman\TelegramBot\Entities\Entity;
86

97
/**

src/Entities/BotCommandScope/BotCommandScopeChat.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
<?php
22

3-
43
namespace Longman\TelegramBot\Entities\BotCommandScope;
54

6-
75
use Longman\TelegramBot\Entities\Entity;
86

97
/**

src/Entities/BotCommandScope/BotCommandScopeChatAdministrators.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
<?php
22

3-
43
namespace Longman\TelegramBot\Entities\BotCommandScope;
54

6-
75
use Longman\TelegramBot\Entities\Entity;
86

97
/**

src/Entities/BotCommandScope/BotCommandScopeChatMember.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
<?php
22

3-
43
namespace Longman\TelegramBot\Entities\BotCommandScope;
54

6-
75
use Longman\TelegramBot\Entities\Entity;
86

97
/**

src/Entities/BotCommandScope/BotCommandScopeDefault.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
<?php
22

3-
43
namespace Longman\TelegramBot\Entities\BotCommandScope;
54

6-
75
use Longman\TelegramBot\Entities\Entity;
86
use Longman\TelegramBot\Entities\InputMedia\InputMediaAnimation;
97

src/Entities/ChatMember/ChatMemberAdministrator.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
<?php
22

3-
43
namespace Longman\TelegramBot\Entities\ChatMember;
54

6-
75
use Longman\TelegramBot\Entities\Entity;
86
use Longman\TelegramBot\Entities\User;
97

@@ -39,5 +37,4 @@ protected function subEntities(): array
3937
'user' => User::class,
4038
];
4139
}
42-
4340
}

src/Entities/ChatMember/ChatMemberBanned.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
<?php
22

3-
43
namespace Longman\TelegramBot\Entities\ChatMember;
54

6-
75
use Longman\TelegramBot\Entities\Entity;
86
use Longman\TelegramBot\Entities\User;
97

@@ -27,5 +25,4 @@ protected function subEntities(): array
2725
'user' => User::class
2826
];
2927
}
30-
3128
}

src/Entities/ChatMember/ChatMemberLeft.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
<?php
22

3-
43
namespace Longman\TelegramBot\Entities\ChatMember;
54

6-
75
use Longman\TelegramBot\Entities\Entity;
86
use Longman\TelegramBot\Entities\User;
97

@@ -26,5 +24,4 @@ protected function subEntities(): array
2624
'user' => User::class
2725
];
2826
}
29-
3027
}

src/Entities/ChatMember/ChatMemberMember.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
<?php
22

3-
43
namespace Longman\TelegramBot\Entities\ChatMember;
54

6-
75
use Longman\TelegramBot\Entities\Entity;
86
use Longman\TelegramBot\Entities\User;
97

@@ -26,5 +24,4 @@ protected function subEntities(): array
2624
'user' => User::class
2725
];
2826
}
29-
3027
}

src/Entities/ChatMember/ChatMemberNotImplemented.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
<?php
22

3-
43
namespace Longman\TelegramBot\Entities\ChatMember;
54

6-
75
use Longman\TelegramBot\Entities\Entity;
86
use Longman\TelegramBot\Entities\User;
97

src/Entities/ChatMember/ChatMemberOwner.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
<?php
22

3-
43
namespace Longman\TelegramBot\Entities\ChatMember;
54

6-
75
use Longman\TelegramBot\Entities\Entity;
86
use Longman\TelegramBot\Entities\User;
97

@@ -28,5 +26,4 @@ protected function subEntities(): array
2826
'user' => User::class,
2927
];
3028
}
31-
3229
}

src/Entities/ChatMember/ChatMemberRestricted.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
<?php
22

3-
43
namespace Longman\TelegramBot\Entities\ChatMember;
54

6-
75
use Longman\TelegramBot\Entities\Entity;
86
use Longman\TelegramBot\Entities\User;
97

@@ -36,5 +34,4 @@ protected function subEntities(): array
3634
'user' => User::class
3735
];
3836
}
39-
4037
}

src/Entities/ChatMember/Factory.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Longman\TelegramBot\Entities\ChatMember;
44

55
use Longman\TelegramBot\Entities\Entity;
6-
use Longman\TelegramBot\Exception\TelegramException;
76

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

21-
if (! isset($type[$data['status'] ?? ''])) {
20+
if (!isset($type[$data['status'] ?? ''])) {
2221
return new ChatMemberNotImplemented($data, $bot_username);
2322
}
2423

src/Entities/Entity.php

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace Longman\TelegramBot\Entities;
1313

14-
use Exception;
1514
use Longman\TelegramBot\Entities\InlineQuery\InlineEntity;
1615
use Longman\TelegramBot\Entities\InputMedia\InputMedia;
1716

@@ -165,28 +164,21 @@ public function __call($method, $args)
165164
* mainly for PhotoSize and Entities object arrays.
166165
*
167166
* @param string $class
168-
* @param string $property
167+
* @param string $property_name
169168
*
170169
* @return array
171170
*/
172-
protected function makePrettyObjectArray(string $class, string $property): array
171+
protected function makePrettyObjectArray(string $class, string $property_name): array
173172
{
174-
$new_objects = [];
175-
173+
$objects = [];
176174
$bot_username = $this->getProperty('bot_username');
177-
try {
178-
if ($objects = $this->getProperty($property)) {
179-
foreach ($objects as $object) {
180-
if (!empty($object)) {
181-
$new_objects[] = Factory::resolveEntityClass($class, $object, $bot_username);
182-
}
183-
}
184-
}
185-
} catch (Exception $e) {
186-
$new_objects = [];
175+
176+
$properties = array_filter($this->getProperty($property_name) ?: []);
177+
foreach ($properties as $property) {
178+
$objects[] = Factory::resolveEntityClass($class, $property, $bot_username);
187179
}
188180

189-
return $new_objects;
181+
return $objects;
190182
}
191183

192184
/**

src/Entities/Keyboard.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@
2020
*
2121
* @link https://core.telegram.org/bots/api#replykeyboardmarkup
2222
*
23-
* @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.
24-
* @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.
25-
* @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.
23+
* @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.
24+
* @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.
25+
* @method string getInputFieldPlaceholder() Optional. The placeholder to be shown in the input field when the keyboard is active; 1-64 characters
26+
* @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.
2627
*
27-
* @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.
28-
* @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.
29-
* @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.
28+
* @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.
29+
* @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.
30+
* @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
31+
* @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.
3032
*/
3133
class Keyboard extends Entity
3234
{

src/Entities/ServerResponse.php

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,10 @@ public function printError($return = false)
106106
* @param array $result
107107
* @param string $bot_username
108108
*
109-
* @return Chat|ChatMember|File|Message|User|UserProfilePhotos|WebhookInfo|Entity
109+
* @return Chat|ChatMember|File|Message|User|UserProfilePhotos|WebhookInfo
110110
*/
111-
private function createResultObject(array $result, string $bot_username)
111+
private function createResultObject(array $result, string $bot_username): Entity
112112
{
113-
$action = Request::getCurrentAction();
114-
115-
// We don't need to save the raw_data of the response object!
116-
$result['raw_data'] = null;
117-
118113
$result_object_types = [
119114
'getChat' => Chat::class,
120115
'getChatMember' => ChatMemberFactory::class,
@@ -125,40 +120,44 @@ private function createResultObject(array $result, string $bot_username)
125120
'getWebhookInfo' => WebhookInfo::class,
126121
];
127122

128-
$object_class = array_key_exists($action, $result_object_types) ? $result_object_types[$action] : Message::class;
123+
$action = Request::getCurrentAction();
124+
$object_class = $result_object_types[$action] ?? Message::class;
125+
126+
// We don't need to save the raw_data of the response object!
127+
$result['raw_data'] = null;
129128

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

133132
/**
134133
* Create and return the objects array of the received result
135134
*
136-
* @param array $result
135+
* @param array $results
137136
* @param string $bot_username
138137
*
139138
* @return BotCommand[]|ChatMember[]|GameHighScore[]|Message[]|Update[]
140139
*/
141-
private function createResultObjects(array $result, string $bot_username): array
140+
private function createResultObjects(array $results, string $bot_username): array
142141
{
143-
$results = [];
144-
$action = Request::getCurrentAction();
145-
146142
$result_object_types = [
147143
'getMyCommands' => BotCommand::class,
148144
'getChatAdministrators' => ChatMemberFactory::class,
149145
'getGameHighScores' => GameHighScore::class,
150146
'sendMediaGroup' => Message::class,
151147
];
152148

153-
$object_class = array_key_exists($action, $result_object_types) ? $result_object_types[$action] : Update::class;
149+
$action = Request::getCurrentAction();
150+
$object_class = $result_object_types[$action] ?? Update::class;
151+
152+
$objects = [];
154153

155-
foreach ($result as $data) {
154+
foreach ($results as $result) {
156155
// We don't need to save the raw_data of the response object!
157-
$data['raw_data'] = null;
156+
$result['raw_data'] = null;
158157

159-
$results[] = Factory::resolveEntityClass($object_class, $data, $bot_username);
158+
$objects[] = Factory::resolveEntityClass($object_class, $result, $bot_username);
160159
}
161160

162-
return $results;
161+
return $objects;
163162
}
164163
}

tests/Unit/Entities/KeyboardTest.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,22 +114,25 @@ public function testKeyboardWithButtonObjects(): void
114114

115115
public function testKeyboardWithDataArray(): void
116116
{
117-
$resize_keyboard = (bool) mt_rand(0, 1);
118-
$one_time_keyboard = (bool) mt_rand(0, 1);
119-
$selective = (bool) mt_rand(0, 1);
117+
$resize_keyboard = (bool) mt_rand(0, 1);
118+
$one_time_keyboard = (bool) mt_rand(0, 1);
119+
$input_field_placeholder = 'placeholder';
120+
$selective = (bool) mt_rand(0, 1);
120121

121122
$keyboard_obj = new Keyboard([
122-
'resize_keyboard' => $resize_keyboard,
123-
'one_time_keyboard' => $one_time_keyboard,
124-
'selective' => $selective,
125-
'keyboard' => [['Button Text 1']],
123+
'resize_keyboard' => $resize_keyboard,
124+
'one_time_keyboard' => $one_time_keyboard,
125+
'input_field_placeholder' => $input_field_placeholder,
126+
'selective' => $selective,
127+
'keyboard' => [['Button Text 1']],
126128
]);
127129

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

131133
self::assertSame($resize_keyboard, $keyboard_obj->getResizeKeyboard());
132134
self::assertSame($one_time_keyboard, $keyboard_obj->getOneTimeKeyboard());
135+
self::assertSame($input_field_placeholder, $keyboard_obj->getInputFieldPlaceholder());
133136
self::assertSame($selective, $keyboard_obj->getSelective());
134137
}
135138

0 commit comments

Comments
 (0)