1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace TelegramBot\Entities; |
5
|
|
|
|
6
|
|
|
use TelegramBot\Entity; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Keyboard |
10
|
|
|
* |
11
|
|
|
* @link https://core.telegram.org/bots/api#replykeyboardmarkup |
12
|
|
|
* |
13
|
|
|
* @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. |
14
|
|
|
* @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. |
15
|
|
|
* @method string getInputFieldPlaceholder() Optional. The placeholder to be shown in the input field when the keyboard is active; |
16
|
|
|
* @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; |
17
|
|
|
* |
18
|
|
|
* @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. |
19
|
|
|
* @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. |
20
|
|
|
* @method $this setInputFieldPlaceholder(string $input_field_placeholder) Optional. The placeholder to be shown in the input field when the keyboard is active; |
21
|
|
|
* @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; |
22
|
|
|
*/ |
23
|
|
|
class Keyboard extends Entity |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
public function __construct(array $data = []) |
27
|
|
|
{ |
28
|
|
|
parent::__construct(array_merge($data, [ |
29
|
|
|
'resize_keyboard' => false, |
30
|
|
|
'one_time_keyboard' => false, |
31
|
|
|
'input_field_placeholder' => null, |
32
|
|
|
'selective' => false |
33
|
|
|
])); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Create a new row in keyboard and add buttons. |
38
|
|
|
* |
39
|
|
|
* @param array<KeyboardButton> $row |
40
|
|
|
* @return Keyboard |
41
|
|
|
*/ |
42
|
|
|
public function addRow(array $row): Keyboard |
43
|
|
|
{ |
44
|
|
|
$keyboard_type = self::getType(); |
|
|
|
|
45
|
|
|
|
46
|
|
|
if (!isset($this->raw_data[$keyboard_type]) || !is_array($this->raw_data[$keyboard_type])) { |
47
|
|
|
$this->raw_data[$keyboard_type] = []; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$new_row = []; |
51
|
|
|
foreach ($row as $button) { |
52
|
|
|
$new_row[] = $button->getRawData(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$this->raw_data[$keyboard_type][] = $new_row; |
56
|
|
|
|
57
|
|
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Remove the current custom keyboard and display the default letter-keyboard. |
62
|
|
|
* |
63
|
|
|
* @link https://core.telegram.org/bots/api/#replykeyboardremove |
64
|
|
|
* |
65
|
|
|
* @param array $data |
66
|
|
|
* |
67
|
|
|
* @return Keyboard |
68
|
|
|
*/ |
69
|
|
|
public static function remove(array $data = []): Keyboard |
70
|
|
|
{ |
71
|
|
|
return new static(array_merge(['keyboard' => [], 'remove_keyboard' => true, 'selective' => false], $data)); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Display a reply interface to the user (act as if the user has selected the bot's message and tapped 'Reply'). |
76
|
|
|
* |
77
|
|
|
* @link https://core.telegram.org/bots/api#forcereply |
78
|
|
|
* |
79
|
|
|
* @param array $data |
80
|
|
|
* |
81
|
|
|
* @return Keyboard |
82
|
|
|
*/ |
83
|
|
|
public static function forceReply(array $data = []): Keyboard |
84
|
|
|
{ |
85
|
|
|
return new static(array_merge(['keyboard' => [], 'force_reply' => true, 'selective' => false], $data)); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get keyboard button type |
90
|
|
|
* |
91
|
|
|
* @return string ["keyboard"|"inline_keyboard"] |
92
|
|
|
*/ |
93
|
|
|
public function getType(): string |
94
|
|
|
{ |
95
|
|
|
$reflection = new \ReflectionClass(static::class); |
96
|
|
|
|
97
|
|
|
$class_name = $reflection->getShortName(); |
98
|
|
|
|
99
|
|
|
return strtolower(ltrim(preg_replace('/[A-Z]/', '_$0', $class_name), '_')); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Creates instance of Keyboard |
104
|
|
|
* |
105
|
|
|
* @return Keyboard |
106
|
|
|
*/ |
107
|
|
|
public static function make(): Keyboard |
108
|
|
|
{ |
109
|
|
|
return new self(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param array $rows |
114
|
|
|
* @return Keyboard |
115
|
|
|
*/ |
116
|
|
|
public function setKeyboard(array $rows): Keyboard |
117
|
|
|
{ |
118
|
|
|
foreach ($rows as $row) { |
119
|
|
|
$this->addRow($row); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
} |
126
|
|
|
|