From 45020eab52ff56d16183cd854c1c19aecf80eeaf Mon Sep 17 00:00:00 2001 From: Alex Date: Mon, 7 Jul 2025 21:05:18 +0300 Subject: [PATCH] Added getSubscriptions method --- src/Api.php | 39 ++++++++++++++++++++++++++---- src/Client.php | 2 ++ src/Enums/UpdateType.php | 23 ++++++++++++++++++ src/ModelFactory.php | 29 ++++++++++++++++++++++ src/Models/BotInfo.php | 22 ++++++++--------- src/Models/Subscription.php | 48 +++++++++++++++++++++++++++++++++++++ 6 files changed, 148 insertions(+), 15 deletions(-) create mode 100644 src/Enums/UpdateType.php create mode 100644 src/Models/Subscription.php diff --git a/src/Api.php b/src/Api.php index 00982c9..226d06a 100644 --- a/src/Api.php +++ b/src/Api.php @@ -4,7 +4,12 @@ declare(strict_types=1); namespace BushlanovDev\MaxMessengerBot; +use BushlanovDev\MaxMessengerBot\Exceptions\ClientApiException; +use BushlanovDev\MaxMessengerBot\Exceptions\NetworkException; +use BushlanovDev\MaxMessengerBot\Exceptions\SerializationException; use BushlanovDev\MaxMessengerBot\Models\BotInfo; +use BushlanovDev\MaxMessengerBot\Models\Subscription; +use InvalidArgumentException; /** * The main entry point for interacting with the Max Bot API. @@ -15,10 +20,11 @@ use BushlanovDev\MaxMessengerBot\Models\BotInfo; class Api { private const string METHOD_GET = 'GET'; - // private const string METHOD_POST = 'POST'; + // private const string METHOD_DELETE = 'DELETE'; private const string ACTION_ME = '/me'; + private const string ACTION_SUBSCRIPTIONS = '/subscriptions'; private readonly ClientApiInterface $client; @@ -30,9 +36,14 @@ class Api * @param string $accessToken Your bot's access token from @MasterBot. * @param ClientApiInterface|null $client Http api client. * @param ModelFactory|null $modelFactory + * + * @throws InvalidArgumentException */ - public function __construct(string $accessToken, ?ClientApiInterface $client = null, ?ModelFactory $modelFactory = null) - { + public function __construct( + string $accessToken, + ?ClientApiInterface $client = null, + ?ModelFactory $modelFactory = null + ) { $this->client = $client ?? new Client( $accessToken, new \GuzzleHttp\Client(), @@ -43,9 +54,13 @@ class Api } /** - * Returns information about the current bot, identified by an access token. + * Information about the current bot, identified by an access token. * * @return BotInfo + * + * @throws ClientApiException + * @throws NetworkException + * @throws SerializationException */ public function getBotInfo(): BotInfo { @@ -53,4 +68,20 @@ class Api $this->client->request(self::METHOD_GET, self::ACTION_ME) ); } + + /** + * List of all active webhook subscriptions. + * + * @return Subscription[] + * + * @throws ClientApiException + * @throws NetworkException + * @throws SerializationException + */ + public function getSubscriptions(): array + { + return $this->modelFactory->createSubscriptions( + $this->client->request(self::METHOD_GET, self::ACTION_SUBSCRIPTIONS) + ); + } } diff --git a/src/Client.php b/src/Client.php index e4a2df4..e67cfd2 100644 --- a/src/Client.php +++ b/src/Client.php @@ -35,6 +35,8 @@ final class Client implements ClientApiInterface * @param RequestFactoryInterface $requestFactory A PSR-17 factory for creating requests. * @param StreamFactoryInterface $streamFactory A PSR-17 factory for creating request body streams. * @param string $apiVersion The API version to use for requests. + * + * @throws InvalidArgumentException */ public function __construct( private readonly string $accessToken, diff --git a/src/Enums/UpdateType.php b/src/Enums/UpdateType.php new file mode 100644 index 0000000..a5b724c --- /dev/null +++ b/src/Enums/UpdateType.php @@ -0,0 +1,23 @@ + $data * * @return BotInfo @@ -24,4 +27,30 @@ class ModelFactory return BotInfo::fromArray($data); } + + /** + * Information about webhook subscription. + * + * @param array $data + * + * @return Subscription + */ + public function createSubscription(array $data): Subscription + { + return Subscription::fromArray($data); + } + + /** + * List of all active webhook subscriptions. + * + * @param array $data + * + * @return Subscription[] + */ + public function createSubscriptions(array $data): array + { + return isset($data['subscriptions']) && is_array($data['subscriptions']) + ? array_map([$this, 'createSubscription'], $data['subscriptions']) + : []; + } } diff --git a/src/Models/BotInfo.php b/src/Models/BotInfo.php index 0502115..97e3721 100644 --- a/src/Models/BotInfo.php +++ b/src/Models/BotInfo.php @@ -5,21 +5,21 @@ declare(strict_types=1); namespace BushlanovDev\MaxMessengerBot\Models; /** - * Information about the current bot, which is identified using an access token. + * Information about the current bot. */ final readonly class BotInfo extends AbstractModel { /** - * @param int $user_id ID user - * @param string $first_name User display name - * @param string|null $last_name User's display last name - * @param string|null $username Unique public name of the user, may be null if the user is not available or no name is set - * @param bool $is_bot Is the user a bot - * @param int $last_activity_time User last activity time in MAX (Unix time in milliseconds). May be irrelevant if the user has disabled the "online" status in the settings - * @param string|null $description User description, may be null if the user has not filled it in (up to 16000 characters) - * @param string|null $avatar_url Avatar URL - * @param string|null $full_avatar_url Larger Avatar URL - * @param BotCommand[]|null $commands Commands supported by the bot (up to 32 elements) + * @param int $user_id ID user. + * @param string $first_name User display name. + * @param string|null $last_name User's display last name. + * @param string|null $username Unique public name of the user, may be null if the user is not available or no name is set. + * @param bool $is_bot Is the user a bot. + * @param int $last_activity_time User last activity time in MAX (Unix time in milliseconds). May be irrelevant if the user has disabled the "online" status in the settings. + * @param string|null $description User description, may be null if the user has not filled it in (up to 16000 characters). + * @param string|null $avatar_url Avatar URL. + * @param string|null $full_avatar_url Larger Avatar URL. + * @param BotCommand[]|null $commands Commands supported by the bot (up to 32 elements). */ public function __construct( public int $user_id, diff --git a/src/Models/Subscription.php b/src/Models/Subscription.php new file mode 100644 index 0000000..76e8050 --- /dev/null +++ b/src/Models/Subscription.php @@ -0,0 +1,48 @@ + UpdateType::from($typeValue), + $data['update_types'], + ); + } + + return new static( + (string)$data['url'], + (int)$data['time'], + $updateTypes, + $data['version'] ?? null, + ); + } +}