1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the TelegramBot package. |
5
|
|
|
* |
6
|
|
|
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Longman\TelegramBot\Entities; |
13
|
|
|
|
14
|
|
|
use Longman\TelegramBot\Exception\TelegramException; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class KeyboardButton |
18
|
|
|
* |
19
|
|
|
* This object represents one button of the reply keyboard. For simple text buttons String can be used instead of this object to specify text of the button. Optional fields request_contact, request_location, and request_poll are mutually exclusive. |
20
|
|
|
* |
21
|
|
|
* @link https://core.telegram.org/bots/api#keyboardbutton |
22
|
|
|
* |
23
|
|
|
* @property bool $request_contact |
24
|
|
|
* @property bool $request_location |
25
|
|
|
* @property KeyboardButtonPollType $request_poll |
26
|
|
|
* @property WebAppInfo $web_app |
27
|
|
|
* |
28
|
|
|
* @method string getText() Text of the button. If none of the optional fields are used, it will be sent to the bot as a message when the button is pressed |
29
|
|
|
* @method bool getRequestContact() Optional. If True, the user's phone number will be sent as a contact when the button is pressed. Available in private chats only |
30
|
|
|
* @method bool getRequestLocation() Optional. If True, the user's current location will be sent when the button is pressed. Available in private chats only |
31
|
|
|
* @method KeyboardButtonPollType getRequestPoll() Optional. If specified, the user will be asked to create a poll and send it to the bot when the button is pressed. Available in private chats only |
32
|
|
|
* @method WebAppInfo getWebApp() Optional. If specified, the described Web App will be launched when the button is pressed. The Web App will be able to send a “web_app_data” service message. Available in private chats only. |
33
|
|
|
* |
34
|
|
|
* @method $this setText(string $text) Text of the button. If none of the optional fields are used, it will be sent to the bot as a message when the button is pressed |
35
|
|
|
* @method $this setRequestContact(bool $request_contact) Optional. If True, the user's phone number will be sent as a contact when the button is pressed. Available in private chats only |
36
|
|
|
* @method $this setRequestLocation(bool $request_location) Optional. If True, the user's current location will be sent when the button is pressed. Available in private chats only |
37
|
|
|
* @method $this setRequestPoll(KeyboardButtonPollType $request_poll) Optional. If specified, the user will be asked to create a poll and send it to the bot when the button is pressed. Available in private chats only |
38
|
|
|
* @method $this setWebApp(WebAppInfo $web_app) Optional. If specified, the described Web App will be launched when the button is pressed. The Web App will be able to send a “web_app_data” service message. Available in private chats only. |
39
|
|
|
*/ |
40
|
|
|
class KeyboardButton extends Entity |
41
|
|
|
{ |
42
|
21 |
|
/** |
43
|
|
|
* @param array|string $data |
44
|
21 |
|
*/ |
45
|
8 |
|
public function __construct($data) |
46
|
|
|
{ |
47
|
21 |
|
if (is_string($data)) { |
48
|
16 |
|
$data = ['text' => $data]; |
49
|
|
|
} |
50
|
|
|
parent::__construct($data); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
protected function subEntities(): array |
54
|
|
|
{ |
55
|
|
|
return [ |
56
|
|
|
'request_poll' => KeyboardButtonPollType::class, |
57
|
1 |
|
'web_app' => WebAppInfo::class, |
58
|
|
|
]; |
59
|
1 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Check if the passed data array could be a KeyboardButton. |
63
|
|
|
* |
64
|
|
|
* @param array $data |
65
|
11 |
|
* |
66
|
|
|
* @return bool |
67
|
11 |
|
*/ |
68
|
1 |
|
public static function couldBe(array $data): bool |
69
|
|
|
{ |
70
|
|
|
return array_key_exists('text', $data); |
71
|
|
|
} |
72
|
10 |
|
|
73
|
10 |
|
/** |
74
|
10 |
|
* {@inheritdoc} |
75
|
10 |
|
*/ |
76
|
|
|
protected function validate(): void |
77
|
10 |
|
{ |
78
|
1 |
|
if ($this->getProperty('text', '') === '') { |
79
|
|
|
throw new TelegramException('You must add some text to the button!'); |
80
|
9 |
|
} |
81
|
|
|
|
82
|
|
|
// Make sure only 1 of the optional request fields is set. |
83
|
|
|
$field_count = array_filter([ |
84
|
|
|
$this->getRequestContact(), |
85
|
16 |
|
$this->getRequestLocation(), |
86
|
|
|
$this->getRequestPoll(), |
87
|
|
|
$this->getWebApp(), |
88
|
16 |
|
]); |
89
|
1 |
|
if (count($field_count) > 1) { |
90
|
|
|
throw new TelegramException('You must use only one of these fields: request_contact, request_location, request_poll, web_app!'); |
91
|
|
|
} |
92
|
16 |
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritdoc} |
96
|
|
|
*/ |
97
|
|
|
public function __call($method, $args) |
98
|
|
|
{ |
99
|
|
|
// Only 1 of these can be set, so clear the others when setting a new one. |
100
|
|
|
if (in_array($method, ['setRequestContact', 'setRequestLocation', 'setRequestPoll', 'setWebApp'], true)) { |
101
|
|
|
unset($this->request_contact, $this->request_location, $this->request_poll, $this->web_app); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return parent::__call($method, $args); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|