mirror of
https://github.com/BushlanovDev/max-bot-api-client-php.git
synced 2026-08-17 15:09:58 +00:00
Added subscribe method
This commit is contained in:
+26
-1
@@ -8,7 +8,9 @@ use BushlanovDev\MaxMessengerBot\Exceptions\ClientApiException;
|
||||
use BushlanovDev\MaxMessengerBot\Exceptions\NetworkException;
|
||||
use BushlanovDev\MaxMessengerBot\Exceptions\SerializationException;
|
||||
use BushlanovDev\MaxMessengerBot\Models\BotInfo;
|
||||
use BushlanovDev\MaxMessengerBot\Models\ResultModel;
|
||||
use BushlanovDev\MaxMessengerBot\Models\Subscription;
|
||||
use BushlanovDev\MaxMessengerBot\Models\SubscriptionRequestBody;
|
||||
use InvalidArgumentException;
|
||||
|
||||
/**
|
||||
@@ -20,7 +22,7 @@ use InvalidArgumentException;
|
||||
class Api
|
||||
{
|
||||
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';
|
||||
@@ -84,4 +86,27 @@ class Api
|
||||
$this->client->request(self::METHOD_GET, self::ACTION_SUBSCRIPTIONS)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Subscribes the bot to receive updates via WebHook.
|
||||
*
|
||||
* @param SubscriptionRequestBody $body
|
||||
*
|
||||
* @return ResultModel
|
||||
*
|
||||
* @throws ClientApiException
|
||||
* @throws NetworkException
|
||||
* @throws SerializationException
|
||||
*/
|
||||
public function subscribe(SubscriptionRequestBody $body): ResultModel
|
||||
{
|
||||
return $this->modelFactory->createResult(
|
||||
$this->client->request(
|
||||
self::METHOD_POST,
|
||||
self::ACTION_SUBSCRIPTIONS,
|
||||
[],
|
||||
$body->toArray(),
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ namespace BushlanovDev\MaxMessengerBot;
|
||||
|
||||
use BushlanovDev\MaxMessengerBot\Models\BotCommand;
|
||||
use BushlanovDev\MaxMessengerBot\Models\BotInfo;
|
||||
use BushlanovDev\MaxMessengerBot\Models\ResultModel;
|
||||
use BushlanovDev\MaxMessengerBot\Models\Subscription;
|
||||
|
||||
/**
|
||||
@@ -13,6 +14,18 @@ use BushlanovDev\MaxMessengerBot\Models\Subscription;
|
||||
*/
|
||||
class ModelFactory
|
||||
{
|
||||
/**
|
||||
* Simple response to request.
|
||||
*
|
||||
* @param array<string, mixed> $data
|
||||
*
|
||||
* @return ResultModel
|
||||
*/
|
||||
public function createResult(array $data): ResultModel
|
||||
{
|
||||
return ResultModel::fromArray($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Information about the current bot.
|
||||
*
|
||||
|
||||
@@ -16,6 +16,25 @@ abstract readonly class AbstractModel
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
return get_object_vars($this);
|
||||
return array_map(function ($value) {
|
||||
return $this->convertValue($value);
|
||||
}, get_object_vars($this));
|
||||
}
|
||||
|
||||
private function convertValue(mixed $value): mixed
|
||||
{
|
||||
if ($value instanceof AbstractModel) {
|
||||
return $value->toArray();
|
||||
}
|
||||
|
||||
if (is_array($value)) {
|
||||
return array_map([$this, 'convertValue'], $value);
|
||||
}
|
||||
|
||||
if ($value instanceof \BackedEnum) {
|
||||
return $value->value;
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace BushlanovDev\MaxMessengerBot\Models;
|
||||
|
||||
/**
|
||||
* Simple response to request.
|
||||
*/
|
||||
final readonly class ResultModel extends AbstractModel
|
||||
{
|
||||
/**
|
||||
* @param bool $success true if request was successful, false otherwise.
|
||||
* @param string|null $message Explanatory message if the result was not successful.
|
||||
*/
|
||||
public function __construct(
|
||||
public bool $success,
|
||||
public ?string $message,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public static function fromArray(array $data): static
|
||||
{
|
||||
return new static(
|
||||
(bool)$data['success'],
|
||||
$data['message'] ?? null,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -33,7 +33,7 @@ final readonly class Subscription extends AbstractModel
|
||||
$updateTypes = null;
|
||||
if (isset($data['update_types']) && is_array($data['update_types'])) {
|
||||
$updateTypes = array_map(
|
||||
fn (string $typeValue): UpdateType => UpdateType::from($typeValue),
|
||||
fn(string $typeValue): UpdateType => UpdateType::from($typeValue),
|
||||
$data['update_types'],
|
||||
);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace BushlanovDev\MaxMessengerBot\Models;
|
||||
|
||||
use BushlanovDev\MaxMessengerBot\Enums\UpdateType;
|
||||
|
||||
final readonly class SubscriptionRequestBody extends AbstractModel
|
||||
{
|
||||
/**
|
||||
* @param string $url URL webhook.
|
||||
* @param string|null $secret Secret key for verifying the authenticity of requests.
|
||||
* @param UpdateType[]|null $update_types List of update types.
|
||||
* @param string|null $version Version of the API.
|
||||
*/
|
||||
public function __construct(
|
||||
public string $url,
|
||||
public ?string $secret = null,
|
||||
public ?array $update_types = null,
|
||||
public ?string $version = null,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @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'],
|
||||
$data['secret'] ?? null,
|
||||
$updateTypes,
|
||||
$data['version'] ?? null,
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user