Skip to content

Commit 67ce295

Browse files
committed
Added getChats & getChatByLink & deleteChat & sendAction
1 parent f195f98 commit 67ce295

File tree

8 files changed

+419
-4
lines changed

8 files changed

+419
-4
lines changed

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@
3030

3131
#### Chats
3232

33-
- [ ] `GET /chats` (`getChats`) — *Получение списка всех чатов бота.*
34-
- [ ] `GET /chats/{chatLink}` (`getChatByLink`) — *Получение информации о чате по ссылке.*
33+
- [x] `GET /chats` (`getChats`) — *Получение списка всех чатов бота.*
34+
- [x] `GET /chats/{chatLink}` (`getChatByLink`) — *Получение информации о чате по ссылке.*
3535
- [x] `GET /chats/{chatId}` (`getChat`) — *Получение информации о чате по ID.*
3636
- [ ] `PATCH /chats/{chatId}` (`editChat`) — *Редактирование информации о чате.*
37-
- [ ] `DELETE /chats/{chatId}` (`deleteChat`) — *Удаление чата.*
38-
- [ ] `POST /chats/{chatId}/actions` (`sendAction`) — *Отправка действия в чат (например, "печатает...").*
37+
- [x] `DELETE /chats/{chatId}` (`deleteChat`) — *Удаление чата.*
38+
- [x] `POST /chats/{chatId}/actions` (`sendAction`) — *Отправка действия в чат (например, "печатает...").*
3939
- [ ] `GET /chats/{chatId}/pin` (`getPinnedMessage`) — *Получение закрепленного сообщения.*
4040
- [ ] `PUT /chats/{chatId}/pin` (`pinMessage`) — *Закрепление сообщения.*
4141
- [ ] `DELETE /chats/{chatId}/pin` (`unpinMessage`) — *Открепление сообщения.*
@@ -68,3 +68,7 @@
6868
- [ ] `GET /messages/{messageId}` (`getMessageById`) — *Получение сообщения по ID.*
6969
- [ ] `GET /videos/{videoToken}` (`getVideoAttachmentDetails`) — *Получение детальной информации о видео.*
7070
- [ ] `POST /answers` (`answerOnCallback`) — *Ответ на нажатие callback-кнопки.*
71+
72+
## Лицензия
73+
74+
Данная библиотека распространяется по лицензии MIT - подробности см. в файле [LICENSE](LICENSE).

src/Api.php

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace BushlanovDev\MaxMessengerBot;
66

77
use BushlanovDev\MaxMessengerBot\Enums\MessageFormat;
8+
use BushlanovDev\MaxMessengerBot\Enums\SenderAction;
89
use BushlanovDev\MaxMessengerBot\Enums\UpdateType;
910
use BushlanovDev\MaxMessengerBot\Enums\UploadType;
1011
use BushlanovDev\MaxMessengerBot\Exceptions\ClientApiException;
@@ -19,6 +20,7 @@
1920
use BushlanovDev\MaxMessengerBot\Models\Attachments\Requests\VideoAttachmentRequest;
2021
use BushlanovDev\MaxMessengerBot\Models\BotInfo;
2122
use BushlanovDev\MaxMessengerBot\Models\Chat;
23+
use BushlanovDev\MaxMessengerBot\Models\ChatList;
2224
use BushlanovDev\MaxMessengerBot\Models\Message;
2325
use BushlanovDev\MaxMessengerBot\Models\MessageLink;
2426
use BushlanovDev\MaxMessengerBot\Models\Result;
@@ -46,6 +48,7 @@ class Api
4648
private const string METHOD_GET = 'GET';
4749
private const string METHOD_POST = 'POST';
4850
private const string METHOD_DELETE = 'DELETE';
51+
// private const string METHOD_PATCH = 'PATCH';
4952

