Add new inline keyboard button for payments.

parent 43b57572
......@@ -8,7 +8,7 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c
- Documents can be sent by providing its contents via Psr7 stream (as opposed to passing a file path).
- Allow setting a custom Guzzle HTTP Client for requests (#511).
- First implementations towards Bots API 3.0.
- New entities, methods and update types for Payments.
- New entities, methods, update types and inline keyboard button for Payments.
### Changed
- [:exclamation:][unreleased-bc-chats-params-array] `Request::sendToActiveChats` and `DB::selectChats` now accept parameters as an options array.
### Deprecated
......
......@@ -22,12 +22,14 @@ use Longman\TelegramBot\Exception\TelegramException;
* @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 string getPay() Optional. Specify True, to send a Pay button.
*
* @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.
* @method $this setPay(bool $pay) Optional. Specify True, to send a Pay button.
*/
class InlineKeyboardButton extends KeyboardButton
{
......@@ -45,7 +47,8 @@ class InlineKeyboardButton extends KeyboardButton
array_key_exists('url', $data) ||
array_key_exists('callback_data', $data) ||
array_key_exists('switch_inline_query', $data) ||
array_key_exists('switch_inline_query_current_chat', $data)
array_key_exists('switch_inline_query_current_chat', $data) ||
array_key_exists('pay', $data)
);
}
......@@ -60,14 +63,14 @@ class InlineKeyboardButton extends KeyboardButton
$num_params = 0;
foreach (['url', 'callback_data', 'switch_inline_query', 'switch_inline_query_current_chat'] as $param) {
foreach (['url', 'callback_data', 'switch_inline_query', 'switch_inline_query_current_chat', 'pay'] 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, switch_inline_query_current_chat!');
throw new TelegramException('You must use only one of these fields: url, callback_data, switch_inline_query, switch_inline_query_current_chat, pay!');
}
}
......@@ -77,8 +80,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', 'setSwitchInlineQueryCurrentChat'], true)) {
unset($this->url, $this->callback_data, $this->switch_inline_query, $this->switch_inline_query_current_chat);
if (in_array($method, ['setUrl', 'setCallbackData', 'setSwitchInlineQuery', 'setSwitchInlineQueryCurrentChat', 'setPay'], true)) {
unset($this->url, $this->callback_data, $this->switch_inline_query, $this->switch_inline_query_current_chat, $this->pay);
}
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, switch_inline_query_current_chat!
* @expectedExceptionMessage You must use only one of these fields: url, callback_data, switch_inline_query, switch_inline_query_current_chat, pay!
*/
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, switch_inline_query_current_chat!
* @expectedExceptionMessage You must use only one of these fields: url, callback_data, switch_inline_query, switch_inline_query_current_chat, pay!
*/
public function testInlineKeyboardButtonTooManyParametersFail()
{
......@@ -74,6 +74,13 @@ class InlineKeyboardButtonTest extends TestCase
'switch_inline_query_current_chat' => 'switch_inline_query_current_chat_value',
]);
},
function () {
new InlineKeyboardButton([
'text' => 'message',
'callback_data' => 'callback_data_value',
'pay' => true,
]);
},
];
$test_funcs[array_rand($test_funcs)]();
......@@ -85,6 +92,7 @@ class InlineKeyboardButtonTest extends TestCase
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']);
new InlineKeyboardButton(['text' => 'message', 'pay' => true]);
}
public function testInlineKeyboardButtonCouldBe()
......@@ -101,6 +109,9 @@ class InlineKeyboardButtonTest extends TestCase
self::assertTrue(InlineKeyboardButton::couldBe(
['text' => 'message', 'switch_inline_query_current_chat' => 'switch_inline_query_current_chat_value']
));
self::assertTrue(InlineKeyboardButton::couldBe(
['text' => 'message', 'pay' => true]
));
self::assertFalse(InlineKeyboardButton::couldBe(['no_text' => 'message']));
self::assertFalse(InlineKeyboardButton::couldBe(['text' => 'message']));
......@@ -111,11 +122,14 @@ class InlineKeyboardButtonTest extends TestCase
self::assertFalse(InlineKeyboardButton::couldBe(
['switch_inline_query' => 'switch_inline_query_value']
));
self::assertFalse(InlineKeyboardButton::couldBe(['pay' => true]));
self::assertFalse(InlineKeyboardButton::couldBe([
'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',
'pay' => true,
]));
}
......@@ -126,23 +140,34 @@ class InlineKeyboardButtonTest extends TestCase
self::assertEmpty($button->getCallbackData());
self::assertEmpty($button->getSwitchInlineQuery());
self::assertEmpty($button->getSwitchInlineQueryCurrentChat());
self::assertEmpty($button->getPay());
$button->setCallbackData('callback_data_value');
self::assertEmpty($button->getUrl());
self::assertSame('callback_data_value', $button->getCallbackData());
self::assertEmpty($button->getSwitchInlineQuery());
self::assertEmpty($button->getSwitchInlineQueryCurrentChat());
self::assertEmpty($button->getPay());
$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());
self::assertEmpty($button->getPay());
$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());
self::assertEmpty($button->getPay());
$button->setPay(true);
self::assertEmpty($button->getUrl());
self::assertEmpty($button->getCallbackData());
self::assertEmpty($button->getSwitchInlineQuery());
self::assertEmpty($button->getSwitchInlineQueryCurrentChat());
self::assertSame(true, $button->getPay());
}
}
......@@ -24,7 +24,7 @@ class InlineKeyboardTest extends TestCase
{
private function getRandomButton($text)
{
$random_params = ['url', 'callback_data', 'switch_inline_query', 'switch_inline_query_current_chat'];
$random_params = ['url', 'callback_data', 'switch_inline_query', 'switch_inline_query_current_chat', 'pay'];
$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