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 getChatAdministrators, getChatMemberCount and getChatMember methods #47

Merged
merged 2 commits into from
Jun 27, 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 @@ -31,6 +31,7 @@
- New #43: Add `setChatTitle` and `setChatDescription` methods.
- New #44: Add `pinChatMessage`, `unpinChatMessage` and `unpinAllChatMessages` methods.
- New #46: Add `leaveChat` method.
- New #47: Add `getChatAdministrators`, `getChatMemberCount` and `getChatMember` methods.
- 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
49 changes: 49 additions & 0 deletions src/Method/GetChatAdministrators.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Vjik\TelegramBot\Api\Method;

use Vjik\TelegramBot\Api\ParseResult\ValueHelper;
use Vjik\TelegramBot\Api\Request\HttpMethod;
use Vjik\TelegramBot\Api\Request\TelegramRequestWithResultPreparingInterface;
use Vjik\TelegramBot\Api\Type\ChatMember;
use Vjik\TelegramBot\Api\Type\ChatMemberFactory;

/**
* @see https://core.telegram.org/bots/api#getchatadministrators
*/
final readonly class GetChatAdministrators implements TelegramRequestWithResultPreparingInterface
{
public function __construct(
private int|string $chatId,
) {
}

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

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

public function getData(): array
{
return ['chat_id' => $this->chatId];
}

/**
* @return ChatMember[]
*/
public function prepareResult(mixed $result): array
{
ValueHelper::assertArrayResult($result);
return array_map(
static fn(mixed $item) => ChatMemberFactory::fromTelegramResult($item),
$result
);
}
}
45 changes: 45 additions & 0 deletions src/Method/GetChatMember.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace Vjik\TelegramBot\Api\Method;

use Vjik\TelegramBot\Api\Request\HttpMethod;
use Vjik\TelegramBot\Api\Request\TelegramRequestWithResultPreparingInterface;
use Vjik\TelegramBot\Api\Type\ChatMember;
use Vjik\TelegramBot\Api\Type\ChatMemberFactory;

/**
* @see https://core.telegram.org/bots/api#getchatmember
*/
final readonly class GetChatMember implements TelegramRequestWithResultPreparingInterface
{
public function __construct(
private int|string $chatId,
private int $userId,
) {
}

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

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

public function getData(): array
{
return [
'chat_id' => $this->chatId,
'user_id' => $this->userId,
];
}

public function prepareResult(mixed $result): ChatMember
{
return ChatMemberFactory::fromTelegramResult($result);
}
}
41 changes: 41 additions & 0 deletions src/Method/GetChatMemberCount.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Vjik\TelegramBot\Api\Method;

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

/**
* @see https://core.telegram.org/bots/api#getchatmembercount
*/
final readonly class GetChatMemberCount implements TelegramRequestWithResultPreparingInterface
{
public function __construct(
private int|string $chatId,
) {
}

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

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

public function getData(): array
{
return ['chat_id' => $this->chatId];
}

public function prepareResult(mixed $result): int
{
ValueHelper::assertIntegerResult($result);
return $result;
}
}
12 changes: 12 additions & 0 deletions src/ParseResult/ValueHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ public static function assertStringResult(mixed $result): void
}
}

/**
* @psalm-assert int $result
*/
public static function assertIntegerResult(mixed $result): void
{
if (!is_int($result)) {
throw new TelegramParseResultException(
'Expected result as integer. Got "' . get_debug_type($result) . '".'
);
}
}

/**
* @psalm-assert true $result
*/
Expand Down
36 changes: 36 additions & 0 deletions src/TelegramBotApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
use Vjik\TelegramBot\Api\Method\ForwardMessage;
use Vjik\TelegramBot\Api\Method\ForwardMessages;
use Vjik\TelegramBot\Api\Method\GetChat;
use Vjik\TelegramBot\Api\Method\GetChatAdministrators;
use Vjik\TelegramBot\Api\Method\GetChatMember;
use Vjik\TelegramBot\Api\Method\GetChatMemberCount;
use Vjik\TelegramBot\Api\Method\GetChatMenuButton;
use Vjik\TelegramBot\Api\Method\GetFile;
use Vjik\TelegramBot\Api\Method\GetMe;
Expand Down Expand Up @@ -78,6 +81,7 @@
use Vjik\TelegramBot\Api\Type\BotShortDescription;
use Vjik\TelegramBot\Api\Type\ChatFullInfo;
use Vjik\TelegramBot\Api\Type\ChatInviteLink;
use Vjik\TelegramBot\Api\Type\ChatMember;
use Vjik\TelegramBot\Api\Type\ChatPermissions;
use Vjik\TelegramBot\Api\Type\File;
use Vjik\TelegramBot\Api\Type\ForceReply;
Expand Down Expand Up @@ -421,6 +425,38 @@ public function getChat(int|string $chatId): FailResult|ChatFullInfo
return $this->send(new GetChat($chatId));
}

