Skip to content

Commit 2e7e9d9

Browse files
committed
Added DialogUnmuted & DialogCleared & DialogRemoved update types
1 parent cf415b7 commit 2e7e9d9

11 files changed

Lines changed: 4974 additions & 8 deletions

docs/swagger.json

Lines changed: 4659 additions & 0 deletions
Large diffs are not rendered by default.

src/Laravel/MaxBotManager.php

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,17 @@
2727
* Provides convenient methods for integrating Max Bot with Laravel applications.
2828
* Handles webhook processing, long polling, and event dispatching within Laravel context.
2929
*/
30-
class MaxBotManager
30+
readonly class MaxBotManager
3131
{
3232
/**
3333
* @param Container $container
3434
* @param Api $api
3535
* @param UpdateDispatcher $dispatcher
3636
*/
3737
public function __construct(
38-
private readonly Container $container,
39-
private readonly Api $api,
40-
private readonly UpdateDispatcher $dispatcher,
38+
private Container $container,
39+
private Api $api,
40+
private UpdateDispatcher $dispatcher,
4141
) {
4242
}
4343

@@ -242,6 +242,45 @@ public function onDialogMuted(callable|string $handler): void
242242
$this->dispatcher->onDialogMuted($this->resolveHandler($handler));
243243
}
244244

