Parse comment and bot admin permission updates

UpdateType already had comment_created, comment_edited,
comment_removed and bot_admin_permissions_changed, but ModelFactory
had no models for them and threw "Unknown or unsupported update type".
Adds the models from schema 0.0.33, maps them in createUpdate() and
adds on*() shortcuts to UpdateDispatcher and MaxBotManager.
This commit is contained in:
Timofey
2026-09-28 00:13:13 +05:00
parent 749c0aab5b
commit d1637add72
12 changed files with 454 additions and 0 deletions
+52
View File
@@ -359,6 +359,58 @@ readonly class MaxBotManager
$this->dispatcher->onMessageChatCreated($this->resolveHandler($handler));
}
/**
* Register a comment created handler.
*
* @param callable|string $handler Can be a closure, callable, or Laravel container binding.
*
* @throws BindingResolutionException
* @codeCoverageIgnore
*/
public function onCommentCreated(callable|string $handler): void
{
$this->dispatcher->onCommentCreated($this->resolveHandler($handler));
}
/**
* Register a comment edited handler.
*
* @param callable|string $handler Can be a closure, callable, or Laravel container binding.
*
* @throws BindingResolutionException
* @codeCoverageIgnore
*/
public function onCommentEdited(callable|string $handler): void
{
$this->dispatcher->onCommentEdited($this->resolveHandler($handler));
}
/**
* Register a comment removed handler.
*
* @param callable|string $handler Can be a closure, callable, or Laravel container binding.
*
* @throws BindingResolutionException
* @codeCoverageIgnore
*/
public function onCommentRemoved(callable|string $handler): void
{
$this->dispatcher->onCommentRemoved($this->resolveHandler($handler));
}
/**
* Register a bot admin permissions changed handler.
*
* @param callable|string $handler Can be a closure, callable, or Laravel container binding.
*
* @throws BindingResolutionException
* @codeCoverageIgnore
*/
public function onBotAdminPermissionsChanged(callable|string $handler): void
{
$this->dispatcher->onBotAdminPermissionsChanged($this->resolveHandler($handler));
}
/**
* Get the API instance.
* @codeCoverageIgnore
+8
View File
@@ -57,8 +57,12 @@ use BushlanovDev\MaxMessengerBot\Models\Updates\AbstractUpdate;
use BushlanovDev\MaxMessengerBot\Models\Updates\BotAddedToChatUpdate;
use BushlanovDev\MaxMessengerBot\Models\Updates\BotRemovedFromChatUpdate;
use BushlanovDev\MaxMessengerBot\Models\Updates\BotStartedUpdate;
use BushlanovDev\MaxMessengerBot\Models\Updates\BotAdminPermissionsChangedUpdate;
use BushlanovDev\MaxMessengerBot\Models\Updates\BotStoppedUpdate;
use BushlanovDev\MaxMessengerBot\Models\Updates\ChatTitleChangedUpdate;
use BushlanovDev\MaxMessengerBot\Models\Updates\CommentCreatedUpdate;
use BushlanovDev\MaxMessengerBot\Models\Updates\CommentEditedUpdate;
use BushlanovDev\MaxMessengerBot\Models\Updates\CommentRemovedUpdate;
use BushlanovDev\MaxMessengerBot\Models\Updates\DialogClearedUpdate;
use BushlanovDev\MaxMessengerBot\Models\Updates\DialogMutedUpdate;
use BushlanovDev\MaxMessengerBot\Models\Updates\DialogRemovedUpdate;
@@ -409,6 +413,10 @@ readonly class ModelFactory
UpdateType::BotStopped => BotStoppedUpdate::fromArray($data),
UpdateType::ChatTitleChanged => ChatTitleChangedUpdate::fromArray($data),
UpdateType::MessageChatCreated => MessageChatCreatedUpdate::fromArray($data),
UpdateType::CommentCreated => CommentCreatedUpdate::fromArray($data),
UpdateType::CommentEdited => CommentEditedUpdate::fromArray($data),
UpdateType::CommentRemoved => CommentRemovedUpdate::fromArray($data),
UpdateType::BotAdminPermissionChanged => BotAdminPermissionsChangedUpdate::fromArray($data),
default => throw new LogicException(
'Unknown or unsupported update type received: ' . ($data['update_type'] ?? 'none')
),
@@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
namespace BushlanovDev\MaxMessengerBot\Models\Updates;
use BushlanovDev\MaxMessengerBot\Attributes\ArrayOf;
use BushlanovDev\MaxMessengerBot\Enums\ChatAdminPermission;
use BushlanovDev\MaxMessengerBot\Enums\UpdateType;
/**
* The bot receives this update when its admin permissions change.
*/
final readonly class BotAdminPermissionsChangedUpdate extends AbstractUpdate
{
/**
* @param int $timestamp Unix-time when event has occurred.
* @param int $chatId Chat identifier where event has occurred.
* @param int $userId User or bot who changed the bot's admin permissions.
* @param int $botId Bot whose admin permissions changed.
* @param bool $isChannel Whether the permissions changed in a channel.
* @param bool $isAdmin Whether the bot is an admin in the chat or channel.
* @param ChatAdminPermission[]|null $permissions The bot's permissions after the change.
*/
public function __construct(
int $timestamp,
public int $chatId,
public int $userId,
public int $botId,
public bool $isChannel,
public bool $isAdmin,
#[ArrayOf(ChatAdminPermission::class)]
public ?array $permissions = null,
) {
parent::__construct(UpdateType::BotAdminPermissionChanged, $timestamp);
}
}
@@ -0,0 +1,26 @@
<?php
declare(strict_types=1);
namespace BushlanovDev\MaxMessengerBot\Models\Updates;
use BushlanovDev\MaxMessengerBot\Enums\UpdateType;
use BushlanovDev\MaxMessengerBot\Models\Message;
/**
* You will get this `update` as soon as a comment is created.
* The bot receives it only if it is an administrator of the channel with the `read_all_messages` permission.
*/
final readonly class CommentCreatedUpdate extends AbstractUpdate
{
/**
* @param int $timestamp Unix-time when event has occurred.
* @param Message $message Newly created comment.
*/
public function __construct(
int $timestamp,
public Message $message,
) {
parent::__construct(UpdateType::CommentCreated, $timestamp);
}
}
@@ -0,0 +1,26 @@
<?php
declare(strict_types=1);
namespace BushlanovDev\MaxMessengerBot\Models\Updates;
use BushlanovDev\MaxMessengerBot\Enums\UpdateType;
use BushlanovDev\MaxMessengerBot\Models\Message;
/**
* You will get this `update` as soon as a comment is edited.
* The bot receives it only if it is an administrator of the channel with the `read_all_messages` permission.
*/
final readonly class CommentEditedUpdate extends AbstractUpdate
{
/**
* @param int $timestamp Unix-time when event has occurred.
* @param Message $message Edited comment.
*/
public function __construct(
int $timestamp,
public Message $message,
) {
parent::__construct(UpdateType::CommentEdited, $timestamp);
}
}
@@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
namespace BushlanovDev\MaxMessengerBot\Models\Updates;
use BushlanovDev\MaxMessengerBot\Enums\UpdateType;
/**
* You will get this `update` as soon as a comment is removed.
* The bot receives it only if it is an administrator of the channel with the `read_all_messages` permission.
*/
final readonly class CommentRemovedUpdate extends AbstractUpdate
{
/**
* @param int $timestamp Unix-time when the event has occurred.
* @param string $messageId Identifier of the removed comment.
* @param int $chatId Chat identifier where the comment has been deleted.
* @param int $userId User who deleted this comment.
* @param string $postId Identifier of the commented post.
*/
public function __construct(
int $timestamp,
public string $messageId,
public int $chatId,
public int $userId,
public string $postId,
) {
parent::__construct(UpdateType::CommentRemoved, $timestamp);
}
}
+52
View File
@@ -293,4 +293,56 @@ final class UpdateDispatcher
{
return $this->addHandler(UpdateType::MessageChatCreated, $handler);
}
/**
* A convenient alias for addHandler(UpdateType::CommentCreated, $handler).
*
* @param callable(Models\Updates\CommentCreatedUpdate, Api): void $handler
*
* @return $this
* @codeCoverageIgnore
*/
public function onCommentCreated(callable $handler): self
{
return $this->addHandler(UpdateType::CommentCreated, $handler);
}
/**
* A convenient alias for addHandler(UpdateType::CommentEdited, $handler).
*
* @param callable(Models\Updates\CommentEditedUpdate, Api): void $handler
*
* @return $this
* @codeCoverageIgnore
*/
public function onCommentEdited(callable $handler): self
{
return $this->addHandler(UpdateType::CommentEdited, $handler);
}
/**
* A convenient alias for addHandler(UpdateType::CommentRemoved, $handler).
*
* @param callable(Models\Updates\CommentRemovedUpdate, Api): void $handler
*
* @return $this
* @codeCoverageIgnore
*/
public function onCommentRemoved(callable $handler): self
{
return $this->addHandler(UpdateType::CommentRemoved, $handler);
}
/**
* A convenient alias for addHandler(UpdateType::BotAdminPermissionChanged, $handler).
*
* @param callable(Models\Updates\BotAdminPermissionsChangedUpdate, Api): void $handler
*
* @return $this
* @codeCoverageIgnore
*/
public function onBotAdminPermissionsChanged(callable $handler): self
{
return $this->addHandler(UpdateType::BotAdminPermissionChanged, $handler);
}
}
+47
View File
@@ -43,8 +43,12 @@ use BushlanovDev\MaxMessengerBot\Models\Recipient;
use BushlanovDev\MaxMessengerBot\Models\Result;
use BushlanovDev\MaxMessengerBot\Models\Subscription;
use BushlanovDev\MaxMessengerBot\Models\UpdateList;
use BushlanovDev\MaxMessengerBot\Models\Updates\BotAdminPermissionsChangedUpdate;
use BushlanovDev\MaxMessengerBot\Models\Updates\BotStartedUpdate;
use BushlanovDev\MaxMessengerBot\Models\Updates\ChatTitleChangedUpdate;
use BushlanovDev\MaxMessengerBot\Models\Updates\CommentCreatedUpdate;
use BushlanovDev\MaxMessengerBot\Models\Updates\CommentEditedUpdate;
use BushlanovDev\MaxMessengerBot\Models\Updates\CommentRemovedUpdate;
use BushlanovDev\MaxMessengerBot\Models\Updates\MessageChatCreatedUpdate;
use BushlanovDev\MaxMessengerBot\Models\Updates\MessageCreatedUpdate;
use BushlanovDev\MaxMessengerBot\Models\UploadEndpoint;
@@ -104,6 +108,10 @@ use Psr\Log\LoggerInterface;
#[UsesClass(LocationAttachment::class)]
#[UsesClass(InlineKeyboardAttachment::class)]
#[UsesClass(KeyboardPayload::class)]
#[UsesClass(CommentCreatedUpdate::class)]
#[UsesClass(CommentEditedUpdate::class)]
#[UsesClass(CommentRemovedUpdate::class)]
#[UsesClass(BotAdminPermissionsChangedUpdate::class)]
final class ModelFactoryTest extends TestCase
{
private ModelFactory $factory;
@@ -348,6 +356,45 @@ final class ModelFactoryTest extends TestCase
$this->assertInstanceOf(UserWithPhoto::class, $chat->dialogWithUser);
}
#[Test]
public function createUpdateHandlesCommentAndBotPermissionUpdates(): void
{
$comment = [
'timestamp' => 1,
'body' => ['mid' => 'mid.comment', 'seq' => 1, 'text' => 'Nice post'],
'recipient' => ['chat_type' => 'channel', 'chat_id' => -100, 'post_id' => 'mid.post'],
];
$created = $this->factory->createUpdate(['update_type' => 'comment_created', 'timestamp' => 1, 'message' => $comment]);
$edited = $this->factory->createUpdate(['update_type' => 'comment_edited', 'timestamp' => 2, 'message' => $comment]);
$removed = $this->factory->createUpdate([
'update_type' => 'comment_removed',
'timestamp' => 3,
'message_id' => 'mid.comment',
'chat_id' => -100,
'user_id' => 42,
'post_id' => 'mid.post',
]);
$permissions = $this->factory->createUpdate([
'update_type' => 'bot_admin_permissions_changed',
'timestamp' => 4,
'chat_id' => -100,
'user_id' => 42,
'bot_id' => 7,
'is_channel' => true,
'is_admin' => true,
'permissions' => ['read_all_messages', 'write'],
]);
$this->assertInstanceOf(CommentCreatedUpdate::class, $created);
$this->assertSame('Nice post', $created->message->body?->text);
$this->assertInstanceOf(CommentEditedUpdate::class, $edited);
$this->assertInstanceOf(CommentRemovedUpdate::class, $removed);
$this->assertSame('mid.post', $removed->postId);
$this->assertInstanceOf(BotAdminPermissionsChangedUpdate::class, $permissions);
$this->assertSame([ChatAdminPermission::ReadAllMessages, ChatAdminPermission::Write], $permissions->permissions);
}
#[Test]
public function createUpdateListHandlesDifferentUpdateTypes(): void
{
@@ -0,0 +1,59 @@
<?php
declare(strict_types=1);
namespace BushlanovDev\MaxMessengerBot\Tests\Models\Updates;
use BushlanovDev\MaxMessengerBot\Attributes\ArrayOf;
use BushlanovDev\MaxMessengerBot\Enums\ChatAdminPermission;
use BushlanovDev\MaxMessengerBot\Enums\UpdateType;
use BushlanovDev\MaxMessengerBot\Models\Updates\BotAdminPermissionsChangedUpdate;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\TestCase;
#[CoversClass(BotAdminPermissionsChangedUpdate::class)]
#[UsesClass(ArrayOf::class)]
final class BotAdminPermissionsChangedUpdateTest extends TestCase
{
#[Test]
public function canBeCreatedFromArray(): void
{
$update = BotAdminPermissionsChangedUpdate::fromArray([
'update_type' => UpdateType::BotAdminPermissionChanged->value,
'timestamp' => 1678886400000,
'chat_id' => -100,
'user_id' => 42,
'bot_id' => 7,
'is_channel' => true,
'is_admin' => true,
'permissions' => ['read_all_messages', 'pin_message'],
]);
$this->assertSame(UpdateType::BotAdminPermissionChanged, $update->updateType);
$this->assertSame(-100, $update->chatId);
$this->assertSame(42, $update->userId);
$this->assertSame(7, $update->botId);
$this->assertTrue($update->isChannel);
$this->assertTrue($update->isAdmin);
$this->assertSame([ChatAdminPermission::ReadAllMessages, ChatAdminPermission::PinMessage], $update->permissions);
}
#[Test]
public function permissionsAreNullWhenTheBotIsNoLongerAdmin(): void
{
$update = BotAdminPermissionsChangedUpdate::fromArray([
'update_type' => UpdateType::BotAdminPermissionChanged->value,
'timestamp' => 1678886400000,
'chat_id' => -100,
'user_id' => 42,
'bot_id' => 7,
'is_channel' => false,
'is_admin' => false,
]);
$this->assertFalse($update->isAdmin);
$this->assertNull($update->permissions);
}
}
@@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
namespace BushlanovDev\MaxMessengerBot\Tests\Models\Updates;
use BushlanovDev\MaxMessengerBot\Enums\UpdateType;
use BushlanovDev\MaxMessengerBot\Models\Message;
use BushlanovDev\MaxMessengerBot\Models\MessageBody;
use BushlanovDev\MaxMessengerBot\Models\Recipient;
use BushlanovDev\MaxMessengerBot\Models\Updates\CommentCreatedUpdate;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\TestCase;
#[CoversClass(CommentCreatedUpdate::class)]
#[UsesClass(Message::class)]
#[UsesClass(MessageBody::class)]
#[UsesClass(Recipient::class)]
final class CommentCreatedUpdateTest extends TestCase
{
#[Test]
public function canBeCreatedFromArray(): void
{
$update = CommentCreatedUpdate::fromArray([
'update_type' => UpdateType::CommentCreated->value,
'timestamp' => 1678886400000,
'message' => [
'timestamp' => 1678886400000,
'body' => ['mid' => 'mid.comment', 'seq' => 5, 'text' => 'Nice post'],
'recipient' => ['chat_type' => 'channel', 'chat_id' => -100],
],
]);
$this->assertSame(UpdateType::CommentCreated, $update->updateType);
$this->assertSame(1678886400000, $update->timestamp);
$this->assertSame('mid.comment', $update->message->body?->mid);
$this->assertSame('Nice post', $update->message->body?->text);
}
}
@@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
namespace BushlanovDev\MaxMessengerBot\Tests\Models\Updates;
use BushlanovDev\MaxMessengerBot\Enums\UpdateType;
use BushlanovDev\MaxMessengerBot\Models\Message;
use BushlanovDev\MaxMessengerBot\Models\MessageBody;
use BushlanovDev\MaxMessengerBot\Models\Recipient;
use BushlanovDev\MaxMessengerBot\Models\Updates\CommentEditedUpdate;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\TestCase;
#[CoversClass(CommentEditedUpdate::class)]
#[UsesClass(Message::class)]
#[UsesClass(MessageBody::class)]
#[UsesClass(Recipient::class)]
final class CommentEditedUpdateTest extends TestCase
{
#[Test]
public function canBeCreatedFromArray(): void
{
$update = CommentEditedUpdate::fromArray([
'update_type' => UpdateType::CommentEdited->value,
'timestamp' => 1678886400000,
'message' => [
'timestamp' => 1678886400000,
'body' => ['mid' => 'mid.comment', 'seq' => 5, 'text' => 'Nice post'],
'recipient' => ['chat_type' => 'channel', 'chat_id' => -100],
],
]);
$this->assertSame(UpdateType::CommentEdited, $update->updateType);
$this->assertSame(1678886400000, $update->timestamp);
$this->assertSame('mid.comment', $update->message->body?->mid);
$this->assertSame('Nice post', $update->message->body?->text);
}
}
@@ -0,0 +1,34 @@
<?php
declare(strict_types=1);
namespace BushlanovDev\MaxMessengerBot\Tests\Models\Updates;
use BushlanovDev\MaxMessengerBot\Enums\UpdateType;
use BushlanovDev\MaxMessengerBot\Models\Updates\CommentRemovedUpdate;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
#[CoversClass(CommentRemovedUpdate::class)]
final class CommentRemovedUpdateTest extends TestCase
{
#[Test]
public function canBeCreatedFromArray(): void
{
$update = CommentRemovedUpdate::fromArray([
'update_type' => UpdateType::CommentRemoved->value,
'timestamp' => 1678886400000,
'message_id' => 'mid.comment',
'chat_id' => -100,
'user_id' => 42,
'post_id' => 'mid.post',
]);
$this->assertSame(UpdateType::CommentRemoved, $update->updateType);
$this->assertSame('mid.comment', $update->messageId);
$this->assertSame(-100, $update->chatId);
$this->assertSame(42, $update->userId);
$this->assertSame('mid.post', $update->postId);
}
}