mirror of
https://github.com/BushlanovDev/max-bot-api-client-php.git
synced 2026-10-09 21:39:36 +00:00
Added getSubscriptions method
This commit is contained in:
+35
-4
@@ -4,7 +4,12 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace BushlanovDev\MaxMessengerBot;
|
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\BotInfo;
|
||||||
|
use BushlanovDev\MaxMessengerBot\Models\Subscription;
|
||||||
|
use InvalidArgumentException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The main entry point for interacting with the Max Bot API.
|
* The main entry point for interacting with the Max Bot API.
|
||||||
@@ -15,10 +20,11 @@ use BushlanovDev\MaxMessengerBot\Models\BotInfo;
|
|||||||
class Api
|
class Api
|
||||||
{
|
{
|
||||||
private const string METHOD_GET = 'GET';
|
private const string METHOD_GET = 'GET';
|
||||||
|
|
||||||
// private const string METHOD_POST = 'POST';
|
// private const string METHOD_POST = 'POST';
|
||||||
|
// private const string METHOD_DELETE = 'DELETE';
|
||||||
|
|
||||||
private const string ACTION_ME = '/me';
|
private const string ACTION_ME = '/me';
|
||||||
|
private const string ACTION_SUBSCRIPTIONS = '/subscriptions';
|
||||||
|
|
||||||
private readonly ClientApiInterface $client;
|
private readonly ClientApiInterface $client;
|
||||||
|
|
||||||
@@ -30,9 +36,14 @@ class Api
|
|||||||
* @param string $accessToken Your bot's access token from @MasterBot.
|
* @param string $accessToken Your bot's access token from @MasterBot.
|
||||||
* @param ClientApiInterface|null $client Http api client.
|
* @param ClientApiInterface|null $client Http api client.
|
||||||
* @param ModelFactory|null $modelFactory
|
* @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(
|
$this->client = $client ?? new Client(
|
||||||
$accessToken,
|
$accessToken,
|
||||||
new \GuzzleHttp\Client(),
|
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
|
* @return BotInfo
|
||||||
|
*
|
||||||
|
* @throws ClientApiException
|
||||||
|
* @throws NetworkException
|
||||||
|
* @throws SerializationException
|
||||||
*/
|
*/
|
||||||
public function getBotInfo(): BotInfo
|
public function getBotInfo(): BotInfo
|
||||||
{
|
{
|
||||||
@@ -53,4 +68,20 @@ class Api
|
|||||||
$this->client->request(self::METHOD_GET, self::ACTION_ME)
|
$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)
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,6 +35,8 @@ final class Client implements ClientApiInterface
|
|||||||
* @param RequestFactoryInterface $requestFactory A PSR-17 factory for creating requests.
|
* @param RequestFactoryInterface $requestFactory A PSR-17 factory for creating requests.
|
||||||
* @param StreamFactoryInterface $streamFactory A PSR-17 factory for creating request body streams.
|
* @param StreamFactoryInterface $streamFactory A PSR-17 factory for creating request body streams.
|
||||||
* @param string $apiVersion The API version to use for requests.
|
* @param string $apiVersion The API version to use for requests.
|
||||||
|
*
|
||||||
|
* @throws InvalidArgumentException
|
||||||
*/
|
*/
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private readonly string $accessToken,
|
private readonly string $accessToken,
|
||||||
|
|||||||
@@ -0,0 +1,23 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace BushlanovDev\MaxMessengerBot\Enums;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents the different types of events (updates) that can happen in a chat.
|
||||||
|
*/
|
||||||
|
enum UpdateType: string
|
||||||
|
{
|
||||||
|
case MessageCreated = 'message_created';
|
||||||
|
case MessageCallback = 'message_callback';
|
||||||
|
case MessageEdited = 'message_edited';
|
||||||
|
case MessageRemoved = 'message_removed';
|
||||||
|
case BotAdded = 'bot_added';
|
||||||
|
case BotRemoved = 'bot_removed';
|
||||||
|
case UserAdded = 'user_added';
|
||||||
|
case UserRemoved = 'user_removed';
|
||||||
|
case BotStarted = 'bot_started';
|
||||||
|
case ChatTitleChanged = 'chat_title_changed';
|
||||||
|
case MessageChatCreated = 'message_chat_created';
|
||||||
|
}
|
||||||
@@ -6,6 +6,7 @@ namespace BushlanovDev\MaxMessengerBot;
|
|||||||
|
|
||||||
use BushlanovDev\MaxMessengerBot\Models\BotCommand;
|
use BushlanovDev\MaxMessengerBot\Models\BotCommand;
|
||||||
use BushlanovDev\MaxMessengerBot\Models\BotInfo;
|
use BushlanovDev\MaxMessengerBot\Models\BotInfo;
|
||||||
|
use BushlanovDev\MaxMessengerBot\Models\Subscription;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates DTOs from raw associative arrays returned by the API client.
|
* Creates DTOs from raw associative arrays returned by the API client.
|
||||||
@@ -13,6 +14,8 @@ use BushlanovDev\MaxMessengerBot\Models\BotInfo;
|
|||||||
class ModelFactory
|
class ModelFactory
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
|
* Information about the current bot.
|
||||||
|
*
|
||||||
* @param array<string, mixed> $data
|
* @param array<string, mixed> $data
|
||||||
*
|
*
|
||||||
* @return BotInfo
|
* @return BotInfo
|
||||||
@@ -24,4 +27,30 @@ class ModelFactory
|
|||||||
|
|
||||||
return BotInfo::fromArray($data);
|
return BotInfo::fromArray($data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Information about webhook subscription.
|
||||||
|
*
|
||||||
|
* @param array<string, mixed> $data
|
||||||
|
*
|
||||||
|
* @return Subscription
|
||||||
|
*/
|
||||||
|
public function createSubscription(array $data): Subscription
|
||||||
|
{
|
||||||
|
return Subscription::fromArray($data);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* List of all active webhook subscriptions.
|
||||||
|
*
|
||||||
|
* @param array<string, mixed> $data
|
||||||
|
*
|
||||||
|
* @return Subscription[]
|
||||||
|
*/
|
||||||
|
public function createSubscriptions(array $data): array
|
||||||
|
{
|
||||||
|
return isset($data['subscriptions']) && is_array($data['subscriptions'])
|
||||||
|
? array_map([$this, 'createSubscription'], $data['subscriptions'])
|
||||||
|
: [];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+11
-11
@@ -5,21 +5,21 @@ declare(strict_types=1);
|
|||||||
namespace BushlanovDev\MaxMessengerBot\Models;
|
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
|
final readonly class BotInfo extends AbstractModel
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @param int $user_id ID user
|
* @param int $user_id ID user.
|
||||||
* @param string $first_name User display name
|
* @param string $first_name User display name.
|
||||||
* @param string|null $last_name User's display last 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 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 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 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 $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 $avatar_url Avatar URL.
|
||||||
* @param string|null $full_avatar_url Larger 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 BotCommand[]|null $commands Commands supported by the bot (up to 32 elements).
|
||||||
*/
|
*/
|
||||||
public function __construct(
|
public function __construct(
|
||||||
public int $user_id,
|
public int $user_id,
|
||||||
|
|||||||
@@ -0,0 +1,48 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace BushlanovDev\MaxMessengerBot\Models;
|
||||||
|
|
||||||
|
use BushlanovDev\MaxMessengerBot\Enums\UpdateType;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Information about webhook subscriptions.
|
||||||
|
*/
|
||||||
|
final readonly class Subscription extends AbstractModel
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @param string $url URL webhook.
|
||||||
|
* @param int $time Unix-time of creating a subscription.
|
||||||
|
* @param UpdateType[]|null $update_types List of update types.
|
||||||
|
* @param string|null $version Version of the API.
|
||||||
|
*/
|
||||||
|
public function __construct(
|
||||||
|
public string $url,
|
||||||
|
public int $time,
|
||||||
|
public ?array $update_types,
|
||||||
|
public ?string $version,
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritdoc
|
||||||
|
*/
|
||||||
|
public static function fromArray(array $data): static
|
||||||
|
{
|
||||||
|
$updateTypes = null;
|
||||||
|
if (isset($data['update_types']) && is_array($data['update_types'])) {
|
||||||
|
$updateTypes = array_map(
|
||||||
|
fn (string $typeValue): UpdateType => UpdateType::from($typeValue),
|
||||||
|
$data['update_types'],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new static(
|
||||||
|
(string)$data['url'],
|
||||||
|
(int)$data['time'],
|
||||||
|
$updateTypes,
|
||||||
|
$data['version'] ?? null,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user