From a30dbc7d153c177ef3b1e7901495d9fe6e5e864d Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Thu, 27 Jun 2024 22:11:25 +0300 Subject: [PATCH 1/2] fix --- src/Method/GetFile.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Method/GetFile.php b/src/Method/GetFile.php index 44f3006..be5557b 100644 --- a/src/Method/GetFile.php +++ b/src/Method/GetFile.php @@ -14,7 +14,7 @@ final readonly class GetFile implements TelegramRequestWithResultPreparingInterface { public function __construct( - public string $fileId, + private string $fileId, ) { } From e879fd3010d767b2bff45399c67dcf3b29991f97 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Thu, 27 Jun 2024 22:24:38 +0300 Subject: [PATCH 2/2] Add `addStickerToSet`, `deleteStickerFromSet` and `replaceStickerInSet` methods --- CHANGELOG.md | 1 + src/Method/Sticker/AddStickerToSet.php | 53 ++++++++++++++++++ src/Method/Sticker/DeleteStickerFromSet.php | 43 +++++++++++++++ src/Method/Sticker/ReplaceStickerInSet.php | 55 +++++++++++++++++++ src/TelegramBotApi.php | 43 +++++++++++++++ tests/Method/Sticker/AddStickerToSetTest.php | 51 +++++++++++++++++ .../Sticker/DeleteStickerFromSetTest.php | 35 ++++++++++++ .../Sticker/ReplaceStickerInSetTest.php | 53 ++++++++++++++++++ tests/TelegramBotApiTest.php | 47 +++++++++++++++- 9 files changed, 380 insertions(+), 1 deletion(-) create mode 100644 src/Method/Sticker/AddStickerToSet.php create mode 100644 src/Method/Sticker/DeleteStickerFromSet.php create mode 100644 src/Method/Sticker/ReplaceStickerInSet.php create mode 100644 tests/Method/Sticker/AddStickerToSetTest.php create mode 100644 tests/Method/Sticker/DeleteStickerFromSetTest.php create mode 100644 tests/Method/Sticker/ReplaceStickerInSetTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index b9f6494..659452e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -39,6 +39,7 @@ - New #52: Add `sendSticker` method. - New #54: Add `getCustomEmojiStickers` method. - New #55: Add `uploadStickerFile` method. +- New #57: Add `addStickerToSet`, `deleteStickerFromSet` and `replaceStickerInSet` 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`. diff --git a/src/Method/Sticker/AddStickerToSet.php b/src/Method/Sticker/AddStickerToSet.php new file mode 100644 index 0000000..33af77c --- /dev/null +++ b/src/Method/Sticker/AddStickerToSet.php @@ -0,0 +1,53 @@ +sticker->toRequestArray($fileCollector); + + return [ + 'user_id' => $this->userId, + 'name' => $this->name, + 'sticker' => $sticker, + ...$fileCollector->get(), + ]; + } + + public function prepareResult(mixed $result): true + { + ValueHelper::assertTrueResult($result); + return $result; + } +} diff --git a/src/Method/Sticker/DeleteStickerFromSet.php b/src/Method/Sticker/DeleteStickerFromSet.php new file mode 100644 index 0000000..c911401 --- /dev/null +++ b/src/Method/Sticker/DeleteStickerFromSet.php @@ -0,0 +1,43 @@ + $this->sticker, + ]; + } + + public function prepareResult(mixed $result): true + { + ValueHelper::assertTrueResult($result); + return $result; + } +} diff --git a/src/Method/Sticker/ReplaceStickerInSet.php b/src/Method/Sticker/ReplaceStickerInSet.php new file mode 100644 index 0000000..0de837d --- /dev/null +++ b/src/Method/Sticker/ReplaceStickerInSet.php @@ -0,0 +1,55 @@ +sticker->toRequestArray($fileCollector); + + return [ + 'user_id' => $this->userId, + 'name' => $this->name, + 'old_sticker' => $this->oldSticker, + 'sticker' => $sticker, + ...$fileCollector->get(), + ]; + } + + public function prepareResult(mixed $result): true + { + ValueHelper::assertTrueResult($result); + return $result; + } +} diff --git a/src/TelegramBotApi.php b/src/TelegramBotApi.php index c232dd2..12cd0b5 100644 --- a/src/TelegramBotApi.php +++ b/src/TelegramBotApi.php @@ -67,10 +67,13 @@ use Vjik\TelegramBot\Api\Method\SetMyDescription; use Vjik\TelegramBot\Api\Method\SetMyName; use Vjik\TelegramBot\Api\Method\SetMyShortDescription; +use Vjik\TelegramBot\Api\Method\Sticker\AddStickerToSet; use Vjik\TelegramBot\Api\Method\Sticker\CreateNewStickerSet; +use Vjik\TelegramBot\Api\Method\Sticker\DeleteStickerFromSet; 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\ReplaceStickerInSet; use Vjik\TelegramBot\Api\Method\Sticker\SendSticker; use Vjik\TelegramBot\Api\Method\Sticker\UploadStickerFile; use Vjik\TelegramBot\Api\Method\UnbanChatMember; @@ -161,6 +164,18 @@ public function send(TelegramRequestInterface $request): mixed : $this->prepareFailResult($request, $response, $decodedBody); } + /** + * @see https://core.telegram.org/bots/api#addstickertoset + * + * @psalm-suppress MixedInferredReturnType,MixedReturnStatement + */ + public function addStickerToSet(int $userId, string $name, InputSticker $sticker): FailResult|true + { + return $this->send( + new AddStickerToSet($userId, $name, $sticker) + ); + } + /** * @see https://core.telegram.org/bots/api#approvechatjoinrequest * @@ -351,6 +366,18 @@ public function deleteMyCommands(?BotCommandScope $scope = null, ?string $langua return $this->send(new DeleteMyCommands($scope, $languageCode)); } + /** + * @see https://core.telegram.org/bots/api#deletestickerfromset + * + * @psalm-suppress MixedInferredReturnType,MixedReturnStatement + */ + public function deleteStickerFromSet(string $sticker): FailResult|true + { + return $this->send( + new DeleteStickerFromSet($sticker) + ); + } + /** * @see https://core.telegram.org/bots/api#deletestickerset * @@ -729,6 +756,22 @@ public function promoteChatMember( ); } + /** + * @see https://core.telegram.org/bots/api#replacestickerinset + * + * @psalm-suppress MixedInferredReturnType,MixedReturnStatement + */ + public function replaceStickerInSet( + int $userId, + string $name, + string $oldSticker, + InputSticker $sticker + ): FailResult|true { + return $this->send( + new ReplaceStickerInSet($userId, $name, $oldSticker, $sticker) + ); + } + /** * @see https://core.telegram.org/bots/api#restrictchatmember * diff --git a/tests/Method/Sticker/AddStickerToSetTest.php b/tests/Method/Sticker/AddStickerToSetTest.php new file mode 100644 index 0000000..7af5c2b --- /dev/null +++ b/tests/Method/Sticker/AddStickerToSetTest.php @@ -0,0 +1,51 @@ +createStream()); + $inputSticker = new InputSticker($file, 'static', ['😀']); + $method = new AddStickerToSet(1, 'test', $inputSticker); + + $this->assertSame(HttpMethod::POST, $method->getHttpMethod()); + $this->assertSame('addStickerToSet', $method->getApiMethod()); + $this->assertSame( + [ + 'user_id' => 1, + 'name' => 'test', + 'sticker' => [ + 'sticker' => 'attach://file0', + 'format' => 'static', + 'emoji_list' => ['😀'], + ], + 'file0' => $file, + ], + $method->getData(), + ); + } + + public function testPrepareResult(): void + { + $method = new AddStickerToSet( + 1, + 'test', + new InputSticker('https://example.com/sticker.webp', 'static', ['😀']) + ); + + $preparedResult = $method->prepareResult(true); + + $this->assertTrue($preparedResult); + } +} diff --git a/tests/Method/Sticker/DeleteStickerFromSetTest.php b/tests/Method/Sticker/DeleteStickerFromSetTest.php new file mode 100644 index 0000000..2166dee --- /dev/null +++ b/tests/Method/Sticker/DeleteStickerFromSetTest.php @@ -0,0 +1,35 @@ +assertSame(HttpMethod::POST, $method->getHttpMethod()); + $this->assertSame('deleteStickerFromSet', $method->getApiMethod()); + $this->assertSame( + [ + 'sticker' => 'id', + ], + $method->getData(), + ); + } + + public function testPrepareResult(): void + { + $method = new DeleteStickerFromSet('id'); + + $preparedResult = $method->prepareResult(true); + + $this->assertTrue($preparedResult); + } +} diff --git a/tests/Method/Sticker/ReplaceStickerInSetTest.php b/tests/Method/Sticker/ReplaceStickerInSetTest.php new file mode 100644 index 0000000..46243dd --- /dev/null +++ b/tests/Method/Sticker/ReplaceStickerInSetTest.php @@ -0,0 +1,53 @@ +createStream()); + $inputSticker = new InputSticker($file, 'static', ['😀']); + $method = new ReplaceStickerInSet(1, 'test', 'oldid', $inputSticker); + + $this->assertSame(HttpMethod::POST, $method->getHttpMethod()); + $this->assertSame('replaceStickerInSet', $method->getApiMethod()); + $this->assertSame( + [ + 'user_id' => 1, + 'name' => 'test', + 'old_sticker' => 'oldid', + 'sticker' => [ + 'sticker' => 'attach://file0', + 'format' => 'static', + 'emoji_list' => ['😀'], + ], + 'file0' => $file, + ], + $method->getData(), + ); + } + + public function testPrepareResult(): void + { + $method = new ReplaceStickerInSet( + 1, + 'test', + 'oldid', + new InputSticker('https://example.com/sticker.webp', 'static', ['😀']) + ); + + $preparedResult = $method->prepareResult(true); + + $this->assertTrue($preparedResult); + } +} diff --git a/tests/TelegramBotApiTest.php b/tests/TelegramBotApiTest.php index 72bc78a..3c39f9b 100644 --- a/tests/TelegramBotApiTest.php +++ b/tests/TelegramBotApiTest.php @@ -18,7 +18,6 @@ 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\ChatMemberMember; use Vjik\TelegramBot\Api\Type\ChatPermissions; use Vjik\TelegramBot\Api\Type\File; @@ -27,6 +26,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\InputSticker; use Vjik\TelegramBot\Api\Type\Sticker\Sticker; use Vjik\TelegramBot\Api\Type\User; use Vjik\TelegramBot\Api\Type\UserProfilePhotos; @@ -105,6 +105,22 @@ public function testResponseWithNotBooleanOk(): void $api->send(new GetMe()); } + public function testAddStickerToSet(): void + { + $api = $this->createApi([ + 'ok' => true, + 'result' => true, + ]); + + $result = $api->addStickerToSet( + 1, + 'test', + new InputSticker('https://example.com/sticker.webp', 'static', ['😀']) + ); + + $this->assertTrue($result); + } + public function testApproveChatJoinRequest(): void { $api = $this->createApi([ @@ -263,6 +279,18 @@ public function testDeleteMyCommands(): void $this->assertTrue($result); } + public function testDeleteStickerFromSet(): void + { + $api = $this->createApi([ + 'ok' => true, + 'result' => true, + ]); + + $result = $api->deleteStickerFromSet('sid'); + + $this->assertTrue($result); + } + public function testDeleteStickerSet(): void { $api = $this->createApi([ @@ -722,6 +750,23 @@ public function testPromoteChatMember(): void $this->assertTrue($result); } + public function testReplaceStickerInSet(): void + { + $api = $this->createApi([ + 'ok' => true, + 'result' => true, + ]); + + $result = $api->replaceStickerInSet( + 1, + 'test', + 'oldid', + new InputSticker('https://example.com/sticker.webp', 'static', ['😀']) + ); + + $this->assertTrue($result); + } + public function testRestrictChatMember(): void { $api = $this->createApi([