245+
/**
246+
* Register a dialog unmute handler.
247+
*
248+
* @param callable|string $handler Can be a closure, callable, or Laravel container binding.
249+
*
250+
* @throws BindingResolutionException
251+
* @codeCoverageIgnore
252+
*/
253+
public function onDialogUnmuted(callable|string $handler): void
254+
{
255+
$this->dispatcher->onDialogUnmuted($this->resolveHandler($handler));
256+
}
257+
258+
/**
259+
* Register a dialog cleared handler.
260+
*
261+
* @param callable|string $handler Can be a closure, callable, or Laravel container binding.
262+
*
263+
* @throws BindingResolutionException
264+
* @codeCoverageIgnore
265+
*/
266+
public function onDialogCleared(callable|string $handler): void
267+
{
268+
$this->dispatcher->onDialogCleared($this->resolveHandler($handler));
269+
}
270+
271+
/**
272+
* Register a dialog removed handler.
273+
*
274+
* @param callable|string $handler Can be a closure, callable, or Laravel container binding.
275+
*
276+
* @throws BindingResolutionException
277+
* @codeCoverageIgnore
278+
*/
279+
public function onDialogRemoved(callable|string $handler): void
280+
{
281+
$this->dispatcher->onDialogRemoved($this->resolveHandler($handler));
282+
}
283+
245284
/**
246285
* Register a user added handler.
247286
*

src/ModelFactory.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@
5858
use BushlanovDev\MaxMessengerBot\Models\Updates\BotStartedUpdate;
5959
use BushlanovDev\MaxMessengerBot\Models\Updates\BotStoppedUpdate;
6060
use BushlanovDev\MaxMessengerBot\Models\Updates\ChatTitleChangedUpdate;
61+
use BushlanovDev\MaxMessengerBot\Models\Updates\DialogClearedUpdate;
6162
use BushlanovDev\MaxMessengerBot\Models\Updates\DialogMutedUpdate;
63+
use BushlanovDev\MaxMessengerBot\Models\Updates\DialogRemovedUpdate;
64+
use BushlanovDev\MaxMessengerBot\Models\Updates\DialogUnmutedUpdate;
6265
use BushlanovDev\MaxMessengerBot\Models\Updates\MessageCallbackUpdate;
6366
use BushlanovDev\MaxMessengerBot\Models\Updates\MessageChatCreatedUpdate;
6467
use BushlanovDev\MaxMessengerBot\Models\Updates\MessageCreatedUpdate;
@@ -388,6 +391,9 @@ public function createUpdate(array $data): AbstractUpdate
388391
UpdateType::BotAdded => BotAddedToChatUpdate::fromArray($data),
389392
UpdateType::BotRemoved => BotRemovedFromChatUpdate::fromArray($data),
390393
UpdateType::DialogMuted => DialogMutedUpdate::fromArray($data),
394+
UpdateType::DialogUnmuted => DialogUnmutedUpdate::fromArray($data),
395+
UpdateType::DialogCleared => DialogClearedUpdate::fromArray($data),
396+
UpdateType::DialogRemoved => DialogRemovedUpdate::fromArray($data),
391397
UpdateType::UserAdded => UserAddedToChatUpdate::fromArray($data),
392398
UpdateType::UserRemoved => UserRemovedFromChatUpdate::fromArray($data),
393399
UpdateType::BotStarted => BotStartedUpdate::fromArray($data),
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace BushlanovDev\MaxMessengerBot\Models\Updates;
6+
7+
use BushlanovDev\MaxMessengerBot\Enums\UpdateType;
8+
use BushlanovDev\MaxMessengerBot\Models\User;
9+
10+
/**
11+
* Event clearing dialog history.
12+
*/
13+
final readonly class DialogClearedUpdate extends AbstractUpdate
14+
{
15+
/**
16+
* @param int $timestamp Unix-time when event has occurred.
17+
* @param int $chatId Dialog identifier where event has occurred.
18+
* @param User $user User pressed the 'Start' button.
19+
* @param string|null $userLocale Current user locale in IETF BCP 47 format.
20+
*/
21+
public function __construct(
22+
int $timestamp,
23+
public int $chatId,
24+
public User $user,
25+
public ?string $userLocale,
26+
) {
27+
parent::__construct(UpdateType::DialogCleared, $timestamp);
28+
}
29+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace BushlanovDev\MaxMessengerBot\Models\Updates;
6+
7+
use BushlanovDev\MaxMessengerBot\Enums\UpdateType;
8+
use BushlanovDev\MaxMessengerBot\Models\User;
9+
10+
/**
11+
* Event deleting a chat.
12+
*/
13+
final readonly class DialogRemovedUpdate extends AbstractUpdate
14+
{
15+
/**
16+
* @param int $timestamp Unix-time when event has occurred.
17+
* @param int $chatId Dialog identifier where event has occurred.
18+
* @param User $user User pressed the 'Start' button.
19+
* @param string|null $userLocale Current user locale in IETF BCP 47 format.
20+
*/
21+
public function __construct(
22+
int $timestamp,
23+
public int $chatId,
24+
public User $user,
25+
public ?string $userLocale,
26+
) {
27+
parent::__construct(UpdateType::DialogRemoved, $timestamp);
28+
}
29+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace BushlanovDev\MaxMessengerBot\Models\Updates;
6+
7+
use BushlanovDev\MaxMessengerBot\Enums\UpdateType;
8+
use BushlanovDev\MaxMessengerBot\Models\User;
9+
10+
/**
11+
* Event of enabling notifications in a dialog.
12+
*/
13+
final readonly class DialogUnmutedUpdate extends AbstractUpdate
14+
{
15+
/**
16+
* @param int $timestamp Unix-time when event has occurred.
17+
* @param int $chatId Dialog identifier where event has occurred.
18+
* @param User $user User pressed the 'Start' button.
19+
* @param string|null $userLocale Current user locale in IETF BCP 47 format.
20+
*/
21+
public function __construct(
22+
int $timestamp,
23+
public int $chatId,
24+
public User $user,
25+
public ?string $userLocale,
26+
) {
27+
parent::__construct(UpdateType::DialogUnmuted, $timestamp);
28+
}
29+
}

src/UpdateDispatcher.php

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ public function onBotRemoved(callable $handler): self
167167
/**
168168
* A convenient alias for addHandler(UpdateType::DialogMuted, $handler).
169169
*
170-
* @param callable(Models\Updates\BotRemovedFromChatUpdate, Api): void $handler
170+
* @param callable(Models\Updates\DialogMutedUpdate, Api): void $handler
171171
*
172172
* @return $this
173173
* @codeCoverageIgnore
@@ -177,6 +177,45 @@ public function onDialogMuted(callable $handler): self
177177
return $this->addHandler(UpdateType::DialogMuted, $handler);
178178
}
179179

180+
/**
181+
* A convenient alias for addHandler(UpdateType::DialogUnmuted, $handler).
182+
*
183+
* @param callable(Models\Updates\DialogUnmutedUpdate, Api): void $handler
184+
*
185+
* @return $this
186+
* @codeCoverageIgnore
187+
*/
188+
public function onDialogUnmuted(callable $handler): self
189+
{
190+
return $this->addHandler(UpdateType::DialogUnmuted, $handler);
191+
}
192+
193+
/**
194+
* A convenient alias for addHandler(UpdateType::DialogCleared, $handler).
195+
*
196+
* @param callable(Models\Updates\DialogClearedUpdate, Api): void $handler
197+
*
198+
* @return $this
199+
* @codeCoverageIgnore
200+
*/
201+
public function onDialogCleared(callable $handler): self
202+
{
203+
return $this->addHandler(UpdateType::DialogCleared, $handler);
204+
}
205+
206+
/**
207+
* A convenient alias for addHandler(UpdateType::DialogRemoved, $handler).
208+
*
209+
* @param callable(Models\Updates\DialogRemovedUpdate, Api): void $handler
210+
*
211+
* @return $this
212+
* @codeCoverageIgnore
213+
*/
214+
public function onDialogRemoved(callable $handler): self
215+
{
216+
return $this->addHandler(UpdateType::DialogRemoved, $handler);
217+
}
218+
180219
/**
181220
* A convenient alias for addHandler(UpdateType::UserAdded, $handler).
182221
*
@@ -219,7 +258,7 @@ public function onBotStarted(callable $handler): self
219258
/**
220259
* A convenient alias for addHandler(UpdateType::BotStopped, $handler).
221260
*
222-
* @param callable(Models\Updates\BotStartedUpdate, Api): void $handler
261+
* @param callable(Models\Updates\BotStoppedUpdate, Api): void $handler
223262
*
224263
* @return $this
225264
* @codeCoverageIgnore
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace BushlanovDev\MaxMessengerBot\Tests\Models\Updates;
6+
7+
use BushlanovDev\MaxMessengerBot\Enums\UpdateType;
8+
use BushlanovDev\MaxMessengerBot\Models\Updates\DialogClearedUpdate;
9+
use BushlanovDev\MaxMessengerBot\Models\User;
10+
use PHPUnit\Framework\Attributes\CoversClass;
11+
use PHPUnit\Framework\Attributes\Test;
12+
use PHPUnit\Framework\Attributes\UsesClass;
13+
use PHPUnit\Framework\TestCase;
14+
15+
#[CoversClass(DialogClearedUpdate::class)]
16+
#[UsesClass(User::class)]
17+
final class DialogClearedUpdateTest extends TestCase
18+
{
19+
#[Test]
20+
public function canBeCreatedFromArray(): void
21+
{
22+
$data = [
23+
'update_type' => UpdateType::DialogCleared->value,
24+
'timestamp' => 1678886400000,
25+
'chat_id' => 123,
26+
'user' => [
27+
'user_id' => 123,
28+
'first_name' => 'John',
29+
'last_name' => 'Doe',
30+
'is_bot' => false,
31+
'last_activity_time' => 1678886400000,
32+
],
33+
'user_locale' => 'ru-ru',
34+
];
35+
36+
$update = DialogClearedUpdate::fromArray($data);
37+
38+
$this->assertInstanceOf(DialogClearedUpdate::class, $update);
39+
$this->assertSame(UpdateType::DialogCleared, $update->updateType);
40+
$this->assertSame(123, $update->user->userId);
41+
$this->assertSame('John', $update->user->firstName);
42+
$this->assertSame('Doe', $update->user->lastName);
43+
$this->assertSame('ru-ru', $update->userLocale);
44+
}
45+
}

tests/Models/Updates/DialogMutedUpdateTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
#[CoversClass(DialogMutedUpdate::class)]
1616
#[UsesClass(User::class)]
17-
class DialogMutedUpdateTest extends TestCase
17+
final class DialogMutedUpdateTest extends TestCase
1818
{
1919
#[Test]
2020
public function canBeCreatedFromArray(): void
@@ -30,7 +30,7 @@ public function canBeCreatedFromArray(): void
3030
'is_bot' => false,
3131
'last_activity_time' => 1678886400000,
3232
],
33-
'mutedUntil' => 1678886400000,
33+
'muted_until' => 1678886400000,
3434
'user_locale' => 'ru-ru',
3535
];
3636

@@ -41,6 +41,7 @@ public function canBeCreatedFromArray(): void
4141
$this->assertSame(123, $update->user->userId);
4242
$this->assertSame('John', $update->user->firstName);
4343
$this->assertSame('Doe', $update->user->lastName);
44+
$this->assertSame(1678886400000, $update->mutedUntil);
4445
$this->assertSame('ru-ru', $update->userLocale);
4546
}
4647
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace BushlanovDev\MaxMessengerBot\Tests\Models\Updates;
6+
7+
use BushlanovDev\MaxMessengerBot\Enums\UpdateType;
8+
use BushlanovDev\MaxMessengerBot\Models\Updates\DialogRemovedUpdate;
9+
use BushlanovDev\MaxMessengerBot\Models\User;
10+
use PHPUnit\Framework\Attributes\CoversClass;
11+
use PHPUnit\Framework\Attributes\Test;
12+
use PHPUnit\Framework\Attributes\UsesClass;
13+
use PHPUnit\Framework\TestCase;
14+
15+
#[CoversClass(DialogRemovedUpdate::class)]
16+
#[UsesClass(User::class)]
17+
final class DialogRemovedUpdateTest extends TestCase
18+
{
19+
#[Test]
20+
public function canBeCreatedFromArray(): void
21+
{
22+
$data = [
23+
'update_type' => UpdateType::DialogRemoved->value,
24+
'timestamp' => 1678886400000,
25+
'chat_id' => 123,
26+
'user' => [
27+
'user_id' => 123,
28+
'first_name' => 'John',
29+
'last_name' => 'Doe',
30+
'is_bot' => false,
31+
'last_activity_time' => 1678886400000,
32+
],
33+
'user_locale' => 'ru-ru',
34+
];
35+
36+
$update = DialogRemovedUpdate::fromArray($data);
37+
38+
$this->assertInstanceOf(DialogRemovedUpdate::class, $update);
39+
$this->assertSame(UpdateType::DialogRemoved, $update->updateType);
40+
$this->assertSame(123, $update->user->userId);
41+
$this->assertSame('John', $update->user->firstName);
42+
$this->assertSame('Doe', $update->user->lastName);
43+
$this->assertSame('ru-ru', $update->userLocale);
44+
}
45+
}

0 commit comments

Comments
 (0)