mirror of
https://github.com/BushlanovDev/max-bot-api-client-php.git
synced 2026-08-17 13:36:16 +00:00
Added DialogUnmuted & DialogCleared & DialogRemoved update types
This commit is contained in:
+4659
File diff suppressed because one or more lines are too long
@@ -27,7 +27,7 @@ use Throwable;
|
||||
* Provides convenient methods for integrating Max Bot with Laravel applications.
|
||||
* Handles webhook processing, long polling, and event dispatching within Laravel context.
|
||||
*/
|
||||
class MaxBotManager
|
||||
readonly class MaxBotManager
|
||||
{
|
||||
/**
|
||||
* @param Container $container
|
||||
@@ -35,9 +35,9 @@ class MaxBotManager
|
||||
* @param UpdateDispatcher $dispatcher
|
||||
*/
|
||||
public function __construct(
|
||||
private readonly Container $container,
|
||||
private readonly Api $api,
|
||||
private readonly UpdateDispatcher $dispatcher,
|
||||
private Container $container,
|
||||
private Api $api,
|
||||
private UpdateDispatcher $dispatcher,
|
||||
) {
|
||||
}
|
||||
|
||||
@@ -242,6 +242,45 @@ class MaxBotManager
|
||||
$this->dispatcher->onDialogMuted($this->resolveHandler($handler));
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a dialog unmute handler.
|
||||
*
|
||||
* @param callable|string $handler Can be a closure, callable, or Laravel container binding.
|
||||
*
|
||||
* @throws BindingResolutionException
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
public function onDialogUnmuted(callable|string $handler): void
|
||||
{
|
||||
$this->dispatcher->onDialogUnmuted($this->resolveHandler($handler));
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a dialog cleared handler.
|
||||
*
|
||||
* @param callable|string $handler Can be a closure, callable, or Laravel container binding.
|
||||
*
|
||||
* @throws BindingResolutionException
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
public function onDialogCleared(callable|string $handler): void
|
||||
{
|
||||
$this->dispatcher->onDialogCleared($this->resolveHandler($handler));
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a dialog removed handler.
|
||||
*
|
||||
* @param callable|string $handler Can be a closure, callable, or Laravel container binding.
|
||||
*
|
||||
* @throws BindingResolutionException
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
public function onDialogRemoved(callable|string $handler): void
|
||||
{
|
||||
$this->dispatcher->onDialogRemoved($this->resolveHandler($handler));
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a user added handler.
|
||||
*
|
||||
|
||||
@@ -58,7 +58,10 @@ use BushlanovDev\MaxMessengerBot\Models\Updates\BotRemovedFromChatUpdate;
|
||||
use BushlanovDev\MaxMessengerBot\Models\Updates\BotStartedUpdate;
|
||||
use BushlanovDev\MaxMessengerBot\Models\Updates\BotStoppedUpdate;
|
||||
use BushlanovDev\MaxMessengerBot\Models\Updates\ChatTitleChangedUpdate;
|
||||
use BushlanovDev\MaxMessengerBot\Models\Updates\DialogClearedUpdate;
|
||||
use BushlanovDev\MaxMessengerBot\Models\Updates\DialogMutedUpdate;
|
||||
use BushlanovDev\MaxMessengerBot\Models\Updates\DialogRemovedUpdate;
|
||||
use BushlanovDev\MaxMessengerBot\Models\Updates\DialogUnmutedUpdate;
|
||||
use BushlanovDev\MaxMessengerBot\Models\Updates\MessageCallbackUpdate;
|
||||
use BushlanovDev\MaxMessengerBot\Models\Updates\MessageChatCreatedUpdate;
|
||||
use BushlanovDev\MaxMessengerBot\Models\Updates\MessageCreatedUpdate;
|
||||
@@ -388,6 +391,9 @@ readonly class ModelFactory
|
||||
UpdateType::BotAdded => BotAddedToChatUpdate::fromArray($data),
|
||||
UpdateType::BotRemoved => BotRemovedFromChatUpdate::fromArray($data),
|
||||
UpdateType::DialogMuted => DialogMutedUpdate::fromArray($data),
|
||||
UpdateType::DialogUnmuted => DialogUnmutedUpdate::fromArray($data),
|
||||
UpdateType::DialogCleared => DialogClearedUpdate::fromArray($data),
|
||||
UpdateType::DialogRemoved => DialogRemovedUpdate::fromArray($data),
|
||||
UpdateType::UserAdded => UserAddedToChatUpdate::fromArray($data),
|
||||
UpdateType::UserRemoved => UserRemovedFromChatUpdate::fromArray($data),
|
||||
UpdateType::BotStarted => BotStartedUpdate::fromArray($data),
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace BushlanovDev\MaxMessengerBot\Models\Updates;
|
||||
|
||||
use BushlanovDev\MaxMessengerBot\Enums\UpdateType;
|
||||
use BushlanovDev\MaxMessengerBot\Models\User;
|
||||
|
||||
/**
|
||||
* Event clearing dialog history.
|
||||
*/
|
||||
final readonly class DialogClearedUpdate extends AbstractUpdate
|
||||
{
|
||||
/**
|
||||
* @param int $timestamp Unix-time when event has occurred.
|
||||
* @param int $chatId Dialog identifier where event has occurred.
|
||||
* @param User $user User pressed the 'Start' button.
|
||||
* @param string|null $userLocale Current user locale in IETF BCP 47 format.
|
||||
*/
|
||||
public function __construct(
|
||||
int $timestamp,
|
||||
public int $chatId,
|
||||
public User $user,
|
||||
public ?string $userLocale,
|
||||
) {
|
||||
parent::__construct(UpdateType::DialogCleared, $timestamp);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace BushlanovDev\MaxMessengerBot\Models\Updates;
|
||||
|
||||
use BushlanovDev\MaxMessengerBot\Enums\UpdateType;
|
||||
use BushlanovDev\MaxMessengerBot\Models\User;
|
||||
|
||||
/**
|
||||
* Event deleting a chat.
|
||||
*/
|
||||
final readonly class DialogRemovedUpdate extends AbstractUpdate
|
||||
{
|
||||
/**
|
||||
* @param int $timestamp Unix-time when event has occurred.
|
||||
* @param int $chatId Dialog identifier where event has occurred.
|
||||
* @param User $user User pressed the 'Start' button.
|
||||
* @param string|null $userLocale Current user locale in IETF BCP 47 format.
|
||||
*/
|
||||
public function __construct(
|
||||
int $timestamp,
|
||||
public int $chatId,
|
||||
public User $user,
|
||||
public ?string $userLocale,
|
||||
) {
|
||||
parent::__construct(UpdateType::DialogRemoved, $timestamp);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace BushlanovDev\MaxMessengerBot\Models\Updates;
|
||||
|
||||
use BushlanovDev\MaxMessengerBot\Enums\UpdateType;
|
||||
use BushlanovDev\MaxMessengerBot\Models\User;
|
||||
|
||||
/**
|
||||
* Event of enabling notifications in a dialog.
|
||||
*/
|
||||
final readonly class DialogUnmutedUpdate extends AbstractUpdate
|
||||
{
|
||||
/**
|
||||
* @param int $timestamp Unix-time when event has occurred.
|
||||
* @param int $chatId Dialog identifier where event has occurred.
|
||||
* @param User $user User pressed the 'Start' button.
|
||||
* @param string|null $userLocale Current user locale in IETF BCP 47 format.
|
||||
*/
|
||||
public function __construct(
|
||||
int $timestamp,
|
||||
public int $chatId,
|
||||
public User $user,
|
||||
public ?string $userLocale,
|
||||
) {
|
||||
parent::__construct(UpdateType::DialogUnmuted, $timestamp);
|
||||
}
|
||||
}
|
||||
@@ -167,7 +167,7 @@ final class UpdateDispatcher
|
||||
/**
|
||||
* A convenient alias for addHandler(UpdateType::DialogMuted, $handler).
|
||||
*
|
||||
* @param callable(Models\Updates\BotRemovedFromChatUpdate, Api): void $handler
|
||||
* @param callable(Models\Updates\DialogMutedUpdate, Api): void $handler
|
||||
*
|
||||
* @return $this
|
||||
* @codeCoverageIgnore
|
||||
@@ -177,6 +177,45 @@ final class UpdateDispatcher
|
||||
return $this->addHandler(UpdateType::DialogMuted, $handler);
|
||||
}
|
||||
|
||||
/**
|
||||
* A convenient alias for addHandler(UpdateType::DialogUnmuted, $handler).
|
||||
*
|
||||
* @param callable(Models\Updates\DialogUnmutedUpdate, Api): void $handler
|
||||
*
|
||||
* @return $this
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
public function onDialogUnmuted(callable $handler): self
|
||||
{
|
||||
return $this->addHandler(UpdateType::DialogUnmuted, $handler);
|
||||
}
|
||||
|
||||
/**
|
||||
* A convenient alias for addHandler(UpdateType::DialogCleared, $handler).
|
||||
*
|
||||
* @param callable(Models\Updates\DialogClearedUpdate, Api): void $handler
|
||||
*
|
||||
* @return $this
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
public function onDialogCleared(callable $handler): self
|
||||
{
|
||||
return $this->addHandler(UpdateType::DialogCleared, $handler);
|
||||
}
|
||||
|
||||
/**
|
||||
* A convenient alias for addHandler(UpdateType::DialogRemoved, $handler).
|
||||
*
|
||||
* @param callable(Models\Updates\DialogRemovedUpdate, Api): void $handler
|
||||
*
|
||||
* @return $this
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
public function onDialogRemoved(callable $handler): self
|
||||
{
|
||||
return $this->addHandler(UpdateType::DialogRemoved, $handler);
|
||||
}
|
||||
|
||||
/**
|
||||
* A convenient alias for addHandler(UpdateType::UserAdded, $handler).
|
||||
*
|
||||
@@ -219,7 +258,7 @@ final class UpdateDispatcher
|
||||
/**
|
||||
* A convenient alias for addHandler(UpdateType::BotStopped, $handler).
|
||||
*
|
||||
* @param callable(Models\Updates\BotStartedUpdate, Api): void $handler
|
||||
* @param callable(Models\Updates\BotStoppedUpdate, Api): void $handler
|
||||
*
|
||||
* @return $this
|
||||
* @codeCoverageIgnore
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace BushlanovDev\MaxMessengerBot\Tests\Models\Updates;
|
||||
|
||||
use BushlanovDev\MaxMessengerBot\Enums\UpdateType;
|
||||
use BushlanovDev\MaxMessengerBot\Models\Updates\DialogClearedUpdate;
|
||||
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(DialogClearedUpdate::class)]
|
||||
#[UsesClass(User::class)]
|
||||
final class DialogClearedUpdateTest extends TestCase
|
||||
{
|
||||
#[Test]
|
||||
public function canBeCreatedFromArray(): void
|
||||
{
|
||||
$data = [
|
||||
'update_type' => UpdateType::DialogCleared->value,
|
||||
'timestamp' => 1678886400000,
|
||||
'chat_id' => 123,
|
||||
'user' => [
|
||||
'user_id' => 123,
|
||||
'first_name' => 'John',
|
||||
'last_name' => 'Doe',
|
||||
'is_bot' => false,
|
||||
'last_activity_time' => 1678886400000,
|
||||
],
|
||||
'user_locale' => 'ru-ru',
|
||||
];
|
||||
|
||||
$update = DialogClearedUpdate::fromArray($data);
|
||||
|
||||
$this->assertInstanceOf(DialogClearedUpdate::class, $update);
|
||||
$this->assertSame(UpdateType::DialogCleared, $update->updateType);
|
||||
$this->assertSame(123, $update->user->userId);
|
||||
$this->assertSame('John', $update->user->firstName);
|
||||
$this->assertSame('Doe', $update->user->lastName);
|
||||
$this->assertSame('ru-ru', $update->userLocale);
|
||||
}
|
||||
}
|
||||
@@ -14,7 +14,7 @@ use PHPUnit\Framework\TestCase;
|
||||
|
||||
#[CoversClass(DialogMutedUpdate::class)]
|
||||
#[UsesClass(User::class)]
|
||||
class DialogMutedUpdateTest extends TestCase
|
||||
final class DialogMutedUpdateTest extends TestCase
|
||||
{
|
||||
#[Test]
|
||||
public function canBeCreatedFromArray(): void
|
||||
@@ -30,7 +30,7 @@ class DialogMutedUpdateTest extends TestCase
|
||||
'is_bot' => false,
|
||||
'last_activity_time' => 1678886400000,
|
||||
],
|
||||
'mutedUntil' => 1678886400000,
|
||||
'muted_until' => 1678886400000,
|
||||
'user_locale' => 'ru-ru',
|
||||
];
|
||||
|
||||
@@ -41,6 +41,7 @@ class DialogMutedUpdateTest extends TestCase
|
||||
$this->assertSame(123, $update->user->userId);
|
||||
$this->assertSame('John', $update->user->firstName);
|
||||
$this->assertSame('Doe', $update->user->lastName);
|
||||
$this->assertSame(1678886400000, $update->mutedUntil);
|
||||
$this->assertSame('ru-ru', $update->userLocale);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace BushlanovDev\MaxMessengerBot\Tests\Models\Updates;
|
||||
|
||||
use BushlanovDev\MaxMessengerBot\Enums\UpdateType;
|
||||
use BushlanovDev\MaxMessengerBot\Models\Updates\DialogRemovedUpdate;
|
||||
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(DialogRemovedUpdate::class)]
|
||||
#[UsesClass(User::class)]
|
||||
final class DialogRemovedUpdateTest extends TestCase
|
||||
{
|
||||
#[Test]
|
||||
public function canBeCreatedFromArray(): void
|
||||
{
|
||||
$data = [
|
||||
'update_type' => UpdateType::DialogRemoved->value,
|
||||
'timestamp' => 1678886400000,
|
||||
'chat_id' => 123,
|
||||
'user' => [
|
||||
'user_id' => 123,
|
||||
'first_name' => 'John',
|
||||
'last_name' => 'Doe',
|
||||
'is_bot' => false,
|
||||
'last_activity_time' => 1678886400000,
|
||||
],
|
||||
'user_locale' => 'ru-ru',
|
||||
];
|
||||
|
||||
$update = DialogRemovedUpdate::fromArray($data);
|
||||
|
||||
$this->assertInstanceOf(DialogRemovedUpdate::class, $update);
|
||||
$this->assertSame(UpdateType::DialogRemoved, $update->updateType);
|
||||
$this->assertSame(123, $update->user->userId);
|
||||
$this->assertSame('John', $update->user->firstName);
|
||||
$this->assertSame('Doe', $update->user->lastName);
|
||||
$this->assertSame('ru-ru', $update->userLocale);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace BushlanovDev\MaxMessengerBot\Tests\Models\Updates;
|
||||
|
||||
use BushlanovDev\MaxMessengerBot\Enums\UpdateType;
|
||||
use BushlanovDev\MaxMessengerBot\Models\Updates\DialogUnmutedUpdate;
|
||||
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(DialogUnmutedUpdate::class)]
|
||||
#[UsesClass(User::class)]
|
||||
final class DialogUnmutedUpdateTest extends TestCase
|
||||
{
|
||||
#[Test]
|
||||
public function canBeCreatedFromArray(): void
|
||||
{
|
||||
$data = [
|
||||
'update_type' => UpdateType::DialogUnmuted->value,
|
||||
'timestamp' => 1678886400000,
|
||||
'chat_id' => 123,
|
||||
'user' => [
|
||||
'user_id' => 123,
|
||||
'first_name' => 'John',
|
||||
'last_name' => 'Doe',
|
||||
'is_bot' => false,
|
||||
'last_activity_time' => 1678886400000,
|
||||
],
|
||||
'user_locale' => 'ru-ru',
|
||||
];
|
||||
|
||||
$update = DialogUnmutedUpdate::fromArray($data);
|
||||
|
||||
$this->assertInstanceOf(DialogUnmutedUpdate::class, $update);
|
||||
$this->assertSame(UpdateType::DialogUnmuted, $update->updateType);
|
||||
$this->assertSame(123, $update->user->userId);
|
||||
$this->assertSame('John', $update->user->firstName);
|
||||
$this->assertSame('Doe', $update->user->lastName);
|
||||
$this->assertSame('ru-ru', $update->userLocale);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user