Skip to content

Commit

Permalink
Add addStickerToSet, deleteStickerFromSet and `replaceStickerInSe…
Browse files Browse the repository at this point in the history
…t` methods (#57)
  • Loading branch information
vjik authored Jun 27, 2024
1 parent a60ffb3 commit 892cd34
Show file tree
Hide file tree
Showing 10 changed files with 381 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
2 changes: 1 addition & 1 deletion src/Method/GetFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
final readonly class GetFile implements TelegramRequestWithResultPreparingInterface
{
public function __construct(
public string $fileId,
private string $fileId,
) {
}

Expand Down
53 changes: 53 additions & 0 deletions src/Method/Sticker/AddStickerToSet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?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\RequestFileCollector;
use Vjik\TelegramBot\Api\Request\TelegramRequestWithResultPreparingInterface;
use Vjik\TelegramBot\Api\Type\Sticker\InputSticker;

/**
* @see https://core.telegram.org/bots/api#addstickertoset
*/
final readonly class AddStickerToSet implements TelegramRequestWithResultPreparingInterface
{
public function __construct(
private int $userId,
private string $name,
private InputSticker $sticker,
) {
}

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

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

public function getData(): array
{
$fileCollector = new RequestFileCollector();
$sticker = $this->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;
}
}
43 changes: 43 additions & 0 deletions src/Method/Sticker/DeleteStickerFromSet.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\Sticker;

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#deletestickerfromset
*/
final readonly class DeleteStickerFromSet implements TelegramRequestWithResultPreparingInterface
{
public function __construct(
private string $sticker,
) {
}

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

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

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

public function prepareResult(mixed $result): true
{
ValueHelper::assertTrueResult($result);
return $result;
}
}
55 changes: 55 additions & 0 deletions src/Method/Sticker/ReplaceStickerInSet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?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\RequestFileCollector;
use Vjik\TelegramBot\Api\Request\TelegramRequestWithResultPreparingInterface;
use Vjik\TelegramBot\Api\Type\Sticker\InputSticker;

/**
* @see https://core.telegram.org/bots/api#replacestickerinset
*/
final readonly class ReplaceStickerInSet implements TelegramRequestWithResultPreparingInterface
{
public function __construct(
private int $userId,
private string $name,
private string $oldSticker,
private InputSticker $sticker,
) {
}

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

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

public function getData(): array
{
$fileCollector = new RequestFileCollector();
$sticker = $this->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;
}
}
43 changes: 43 additions & 0 deletions src/TelegramBotApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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
*
Expand Down
51 changes: 51 additions & 0 deletions tests/Method/Sticker/AddStickerToSetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

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

use HttpSoft\Message\StreamFactory;
use PHPUnit\Framework\TestCase;
use Vjik\TelegramBot\Api\Method\Sticker\AddStickerToSet;
use Vjik\TelegramBot\Api\Request\HttpMethod;
use Vjik\TelegramBot\Api\Type\InputFile;
use Vjik\TelegramBot\Api\Type\Sticker\InputSticker;

final class AddStickerToSetTest extends TestCase
{
public function testBase(): void
{
$file = new InputFile((new StreamFactory())->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);
}
}
35 changes: 35 additions & 0 deletions tests/Method/Sticker/DeleteStickerFromSetTest.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\Sticker;

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

final class DeleteStickerFromSetTest extends TestCase
{
public function testBase(): void
{
$method = new DeleteStickerFromSet('id');

$this->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);
}
}
53 changes: 53 additions & 0 deletions tests/Method/Sticker/ReplaceStickerInSetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

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

use HttpSoft\Message\StreamFactory;
use PHPUnit\Framework\TestCase;
use Vjik\TelegramBot\Api\Method\Sticker\ReplaceStickerInSet;
use Vjik\TelegramBot\Api\Request\HttpMethod;
use Vjik\TelegramBot\Api\Type\InputFile;
use Vjik\TelegramBot\Api\Type\Sticker\InputSticker;

final class ReplaceStickerInSetTest extends TestCase
{
public function testBase(): void
{
$file = new InputFile((new StreamFactory())->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);
}
}
Loading

0 comments on commit 892cd34

Please sign in to comment.