Added attachment RequestContactButton & RequestGeoLocationButton

This commit is contained in:
Alex
2025-07-17 19:15:59 +03:00
parent b29b9f1155
commit 028d2591f8
10 changed files with 145 additions and 10 deletions
-1
View File
@@ -16,4 +16,3 @@
Откройте диалог с [MasterBot](https://max.ru/MasterBot), следуйте инструкциям и создайте нового бота. После создания
бота MasterBot отправит вам токен.
+5
View File
@@ -26,6 +26,9 @@ use ReflectionException;
*/
class Api
{
private const string API_BASE_URL = 'https://botapi.max.ru';
public const string API_VERSION = '0.0.6';
private const string METHOD_GET = 'GET';
private const string METHOD_POST = 'POST';
private const string METHOD_DELETE = 'DELETE';
@@ -57,6 +60,8 @@ class Api
new \GuzzleHttp\Client(),
new \GuzzleHttp\Psr7\HttpFactory(),
new \GuzzleHttp\Psr7\HttpFactory(),
self::API_BASE_URL,
self::API_VERSION,
);
$this->modelFactory = $modelFactory ?? new ModelFactory();
}
+8 -8
View File
@@ -27,16 +27,13 @@ use Psr\Http\Message\StreamFactoryInterface;
*/
final class Client implements ClientApiInterface
{
private const string API_BASE_URL = 'https://botapi.max.ru';
private const string DEFAULT_API_VERSION = '0.0.6';
/**
* @param string $accessToken Your bot's access token from @MasterBot.
* @param ClientInterface $httpClient A PSR-18 compatible HTTP client (e.g., Guzzle).
* @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.
* @param string $baseUrl The base URL for API requests.
* @param string|null $apiVersion The API version to use for requests.
*
* @throws InvalidArgumentException
*/
@@ -45,7 +42,8 @@ final class Client implements ClientApiInterface
private readonly ClientInterface $httpClient,
private readonly RequestFactoryInterface $requestFactory,
private readonly StreamFactoryInterface $streamFactory,
private readonly string $apiVersion = self::DEFAULT_API_VERSION,
private readonly string $baseUrl,
private readonly ?string $apiVersion = null,
) {
if (empty($accessToken)) {
throw new InvalidArgumentException('Access token cannot be empty.');
@@ -58,9 +56,11 @@ final class Client implements ClientApiInterface
public function request(string $method, string $uri, array $queryParams = [], array $body = []): array
{
$queryParams['access_token'] = $this->accessToken;
$queryParams['v'] = $this->apiVersion;
if (!empty($this->apiVersion)) {
$queryParams['v'] = $this->apiVersion;
}
$fullUrl = self::API_BASE_URL . $uri . '?' . http_build_query($queryParams);
$fullUrl = $this->baseUrl . $uri . '?' . http_build_query($queryParams);
$request = $this->requestFactory->createRequest($method, $fullUrl);
if (!empty($body)) {
@@ -7,6 +7,9 @@ namespace BushlanovDev\MaxMessengerBot\Models\Attachments\Buttons;
use BushlanovDev\MaxMessengerBot\Enums\ButtonType;
use BushlanovDev\MaxMessengerBot\Enums\Intent;
/**
* Sends a notification with payload to a bot (via WebHook or long polling).
*/
final readonly class CallbackButton extends AbstractButton
{
public string $payload;
@@ -6,6 +6,9 @@ namespace BushlanovDev\MaxMessengerBot\Models\Attachments\Buttons;
use BushlanovDev\MaxMessengerBot\Enums\ButtonType;
/**
* Makes a user to follow a link.
*/
final readonly class LinkButton extends AbstractButton
{
public string $url;
@@ -0,0 +1,21 @@
<?php
declare(strict_types=1);
namespace BushlanovDev\MaxMessengerBot\Models\Attachments\Buttons;
use BushlanovDev\MaxMessengerBot\Enums\ButtonType;
/**
* Requests the user permission to access contact information (phone number, short link, email).
*/
final readonly class RequestContactButton extends AbstractButton
{
/**
* @param string $text Visible button text (1 to 128 characters).
*/
public function __construct(string $text)
{
parent::__construct(ButtonType::RequestContact, $text);
}
}
@@ -0,0 +1,26 @@
<?php
declare(strict_types=1);
namespace BushlanovDev\MaxMessengerBot\Models\Attachments\Buttons;
use BushlanovDev\MaxMessengerBot\Enums\ButtonType;
/**
* After pressing this type of button client sends new message with attachment of current user geo location.
*/
final readonly class RequestGeoLocationButton extends AbstractButton
{
public bool $quick;
/**
* @param string $text Visible button text (1 to 128 characters).
* @param bool $quick If true, sends location without asking user's confirmation.
*/
public function __construct(string $text, bool $quick = false)
{
parent::__construct(ButtonType::RequestGeoLocation, $text);
$this->quick = $quick;
}
}
+2 -1
View File
@@ -74,6 +74,7 @@ final class ClientTest extends TestCase
$this->httpClientMock,
$this->requestFactoryMock,
$this->streamFactoryMock,
self::API_BASE_URL,
self::API_VERSION,
);
}
@@ -84,7 +85,7 @@ final class ClientTest extends TestCase
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Access token cannot be empty.');
new Client('', $this->httpClientMock, $this->requestFactoryMock, $this->streamFactoryMock);
new Client('', $this->httpClientMock, $this->requestFactoryMock, $this->streamFactoryMock, '', '');
}
#[Test]
@@ -0,0 +1,30 @@
<?php
declare(strict_types=1);
namespace BushlanovDev\MaxMessengerBot\Tests\Models\Attachments\Buttons;
use BushlanovDev\MaxMessengerBot\Enums\ButtonType;
use BushlanovDev\MaxMessengerBot\Models\Attachments\Buttons\RequestContactButton;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
#[CoversClass(RequestContactButton::class)]
final class RequestContactTest extends TestCase
{
#[Test]
public function toArraySerializesCorrectly(): void
{
$button = new RequestContactButton('Test Button');
$expectedArray = [
'type' => ButtonType::RequestContact->value,
'text' => 'Test Button',
];
$resultArray = $button->toArray();
$this->assertSame($expectedArray, $resultArray);
}
}
@@ -0,0 +1,47 @@
<?php
declare(strict_types=1);
namespace BushlanovDev\MaxMessengerBot\Tests\Models\Attachments\Buttons;
use BushlanovDev\MaxMessengerBot\Enums\ButtonType;
use BushlanovDev\MaxMessengerBot\Models\Attachments\Buttons\RequestGeoLocationButton;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
#[CoversClass(RequestGeoLocationButton::class)]
final class RequestGeoLocationTest extends TestCase
{
#[Test]
public function toArraySerializesCorrectly(): void
{
$button = new RequestGeoLocationButton('Test Button');
$expectedArray = [
'quick' => false,
'type' => ButtonType::RequestGeoLocation->value,
'text' => 'Test Button',
];
$resultArray = $button->toArray();
$this->assertSame($expectedArray, $resultArray);
}
#[Test]
public function toArraySerializesCorrectlyWithQuick(): void
{
$button = new RequestGeoLocationButton('Test Button', true);
$expectedArray = [
'quick' => true,
'type' => ButtonType::RequestGeoLocation->value,
'text' => 'Test Button',
];
$resultArray = $button->toArray();
$this->assertSame($expectedArray, $resultArray);
}
}