Skip to content

Commit

Permalink
Merge pull request #62 from Uterok/feature/web-app-buttons
Browse files Browse the repository at this point in the history
Added menu button and web app components
  • Loading branch information
greenplugin authored May 17, 2022
2 parents d607373 + 7595fc9 commit 066901e
Show file tree
Hide file tree
Showing 11 changed files with 306 additions and 1 deletion.
15 changes: 14 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,24 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip
- Nothing
--->

## Unreleased -
## 1.8.0 - 2022-05-18

### Added
- #### WebApp
- Added class `WebAppInfoType`. Classes `InlineKeyboardButtonType` and `KeyboardButtonType` extended with parameter `webApp`;

- #### MenuButton
- Added class `MenuButtonType`;
- Added classes `GetChatMenuButtonMethod` and `SetChatMenuButtonMethod` for setting chat menu button and getting info about it;
- Added class `SetChatMenuButtonNormalizer` for normalize data from `SetChatMenuButtonMethod` before making request;


## 1.7.1 - 2022-05-18

### Added
- Supported for symfony 6.x components


## 1.7.0 - 2020-12-12
#### Bot API 5.0 - november November 4, 2020

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ $bot = new \TgBotApi\BotApiBase\BotApi('<bot key>', $apiClient, new \TgBotApi\Bo
|`getChat`|GetChatMethod|ChatType|
|`getChatAdministrators`|GetChatAdministratorsMethod|ChatMemberType[]|
|`getChatMember`|GetChatMemberMethod|ChatMemberType|
|`getChatMenuButton`|GetChatMenuButtonMethod|MenuButtonType|
|`getGameHighScores`|GetGameHighScoresMethod|GameHighScoreType[]|
|`getStickerSet`|GetStickerSetMethod|StickerSetType|
|`getFile`|GetFileMethod|FileType|
Expand Down
2 changes: 2 additions & 0 deletions src/BotApiNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use TgBotApi\BotApiBase\Normalizer\PollNormalizer;
use TgBotApi\BotApiBase\Normalizer\SetMyCommandsNormalizer;
use TgBotApi\BotApiBase\Normalizer\UserProfilePhotosNormalizer;
use TgBotApi\BotApiBase\Normalizer\SetChatMenuButtonNormalizer;

/**
* Class BotApiNormalizer.
Expand Down Expand Up @@ -85,6 +86,7 @@ public function normalize($method): BotApiRequestInterface
new EditMessageMediaNormalizer(new InputMediaNormalizer($objectNormalizer, $files), $objectNormalizer),
new JsonSerializableNormalizer($objectNormalizer),
new AnswerInlineQueryNormalizer($objectNormalizer),
new SetChatMenuButtonNormalizer($objectNormalizer),
new DateTimeNormalizer(),
$objectNormalizer,
]);
Expand Down
47 changes: 47 additions & 0 deletions src/Method/GetChatMenuButtonMethod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace TgBotApi\BotApiBase\Method;

use TgBotApi\BotApiBase\Method\Interfaces\MethodInterface;
use TgBotApi\BotApiBase\Method\Traits\FillFromArrayTrait;

/**
* Class GetChatMenuButtonMethod.
*
* Use this method to get the current value of the bot's menu button in a private chat,
* or the default menu button. Returns MenuButton on success.
*
* @see https://core.telegram.org/bots/api#getchatmenubutton
*/
class GetChatMenuButtonMethod implements MethodInterface
{
use FillFromArrayTrait;

/**
* Optional. Unique identifier for the target private chat. If not specified, default bot's menu button will be returned.
*
* @var int|null
*/
public $chatId;

/**
* GetChatMenuButtonMethod constructor.
*
* @param array|null $data
*
* @throws \TgBotApi\BotApiBase\Exception\BadArgumentException
*
* @return GetChatMenuButtonMethod
*/
public static function create(array $data = null): GetChatMenuButtonMethod
{
$instance = new static();
if ($data) {
$instance->fill($data);
}

return $instance;
}
}
58 changes: 58 additions & 0 deletions src/Method/SetChatMenuButtonMethod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace TgBotApi\BotApiBase\Method;

use TgBotApi\BotApiBase\Method\Interfaces\SetMethodAliasInterface;
use TgBotApi\BotApiBase\Method\Traits\FillFromArrayTrait;
use TgBotApi\BotApiBase\Type\MenuButtonType;

/**
* Class SetChatMenuButtonMethod.
*
* Use this method to change the bot's menu button in a private chat, or the default menu button.
* Returns True on success.
*
* @see https://core.telegram.org/bots/api#setchatmenubutton
*/
class SetChatMenuButtonMethod implements SetMethodAliasInterface
{
use FillFromArrayTrait;

/**
* Unique identifier for the target private chat. If not specified, default bot's menu
* button will be changed
*
* @var integer|null
*/
public $chatId;

/**
* Optional. A JSON-serialized object for the bot's new menu button.
* Defaults to 'default' type
*
* @var MenuButtonType|null
*/
public $menuButton;


/**
* SetChatMenuButtonMethod constructor.
*
* @param array|null $data
*
* @throws \TgBotApi\BotApiBase\Exception\BadArgumentException
*
* @return SetChatMenuButtonMethod
*/
public static function create(array $data = null): SetChatMenuButtonMethod
{
$instance = new static();
if ($data) {
$instance->fill($data);
}

return $instance;
}
}
58 changes: 58 additions & 0 deletions src/Normalizer/SetChatMenuButtonNormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace TgBotApi\BotApiBase\Normalizer;

use Symfony\Component\Serializer\Exception\ExceptionInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Serializer;
use TgBotApi\BotApiBase\Method\SetChatMenuButtonMethod;

/**
* Class SetChatMenuButtonNormalizer.
*/
class SetChatMenuButtonNormalizer implements NormalizerInterface
{
/**
* @var NormalizerInterface
*/
private $objectNormalizer;

/**
* JsonSerializableNormalizer constructor.
*/
public function __construct(NormalizerInterface $objectNormalizer)
{
$this->objectNormalizer = $objectNormalizer;
}

/**
* @param SetChatMenuButtonMethod $topic
* @param null $format
*
* @throws ExceptionInterface
*
* @return array|bool|false|float|int|string
*/
public function normalize($topic, $format = null, array $context = [])
{
$serializer = new Serializer([
new JsonSerializableNormalizer($this->objectNormalizer),
$this->objectNormalizer,
]);

$topic->menuButton = \json_encode($serializer->normalize($topic->menuButton, null, ['skip_null_values' => true]));

return $serializer->normalize($topic, null, ['skip_null_values' => true]);
}

/**
* @param mixed $data
* @param null $format
*/
public function supportsNormalization($data, $format = null): bool
{
return $data instanceof SetChatMenuButtonMethod;
}
}
10 changes: 10 additions & 0 deletions src/Traits/GetMethodTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use TgBotApi\BotApiBase\Method\GetChatMemberMethod;
use TgBotApi\BotApiBase\Method\GetChatMembersCountMethod;
use TgBotApi\BotApiBase\Method\GetChatMethod;
use TgBotApi\BotApiBase\Method\GetChatMenuButtonMethod;
use TgBotApi\BotApiBase\Method\GetFileMethod;
use TgBotApi\BotApiBase\Method\GetGameHighScoresMethod;
use TgBotApi\BotApiBase\Method\GetMeMethod;
Expand All @@ -23,6 +24,7 @@
use TgBotApi\BotApiBase\Type\ChatType;
use TgBotApi\BotApiBase\Type\FileType;
use TgBotApi\BotApiBase\Type\GameHighScoreType;
use TgBotApi\BotApiBase\Type\MenuButtonType;
use TgBotApi\BotApiBase\Type\StickerSetType;
use TgBotApi\BotApiBase\Type\UpdateType;
use TgBotApi\BotApiBase\Type\UserProfilePhotosType;
Expand Down Expand Up @@ -118,6 +120,14 @@ public function getChatMember(GetChatMemberMethod $method): ChatMemberType
return $this->call($method, ChatMemberType::class);
}

