Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add promoteChatMember method #35

Merged
merged 1 commit into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
- New #32: Add `setMessageReaction` method and `ReactionType::toRequestArray()` method.
- New #33: Add `banChatMember` and `unbanChatMember` methods.
- New #34: Add `restrictChatMember` method and `ChatPermissions::toRequestArray()` method.
- New #35: Add `promoteChatMember` method.
- Chg #24: Move update methods to `Vjik\TelegramBot\Api\Method\Update` namespace, and update types to
`Vjik\TelegramBot\Api\Type\Update` namespace.
- Chg #30: Remove `TelegramRequestWithFilesInterface`.
Expand Down
76 changes: 76 additions & 0 deletions src/Method/PromoteChatMember.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

declare(strict_types=1);

namespace Vjik\TelegramBot\Api\Method;

use Vjik\TelegramBot\Api\Request\HttpMethod;
use Vjik\TelegramBot\Api\Request\TelegramRequestWithResultPreparingInterface;

/**
* @see https://core.telegram.org/bots/api#promotechatmember
*/
final readonly class PromoteChatMember implements TelegramRequestWithResultPreparingInterface
{
public function __construct(
private int|string $chatId,
private int $userId,
private ?bool $isAnonymous = null,
private ?bool $canManageChat = null,
private ?bool $canDeleteMessages = null,
private ?bool $canManageVideoChats = null,
private ?bool $canRestrictMembers = null,
private ?bool $canPromoteMembers = null,
private ?bool $canChangeInfo = null,
private ?bool $canInviteUsers = null,
private ?bool $canPostStories = null,
private ?bool $canEditStories = null,
private ?bool $canDeleteStories = null,
private ?bool $canPostMessages = null,
private ?bool $canEditMessages = null,
private ?bool $canPinMessages = null,
private ?bool $canManageTopics = null,
) {
}

public function getHttpMethod(): HttpMethod
{
return HttpMethod::POST;
}

public function getApiMethod(): string
{
return 'promoteChatMember';
}

public function getData(): array
{
return array_filter(
[
'chat_id' => $this->chatId,
'user_id' => $this->userId,
'is_anonymous' => $this->isAnonymous,
'can_manage_chat' => $this->canManageChat,
'can_delete_messages' => $this->canDeleteMessages,
'can_manage_video_chats' => $this->canManageVideoChats,
'can_restrict_members' => $this->canRestrictMembers,
'can_promote_members' => $this->canPromoteMembers,
'can_change_info' => $this->canChangeInfo,
'can_invite_users' => $this->canInviteUsers,
'can_post_stories' => $this->canPostStories,
'can_edit_stories' => $this->canEditStories,
'can_delete_stories' => $this->canDeleteStories,
'can_post_messages' => $this->canPostMessages,
'can_edit_messages' => $this->canEditMessages,
'can_pin_messages' => $this->canPinMessages,
'can_manage_topics' => $this->canManageTopics,
],
static fn(mixed $value): bool => $value !== null,
);
}

public function prepareResult(mixed $result): true
{
return true;
}
}
48 changes: 48 additions & 0 deletions src/TelegramBotApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Vjik\TelegramBot\Api\Method\GetUserProfilePhotos;
use Vjik\TelegramBot\Api\Method\LogOut;
use Vjik\TelegramBot\Api\Method\Payments\GetStarTransactions;
use Vjik\TelegramBot\Api\Method\PromoteChatMember;
use Vjik\TelegramBot\Api\Method\RestrictChatMember;
use Vjik\TelegramBot\Api\Method\SendAnimation;
use Vjik\TelegramBot\Api\Method\SendAudio;
Expand Down Expand Up @@ -440,6 +441,53 @@ public function logOut(): FailResult|true
return $this->send(new LogOut());
}

/**
* @see https://core.telegram.org/bots/api#promotechatmember
*
* @psalm-suppress MixedInferredReturnType,MixedReturnStatement
*/
public function promoteChatMember(
int|string $chatId,
int $userId,
?bool $isAnonymous = null,
?bool $canManageChat = null,
?bool $canDeleteMessages = null,
?bool $canManageVideoChats = null,
?bool $canRestrictMembers = null,
?bool $canPromoteMembers = null,
?bool $canChangeInfo = null,
?bool $canInviteUsers = null,
?bool $canPostStories = null,
?bool $canEditStories = null,
?bool $canDeleteStories = null,
?bool $canPostMessages = null,
?bool $canEditMessages = null,
?bool $canPinMessages = null,
?bool $canManageTopics = null,
): FailResult|true {
return $this->send(
new PromoteChatMember(
$chatId,
$userId,
$isAnonymous,
$canManageChat,
$canDeleteMessages,
$canManageVideoChats,
$canRestrictMembers,
$canPromoteMembers,
$canChangeInfo,
$canInviteUsers,
$canPostStories,
$canEditStories,
$canDeleteStories,
$canPostMessages,
$canEditMessages,
$canPinMessages,
$canManageTopics
)
);
}

/**
* @see https://core.telegram.org/bots/api#restrictchatmember
*
Expand Down
82 changes: 82 additions & 0 deletions tests/Method/PromoteChatMemberTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

declare(strict_types=1);

namespace Vjik\TelegramBot\Api\Tests\Method;

use PHPUnit\Framework\TestCase;
use Vjik\TelegramBot\Api\Method\PromoteChatMember;
use Vjik\TelegramBot\Api\Request\HttpMethod;

final class PromoteChatMemberTest extends TestCase
{
public function testBase(): void
{
$method = new PromoteChatMember(1, 2);

$this->assertSame(HttpMethod::POST, $method->getHttpMethod());
$this->assertSame('promoteChatMember', $method->getApiMethod());
$this->assertSame(
[
'chat_id' => 1,
'user_id' => 2,
],
$method->getData(),
);
}

public function testFull(): void
{
$method = new PromoteChatMember(
1,
2,
true,
true,
false,
true,
false,
false,
false,
true,
true,
false,
true,
false,
true,
true,
true,
);

$this->assertSame(
[
'chat_id' => 1,
'user_id' => 2,
'is_anonymous' => true,
'can_manage_chat' => true,
'can_delete_messages' => false,
'can_manage_video_chats' => true,
'can_restrict_members' => false,
'can_promote_members' => false,
'can_change_info' => false,
'can_invite_users' => true,
'can_post_stories' => true,
'can_edit_stories' => false,
'can_delete_stories' => true,
'can_post_messages' => false,
'can_edit_messages' => true,
'can_pin_messages' => true,
'can_manage_topics' => true,
],
$method->getData()
);
}

public function testPrepareResult(): void
{
$method = new PromoteChatMember(1, 2);

$preparedResult = $method->prepareResult(true);

$this->assertTrue($preparedResult);
}
}
12 changes: 12 additions & 0 deletions tests/TelegramBotApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,18 @@ public function testLogOut(): void
$this->assertTrue($result);
}

public function testPromoteChatMember(): void
{
$api = $this->createApi([
'ok' => true,
'result' => true,
]);

$result = $api->promoteChatMember(1, 2);

$this->assertTrue($result);
}

public function testRestrictChatMember(): void
{
$api = $this->createApi([
Expand Down
Loading