Skip to content

Commit 7a1f346

Browse files
committed
apply changes to develnext-bundle
1 parent 7b038b0 commit 7a1f346

File tree

10 files changed

+372
-7
lines changed

10 files changed

+372
-7
lines changed

package.php.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ config:
3333
- /LICENSE
3434

3535
develnext-bundle:
36-
version: 1.0.0
36+
version: 1.1.0
3737
name: Telegram Bot API
3838
author: broelik
3939
icon: "develnext/bundle/telegram/icon32.png"

src-bundle/vendor/develnext.bundle.telegram.TelegramBotAPIBundle/telegram/TelegramBotApi.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use php\net\URLConnection;
1818
use telegram\exception\TelegramError;
1919
use telegram\exception\TelegramException;
20+
use telegram\query\TAnswerCallbackQuery;
2021
use telegram\query\TForwardMessageQuery;
2122
use telegram\query\TGetFileQuery;
2223
use telegram\query\TGetMeQuery;
@@ -163,6 +164,13 @@ function getUpdates(){
163164
return new TGetUpdatesQuery($this);
164165
}
165166

167+
/**
168+
* @return TAnswerCallbackQuery
169+
*/
170+
function answerCallbackQuery(){
171+
return new TAnswerCallbackQuery($this);
172+
}
173+
166174

167175
function setProxy(?Proxy $proxy){
168176
$this->proxy = $proxy;
@@ -196,10 +204,14 @@ function query($method, array $args = [], bool $multipart = false){
196204
else{
197205
$connection->getOutputStream()->write($this->json->format($args));
198206
}
207+
199208
if($connection->responseCode != 200){
200-
throw new TelegramException("Server response invalid status code {$connection->responseCode}");
209+
$rawResponse = $connection->getErrorStream()->readFully();
210+
if(strlen($rawResponse) == 0) throw new TelegramException("Server response invalid status code {$connection->responseCode}");
211+
} else {
212+
$rawResponse = $connection->getInputStream()->readFully();
201213
}
202-
$rawResponse = $connection->getInputStream()->readFully();
214+
203215
$connection->disconnect();
204216
$response = $this->json->parse($rawResponse);
205217
if(!$response->ok){
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: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
namespace telegram\object\markup;
3+
4+
use telegram\exception\TelegramException;
5+
6+
/**
7+
* --EN--
8+
* This object represents an inline keyboard that appears right next to the message it belongs to.
9+
*
10+
* --RU--
11+
* Отображает клавиатуру сразу под отправленным сообщением
12+
*
13+
* @see https://core.telegram.org/bots/api#inlinekeyboardmarkup
14+
*/
15+
class TInlineKeyboard extends TReplyKeyboard {
16+
/**
17+
* Callback data limit
18+
*/
19+
const MAX_CBDATA_LENGTH = 64;
20+
21+
/**
22+
* Добавить кнопку
23+
* @param string $text Текст кнопки
24+
* @param string $data Данные, которые будут возвращены при нажатии или ссылка для перехода
25+
* @return TInlineKeyboard
26+
*/
27+
public function button(string $text){
28+
$btn = ['text' => $text];
29+
30+
if(func_num_args() < 2){
31+
throw new TelegramException('Parameter callback_data required for InlineKeyboard buttons');
32+
} else {
33+
$data = func_get_arg(1);
34+
}
35+
36+
if(!is_string($data)){
37+
throw new TelegramException('Parameter callback_data should be a string');
38+
}
39+
40+
if(strpos($data, 'http:') === 0 || strpos($data, 'https:') === 0 || strpos($data, 'tg:') === 0){
41+
$btn['url'] = $data;
42+
} else {
43+
if(strlen($data) > self::MAX_CBDATA_LENGTH){
44+
throw new TelegramException('Parameter callback_data should be a string no more than ' . MAX_CBDATA_LENGTH . ' bytes in size');
45+
}
46+
47+
$btn['callback_data'] = $data;
48+
}
49+
50+
$this->buttons[$this->row][] = $btn;
51+
52+
return $this;
53+
}
54+
55+
public function getMarkup(): array {
56+
$markup = $this->params;
57+
$markup['inline_keyboard'] = $this->buttons;
58+
59+
return $markup;
60+
}
61+
}
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+
}

0 commit comments

Comments
 (0)