/**
* @throws ResponseException
*/
public function getChatMenuButton(GetChatMenuButtonMethod $method): MenuButtonType
{
return $this->call($method, MenuButtonType::class);
}

/**
* @throws ResponseException
*
Expand Down
9 changes: 9 additions & 0 deletions src/Type/InlineKeyboardButtonType.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ class InlineKeyboardButtonType
*/
public $pay;

/**
* Optional. Description of the Web App that will be launched when the user presses the button.
* The Web App will be able to send an arbitrary message on behalf of the user using the method answerWebAppQuery.
* Available only in private chats between a user and the bot.
*
* @var WebAppInfoType|null
*/
public $webApp;

/**
* @param string $text
* @param array|null $data
Expand Down
8 changes: 8 additions & 0 deletions src/Type/KeyboardButtonType.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ class KeyboardButtonType
*/
public $requestPoll;

/**
* 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.
*
* @var WebAppInfoType|null
*/
public $webApp;

/**
* @throws BadArgumentException
*/
Expand Down
62 changes: 62 additions & 0 deletions src/Type/MenuButtonType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);

namespace TgBotApi\BotApiBase\Type;

use TgBotApi\BotApiBase\Exception\BadArgumentException;
use TgBotApi\BotApiBase\Method\Traits\FillFromArrayTrait;

/**
* Class MenuButtonType.
*
* @see https://core.telegram.org/bots/api#menubutton
*/
class MenuButtonType
{
use FillFromArrayTrait;

public const TYPE_COMMANDS = 'commands';
public const TYPE_WEB_APP = 'web_app';
public const TYPE_DEFAULT = 'default';

/**
* Type of the button must be one of the list: commands, web_app, default
*
* @var string
*/
public $type;

/**
* Text on the button. Required for type 'web_app'
*
* @var string|null
*/
public $text;

/**
* Description of the Web App that will be launched when the user presses the button. Required for type 'web_app'
*
* @var WebAppInfoType|null
*/
public $webApp;

/**
* @param string $type
* @param array|null $data
*
* @throws BadArgumentException
*
* @return MenuButtonType
*/
public static function create(string $type, array $data = null): MenuButtonType
{
$instance = new static();
$instance->type = $type;
if ($data) {
$instance->fill($data);
}

return $instance;
}
}
37 changes: 37 additions & 0 deletions src/Type/WebAppInfoType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace TgBotApi\BotApiBase\Type;

use TgBotApi\BotApiBase\Exception\BadArgumentException;

/**
* Class MenuButtonType.
*
* @see https://core.telegram.org/bots/api#webappinfo
*/
class WebAppInfoType
{
/**
* An HTTPS URL of a Web App to be opened with additional data as specified in Initializing Web Apps
*
* @var string
*/
public $url;

/**
* @param string $url
*
* @throws BadArgumentException
*
* @return WebAppInfoType
*/
public static function create(string $url): WebAppInfoType
{
$instance = new static();
$instance->url = $url;

return $instance;
}
}

0 comments on commit 066901e

Please sign in to comment.