/**
* @see https://core.telegram.org/bots/api#getchatadministrators
*
* @return FailResult|ChatMember[]
*
* @psalm-suppress MixedInferredReturnType,MixedReturnStatement
*/
public function getChatAdministrators(int|string $chatId): FailResult|array
{
return $this->send(new GetChatAdministrators($chatId));
}

/**
* @see https://core.telegram.org/bots/api#getchatmembercount
*
* @psalm-suppress MixedInferredReturnType,MixedReturnStatement
*/
public function getChatMemberCount(int|string $chatId): FailResult|int
{
return $this->send(new GetChatMemberCount($chatId));
}

/**
* @see https://core.telegram.org/bots/api#getchatmember
*
* @psalm-suppress MixedInferredReturnType,MixedReturnStatement
*/
public function getChatMember(int|string $chatId, int $userId): FailResult|ChatMember
{
return $this->send(new GetChatMember($chatId, $userId));
}

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

declare(strict_types=1);

namespace Vjik\TelegramBot\Api\Tests\Method;

use PHPUnit\Framework\TestCase;
use Vjik\TelegramBot\Api\Method\GetChatAdministrators;
use Vjik\TelegramBot\Api\Request\HttpMethod;
use Vjik\TelegramBot\Api\Type\ChatMemberMember;

final class GetChatAdministratorsTest extends TestCase
{
public function testBase(): void
{
$method = new GetChatAdministrators(1);

$this->assertSame(HttpMethod::GET, $method->getHttpMethod());
$this->assertSame('getChatAdministrators', $method->getApiMethod());
$this->assertSame(
[
'chat_id' => 1,
],
$method->getData(),
);
}

public function testPrepareResult(): void
{
$method = new GetChatAdministrators(1);

$preparedResult = $method->prepareResult([
[
'status' => 'member',
'user' => ['id' => 23, 'is_bot' => false, 'first_name' => 'Mike'],
],
]);

$this->assertCount(1, $preparedResult);
$this->assertInstanceOf(ChatMemberMember::class, $preparedResult[0]);
$this->assertSame(23, $preparedResult[0]->user->id);
}
}
35 changes: 35 additions & 0 deletions tests/Method/GetChatMemberCountTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Vjik\TelegramBot\Api\Tests\Method;

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

final class GetChatMemberCountTest extends TestCase
{
public function testBase(): void
{
$method = new GetChatMemberCount(1);

$this->assertSame(HttpMethod::GET, $method->getHttpMethod());
$this->assertSame('getChatMemberCount', $method->getApiMethod());
$this->assertSame(
[
'chat_id' => 1,
],
$method->getData(),
);
}

public function testPrepareResult(): void
{
$method = new GetChatMemberCount(1);

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

$this->assertSame(23, $preparedResult);
}
}
41 changes: 41 additions & 0 deletions tests/Method/GetChatMemberTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Vjik\TelegramBot\Api\Tests\Method;

use PHPUnit\Framework\TestCase;
use Vjik\TelegramBot\Api\Method\GetChatMember;
use Vjik\TelegramBot\Api\Request\HttpMethod;
use Vjik\TelegramBot\Api\Type\ChatMemberMember;

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

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

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

$preparedResult = $method->prepareResult([
'status' => 'member',
'user' => ['id' => 23, 'is_bot' => false, 'first_name' => 'Mike'],
]);

$this->assertInstanceOf(ChatMemberMember::class, $preparedResult);
$this->assertSame(23, $preparedResult->user->id);
}
}
9 changes: 9 additions & 0 deletions tests/ParseResult/ValueHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ public function testAssertStringResult(): void
ValueHelper::assertStringResult([]);
}

public function testAssertIntegerResult(): void
{
ValueHelper::assertIntegerResult(25);

$this->expectException(TelegramParseResultException::class);
$this->expectExceptionMessage('Expected result as integer. Got "array".');
ValueHelper::assertIntegerResult([]);
}

public function testAssertTrueResult(): void
{
ValueHelper::assertTrueResult(true);
Expand Down
Loading
Loading