Skip to content

Commit 066901e

Browse files
authored
Merge pull request #62 from Uterok/feature/web-app-buttons
Added menu button and web app components
2 parents d607373 + 7595fc9 commit 066901e

11 files changed

+306
-1
lines changed

CHANGELOG.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,24 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip
2323
- Nothing
2424
--->
2525

26-
## Unreleased -
26+
## 1.8.0 - 2022-05-18
27+
28+
### Added
29+
- #### WebApp
30+
- Added class `WebAppInfoType`. Classes `InlineKeyboardButtonType` and `KeyboardButtonType` extended with parameter `webApp`;
31+
32+
- #### MenuButton
33+
- Added class `MenuButtonType`;
34+
- Added classes `GetChatMenuButtonMethod` and `SetChatMenuButtonMethod` for setting chat menu button and getting info about it;
35+
- Added class `SetChatMenuButtonNormalizer` for normalize data from `SetChatMenuButtonMethod` before making request;
36+
37+
38+
## 1.7.1 - 2022-05-18
2739

2840
### Added
2941
- Supported for symfony 6.x components
3042

43+
3144
## 1.7.0 - 2020-12-12
3245
#### Bot API 5.0 - november November 4, 2020
3346

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ $bot = new \TgBotApi\BotApiBase\BotApi('<bot key>', $apiClient, new \TgBotApi\Bo
8787
|`getChat`|GetChatMethod|ChatType|
8888
|`getChatAdministrators`|GetChatAdministratorsMethod|ChatMemberType[]|
8989
|`getChatMember`|GetChatMemberMethod|ChatMemberType|
90+
|`getChatMenuButton`|GetChatMenuButtonMethod|MenuButtonType|
9091
|`getGameHighScores`|GetGameHighScoresMethod|GameHighScoreType[]|
9192
|`getStickerSet`|GetStickerSetMethod|StickerSetType|
9293
|`getFile`|GetFileMethod|FileType|

src/BotApiNormalizer.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use TgBotApi\BotApiBase\Normalizer\PollNormalizer;
2525
use TgBotApi\BotApiBase\Normalizer\SetMyCommandsNormalizer;
2626
use TgBotApi\BotApiBase\Normalizer\UserProfilePhotosNormalizer;
27+
use TgBotApi\BotApiBase\Normalizer\SetChatMenuButtonNormalizer;
2728

2829
/**
2930
* Class BotApiNormalizer.
@@ -85,6 +86,7 @@ public function normalize($method): BotApiRequestInterface
8586
new EditMessageMediaNormalizer(new InputMediaNormalizer($objectNormalizer, $files), $objectNormalizer),
8687
new JsonSerializableNormalizer($objectNormalizer),
8788
new AnswerInlineQueryNormalizer($objectNormalizer),
89+
new SetChatMenuButtonNormalizer($objectNormalizer),
8890
new DateTimeNormalizer(),
8991
$objectNormalizer,
9092
]);
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TgBotApi\BotApiBase\Method;
6+
7+
use TgBotApi\BotApiBase\Method\Interfaces\MethodInterface;
8+
use TgBotApi\BotApiBase\Method\Traits\FillFromArrayTrait;
9+
10+
/**
11+
* Class GetChatMenuButtonMethod.
12+
*
13+
* Use this method to get the current value of the bot's menu button in a private chat,
14+
* or the default menu button. Returns MenuButton on success.
15+
*
16+
* @see https://core.telegram.org/bots/api#getchatmenubutton
17+
*/
18+
class GetChatMenuButtonMethod implements MethodInterface
19+
{
20+
use FillFromArrayTrait;
21+
22+
/**
23+
* Optional. Unique identifier for the target private chat. If not specified, default bot's menu button will be returned.
24+
*
25+
* @var int|null
26+
*/
27+
public $chatId;
28+
29+
/**
30+
* GetChatMenuButtonMethod constructor.
31+
*
32+
* @param array|null $data
33+
*
34+
* @throws \TgBotApi\BotApiBase\Exception\BadArgumentException
35+
*
36+
* @return GetChatMenuButtonMethod
37+
*/
38+
public static function create(array $data = null): GetChatMenuButtonMethod
39+
{
40+
$instance = new static();
41+
if ($data) {
42+
$instance->fill($data);
43+
}
44+
45+
return $instance;
46+
}
47+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TgBotApi\BotApiBase\Method;
6+
7+
use TgBotApi\BotApiBase\Method\Interfaces\SetMethodAliasInterface;
8+
use TgBotApi\BotApiBase\Method\Traits\FillFromArrayTrait;
9+
use TgBotApi\BotApiBase\Type\MenuButtonType;
10+
11+
/**
12+
* Class SetChatMenuButtonMethod.
13+
*
14+
* Use this method to change the bot's menu button in a private chat, or the default menu button.
15+
* Returns True on success.
16+
*
17+
* @see https://core.telegram.org/bots/api#setchatmenubutton
18+
*/
19+
class SetChatMenuButtonMethod implements SetMethodAliasInterface
20+
{
21+
use FillFromArrayTrait;
22+
23+
/**
24+
* Unique identifier for the target private chat. If not specified, default bot's menu
25+
* button will be changed
26+
*
27+
* @var integer|null
28+
*/
29+
public $chatId;
30+
31+
/**
32+
* Optional. A JSON-serialized object for the bot's new menu button.
33+
* Defaults to 'default' type
34+
*
35+
* @var MenuButtonType|null
36+
*/
37+
public $menuButton;
38+
39+
40+
/**
41+
* SetChatMenuButtonMethod constructor.
42+
*
43+
* @param array|null $data
44+
*
45+
* @throws \TgBotApi\BotApiBase\Exception\BadArgumentException
46+
*
47+
* @return SetChatMenuButtonMethod
48+
*/
49+
public static function create(array $data = null): SetChatMenuButtonMethod
50+
{
51+
$instance = new static();
52+
if ($data) {
53+
$instance->fill($data);
54+
}
55+
56+
return $instance;
57+
}
58+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TgBotApi\BotApiBase\Normalizer;
6+
7+
use Symfony\Component\Serializer\Exception\ExceptionInterface;
8+
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
9+
use Symfony\Component\Serializer\Serializer;
10+
use TgBotApi\BotApiBase\Method\SetChatMenuButtonMethod;
11+
12+
/**
13+
* Class SetChatMenuButtonNormalizer.
14+
*/
15+
class SetChatMenuButtonNormalizer implements NormalizerInterface
16+
{
17+
/**
18+
* @var NormalizerInterface
19+
*/
20+
private $objectNormalizer;
21+
22+
/**
23+
* JsonSerializableNormalizer constructor.
24+
*/
25+
public function __construct(NormalizerInterface $objectNormalizer)
26+
{
27+
$this->objectNormalizer = $objectNormalizer;
28+
}
29+
30+
/**
31+
* @param SetChatMenuButtonMethod $topic
32+
* @param null $format
33+
*
34+
* @throws ExceptionInterface
35+
*
36+
* @return array|bool|false|float|int|string
37+
*/
38+
public function normalize($topic, $format = null, array $context = [])
39+
{
40+
$serializer = new Serializer([
41+
new JsonSerializableNormalizer($this->objectNormalizer),
42+
$this->objectNormalizer,
43+
]);
44+
45+
$topic->menuButton = \json_encode($serializer->normalize($topic->menuButton, null, ['skip_null_values' => true]));
46+
47+
return $serializer->normalize($topic, null, ['skip_null_values' => true]);
48+
}
49+
50+
/**
51+
* @param mixed $data
52+
* @param null $format
53+
*/
54+
public function supportsNormalization($data, $format = null): bool
55+
{
56+
return $data instanceof SetChatMenuButtonMethod;
57+
}
58+
}

src/Traits/GetMethodTrait.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use TgBotApi\BotApiBase\Method\GetChatMemberMethod;
1010
use TgBotApi\BotApiBase\Method\GetChatMembersCountMethod;
1111
use TgBotApi\BotApiBase\Method\GetChatMethod;
12+
use TgBotApi\BotApiBase\Method\GetChatMenuButtonMethod;
1213
use TgBotApi\BotApiBase\Method\GetFileMethod;
1314
use TgBotApi\BotApiBase\Method\GetGameHighScoresMethod;
1415
use TgBotApi\BotApiBase\Method\GetMeMethod;
@@ -23,6 +24,7 @@
2324
use TgBotApi\BotApiBase\Type\ChatType;
2425
use TgBotApi\BotApiBase\Type\FileType;
2526
use TgBotApi\BotApiBase\Type\GameHighScoreType;
27+
use TgBotApi\BotApiBase\Type\MenuButtonType;
2628
use TgBotApi\BotApiBase\Type\StickerSetType;
2729
use TgBotApi\BotApiBase\Type\UpdateType;
2830
use TgBotApi\BotApiBase\Type\UserProfilePhotosType;
@@ -118,6 +120,14 @@ public function getChatMember(GetChatMemberMethod $method): ChatMemberType
118120
return $this->call($method, ChatMemberType::class);
119121
}
120122

123+
/**
124+
* @throws ResponseException
125+
*/
126+
public function getChatMenuButton(GetChatMenuButtonMethod $method): MenuButtonType
127+
{
128+
return $this->call($method, MenuButtonType::class);
129+
}
130+
121131
/**
122132
* @throws ResponseException
123133
*

src/Type/InlineKeyboardButtonType.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,15 @@ class InlineKeyboardButtonType
8686
*/
8787
public $pay;
8888

89+
/**
90+
* Optional. Description of the Web App that will be launched when the user presses the button.
91+
* The Web App will be able to send an arbitrary message on behalf of the user using the method answerWebAppQuery.
92+
* Available only in private chats between a user and the bot.
93+
*
94+
* @var WebAppInfoType|null
95+
*/
96+
public $webApp;
97+
8998
/**
9099
* @param string $text
91100
* @param array|null $data

src/Type/KeyboardButtonType.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ class KeyboardButtonType
5151
*/
5252
public $requestPoll;
5353

54+
/**
55+
* Optional. If specified, the described Web App will be launched when the button is pressed.
56+
* The Web App will be able to send a “web_app_data” service message. Available in private chats only.
57+
*
58+
* @var WebAppInfoType|null
59+
*/
60+
public $webApp;
61+
5462
/**
5563
* @throws BadArgumentException
5664
*/

src/Type/MenuButtonType.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TgBotApi\BotApiBase\Type;
6+
7+
use TgBotApi\BotApiBase\Exception\BadArgumentException;
8+
use TgBotApi\BotApiBase\Method\Traits\FillFromArrayTrait;
9+
10+
/**
11+
* Class MenuButtonType.
12+
*
13+
* @see https://core.telegram.org/bots/api#menubutton
14+
*/
15+
class MenuButtonType
16+
{
17+
use FillFromArrayTrait;
18+
19+
public const TYPE_COMMANDS = 'commands';
20+
public const TYPE_WEB_APP = 'web_app';
21+
public const TYPE_DEFAULT = 'default';
22+
23+
/**
24+
* Type of the button must be one of the list: commands, web_app, default
25+
*
26+
* @var string
27+
*/
28+
public $type;
29+
30+
/**
31+
* Text on the button. Required for type 'web_app'
32+
*
33+
* @var string|null
34+
*/
35+
public $text;
36+
37+
/**
38+
* Description of the Web App that will be launched when the user presses the button. Required for type 'web_app'
39+
*
40+
* @var WebAppInfoType|null
41+
*/
42+
public $webApp;
43+
44+
/**
45+
* @param string $type
46+
* @param array|null $data
47+
*
48+
* @throws BadArgumentException
49+
*
50+
* @return MenuButtonType
51+
*/
52+
public static function create(string $type, array $data = null): MenuButtonType
53+
{
54+
$instance = new static();
55+
$instance->type = $type;
56+
if ($data) {
57+
$instance->fill($data);
58+
}
59+
60+
return $instance;
61+
}
62+
}

src/Type/WebAppInfoType.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TgBotApi\BotApiBase\Type;
6+
7+
use TgBotApi\BotApiBase\Exception\BadArgumentException;
8+
9+
/**
10+
* Class MenuButtonType.
11+
*
12+
* @see https://core.telegram.org/bots/api#webappinfo
13+
*/
14+
class WebAppInfoType
15+
{
16+
/**
17+
* An HTTPS URL of a Web App to be opened with additional data as specified in Initializing Web Apps
18+
*
19+
* @var string
20+
*/
21+
public $url;
22+
23+
/**
24+
* @param string $url
25+
*
26+
* @throws BadArgumentException
27+
*
28+
* @return WebAppInfoType
29+
*/
30+
public static function create(string $url): WebAppInfoType
31+
{
32+
$instance = new static();
33+
$instance->url = $url;
34+
35+
return $instance;
36+
}
37+
}

0 commit comments

Comments
 (0)