-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #62 from Uterok/feature/web-app-buttons
Added menu button and web app components
- Loading branch information
Showing
11 changed files
with
306 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |