Skip to content

Commit d5b950b

Browse files
committed
Added MessageCallbackUpdate & MessageEditedUpdate & MessageRemovedUpdate & BotAddedToChatUpdate
1 parent 62c4b80 commit d5b950b

14 files changed

+503
-1
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"sort-packages": true
4242
},
4343
"scripts": {
44-
"analyse": "vendor/bin/phpstan analyse -c phpstan.neon",
44+
"analyse": "vendor/bin/phpstan analyse -c phpstan.neon --memory-limit=256M",
4545
"format": "vendor/bin/php-cs-fixer fix --allow-risky=yes src",
4646
"test": "vendor/bin/phpunit",
4747
"test-coverage": "vendor/bin/phpunit --coverage-html coverage"

src/Api.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,12 @@ public function __construct(
6868
) {
6969
if ($client === null) {
7070
if (!class_exists(\GuzzleHttp\Client::class) || !class_exists(\GuzzleHttp\Psr7\HttpFactory::class)) {
71+
// @codeCoverageIgnoreStart
7172
throw new LogicException(
7273
'No client was provided and "guzzlehttp/guzzle" is not found. ' .
7374
'Please run "composer require guzzlehttp/guzzle" or create and pass your own implementation of ClientApiInterface.'
7475
);
76+
// @codeCoverageIgnoreEnd
7577
}
7678

7779
$guzzle = new \GuzzleHttp\Client();

src/ModelFactory.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@
1212
use BushlanovDev\MaxMessengerBot\Models\Subscription;
1313
use BushlanovDev\MaxMessengerBot\Models\UpdateList;
1414
use BushlanovDev\MaxMessengerBot\Models\Updates\AbstractUpdate;
15+
use BushlanovDev\MaxMessengerBot\Models\Updates\BotAddedToChatUpdate;
1516
use BushlanovDev\MaxMessengerBot\Models\Updates\BotStartedUpdate;
17+
use BushlanovDev\MaxMessengerBot\Models\Updates\MessageCallbackUpdate;
1618
use BushlanovDev\MaxMessengerBot\Models\Updates\MessageCreatedUpdate;
19+
use BushlanovDev\MaxMessengerBot\Models\Updates\MessageEditedUpdate;
20+
use BushlanovDev\MaxMessengerBot\Models\Updates\MessageRemovedUpdate;
1721
use BushlanovDev\MaxMessengerBot\Models\UploadEndpoint;
1822
use LogicException;
1923
use ReflectionException;
@@ -154,6 +158,10 @@ public function createUpdate(array $data): AbstractUpdate
154158
{
155159
return match (UpdateType::tryFrom($data['update_type'] ?? '')) {
156160
UpdateType::MessageCreated => MessageCreatedUpdate::fromArray($data),
161+
UpdateType::MessageCallback => MessageCallbackUpdate::fromArray($data),
162+
UpdateType::MessageEdited => MessageEditedUpdate::fromArray($data),
163+
UpdateType::MessageRemoved => MessageRemovedUpdate::fromArray($data),
164+
UpdateType::BotAdded => BotAddedToChatUpdate::fromArray($data),
157165
UpdateType::BotStarted => BotStartedUpdate::fromArray($data),
158166
default => throw new LogicException(
159167
'Unknown or unsupported update type received: ' . ($data['update_type'] ?? 'none')

src/Models/Callback.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace BushlanovDev\MaxMessengerBot\Models;
6+
7+
/**
8+
* Object sent to bot when user presses a callback button.
9+
*/
10+
final readonly class Callback extends AbstractModel
11+
{
12+
/**
13+
* @param int $timestamp Unix-time when user pressed the button.
14+
* @param string $callbackId Identifier of the callback, unique for the message.
15+
* @param string $payload Payload from the pressed button.
16+
* @param User $user User who pressed the button.
17+
*/
18+
public function __construct(
19+
public int $timestamp,
20+
public string $callbackId,
21+
public string $payload,
22+
public User $user,
23+
) {
24+
}
25+
}
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+
* You will receive this update when the bot has been added to a chat.
12+
*/
13+
final readonly class BotAddedToChatUpdate extends AbstractUpdate
14+
{
15+
/**
16+
* @param int $timestamp Unix-time when the event has occurred.
17+
* @param int $chatId Chat ID where the bot was added.
18+
* @param User $user User who added the bot to the chat.
19+
* @param bool $isChannel Indicates whether the bot has been added to a channel or not.
20+
*/
21+
public function __construct(
22+
int $timestamp,
23+
public int $chatId,
24+
public User $user,
25+
public bool $isChannel,
26+
) {
27+
parent::__construct(UpdateType::BotAdded, $timestamp);
28+
}
29+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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\Callback;
9+
use BushlanovDev\MaxMessengerBot\Models\Message;
10+
11+
/**
12+
* You will get this update as soon as a user presses a callback button.
13+
*/
14+
final readonly class MessageCallbackUpdate extends AbstractUpdate
15+
{
16+
/**
17+
* @param int $timestamp Unix-time when event has occurred.
18+
* @param Callback $callback The callback query itself.
19+
* @param Message|null $message Original message with the inline keyboard. Can be null if the message was deleted.
20+
* @param string|null $userLocale Current user locale in IETF BCP 47 format.
21+
*/
22+
public function __construct(
23+
int $timestamp,
24+
public Callback $callback,
25+
public ?Message $message,
26+
public ?string $userLocale,
27+
) {
28+
parent::__construct(UpdateType::MessageCallback, $timestamp);
29+
}
30+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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\Message;
9+
10+
/**
11+
* You will get this update as soon as a message is edited.
12+
*/
13+
final readonly class MessageEditedUpdate extends AbstractUpdate
14+
{
15+
/**
16+
* @param int $timestamp Unix-time when the event has occurred.
17+
* @param Message $message The edited message.
18+
*/
19+
public function __construct(
20+
int $timestamp,
21+
public Message $message,
22+
) {
23+
parent::__construct(UpdateType::MessageEdited, $timestamp);
24+
}
25+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace BushlanovDev\MaxMessengerBot\Models\Updates;
6+
7+
use BushlanovDev\MaxMessengerBot\Enums\UpdateType;
8+
9+
/**
10+
* You will get this update as soon as a message is removed.
11+
*/
12+
final readonly class MessageRemovedUpdate extends AbstractUpdate
13+
{
14+
/**
15+
* @param int $timestamp Unix-time when the event has occurred.
16+
* @param string $messageId Identifier of the removed message.
17+
* @param int $chatId Chat identifier where the message has been deleted.
18+
* @param int $userId User who deleted this message.
19+
*/
20+
public function __construct(
21+
int $timestamp,
22+
public string $messageId,
23+
public int $chatId,
24+
public int $userId,
25+
) {
26+
parent::__construct(UpdateType::MessageRemoved, $timestamp);
27+
}
28+
}

src/WebhookHandler.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,57 @@ public function onMessageCreated(callable $handler): self
6262
return $this->addHandler(UpdateType::MessageCreated, $handler);
6363
}
6464

65+
/**
66+
* A convenient alias for addHandler(UpdateType::MessageCallback, $handler).
67+
*
68+
* @param callable(Models\Updates\MessageCallbackUpdate, Api): void $handler
69+
*
70+
* @return $this
71+
* @codeCoverageIgnore
72+
*/
73+
public function onMessageCallback(callable $handler): self
74+
{
75+
return $this->addHandler(UpdateType::MessageCallback, $handler);
76+
}
77+
78+
/**
79+
* A convenient alias for addHandler(UpdateType::MessageEdited, $handler).
80+
*
81+
* @param callable(Models\Updates\MessageEditedUpdate, Api): void $handler
82+
*
83+
* @return $this
84+
* @codeCoverageIgnore
85+
*/
86+
public function onMessageEdited(callable $handler): self
87+
{
88+
return $this->addHandler(UpdateType::MessageEdited, $handler);
89+
}
90+
91+
/**
92+
* A convenient alias for addHandler(UpdateType::MessageRemoved, $handler).
93+
*
94+
* @param callable(Models\Updates\MessageRemovedUpdate, Api): void $handler
95+
*
96+
* @return $this
97+
* @codeCoverageIgnore
98+
*/
99+
public function onMessageRemoved(callable $handler): self
100+
{
101+
return $this->addHandler(UpdateType::MessageRemoved, $handler);
102+
}
103+
/**
104+
* A convenient alias for addHandler(UpdateType::BotAdded, $handler).
105+
*
106+
* @param callable(Models\Updates\BotAddedToChatUpdate, Api): void $handler
107+
*
108+
* @return $this
109+
* @codeCoverageIgnore
110+
*/
111+
public function onBotAdded(callable $handler): self
112+
{
113+
return $this->addHandler(UpdateType::BotAdded, $handler);
114+
}
115+
65116
/**
66117
* A convenient alias for addHandler(UpdateType::BotStarted, $handler).
67118
*
@@ -92,10 +143,12 @@ public function handle(?ServerRequestInterface $request = null): void
92143
{
93144
if ($request === null) {
94145
if (!class_exists(\GuzzleHttp\Psr7\ServerRequest::class)) {
146+
// @codeCoverageIgnoreStart
95147
throw new \LogicException(
96148
'No ServerRequest was provided and "guzzlehttp/psr7" is not found. ' .
97149
'Please run "composer require guzzlehttp/psr7" or create and pass your own PSR-7 request object.',
98150
);
151+
// @codeCoverageIgnoreEnd
99152
}
100153
$request = \GuzzleHttp\Psr7\ServerRequest::fromGlobals();
101154
}

tests/Models/CallbackTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace BushlanovDev\MaxMessengerBot\Tests\Models;
6+
7+
use BushlanovDev\MaxMessengerBot\Models\Callback;
8+
use BushlanovDev\MaxMessengerBot\Models\User;
9+
use PHPUnit\Framework\Attributes\CoversClass;
10+
use PHPUnit\Framework\Attributes\Test;
11+
use PHPUnit\Framework\Attributes\UsesClass;
12+
use PHPUnit\Framework\TestCase;
13+
14+
#[CoversClass(Callback::class)]
15+
#[UsesClass(User::class)]
16+
final class CallbackTest extends TestCase
17+
{
18+
#[Test]
19+
public function canBeCreatedFromArray(): void
20+
{
21+
$data = [
22+
'timestamp' => 1678886400,
23+
'callback_id' => 'cb.12345.abc',
24+
'payload' => 'button_1_pressed',
25+
'user' => [
26+
'user_id' => 101,
27+
'first_name' => 'Jane',
28+
'is_bot' => false,
29+
'last_activity_time' => 1678886000,
30+
'last_name' => null,
31+
'username' => null,
32+
'description' => null,
33+
'avatar_url' => null,
34+
'full_avatar_url' => null,
35+
],
36+
];
37+
38+
$callback = Callback::fromArray($data);
39+
40+
$this->assertInstanceOf(Callback::class, $callback);
41+
$this->assertSame(1678886400, $callback->timestamp);
42+
$this->assertSame('cb.12345.abc', $callback->callbackId);
43+
$this->assertSame('button_1_pressed', $callback->payload);
44+
$this->assertInstanceOf(User::class, $callback->user);
45+
$this->assertSame(101, $callback->user->userId);
46+
$this->assertEquals($data, $callback->toArray());
47+
}
48+
}

0 commit comments

Comments
 (0)