5053
private const string ACTION_ME = '/me';
5154
private const string ACTION_SUBSCRIPTIONS = '/subscriptions';
@@ -477,4 +480,99 @@ public function getChat(int $chatId): Chat
477480
$this->client->request(self::METHOD_GET, self::ACTION_CHATS . '/' . $chatId)
478481
);
479482
}
483+
484+
/**
485+
* Returns chat/channel information by its public link or a dialog with a user by their username.
486+
* The link should be prefixed with '@' or can be passed without it.
487+
*
488+
* @param string $chatLink Public chat link (e.g., '@mychannel') or username (e.g., '@john_doe').
489+
*
490+
* @return Chat
491+
* @throws ClientApiException
492+
* @throws NetworkException
493+
* @throws ReflectionException
494+
* @throws SerializationException
495+
*/
496+
public function getChatByLink(string $chatLink): Chat
497+
{
498+
return $this->modelFactory->createChat(
499+
$this->client->request(
500+
self::METHOD_GET,
501+
self::ACTION_CHATS . '/' . $chatLink,
502+
)
503+
);
504+
}
505+
506+
/**
507+
* Returns information about chats that the bot participated in. The result is a paginated list.
508+
*
509+
* @param int|null $count Number of chats requested (1-100, default 50).
510+
* @param int|null $marker Points to the next data page. Use null for the first page.
511+
*
512+
* @return ChatList
513+
* @throws ClientApiException
514+
* @throws NetworkException
515+
* @throws ReflectionException
516+
* @throws SerializationException
517+
*/
518+
public function getChats(?int $count = null, ?int $marker = null): ChatList
519+
{
520+
$query = [
521+
'count' => $count,
522+
'marker' => $marker,
523+
];
524+
525+
return $this->modelFactory->createChatList(
526+
$this->client->request(
527+
self::METHOD_GET,
528+
self::ACTION_CHATS,
529+
array_filter($query, fn($value) => $value !== null),
530+
)
531+
);
532+
}
533+
534+
/**
535+
* Deletes a chat for all participants. The bot must have appropriate permissions.
536+
*
537+
* @param int $chatId Chat identifier to delete.
538+
*
539+
* @return Result
540+
* @throws ClientApiException
541+
* @throws NetworkException
542+
* @throws ReflectionException
543+
* @throws SerializationException
544+
*/
545+
public function deleteChat(int $chatId): Result
546+
{
547+
return $this->modelFactory->createResult(
548+
$this->client->request(
549+
self::METHOD_DELETE,
550+
self::ACTION_CHATS . '/' . $chatId,
551+
)
552+
);
553+
}
554+
555+
/**
556+
* Sends a specific action to a chat, such as 'typing...'. This is used to show bot activity to the user.
557+
*
558+
* @param int $chatId The identifier of the target chat.
559+
* @param SenderAction $action The action to be sent.
560+
*
561+
* @return Result
562+
* @throws ClientApiException
563+
* @throws NetworkException
564+
* @throws ReflectionException
565+
* @throws SerializationException
566+
*/
567+
public function sendAction(int $chatId, SenderAction $action): Result
568+
{
569+
return $this->modelFactory->createResult(
570+
$this->client->request(
571+
self::METHOD_POST,
572+
self::ACTION_CHATS . '/' . $chatId . '/actions',
573+
[],
574+
['action' => $action->value],
575+
)
576+
);
577+
}
480578
}

src/Enums/SenderAction.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace BushlanovDev\MaxMessengerBot\Enums;
6+
7+
/**
8+
* Represents the different actions a bot can send to a chat to indicate its status.
9+
*/
10+
enum SenderAction: string
11+
{
12+
case TypingOn = 'typing_on';
13+
case SendingPhoto = 'sending_photo';
14+
case SendingVideo = 'sending_video';
15+
case SendingAudio = 'sending_audio';
16+
case SendingFile = 'sending_file';
17+
case MarkSeen = 'mark_seen';
18+
}

src/ModelFactory.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use BushlanovDev\MaxMessengerBot\Enums\UpdateType;
88
use BushlanovDev\MaxMessengerBot\Models\BotInfo;
99
use BushlanovDev\MaxMessengerBot\Models\Chat;
10+
use BushlanovDev\MaxMessengerBot\Models\ChatList;
1011
use BushlanovDev\MaxMessengerBot\Models\Message;
1112
use BushlanovDev\MaxMessengerBot\Models\Result;
1213
use BushlanovDev\MaxMessengerBot\Models\Subscription;
@@ -178,4 +179,17 @@ public function createUpdate(array $data): AbstractUpdate
178179
),
179180
};
180181
}
182+
183+
/**
184+
* Information about chat list.
185+
*
186+
* @param array<string, mixed> $data
187+
*
188+
* @return ChatList
189+
* @throws ReflectionException
190+
*/
191+
public function createChatList(array $data): ChatList
192+
{
193+
return ChatList::fromArray($data);
194+
}
181195
}

src/Models/ChatList.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace BushlanovDev\MaxMessengerBot\Models;
6+
7+
use BushlanovDev\MaxMessengerBot\Attributes\ArrayOf;
8+
9+
/**
10+
* Represents a paginated list of chats.
11+
*/
12+
final readonly class ChatList extends AbstractModel
13+
{
14+
/**
15+
* @param Chat[] $chats List of requested chats.
16+
* @param int|null $marker Reference to the next page of requested chats. Can be null if it's the last page.
17+
*/
18+
public function __construct(
19+
#[ArrayOf(Chat::class)]
20+
public array $chats,
21+
public ?int $marker,
22+
) {
23+
}
24+
}

0 commit comments

Comments
 (0)