Skip to content

Commit

Permalink
Add setChatStickerSet and deleteChatStickerSet methods (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik authored Jun 28, 2024
1 parent 97b9a0b commit 9bf50d0
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
- New #60: Add `setStickerSetTitle` method.
- New #61: Add `setStickerSetThumbnail` method.
- New #62: Add `setCustomEmojiStickerSetThumbnail` method.
- New #63: Add `setChatStickerSet` and `deleteChatStickerSet` 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
43 changes: 43 additions & 0 deletions src/Method/DeleteChatStickerSet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?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#deletechatstickerset
*/
final readonly class DeleteChatStickerSet implements TelegramRequestWithResultPreparingInterface
{
public function __construct(
private int|string $chatId,
) {
}

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

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

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

public function prepareResult(mixed $result): true
{
ValueHelper::assertTrueResult($result);
return $result;
}
}
45 changes: 45 additions & 0 deletions src/Method/SetChatStickerSet.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\ParseResult\ValueHelper;
use Vjik\TelegramBot\Api\Request\HttpMethod;
use Vjik\TelegramBot\Api\Request\TelegramRequestWithResultPreparingInterface;

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

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

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

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

public function prepareResult(mixed $result): true
{
ValueHelper::assertTrueResult($result);
return $result;
}
}
26 changes: 26 additions & 0 deletions src/TelegramBotApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Vjik\TelegramBot\Api\Method\CreateChatInviteLink;
use Vjik\TelegramBot\Api\Method\DeclineChatJoinRequest;
use Vjik\TelegramBot\Api\Method\DeleteChatPhoto;
use Vjik\TelegramBot\Api\Method\DeleteChatStickerSet;
use Vjik\TelegramBot\Api\Method\DeleteMyCommands;
use Vjik\TelegramBot\Api\Method\EditChatInviteLink;
use Vjik\TelegramBot\Api\Method\ExportChatInviteLink;
Expand Down Expand Up @@ -61,6 +62,7 @@
use Vjik\TelegramBot\Api\Method\SetChatMenuButton;
use Vjik\TelegramBot\Api\Method\SetChatPermissions;
use Vjik\TelegramBot\Api\Method\SetChatPhoto;
use Vjik\TelegramBot\Api\Method\SetChatStickerSet;
use Vjik\TelegramBot\Api\Method\SetChatTitle;
use Vjik\TelegramBot\Api\Method\SetMessageReaction;
use Vjik\TelegramBot\Api\Method\SetMyCommands;
Expand Down Expand Up @@ -364,6 +366,18 @@ public function deleteChatPhoto(int|string $chatId): FailResult|true
);
}

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

/**
* @see https://core.telegram.org/bots/api#deletemycommands
*
Expand Down Expand Up @@ -1528,6 +1542,18 @@ public function setChatPhoto(int|string $chatId, InputFile $photo): FailResult|t
);
}

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

/**
* @see https://core.telegram.org/bots/api#setchattitle
*
Expand Down
35 changes: 35 additions & 0 deletions tests/Method/DeleteChatStickerSetTest.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\DeleteChatStickerSet;
use Vjik\TelegramBot\Api\Request\HttpMethod;

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

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

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

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

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

declare(strict_types=1);

namespace Vjik\TelegramBot\Api\Tests\Method;

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

final class SetChatStickerSetTest extends TestCase
{
public function testBase(): void
{
$method = new SetChatStickerSet(1, 'animals_by_bot');

$this->assertSame(HttpMethod::POST, $method->getHttpMethod());
$this->assertSame('setChatStickerSet', $method->getApiMethod());
$this->assertSame(
[
'chat_id' => 1,
'sticker_set_name' => 'animals_by_bot',
],
$method->getData(),
);
}

public function testPrepareResult(): void
{
$method = new SetChatStickerSet(1, 'animals_by_bot');

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

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

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

$result = $api->deleteChatStickerSet(1);

$this->assertTrue($result);
}

public function testDeleteMyCommands(): void
{
$api = $this->createApi([
Expand Down Expand Up @@ -1180,6 +1192,18 @@ public function testSetChatPhoto(): void
$this->assertTrue($result);
}

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

$result = $api->setChatStickerSet(1, 'animals_by_bot');

$this->assertTrue($result);
}

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

0 comments on commit 9bf50d0

Please sign in to comment.