Added MessageCallbackUpdate & MessageEditedUpdate & MessageRemovedUpdate & BotAddedToChatUpdate

This commit is contained in:
Alex
2025-07-24 09:11:01 +03:00
parent 62c4b80fae
commit d5b950bcd9
14 changed files with 503 additions and 1 deletions
+1 -1
View File
@@ -41,7 +41,7 @@
"sort-packages": true
},
"scripts": {
"analyse": "vendor/bin/phpstan analyse -c phpstan.neon",
"analyse": "vendor/bin/phpstan analyse -c phpstan.neon --memory-limit=256M",
"format": "vendor/bin/php-cs-fixer fix --allow-risky=yes src",
"test": "vendor/bin/phpunit",
"test-coverage": "vendor/bin/phpunit --coverage-html coverage"
+2
View File
@@ -68,10 +68,12 @@ class Api
) {
if ($client === null) {
if (!class_exists(\GuzzleHttp\Client::class) || !class_exists(\GuzzleHttp\Psr7\HttpFactory::class)) {
// @codeCoverageIgnoreStart
throw new LogicException(
'No client was provided and "guzzlehttp/guzzle" is not found. ' .
'Please run "composer require guzzlehttp/guzzle" or create and pass your own implementation of ClientApiInterface.'
);
// @codeCoverageIgnoreEnd
}
$guzzle = new \GuzzleHttp\Client();
+8
View File
@@ -12,8 +12,12 @@ use BushlanovDev\MaxMessengerBot\Models\Result;
use BushlanovDev\MaxMessengerBot\Models\Subscription;
use BushlanovDev\MaxMessengerBot\Models\UpdateList;
use BushlanovDev\MaxMessengerBot\Models\Updates\AbstractUpdate;
use BushlanovDev\MaxMessengerBot\Models\Updates\BotAddedToChatUpdate;
use BushlanovDev\MaxMessengerBot\Models\Updates\BotStartedUpdate;
use BushlanovDev\MaxMessengerBot\Models\Updates\MessageCallbackUpdate;
use BushlanovDev\MaxMessengerBot\Models\Updates\MessageCreatedUpdate;
use BushlanovDev\MaxMessengerBot\Models\Updates\MessageEditedUpdate;
use BushlanovDev\MaxMessengerBot\Models\Updates\MessageRemovedUpdate;
use BushlanovDev\MaxMessengerBot\Models\UploadEndpoint;
use LogicException;
use ReflectionException;
@@ -154,6 +158,10 @@ class ModelFactory
{
return match (UpdateType::tryFrom($data['update_type'] ?? '')) {
UpdateType::MessageCreated => MessageCreatedUpdate::fromArray($data),
UpdateType::MessageCallback => MessageCallbackUpdate::fromArray($data),
UpdateType::MessageEdited => MessageEditedUpdate::fromArray($data),
UpdateType::MessageRemoved => MessageRemovedUpdate::fromArray($data),
UpdateType::BotAdded => BotAddedToChatUpdate::fromArray($data),
UpdateType::BotStarted => BotStartedUpdate::fromArray($data),
default => throw new LogicException(
'Unknown or unsupported update type received: ' . ($data['update_type'] ?? 'none')
+25
View File
@@ -0,0 +1,25 @@
<?php
declare(strict_types=1);
namespace BushlanovDev\MaxMessengerBot\Models;
/**
* Object sent to bot when user presses a callback button.
*/
final readonly class Callback extends AbstractModel
{
/**
* @param int $timestamp Unix-time when user pressed the button.
* @param string $callbackId Identifier of the callback, unique for the message.
* @param string $payload Payload from the pressed button.
* @param User $user User who pressed the button.
*/
public function __construct(
public int $timestamp,
public string $callbackId,
public string $payload,
public User $user,
) {
}
}
@@ -0,0 +1,29 @@
<?php
declare(strict_types=1);
namespace BushlanovDev\MaxMessengerBot\Models\Updates;
use BushlanovDev\MaxMessengerBot\Enums\UpdateType;
use BushlanovDev\MaxMessengerBot\Models\User;
/**
* You will receive this update when the bot has been added to a chat.
*/
final readonly class BotAddedToChatUpdate extends AbstractUpdate
{
/**
* @param int $timestamp Unix-time when the event has occurred.
* @param int $chatId Chat ID where the bot was added.
* @param User $user User who added the bot to the chat.
* @param bool $isChannel Indicates whether the bot has been added to a channel or not.
*/
public function __construct(
int $timestamp,
public int $chatId,
public User $user,
public bool $isChannel,
) {
parent::__construct(UpdateType::BotAdded, $timestamp);
}
}
@@ -0,0 +1,30 @@
<?php
declare(strict_types=1);
namespace BushlanovDev\MaxMessengerBot\Models\Updates;
use BushlanovDev\MaxMessengerBot\Enums\UpdateType;
use BushlanovDev\MaxMessengerBot\Models\Callback;
use BushlanovDev\MaxMessengerBot\Models\Message;
/**
* You will get this update as soon as a user presses a callback button.
*/
final readonly class MessageCallbackUpdate extends AbstractUpdate
{
/**
* @param int $timestamp Unix-time when event has occurred.
* @param Callback $callback The callback query itself.
* @param Message|null $message Original message with the inline keyboard. Can be null if the message was deleted.
* @param string|null $userLocale Current user locale in IETF BCP 47 format.
*/
public function __construct(
int $timestamp,
public Callback $callback,
public ?Message $message,
public ?string $userLocale,
) {
parent::__construct(UpdateType::MessageCallback, $timestamp);
}
}
@@ -0,0 +1,25 @@
<?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 message is edited.
*/
final readonly class MessageEditedUpdate extends AbstractUpdate
{
/**
* @param int $timestamp Unix-time when the event has occurred.
* @param Message $message The edited message.
*/
public function __construct(
int $timestamp,
public Message $message,
) {
parent::__construct(UpdateType::MessageEdited, $timestamp);
}
}
@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace BushlanovDev\MaxMessengerBot\Models\Updates;
use BushlanovDev\MaxMessengerBot\Enums\UpdateType;
/**
* You will get this update as soon as a message is removed.
*/
final readonly class MessageRemovedUpdate extends AbstractUpdate
{
/**
* @param int $timestamp Unix-time when the event has occurred.
* @param string $messageId Identifier of the removed message.
* @param int $chatId Chat identifier where the message has been deleted.
* @param int $userId User who deleted this message.
*/
public function __construct(
int $timestamp,
public string $messageId,
public int $chatId,
public int $userId,
) {
parent::__construct(UpdateType::MessageRemoved, $timestamp);
}
}
+53
View File
@@ -62,6 +62,57 @@ final class WebhookHandler
return $this->addHandler(UpdateType::MessageCreated, $handler);
}
/**
* A convenient alias for addHandler(UpdateType::MessageCallback, $handler).
*
* @param callable(Models\Updates\MessageCallbackUpdate, Api): void $handler
*
* @return $this
* @codeCoverageIgnore
*/
public function onMessageCallback(callable $handler): self
{
return $this->addHandler(UpdateType::MessageCallback, $handler);
}
/**
* A convenient alias for addHandler(UpdateType::MessageEdited, $handler).
*
* @param callable(Models\Updates\MessageEditedUpdate, Api): void $handler
*
* @return $this
* @codeCoverageIgnore
*/
public function onMessageEdited(callable $handler): self
{
return $this->addHandler(UpdateType::MessageEdited, $handler);
}
/**
* A convenient alias for addHandler(UpdateType::MessageRemoved, $handler).
*
* @param callable(Models\Updates\MessageRemovedUpdate, Api): void $handler
*
* @return $this
* @codeCoverageIgnore
*/
public function onMessageRemoved(callable $handler): self
{
return $this->addHandler(UpdateType::MessageRemoved, $handler);
}
/**
* A convenient alias for addHandler(UpdateType::BotAdded, $handler).
*
* @param callable(Models\Updates\BotAddedToChatUpdate, Api): void $handler
*
* @return $this
* @codeCoverageIgnore
*/
public function onBotAdded(callable $handler): self
{
return $this->addHandler(UpdateType::BotAdded, $handler);
}
/**
* A convenient alias for addHandler(UpdateType::BotStarted, $handler).
*
@@ -92,10 +143,12 @@ final class WebhookHandler
{
if ($request === null) {
if (!class_exists(\GuzzleHttp\Psr7\ServerRequest::class)) {
// @codeCoverageIgnoreStart
throw new \LogicException(
'No ServerRequest was provided and "guzzlehttp/psr7" is not found. ' .
'Please run "composer require guzzlehttp/psr7" or create and pass your own PSR-7 request object.',
);
// @codeCoverageIgnoreEnd
}
$request = \GuzzleHttp\Psr7\ServerRequest::fromGlobals();
}
+48
View File
@@ -0,0 +1,48 @@
<?php
declare(strict_types=1);
namespace BushlanovDev\MaxMessengerBot\Tests\Models;
use BushlanovDev\MaxMessengerBot\Models\Callback;
use BushlanovDev\MaxMessengerBot\Models\User;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\TestCase;
#[CoversClass(Callback::class)]
#[UsesClass(User::class)]
final class CallbackTest extends TestCase
{
#[Test]
public function canBeCreatedFromArray(): void
{
$data = [
'timestamp' => 1678886400,
'callback_id' => 'cb.12345.abc',
'payload' => 'button_1_pressed',
'user' => [
'user_id' => 101,
'first_name' => 'Jane',
'is_bot' => false,
'last_activity_time' => 1678886000,
'last_name' => null,
'username' => null,
'description' => null,
'avatar_url' => null,
'full_avatar_url' => null,
],
];
$callback = Callback::fromArray($data);
$this->assertInstanceOf(Callback::class, $callback);
$this->assertSame(1678886400, $callback->timestamp);
$this->assertSame('cb.12345.abc', $callback->callbackId);
$this->assertSame('button_1_pressed', $callback->payload);
$this->assertInstanceOf(User::class, $callback->user);
$this->assertSame(101, $callback->user->userId);
$this->assertEquals($data, $callback->toArray());
}
}
@@ -0,0 +1,74 @@
<?php
declare(strict_types=1);
namespace BushlanovDev\MaxMessengerBot\Tests\Models\Updates;
use BushlanovDev\MaxMessengerBot\Enums\UpdateType;
use BushlanovDev\MaxMessengerBot\Models\Updates\BotAddedToChatUpdate;
use BushlanovDev\MaxMessengerBot\Models\User;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\TestCase;
#[CoversClass(BotAddedToChatUpdate::class)]
#[UsesClass(User::class)]
final class BotAddedToChatUpdateTest extends TestCase
{
#[Test]
public function canBeCreatedFromArrayAndSerialized(): void
{
$data = [
'update_type' => 'bot_added',
'timestamp' => 1679000000,
'chat_id' => 987654321,
'user' => [
'user_id' => 101,
'first_name' => 'Admin',
'is_bot' => false,
'last_activity_time' => 1678000000,
'last_name' => null,
'username' => null,
'description' => null,
'avatar_url' => null,
'full_avatar_url' => null,
],
'is_channel' => false,
];
$update = BotAddedToChatUpdate::fromArray($data);
$this->assertInstanceOf(BotAddedToChatUpdate::class, $update);
$this->assertSame(UpdateType::BotAdded, $update->updateType);
$this->assertSame(1679000000, $update->timestamp);
$this->assertSame(987654321, $update->chatId);
$this->assertFalse($update->isChannel);
$this->assertInstanceOf(User::class, $update->user);
$this->assertSame(101, $update->user->userId);
$this->assertEquals($data, $update->toArray());
}
#[Test]
public function canBeCreatedForChannel(): void
{
$data = [
'update_type' => 'bot_added',
'timestamp' => 1679000001,
'chat_id' => 111222333,
'user' => [
'user_id' => 102,
'first_name' => 'ChannelCreator',
'is_bot' => false,
'last_activity_time' => 1678000001,
],
'is_channel' => true,
];
$update = BotAddedToChatUpdate::fromArray($data);
$this->assertInstanceOf(BotAddedToChatUpdate::class, $update);
$this->assertTrue($update->isChannel);
$this->assertSame(111222333, $update->chatId);
}
}
@@ -0,0 +1,86 @@
<?php
declare(strict_types=1);
namespace BushlanovDev\MaxMessengerBot\Tests\Models\Updates;
use BushlanovDev\MaxMessengerBot\Enums\UpdateType;
use BushlanovDev\MaxMessengerBot\Models\Callback;
use BushlanovDev\MaxMessengerBot\Models\Message;
use BushlanovDev\MaxMessengerBot\Models\MessageBody;
use BushlanovDev\MaxMessengerBot\Models\Recipient;
use BushlanovDev\MaxMessengerBot\Models\Updates\MessageCallbackUpdate;
use BushlanovDev\MaxMessengerBot\Models\User;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\TestCase;
#[CoversClass(MessageCallbackUpdate::class)]
#[UsesClass(Callback::class)]
#[UsesClass(Message::class)]
#[UsesClass(MessageBody::class)]
#[UsesClass(Recipient::class)]
#[UsesClass(User::class)]
final class MessageCallbackUpdateTest extends TestCase
{
#[Test]
public function canBeCreatedFromArrayWithAllData(): void
{
$data = [
'update_type' => 'message_callback',
'timestamp' => 1678886400,
'callback' => [
'timestamp' => 1678886400,
'callback_id' => 'cb.12345.abc',
'payload' => 'button_1_pressed',
'user' => [
'user_id' => 101, 'first_name' => 'Jane',
'is_bot' => false, 'last_activity_time' => 1678886000,
],
],
'message' => [
'timestamp' => 1678886300,
'body' => ['mid' => 'mid.123', 'seq' => 1, 'text' => 'Press a button'],
'recipient' => ['chat_type' => 'dialog', 'user_id' => 101],
],
'user_locale' => 'en-US',
];
$update = MessageCallbackUpdate::fromArray($data);
$this->assertInstanceOf(MessageCallbackUpdate::class, $update);
$this->assertSame(UpdateType::MessageCallback, $update->updateType);
$this->assertInstanceOf(Callback::class, $update->callback);
$this->assertInstanceOf(Message::class, $update->message);
$this->assertSame('en-US', $update->userLocale);
$this->assertSame('button_1_pressed', $update->callback->payload);
$this->assertSame('mid.123', $update->message->body->mid);
}
#[Test]
public function canBeCreatedWithNullableFieldsAsNull(): void
{
$data = [
'update_type' => 'message_callback',
'timestamp' => 1678886400,
'callback' => [
'timestamp' => 1678886400,
'callback_id' => 'cb.12345.abc',
'payload' => 'button_1_pressed',
'user' => [
'user_id' => 101, 'first_name' => 'Jane',
'is_bot' => false, 'last_activity_time' => 1678886000,
],
],
'message' => null,
'user_locale' => null,
];
$update = MessageCallbackUpdate::fromArray($data);
$this->assertInstanceOf(MessageCallbackUpdate::class, $update);
$this->assertNull($update->message);
$this->assertNull($update->userLocale);
}
}
@@ -0,0 +1,57 @@
<?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\Sender;
use BushlanovDev\MaxMessengerBot\Models\Updates\MessageEditedUpdate;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\TestCase;
#[CoversClass(MessageEditedUpdate::class)]
#[UsesClass(Message::class)]
#[UsesClass(MessageBody::class)]
#[UsesClass(Recipient::class)]
#[UsesClass(Sender::class)]
final class MessageEditedUpdateTest extends TestCase
{
#[Test]
public function canBeCreatedFromArray(): void
{
$data = [
'update_type' => 'message_edited',
'timestamp' => 1678887000,
'message' => [
'timestamp' => 1678886300,
'body' => [
'mid' => 'mid.123.xyz',
'seq' => 1,
'text' => 'This is the new, edited text!',
],
'recipient' => ['chat_type' => 'dialog', 'user_id' => 101],
'sender' => [
'user_id' => 101,
'first_name' => 'Jane',
'is_bot' => false,
'last_activity_time' => 1678886000,
],
],
];
$update = MessageEditedUpdate::fromArray($data);
$this->assertInstanceOf(MessageEditedUpdate::class, $update);
$this->assertSame(UpdateType::MessageEdited, $update->updateType);
$this->assertSame(1678887000, $update->timestamp);
$this->assertInstanceOf(Message::class, $update->message);
$this->assertSame('This is the new, edited text!', $update->message->body->text);
$this->assertSame('mid.123.xyz', $update->message->body->mid);
}
}
@@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
namespace BushlanovDev\MaxMessengerBot\Tests\Models\Updates;
use BushlanovDev\MaxMessengerBot\Enums\UpdateType;
use BushlanovDev\MaxMessengerBot\Models\Updates\MessageRemovedUpdate;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
#[CoversClass(MessageRemovedUpdate::class)]
final class MessageRemovedUpdateTest extends TestCase
{
#[Test]
public function canBeCreatedFromArrayAndSerializedBack(): void
{
$data = [
'update_type' => 'message_removed',
'timestamp' => 1678888000,
'message_id' => 'mid.was.here.now.gone',
'chat_id' => 123456789,
'user_id' => 98765,
];
$update = MessageRemovedUpdate::fromArray($data);
$this->assertInstanceOf(MessageRemovedUpdate::class, $update);
$this->assertSame(UpdateType::MessageRemoved, $update->updateType);
$this->assertSame(1678888000, $update->timestamp);
$this->assertSame('mid.was.here.now.gone', $update->messageId);
$this->assertSame(123456789, $update->chatId);
$this->assertSame(98765, $update->userId);
$this->assertEquals($data, $update->toArray());
}
}