Skip to content

Commit

Permalink
Add getCustomEmojiStickers method (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik authored Jun 27, 2024
1 parent 7d72a48 commit 560c68e
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
- New #50: Add `deleteStickerSet` method.
- New #51: Add `getStickerSet` method and `StickerSet` type.
- New #52: Add `sendSticker` method.
- New #54: Add `getCustomEmojiStickers` 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
50 changes: 50 additions & 0 deletions src/Method/Sticker/GetCustomEmojiStickers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace Vjik\TelegramBot\Api\Method\Sticker;

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

/**
* @see https://core.telegram.org/bots/api#getcustomemojistickers
*/
final readonly class GetCustomEmojiStickers implements TelegramRequestWithResultPreparingInterface
{
/**
* @param string[] $customEmojiIds
*/
public function __construct(
private array $customEmojiIds,
) {
}

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

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

public function getData(): array
{
return [
'custom_emoji_ids' => $this->customEmojiIds,
];
}

/**
* @return Sticker[]
*/
public function prepareResult(mixed $result): array
{
ValueHelper::assertArrayResult($result);
return ValueHelper::getArrayOfStickers($result);
}
}
6 changes: 3 additions & 3 deletions src/ParseResult/ValueHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -445,12 +445,12 @@ public static function getArrayOfStarTransactions(array $result, string $key): a
/**
* @return Sticker[]
*/
public static function getArrayOfStickers(array $result, string $key): array
public static function getArrayOfStickers(array $result, ?string $key = null): array
{
$transactions = ValueHelper::getArray($result, $key);
$stickers = $key === null ? $result : ValueHelper::getArray($result, $key);
return array_map(
static fn($item) => Sticker::fromTelegramResult($item),
$transactions
$stickers
);
}

Expand Down
15 changes: 15 additions & 0 deletions src/TelegramBotApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
use Vjik\TelegramBot\Api\Method\SetMyShortDescription;
use Vjik\TelegramBot\Api\Method\Sticker\CreateNewStickerSet;
use Vjik\TelegramBot\Api\Method\Sticker\DeleteStickerSet;
use Vjik\TelegramBot\Api\Method\Sticker\GetCustomEmojiStickers;
use Vjik\TelegramBot\Api\Method\Sticker\GetStickerSet;
use Vjik\TelegramBot\Api\Method\Sticker\SendSticker;
use Vjik\TelegramBot\Api\Method\UnbanChatMember;
Expand Down Expand Up @@ -108,6 +109,7 @@
use Vjik\TelegramBot\Api\Type\ReplyParameters;
use Vjik\TelegramBot\Api\Type\ResponseParameters;
use Vjik\TelegramBot\Api\Type\Sticker\InputSticker;
use Vjik\TelegramBot\Api\Type\Sticker\Sticker;
use Vjik\TelegramBot\Api\Type\Sticker\StickerSet;
use Vjik\TelegramBot\Api\Type\User;
use Vjik\TelegramBot\Api\Type\UserProfilePhotos;
Expand Down Expand Up @@ -503,6 +505,19 @@ public function getChatMenuButton(?int $chatId = null): FailResult|MenuButton
return $this->send(new GetChatMenuButton($chatId));
}

/**
* @see https://core.telegram.org/bots/api#getcustomemojistickers
*
* @param string[] $customEmojiIds
* @return FailResult|Sticker[]
*
* @psalm-suppress MixedInferredReturnType,MixedReturnStatement
*/
public function getCustomEmojiStickers(array $customEmojiIds): FailResult|array
{
return $this->send(new GetCustomEmojiStickers($customEmojiIds));
}

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

declare(strict_types=1);

namespace Vjik\TelegramBot\Api\Tests\Method\Sticker;

use PHPUnit\Framework\TestCase;
use Vjik\TelegramBot\Api\Method\Sticker\GetCustomEmojiStickers;
use Vjik\TelegramBot\Api\Request\HttpMethod;
use Vjik\TelegramBot\Api\Type\Sticker\Sticker;

final class GetCustomEmojiStickersTest extends TestCase
{
public function testBase(): void
{
$method = new GetCustomEmojiStickers(['id1']);

$this->assertSame(HttpMethod::GET, $method->getHttpMethod());
$this->assertSame('getCustomEmojiStickers', $method->getApiMethod());
$this->assertSame(
[
'custom_emoji_ids' => ['id1'],
],
$method->getData(),
);
}

public function testPrepareResult(): void
{
$method = new GetCustomEmojiStickers(['id1']);

$preparedResult = $method->prepareResult([
[
'file_id' => 'x1',
'file_unique_id' => 'fullX1',
'type' => 'regular',
'width' => 100,
'height' => 120,
'is_animated' => false,
'is_video' => true,
],
]);

$this->assertIsArray($preparedResult);
$this->assertCount(1, $preparedResult);
$this->assertInstanceOf(Sticker::class, $preparedResult[0]);
$this->assertSame('x1', $preparedResult[0]->fileId);
}
}
7 changes: 7 additions & 0 deletions tests/ParseResult/ValueHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1111,6 +1111,13 @@ public function testGetArrayOfStickers(): void
],
ValueHelper::getArrayOfStickers($result, 'key'),
);
$this->assertEquals(
[
new Sticker('fid1', 'fuid1', 'regular', 200, 300, false, false),
new Sticker('fid2', 'fuid2', 'regular', 512, 256, true, true),
],
ValueHelper::getArrayOfStickers($result['key']),
);

$exception = null;
try {
Expand Down
26 changes: 26 additions & 0 deletions tests/TelegramBotApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use Vjik\TelegramBot\Api\Type\Message;
use Vjik\TelegramBot\Api\Type\MessageId;
use Vjik\TelegramBot\Api\Type\Payments\StarTransactions;
use Vjik\TelegramBot\Api\Type\Sticker\Sticker;
use Vjik\TelegramBot\Api\Type\User;
use Vjik\TelegramBot\Api\Type\UserProfilePhotos;
use Vjik\TelegramBot\Api\Type\Update\Update;
Expand Down Expand Up @@ -463,6 +464,31 @@ public function testGetFile(): void
$this->assertSame('f1', $result->fileId);
}

public function testGetCustomEmojiStickers(): void
{
$api = $this->createApi([
'ok' => true,
'result' => [
[
'file_id' => 'x1',
'file_unique_id' => 'fullX1',
'type' => 'regular',
'width' => 100,
'height' => 120,
'is_animated' => false,
'is_video' => true,
],
],
]);

$result = $api->getCustomEmojiStickers(['id1']);

$this->assertIsArray($result);
$this->assertCount(1, $result);
$this->assertInstanceOf(Sticker::class, $result[0]);
$this->assertSame('x1', $result[0]->fileId);
}

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

0 comments on commit 560c68e

Please sign in to comment.