Add new switch_inline_query_current_chat option to inline keyboard.

(Based on 79c58a9bc29b75f5a311f471a61c47c2cdffea67)
parent be26b71d
......@@ -35,8 +35,12 @@ class InlinekeyboardCommand extends UserCommand
{
$chat_id = $this->getMessage()->getChat()->getId();
$switch_element = mt_rand(0, 9) < 5 ? 'true' : 'false';
$inline_keyboard = new InlineKeyboard([
['text' => 'inline', 'switch_inline_query' => 'true'],
['text' => 'inline', 'switch_inline_query' => $switch_element],
['text' => 'inline current chat', 'switch_inline_query_current_chat' => $switch_element],
], [
['text' => 'callback', 'callback_data' => 'identifier'],
['text' => 'open url', 'url' => 'https://github.com/akalongman/php-telegram-bot'],
]);
......
......@@ -17,15 +17,17 @@ use Longman\TelegramBot\Exception\TelegramException;
*
* @link https://core.telegram.org/bots/api#inlinekeyboardbutton
*
* @method string getText() Label text on the button
* @method string getUrl() Optional. HTTP url to be opened when button is pressed
* @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 getText() Label text on the button
* @method string getUrl() Optional. HTTP url to be opened when button is pressed
* @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.
*
* @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 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 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 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.
*/
class InlineKeyboardButton extends KeyboardButton
{
......@@ -39,11 +41,12 @@ class InlineKeyboardButton extends KeyboardButton
public static function couldBe($data)
{
return is_array($data) &&
array_key_exists('text', $data) && (
array_key_exists('text', $data) && (
array_key_exists('url', $data) ||
array_key_exists('callback_data', $data) ||
array_key_exists('switch_inline_query', $data)
);
array_key_exists('switch_inline_query', $data) ||
array_key_exists('switch_inline_query_current_chat', $data)
);
}
/**
......@@ -57,14 +60,14 @@ class InlineKeyboardButton extends KeyboardButton
$num_params = 0;
foreach (['url', 'callback_data', 'switch_inline_query'] as $param) {
foreach (['url', 'callback_data', 'switch_inline_query', 'switch_inline_query_current_chat'] as $param) {
if (!empty($this->getProperty($param))) {
$num_params++;
}
}
if ($num_params !== 1) {
throw new TelegramException('You must use only one of these fields: url, callback_data, switch_inline_query!');
throw new TelegramException('You must use only one of these fields: url, callback_data, switch_inline_query, switch_inline_query_current_chat!');
}
}
......@@ -74,8 +77,8 @@ class InlineKeyboardButton extends KeyboardButton
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'], true)) {
unset($this->url, $this->callback_data, $this->switch_inline_query);
if (in_array($method, ['setUrl', 'setCallbackData', 'setSwitchInlineQuery', 'setSwitchInlineQueryCurrentChat'], true)) {
unset($this->url, $this->callback_data, $this->switch_inline_query, $this->switch_inline_query_current_chat);
}
return parent::__call($method, $args);
......
......@@ -32,7 +32,7 @@ class InlineKeyboardButtonTest extends TestCase
/**
* @expectedException \Longman\TelegramBot\Exception\TelegramException
* @expectedExceptionMessage You must use only one of these fields: url, callback_data, switch_inline_query!
* @expectedExceptionMessage You must use only one of these fields: url, callback_data, switch_inline_query, switch_inline_query_current_chat!
*/
public function testInlineKeyboardButtonNoParameterFail()
{
......@@ -41,7 +41,7 @@ class InlineKeyboardButtonTest extends TestCase
/**
* @expectedException \Longman\TelegramBot\Exception\TelegramException
* @expectedExceptionMessage You must use only one of these fields: url, callback_data, switch_inline_query!
* @expectedExceptionMessage You must use only one of these fields: url, callback_data, switch_inline_query, switch_inline_query_current_chat!
*/
public function testInlineKeyboardButtonTooManyParametersFail()
{
......@@ -67,6 +67,13 @@ class InlineKeyboardButtonTest extends TestCase
'switch_inline_query' => 'switch_inline_query_value',
]);
},
function () {
new InlineKeyboardButton([
'text' => 'message',
'callback_data' => 'callback_data_value',
'switch_inline_query_current_chat' => 'switch_inline_query_current_chat_value',
]);
},
];
$test_funcs[array_rand($test_funcs)]();
......@@ -77,6 +84,7 @@ class InlineKeyboardButtonTest extends TestCase
new InlineKeyboardButton(['text' => 'message', 'url' => 'url_value']);
new InlineKeyboardButton(['text' => 'message', 'callback_data' => 'callback_data_value']);
new InlineKeyboardButton(['text' => 'message', 'switch_inline_query' => 'switch_inline_query_value']);
new InlineKeyboardButton(['text' => 'message', 'switch_inline_query_current_chat' => 'switch_inline_query_current_chat_value']);
}
public function testInlineKeyboardButtonCouldBe()
......@@ -90,6 +98,9 @@ class InlineKeyboardButtonTest extends TestCase
self::assertTrue(InlineKeyboardButton::couldBe(
['text' => 'message', 'switch_inline_query' => 'switch_inline_query_value']
));
self::assertTrue(InlineKeyboardButton::couldBe(
['text' => 'message', 'switch_inline_query_current_chat' => 'switch_inline_query_current_chat_value']
));
self::assertFalse(InlineKeyboardButton::couldBe(['no_text' => 'message']));
self::assertFalse(InlineKeyboardButton::couldBe(['text' => 'message']));
......@@ -101,9 +112,10 @@ class InlineKeyboardButtonTest extends TestCase
['switch_inline_query' => 'switch_inline_query_value']
));
self::assertFalse(InlineKeyboardButton::couldBe([
'url' => 'url_value',
'callback_data' => 'callback_data_value',
'switch_inline_query' => 'switch_inline_query_value',
'url' => 'url_value',
'callback_data' => 'callback_data_value',
'switch_inline_query' => 'switch_inline_query_value',
'switch_inline_query_current_chat' => 'switch_inline_query_current_chat_value',
]));
}
......@@ -113,15 +125,24 @@ class InlineKeyboardButtonTest extends TestCase
self::assertSame('url_value', $button->getUrl());
self::assertEmpty($button->getCallbackData());
self::assertEmpty($button->getSwitchInlineQuery());
self::assertEmpty($button->getSwitchInlineQueryCurrentChat());
$button->setCallbackData('callback_data_value');
self::assertEmpty($button->getUrl());
self::assertSame('callback_data_value', $button->getCallbackData());
self::assertEmpty($button->getSwitchInlineQuery());
self::assertEmpty($button->getSwitchInlineQueryCurrentChat());
$button->setSwitchInlineQuery('switch_inline_query_value');
self::assertEmpty($button->getUrl());
self::assertEmpty($button->getCallbackData());
self::assertSame('switch_inline_query_value', $button->getSwitchInlineQuery());
self::assertEmpty($button->getSwitchInlineQueryCurrentChat());
$button->setSwitchInlineQueryCurrentChat('switch_inline_query_current_chat_value');
self::assertEmpty($button->getUrl());
self::assertEmpty($button->getCallbackData());
self::assertEmpty($button->getSwitchInlineQuery());
self::assertSame('switch_inline_query_current_chat_value', $button->getSwitchInlineQueryCurrentChat());
}
}
......@@ -24,7 +24,7 @@ class InlineKeyboardTest extends TestCase
{
private function getRandomButton($text)
{
$random_params = ['url', 'callback_data', 'switch_inline_query'];
$random_params = ['url', 'callback_data', 'switch_inline_query', 'switch_inline_query_current_chat'];
$param = $random_params[array_rand($random_params, 1)];
$data = [
'text' => $text,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment