mirror of
https://github.com/BushlanovDev/max-bot-api-client-php.git
synced 2026-10-08 12:58:34 +00:00
Added unsubscribe method
This commit is contained in:
+31
-12
@@ -4,13 +4,13 @@ declare(strict_types=1);
|
||||
|
||||
namespace BushlanovDev\MaxMessengerBot;
|
||||
|
||||
use BushlanovDev\MaxMessengerBot\Enums\UpdateType;
|
||||
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\Result;
|
||||
use BushlanovDev\MaxMessengerBot\Models\Subscription;
|
||||
use BushlanovDev\MaxMessengerBot\Models\SubscriptionRequestBody;
|
||||
use InvalidArgumentException;
|
||||
|
||||
/**
|
||||
@@ -23,7 +23,7 @@ class Api
|
||||
{
|
||||
private const string METHOD_GET = 'GET';
|
||||
private const string METHOD_POST = 'POST';
|
||||
// private const string METHOD_DELETE = 'DELETE';
|
||||
private const string METHOD_DELETE = 'DELETE';
|
||||
|
||||
private const string ACTION_ME = '/me';
|
||||
private const string ACTION_SUBSCRIPTIONS = '/subscriptions';
|
||||
@@ -90,22 +90,41 @@ class Api
|
||||
/**
|
||||
* Subscribes the bot to receive updates via WebHook.
|
||||
*
|
||||
* @param SubscriptionRequestBody $body
|
||||
* @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.
|
||||
*
|
||||
* @return ResultModel
|
||||
*
|
||||
* @throws ClientApiException
|
||||
* @throws NetworkException
|
||||
* @throws SerializationException
|
||||
* @return Result
|
||||
*/
|
||||
public function subscribe(SubscriptionRequestBody $body): ResultModel
|
||||
{
|
||||
public function subscribe(
|
||||
string $url,
|
||||
?string $secret = null,
|
||||
?array $update_types = null,
|
||||
): Result {
|
||||
return $this->modelFactory->createResult(
|
||||
$this->client->request(
|
||||
self::METHOD_POST,
|
||||
self::ACTION_SUBSCRIPTIONS,
|
||||
[],
|
||||
$body->toArray(),
|
||||
compact('url', 'secret', 'update_types'),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unsubscribes bot from receiving updates via WebHook.
|
||||
*
|
||||
* @param string $url URL webhook.
|
||||
*
|
||||
* @return Result
|
||||
*/
|
||||
public function unsubscribe(string $url): Result
|
||||
{
|
||||
return $this->modelFactory->createResult(
|
||||
$this->client->request(
|
||||
self::METHOD_DELETE,
|
||||
self::ACTION_SUBSCRIPTIONS,
|
||||
compact('url'),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ namespace BushlanovDev\MaxMessengerBot;
|
||||
|
||||
use BushlanovDev\MaxMessengerBot\Models\BotCommand;
|
||||
use BushlanovDev\MaxMessengerBot\Models\BotInfo;
|
||||
use BushlanovDev\MaxMessengerBot\Models\ResultModel;
|
||||
use BushlanovDev\MaxMessengerBot\Models\Result;
|
||||
use BushlanovDev\MaxMessengerBot\Models\Subscription;
|
||||
|
||||
/**
|
||||
@@ -19,11 +19,11 @@ class ModelFactory
|
||||
*
|
||||
* @param array<string, mixed> $data
|
||||
*
|
||||
* @return ResultModel
|
||||
* @return Result
|
||||
*/
|
||||
public function createResult(array $data): ResultModel
|
||||
public function createResult(array $data): Result
|
||||
{
|
||||
return ResultModel::fromArray($data);
|
||||
return Result::fromArray($data);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -7,7 +7,7 @@ namespace BushlanovDev\MaxMessengerBot\Models;
|
||||
/**
|
||||
* Simple response to request.
|
||||
*/
|
||||
final readonly class ResultModel extends AbstractModel
|
||||
final readonly class Result extends AbstractModel
|
||||
{
|
||||
/**
|
||||
* @param bool $success true if request was successful, false otherwise.
|
||||
@@ -1,45 +0,0 @@
|
||||
<?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