Skip to content

Commit a6003ed

Browse files
committed
Reply markup features
1 parent fffac70 commit a6003ed

File tree

7 files changed

+258
-4
lines changed

7 files changed

+258
-4
lines changed

src/telegram/object/TMarkup.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
namespace telegram\object;
3+
4+
use telegram\object\markup\TForceReply;
5+
use telegram\object\markup\TInlineKeyboard;
6+
use telegram\object\markup\TReplyKeyboard;
7+
use telegram\object\markup\TReplyKeyboardRemove;
8+
9+
class TMarkup
10+
{
11+
/**
12+
* @return TReplyKeyboard
13+
*/
14+
public static function replyKeyboard(){
15+
return new TReplyKeyboard;
16+
}
17+
18+
/**
19+
* @return TInlineKeyboard
20+
*/
21+
public static function inlineKeyboard(){
22+
return new TInlineKeyboard;
23+
}
24+
25+
/**
26+
* @return TForceReply
27+
*/
28+
public static function forceReply(){
29+
return new TForceReply;
30+
}
31+
32+
/**
33+
* @return TReplyKeyboardRemove
34+
*/
35+
public static function removeKeyboard(){
36+
return new TReplyKeyboardRemove;
37+
}
38+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
namespace telegram\object\markup;
3+
4+
class AbstractMarkup {
5+
/**
6+
* @var array
7+
*/
8+
protected $markup = [];
9+
10+
public function getMarkup(): array {
11+
return $this->markup;
12+
}
13+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
namespace telegram\object\markup;
3+
4+
/**
5+
* --EN--
6+
* Shows reply interface to the user, as if they manually selected the bot‘s message and tapped 'Reply'
7+
*
8+
* @see https://core.telegram.org/bots/api#forcereply
9+
*/
10+
class TForceReply extends AbstractMarkup {
11+
public function __construct(){
12+
$this->markup['force_reply'] = true;
13+
}
14+
15+
/**
16+
* --EN--
17+
* Use this parameter if you want to force reply from 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.
18+
*
19+
* @param bool $value
20+
*/
21+
public function setSelective(bool $value){
22+
$this->markup['selective'] = $value;
23+
return $this;
24+
}
25+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
namespace telegram\object\markup;
3+
4+
/**
5+
* --EN--
6+
* This object represents an inline keyboard that appears right next to the message it belongs to.
7+
*
8+
* --RU--
9+
* Отображает клавиатуру сразу под отправленным сообщением
10+
*
11+
* @see https://core.telegram.org/bots/api#inlinekeyboardmarkup
12+
*/
13+
class TInlineKeyboard extends TReplyKeyboard {
14+
/**
15+
* Добавить кнопку
16+
* @param string $text Текст кнопки
17+
* @param string $data Данные, которые будут возвращены при нажатии или ссылка для перехода
18+
* @return TInlineKeyboard
19+
*/
20+
public function button(string $text, string $data){
21+
$btn = ['text' => $text];
22+
23+
if(strpos($data, 'http:') === 0 || strpos($data, 'https:') === 0){
24+
$btn['url'] = $data;
25+
} else {
26+
$btn['callback_data'] = $data;
27+
}
28+
29+
$this->buttons[$this->row][] = $btn;
30+
31+
return $this;
32+
}
33+
34+
public function getMarkup(): array {
35+
$markup = $this->params;
36+
$markup['inline_keyboard'] = $this->buttons;
37+
38+
return $markup;
39+
}
40+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
namespace telegram\object\markup;
3+
4+
/**
5+
* --EN--
6+
* This object represents a custom keyboard with reply options
7+
*
8+
* --RU--
9+
* Отображает клавиатуру возле поля для ввода сообщения
10+
*
11+
* @see https://core.telegram.org/bots/api#replykeyboardmarkup
12+
*/
13+
class TReplyKeyboard extends AbstractMarkup {
14+
15+
/**
16+
* @var array
17+
*/
18+
protected $params = [];
19+
20+
/**
21+
* Кнопки
22+
* @var array
23+
*/
24+
protected $buttons = [];
25+
26+
/**
27+
* Указатель текущей строки в кнопках
28+
* @var int
29+
*/
30+
protected $row = -1;
31+
32+
public function __construct(array $params = []){
33+
$this->params = $params;
34+
$this->row();
35+
}
36+
37+
/**
38+
* Добавить новую строку в панель кнопок
39+
* @return TReplyKeyboard
40+
*/
41+
public function row(){
42+
$this->row++;
43+
if(!isset($this->buttons[$this->row])) $this->buttons[$this->row] = [];
44+
45+
return $this;
46+
}
47+
48+
/**
49+
* Добавить кнопку
50+
* @param string $text Текст кнопки
51+
* @return TReplyKeyboard
52+
*/
53+
public function button(string $text){
54+
$this->buttons[$this->row][] = ['text' => $text];
55+
56+
return $this;
57+
}
58+
59+
/**
60+
* Указывает клиенту подогнать высоту клавиатуры под количество кнопок
61+
* (сделать её меньше, если кнопок мало и наоборот)
62+
* @param bool $value
63+
* @return TReplyKeyboard
64+
*/
65+
public function setResize(bool $value){
66+
$this->params['resize_keyboard'] = $value;
67+
return $this;
68+
}
69+
70+
/**
71+
* Указывает клиенту скрыть клавиатуру после использования (после нажатия на кнопку).
72+
* Её по-прежнему можно будет открыть через иконку в поле ввода сообщения.
73+
* @param bool $value
74+
* @return TReplyKeyboard
75+
*/
76+
public function setOneTime(bool $value){
77+
$this->params['one_time_keyboard'] = $value;
78+
return $this;
79+
}
80+
81+
/**
82+
* @param bool $value
83+
* @return TReplyKeyboard
84+
*/
85+
public function setSelective(bool $value){
86+
$this->params['selective'] = $value;
87+
return $this;
88+
}
89+
90+
/**
91+
* @return array
92+
*/
93+
public function getMarkup(): array {
94+
$markup = $this->params;
95+
$markup['keyboard'] = $this->buttons;
96+
97+
return $markup;
98+
}
99+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
namespace telegram\object\markup;
3+
4+
/**
5+
* --EN--
6+
* Upon receiving a message with this object, Telegram clients will remove the current custom keyboard and display the default letter-keyboard. By default, custom keyboards are displayed until a new keyboard is sent by a bot. An exception is made for one-time keyboards that are hidden immediately after the user presses a button
7+
*
8+
* @see https://core.telegram.org/bots/api#replykeyboardremove
9+
*/
10+
class TReplyKeyboardRemove extends AbstractMarkup {
11+
public function __construct(){
12+
$this->markup['remove_keyboard'] = true;
13+
}
14+
15+
/**
16+
* --EN--
17+
* Use this parameter if you want to force reply from 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.
18+
*
19+
* @param bool $value
20+
*/
21+
public function setSelective(bool $value){
22+
$this->markup['selective'] = $value;
23+
return $this;
24+
}
25+
}

src/telegram/query/TSendMessageQuery.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
namespace telegram\query;
55

66

7-
use telegram\object\TMessage;
87
use telegram\TelegramBotApi;
8+
use telegram\exception\TelegramException;
9+
use telegram\object\TMessage;
10+
use telegram\object\markup\AbstractMarkup;
911

1012
class TSendMessageQuery extends TBaseQuery{
1113
public function __construct(TelegramBotApi $api){
@@ -47,11 +49,23 @@ public function disable_notification($value){
4749
public function reply_to_message_id($value){
4850
return $this->put(__FUNCTION__, (int)$value);
4951
}
50-
/*
52+
53+
/**
54+
* @param array|TAbstractMarkup
55+
* @return TSendMessageQuery
56+
*/
5157
public function reply_markup($value){
52-
return $this->put(__FUNCTION__, $value);
58+
if(is_array($value)){
59+
return $this->put(__FUNCTION__, $value);
60+
}
61+
elseif($value instanceof AbstractMarkup){
62+
return $this->put(__FUNCTION__, $value->getMarkup());
63+
}
64+
else {
65+
throw new TelegramException('Invalid reply_markup parameter');
66+
}
5367
}
54-
*/
68+
5569
/**
5670
* @return TMessage
5771
*/

0 commit comments

Comments
 (0)