mirror of
https://github.com/BushlanovDev/max-bot-api-client-php.git
synced 2026-08-30 12:07:05 +00:00
Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 03b1f158d9 | |||
| d853faacf7 | |||
| 576f02efbd | |||
| da7a07145b | |||
| 7cdbfc0f47 | |||
| f29344d2ec |
+4
-4
@@ -48,9 +48,9 @@ use RuntimeException;
|
|||||||
*/
|
*/
|
||||||
class Api
|
class Api
|
||||||
{
|
{
|
||||||
public const string LIBRARY_VERSION = '1.3.0';
|
public const string LIBRARY_VERSION = '1.3.3';
|
||||||
|
|
||||||
public const string API_VERSION = '0.0.6';
|
public const string API_VERSION = '1.2.5';
|
||||||
|
|
||||||
private const string API_BASE_URL = 'https://platform-api.max.ru';
|
private const string API_BASE_URL = 'https://platform-api.max.ru';
|
||||||
|
|
||||||
@@ -128,7 +128,7 @@ class Api
|
|||||||
$httpFactory,
|
$httpFactory,
|
||||||
$httpFactory,
|
$httpFactory,
|
||||||
self::API_BASE_URL,
|
self::API_BASE_URL,
|
||||||
self::API_VERSION,
|
null,
|
||||||
$this->logger,
|
$this->logger,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -363,7 +363,7 @@ class Api
|
|||||||
$this->buildNewMessageBody($text, $attachments, $format, $link, $notify),
|
$this->buildNewMessageBody($text, $attachments, $format, $link, $notify),
|
||||||
);
|
);
|
||||||
|
|
||||||
return $this->modelFactory->createMessage($response['message']);
|
return $this->modelFactory->createMessageFromSendResponse($response);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -76,8 +76,8 @@ class MaxBotServiceProvider extends ServiceProvider
|
|||||||
$guzzle,
|
$guzzle,
|
||||||
$httpFactory,
|
$httpFactory,
|
||||||
$httpFactory,
|
$httpFactory,
|
||||||
$config->get('maxbot.base_url', 'https://botapi.max.ru'),
|
$config->get('maxbot.base_url', 'https://platform-api.max.ru'),
|
||||||
$config->get('maxbot.api_version', Api::API_VERSION),
|
$config->get('maxbot.api_version'),
|
||||||
$logger,
|
$logger,
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|||||||
+30
-1
@@ -128,6 +128,33 @@ class ModelFactory
|
|||||||
: [];
|
: [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a Message from the specific response structure of the sendMessage endpoint.
|
||||||
|
*
|
||||||
|
* @param array<string, mixed> $data The raw response from the client.
|
||||||
|
*
|
||||||
|
* @return Message
|
||||||
|
* @throws ReflectionException
|
||||||
|
*/
|
||||||
|
public function createMessageFromSendResponse(array $data): Message
|
||||||
|
{
|
||||||
|
$messageData = $data['message'];
|
||||||
|
|
||||||
|
$topLevelData = [
|
||||||
|
'chat_id' => $data['chat_id'] ?? null,
|
||||||
|
'recipient_id' => $data['recipient_id'] ?? null,
|
||||||
|
'message_id' => $data['message_id'] ?? null,
|
||||||
|
];
|
||||||
|
$messageData = array_merge($messageData, array_filter($topLevelData, fn($value) => $value !== null));
|
||||||
|
|
||||||
|
if (isset($messageData['message']) && is_array($messageData['message'])) {
|
||||||
|
$messageData['body'] = $messageData['message'];
|
||||||
|
unset($messageData['message']);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->createMessage($messageData);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Message.
|
* Message.
|
||||||
*
|
*
|
||||||
@@ -267,7 +294,9 @@ class ModelFactory
|
|||||||
InlineButtonType::RequestGeoLocation => RequestGeoLocationButton::fromArray($data),
|
InlineButtonType::RequestGeoLocation => RequestGeoLocationButton::fromArray($data),
|
||||||
InlineButtonType::Chat => ChatButton::fromArray($data),
|
InlineButtonType::Chat => ChatButton::fromArray($data),
|
||||||
InlineButtonType::OpenApp => OpenAppButton::fromArray($data),
|
InlineButtonType::OpenApp => OpenAppButton::fromArray($data),
|
||||||
default => throw new LogicException('Unknown or unsupported inline button type: ' . ($data['type'] ?? 'none')),
|
default => throw new LogicException(
|
||||||
|
'Unknown or unsupported inline button type: ' . ($data['type'] ?? 'none')
|
||||||
|
),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,6 +17,9 @@ final readonly class Message extends AbstractModel
|
|||||||
* @param string|null $url Message public URL. Can be null for dialogs or non-public chats/channels.
|
* @param string|null $url Message public URL. Can be null for dialogs or non-public chats/channels.
|
||||||
* @param LinkedMessage|null $link Forwarded or replied message.
|
* @param LinkedMessage|null $link Forwarded or replied message.
|
||||||
* @param MessageStat|null $stat Message statistics. Available only for channels.
|
* @param MessageStat|null $stat Message statistics. Available only for channels.
|
||||||
|
* @param int|null $chatId Chat identifier.
|
||||||
|
* @param int|null $recipientId User identifier, if message was sent to user.
|
||||||
|
* @param string|null $messageId Unique identifier of message.
|
||||||
*/
|
*/
|
||||||
public function __construct(
|
public function __construct(
|
||||||
public int $timestamp,
|
public int $timestamp,
|
||||||
@@ -26,6 +29,9 @@ final readonly class Message extends AbstractModel
|
|||||||
public ?string $url,
|
public ?string $url,
|
||||||
public ?LinkedMessage $link,
|
public ?LinkedMessage $link,
|
||||||
public ?MessageStat $stat,
|
public ?MessageStat $stat,
|
||||||
|
public ?int $chatId = null,
|
||||||
|
public ?int $recipientId = null,
|
||||||
|
public ?string $messageId = null,
|
||||||
) {
|
) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+52
-25
@@ -336,7 +336,7 @@ final class ApiTest extends TestCase
|
|||||||
$apiResponse = [
|
$apiResponse = [
|
||||||
'message' => [
|
'message' => [
|
||||||
'timestamp' => time(),
|
'timestamp' => time(),
|
||||||
'body' => ['mid' => 'mid.456.xyz', 'seq' => 101, 'text' => $text],
|
'message' => ['mid' => 'mid.456.xyz', 'seq' => 101, 'text' => $text],
|
||||||
'recipient' => ['chat_type' => 'dialog', 'user_id' => 123, 'chat_id' => null],
|
'recipient' => ['chat_type' => 'dialog', 'user_id' => 123, 'chat_id' => null],
|
||||||
'sender' => [
|
'sender' => [
|
||||||
'user_id' => 123,
|
'user_id' => 123,
|
||||||
@@ -348,9 +348,18 @@ final class ApiTest extends TestCase
|
|||||||
],
|
],
|
||||||
'url' => 'https://max.ru/message/123',
|
'url' => 'https://max.ru/message/123',
|
||||||
],
|
],
|
||||||
|
'chat_id' => 20414985,
|
||||||
|
'recipient_id' => 4328369,
|
||||||
|
'message_id' => 'mid.456.xyz',
|
||||||
];
|
];
|
||||||
|
|
||||||
$expectedMessageObject = Message::fromArray($apiResponse['message']);
|
$finalMessageData = $apiResponse['message'];
|
||||||
|
$finalMessageData['body'] = $finalMessageData['message'];
|
||||||
|
unset($finalMessageData['message']);
|
||||||
|
$finalMessageData['chat_id'] = $apiResponse['chat_id'];
|
||||||
|
$finalMessageData['recipient_id'] = $apiResponse['recipient_id'];
|
||||||
|
$finalMessageData['message_id'] = $apiResponse['message_id'];
|
||||||
|
$expectedMessageObject = Message::fromArray($finalMessageData);
|
||||||
|
|
||||||
$this->clientMock
|
$this->clientMock
|
||||||
->expects($this->once())
|
->expects($this->once())
|
||||||
@@ -360,8 +369,8 @@ final class ApiTest extends TestCase
|
|||||||
|
|
||||||
$this->modelFactoryMock
|
$this->modelFactoryMock
|
||||||
->expects($this->once())
|
->expects($this->once())
|
||||||
->method('createMessage')
|
->method('createMessageFromSendResponse')
|
||||||
->with($apiResponse['message'])
|
->with($apiResponse)
|
||||||
->willReturn($expectedMessageObject);
|
->willReturn($expectedMessageObject);
|
||||||
|
|
||||||
$result = $this->api->sendMessage(
|
$result = $this->api->sendMessage(
|
||||||
@@ -416,12 +425,17 @@ final class ApiTest extends TestCase
|
|||||||
$apiResponse = [
|
$apiResponse = [
|
||||||
'message' => [
|
'message' => [
|
||||||
'timestamp' => time(),
|
'timestamp' => time(),
|
||||||
'body' => ['mid' => 'mid.test.123', 'seq' => 1, 'text' => $text],
|
'message' => ['mid' => 'mid.test.123', 'seq' => 1, 'text' => $text],
|
||||||
'recipient' => ['chat_type' => 'dialog', 'user_id' => 123, 'chat_id' => null],
|
'recipient' => ['chat_type' => 'dialog', 'user_id' => 123, 'chat_id' => null],
|
||||||
]
|
],
|
||||||
|
'message_id' => 'mid.test.123',
|
||||||
];
|
];
|
||||||
|
|
||||||
$expectedMessageObject = Message::fromArray($apiResponse['message']);
|
$finalMessageData = $apiResponse['message'];
|
||||||
|
$finalMessageData['body'] = $finalMessageData['message'];
|
||||||
|
unset($finalMessageData['message']);
|
||||||
|
$finalMessageData['message_id'] = $apiResponse['message_id'];
|
||||||
|
$expectedMessageObject = Message::fromArray($finalMessageData);
|
||||||
|
|
||||||
$this->clientMock
|
$this->clientMock
|
||||||
->expects($this->once())
|
->expects($this->once())
|
||||||
@@ -436,8 +450,8 @@ final class ApiTest extends TestCase
|
|||||||
|
|
||||||
$this->modelFactoryMock
|
$this->modelFactoryMock
|
||||||
->expects($this->once())
|
->expects($this->once())
|
||||||
->method('createMessage')
|
->method('createMessageFromSendResponse')
|
||||||
->with($apiResponse['message'])
|
->with($apiResponse)
|
||||||
->willReturn($expectedMessageObject);
|
->willReturn($expectedMessageObject);
|
||||||
|
|
||||||
$result = $this->api->sendMessage(
|
$result = $this->api->sendMessage(
|
||||||
@@ -803,11 +817,15 @@ final class ApiTest extends TestCase
|
|||||||
$apiResponse = [
|
$apiResponse = [
|
||||||
'message' => [
|
'message' => [
|
||||||
'timestamp' => time(),
|
'timestamp' => time(),
|
||||||
'body' => ['mid' => 'mid.sticker.1', 'seq' => 10],
|
'message' => ['mid' => 'mid.sticker.1', 'seq' => 10],
|
||||||
'recipient' => ['chat_type' => 'dialog', 'user_id' => $chatId],
|
'recipient' => ['chat_type' => 'dialog', 'user_id' => $chatId],
|
||||||
]
|
]
|
||||||
];
|
];
|
||||||
$expectedMessageObject = Message::fromArray($apiResponse['message']);
|
|
||||||
|
$finalMessageData = $apiResponse['message'];
|
||||||
|
$finalMessageData['body'] = $finalMessageData['message'];
|
||||||
|
unset($finalMessageData['message']);
|
||||||
|
$expectedMessageObject = Message::fromArray($finalMessageData);
|
||||||
|
|
||||||
$this->clientMock->expects($this->once())
|
$this->clientMock->expects($this->once())
|
||||||
->method('request')
|
->method('request')
|
||||||
@@ -815,8 +833,8 @@ final class ApiTest extends TestCase
|
|||||||
->willReturn($apiResponse);
|
->willReturn($apiResponse);
|
||||||
|
|
||||||
$this->modelFactoryMock->expects($this->once())
|
$this->modelFactoryMock->expects($this->once())
|
||||||
->method('createMessage')
|
->method('createMessageFromSendResponse')
|
||||||
->with($apiResponse['message'])
|
->with($apiResponse)
|
||||||
->willReturn($expectedMessageObject);
|
->willReturn($expectedMessageObject);
|
||||||
|
|
||||||
$result = $this->api->sendMessage(chatId: $chatId, attachments: [$stickerRequest]);
|
$result = $this->api->sendMessage(chatId: $chatId, attachments: [$stickerRequest]);
|
||||||
@@ -849,11 +867,14 @@ final class ApiTest extends TestCase
|
|||||||
$apiResponse = [
|
$apiResponse = [
|
||||||
'message' => [
|
'message' => [
|
||||||
'timestamp' => time(),
|
'timestamp' => time(),
|
||||||
'body' => ['mid' => 'mid.contact.1', 'seq' => 11],
|
'message' => ['mid' => 'mid.contact.1', 'seq' => 11],
|
||||||
'recipient' => ['chat_type' => 'dialog', 'user_id' => $chatId],
|
'recipient' => ['chat_type' => 'dialog', 'user_id' => $chatId],
|
||||||
]
|
]
|
||||||
];
|
];
|
||||||
$expectedMessageObject = Message::fromArray($apiResponse['message']);
|
$finalMessageData = $apiResponse['message'];
|
||||||
|
$finalMessageData['body'] = $finalMessageData['message'];
|
||||||
|
unset($finalMessageData['message']);
|
||||||
|
$expectedMessageObject = Message::fromArray($finalMessageData);
|
||||||
|
|
||||||
$this->clientMock->expects($this->once())
|
$this->clientMock->expects($this->once())
|
||||||
->method('request')
|
->method('request')
|
||||||
@@ -861,8 +882,8 @@ final class ApiTest extends TestCase
|
|||||||
->willReturn($apiResponse);
|
->willReturn($apiResponse);
|
||||||
|
|
||||||
$this->modelFactoryMock->expects($this->once())
|
$this->modelFactoryMock->expects($this->once())
|
||||||
->method('createMessage')
|
->method('createMessageFromSendResponse')
|
||||||
->with($apiResponse['message'])
|
->with($apiResponse)
|
||||||
->willReturn($expectedMessageObject);
|
->willReturn($expectedMessageObject);
|
||||||
|
|
||||||
$result = $this->api->sendMessage(chatId: $chatId, attachments: [$contactRequest]);
|
$result = $this->api->sendMessage(chatId: $chatId, attachments: [$contactRequest]);
|
||||||
@@ -895,11 +916,14 @@ final class ApiTest extends TestCase
|
|||||||
$apiResponse = [
|
$apiResponse = [
|
||||||
'message' => [
|
'message' => [
|
||||||
'timestamp' => time(),
|
'timestamp' => time(),
|
||||||
'body' => ['mid' => 'mid.location.1', 'seq' => 12],
|
'message' => ['mid' => 'mid.location.1', 'seq' => 12],
|
||||||
'recipient' => ['chat_type' => 'dialog', 'user_id' => $chatId],
|
'recipient' => ['chat_type' => 'dialog', 'user_id' => $chatId],
|
||||||
]
|
]
|
||||||
];
|
];
|
||||||
$expectedMessageObject = Message::fromArray($apiResponse['message']);
|
$finalMessageData = $apiResponse['message'];
|
||||||
|
$finalMessageData['body'] = $finalMessageData['message'];
|
||||||
|
unset($finalMessageData['message']);
|
||||||
|
$expectedMessageObject = Message::fromArray($finalMessageData);
|
||||||
|
|
||||||
$this->clientMock->expects($this->once())
|
$this->clientMock->expects($this->once())
|
||||||
->method('request')
|
->method('request')
|
||||||
@@ -907,8 +931,8 @@ final class ApiTest extends TestCase
|
|||||||
->willReturn($apiResponse);
|
->willReturn($apiResponse);
|
||||||
|
|
||||||
$this->modelFactoryMock->expects($this->once())
|
$this->modelFactoryMock->expects($this->once())
|
||||||
->method('createMessage')
|
->method('createMessageFromSendResponse')
|
||||||
->with($apiResponse['message'])
|
->with($apiResponse)
|
||||||
->willReturn($expectedMessageObject);
|
->willReturn($expectedMessageObject);
|
||||||
|
|
||||||
$result = $this->api->sendMessage(chatId: $chatId, attachments: [$locationRequest]);
|
$result = $this->api->sendMessage(chatId: $chatId, attachments: [$locationRequest]);
|
||||||
@@ -940,11 +964,14 @@ final class ApiTest extends TestCase
|
|||||||
$apiResponse = [
|
$apiResponse = [
|
||||||
'message' => [
|
'message' => [
|
||||||
'timestamp' => time(),
|
'timestamp' => time(),
|
||||||
'body' => ['mid' => 'mid.share.1', 'seq' => 13],
|
'message' => ['mid' => 'mid.share.1', 'seq' => 13],
|
||||||
'recipient' => ['chat_type' => 'dialog', 'user_id' => $chatId],
|
'recipient' => ['chat_type' => 'dialog', 'user_id' => $chatId],
|
||||||
]
|
]
|
||||||
];
|
];
|
||||||
$expectedMessageObject = Message::fromArray($apiResponse['message']);
|
$finalMessageData = $apiResponse['message'];
|
||||||
|
$finalMessageData['body'] = $finalMessageData['message'];
|
||||||
|
unset($finalMessageData['message']);
|
||||||
|
$expectedMessageObject = Message::fromArray($finalMessageData);
|
||||||
|
|
||||||
$this->clientMock->expects($this->once())
|
$this->clientMock->expects($this->once())
|
||||||
->method('request')
|
->method('request')
|
||||||
@@ -952,8 +979,8 @@ final class ApiTest extends TestCase
|
|||||||
->willReturn($apiResponse);
|
->willReturn($apiResponse);
|
||||||
|
|
||||||
$this->modelFactoryMock->expects($this->once())
|
$this->modelFactoryMock->expects($this->once())
|
||||||
->method('createMessage')
|
->method('createMessageFromSendResponse')
|
||||||
->with($apiResponse['message'])
|
->with($apiResponse)
|
||||||
->willReturn($expectedMessageObject);
|
->willReturn($expectedMessageObject);
|
||||||
|
|
||||||
$result = $this->api->sendMessage(chatId: $chatId, attachments: [$shareRequest]);
|
$result = $this->api->sendMessage(chatId: $chatId, attachments: [$shareRequest]);
|
||||||
|
|||||||
@@ -220,6 +220,36 @@ final class ModelFactoryTest extends TestCase
|
|||||||
$this->assertSame(UpdateType::MessageCreated, $subscriptions[0]->updateTypes[0]);
|
$this->assertSame(UpdateType::MessageCreated, $subscriptions[0]->updateTypes[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[Test]
|
||||||
|
public function createMessageFromSendResponseCorrectlyTransformsData(): void
|
||||||
|
{
|
||||||
|
$apiResponse = [
|
||||||
|
'message' => [
|
||||||
|
'recipient' => ['chat_id' => 20414985, 'chat_type' => 'dialog', 'user_id' => 4328369],
|
||||||
|
'timestamp' => 1760089962345,
|
||||||
|
'sender' => [
|
||||||
|
'user_id' => 24480184,
|
||||||
|
'first_name' => 'Autobot',
|
||||||
|
'is_bot' => true,
|
||||||
|
'last_activity_time' => 1760089962354,
|
||||||
|
],
|
||||||
|
'message' => ['mid' => 'mid.xyz', 'seq' => 1153, 'text' => '123123'],
|
||||||
|
],
|
||||||
|
'chat_id' => 20414985,
|
||||||
|
'recipient_id' => 4328369,
|
||||||
|
'message_id' => 'mid.xyz',
|
||||||
|
];
|
||||||
|
|
||||||
|
$message = $this->factory->createMessageFromSendResponse($apiResponse);
|
||||||
|
|
||||||
|
$this->assertInstanceOf(Message::class, $message);
|
||||||
|
$this->assertInstanceOf(MessageBody::class, $message->body);
|
||||||
|
$this->assertSame('mid.xyz', $message->body->mid);
|
||||||
|
$this->assertSame(20414985, $message->chatId);
|
||||||
|
$this->assertSame(4328369, $message->recipientId);
|
||||||
|
$this->assertSame('mid.xyz', $message->messageId);
|
||||||
|
}
|
||||||
|
|
||||||
#[Test]
|
#[Test]
|
||||||
public function createMessage(): void
|
public function createMessage(): void
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -64,6 +64,9 @@ final class MessageTest extends TestCase
|
|||||||
'stat' => [
|
'stat' => [
|
||||||
'views' => 500,
|
'views' => 500,
|
||||||
],
|
],
|
||||||
|
'chat_id' => null,
|
||||||
|
'recipient_id' => null,
|
||||||
|
'message_id' => null,
|
||||||
];
|
];
|
||||||
|
|
||||||
$message = Message::fromArray($data);
|
$message = Message::fromArray($data);
|
||||||
|
|||||||
Reference in New Issue
Block a user