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 sendAnimation method #27

Merged
merged 1 commit into from
Jun 23, 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 @@ -13,6 +13,7 @@
- New #23: Add `logOut` and `close` methods.
- New #25: Add `forwardMessage` and `forwardMessages` methods, and `MessageId` type.
- New #26: Add `copyMessage` and `copyMessages` methods.
- New #27: Add `sendAnimation` method.
- Chg #24: Move update methods to `Vjik\TelegramBot\Api\Method\Update` namespace, and update types to
`Vjik\TelegramBot\Api\Type\Update` namespace.

Expand Down
106 changes: 106 additions & 0 deletions src/Method/SendAnimation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

declare(strict_types=1);

namespace Vjik\TelegramBot\Api\Method;

use Vjik\TelegramBot\Api\Request\HttpMethod;
use Vjik\TelegramBot\Api\Request\TelegramRequestWithFilesInterface;
use Vjik\TelegramBot\Api\Request\TelegramRequestWithResultPreparingInterface;
use Vjik\TelegramBot\Api\Type\ForceReply;
use Vjik\TelegramBot\Api\Type\InlineKeyboardMarkup;
use Vjik\TelegramBot\Api\Type\InputFile;
use Vjik\TelegramBot\Api\Type\Message;
use Vjik\TelegramBot\Api\Type\MessageEntity;
use Vjik\TelegramBot\Api\Type\ReplyKeyboardMarkup;
use Vjik\TelegramBot\Api\Type\ReplyKeyboardRemove;
use Vjik\TelegramBot\Api\Type\ReplyParameters;

/**
* @see https://core.telegram.org/bots/api#sendanimation
*/
final readonly class SendAnimation implements
TelegramRequestWithFilesInterface,
TelegramRequestWithResultPreparingInterface
{
/**
* @param MessageEntity[]|null $captionEntities
*/
public function __construct(
private int|string $chatId,
private InputFile|string $animation,
private ?string $businessConnectionId = null,
private ?int $messageThreadId = null,
private ?int $duration = null,
private ?int $width = null,
private ?int $height = null,
private InputFile|string|null $thumbnail = null,
private ?string $caption = null,
private ?string $parseMode = null,
private ?array $captionEntities = null,
private ?bool $showCaptionAboveMedia = null,
private ?bool $hasSpoiler = null,
private ?bool $disableNotification = null,
private ?bool $protectContent = null,
private ?string $messageEffectId = null,
private ?ReplyParameters $replyParameters = null,
private InlineKeyboardMarkup|ReplyKeyboardMarkup|ReplyKeyboardRemove|ForceReply|null $replyMarkup = null,
) {
}

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

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

public function getData(): array
{
return array_filter(
[
'business_connection_id' => $this->businessConnectionId,
'chat_id' => $this->chatId,
'message_thread_id' => $this->messageThreadId,
'animation' => is_string($this->animation) ? $this->animation : null,
'duration' => $this->duration,
'width' => $this->width,
'height' => $this->height,
'thumbnail' => is_string($this->thumbnail) ? $this->thumbnail : null,
'caption' => $this->caption,
'parse_mode' => $this->parseMode,
'caption_entities' => $this->captionEntities === null ? null : array_map(
static fn(MessageEntity $entity) => $entity->toRequestArray(),
$this->captionEntities,
),
'show_caption_above_media' => $this->showCaptionAboveMedia,
'has_spoiler' => $this->hasSpoiler,
'disable_notification' => $this->disableNotification,
'protect_content' => $this->protectContent,
'message_effect_id' => $this->messageEffectId,
'reply_parameters' => $this->replyParameters?->toRequestArray(),
'reply_markup' => $this->replyMarkup?->toRequestArray(),
],
static fn(mixed $value): bool => $value !== null,
);
}

public function getFiles(): array
{
return array_filter(
[
'animation' => $this->animation instanceof InputFile ? $this->animation : null,
'thumbnail' => $this->thumbnail instanceof InputFile ? $this->thumbnail : null,
],
static fn(mixed $value): bool => $value !== null,
);
}

public function prepareResult(mixed $result): Message
{
return Message::fromTelegramResult($result);
}
}
52 changes: 52 additions & 0 deletions src/TelegramBotApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Vjik\TelegramBot\Api\Method\GetUserProfilePhotos;
use Vjik\TelegramBot\Api\Method\LogOut;
use Vjik\TelegramBot\Api\Method\Payments\GetStarTransactions;
use Vjik\TelegramBot\Api\Method\SendAnimation;
use Vjik\TelegramBot\Api\Method\SendAudio;
use Vjik\TelegramBot\Api\Method\SendChatAction;
use Vjik\TelegramBot\Api\Method\SendContact;
Expand Down Expand Up @@ -410,6 +411,57 @@ public function logOut(): FailResult|true
return $this->send(new LogOut());
}

/**
* @param MessageEntity[]|null $captionEntities
*
* @see https://core.telegram.org/bots/api#sendanimation
*
* @psalm-suppress MixedInferredReturnType,MixedReturnStatement
*/
public function sendAnimation(
int|string $chatId,
InputFile|string $animation,
?string $businessConnectionId = null,
?int $messageThreadId = null,
?int $duration = null,
?int $width = null,
?int $height = null,
InputFile|string|null $thumbnail = null,
?string $caption = null,
?string $parseMode = null,
?array $captionEntities = null,
?bool $showCaptionAboveMedia = null,
?bool $hasSpoiler = null,
?bool $disableNotification = null,
?bool $protectContent = null,
?string $messageEffectId = null,
?ReplyParameters $replyParameters = null,
InlineKeyboardMarkup|ReplyKeyboardMarkup|ReplyKeyboardRemove|ForceReply|null $replyMarkup = null,
): FailResult|Message {
return $this->send(
new SendAnimation(
$chatId,
$animation,
$businessConnectionId,
$messageThreadId,
$duration,
$width,
$height,
$thumbnail,
$caption,
$parseMode,
$captionEntities,
$showCaptionAboveMedia,
$hasSpoiler,
$disableNotification,
$protectContent,
$messageEffectId,
$replyParameters,
$replyMarkup,
)
);
}

/**
* @param MessageEntity[]|null $captionEntities
*
Expand Down
107 changes: 107 additions & 0 deletions tests/Method/SendAnimationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

declare(strict_types=1);

namespace Method;

use HttpSoft\Message\StreamFactory;
use PHPUnit\Framework\TestCase;
use Vjik\TelegramBot\Api\Method\SendAnimation;
use Vjik\TelegramBot\Api\Request\HttpMethod;
use Vjik\TelegramBot\Api\Type\ForceReply;
use Vjik\TelegramBot\Api\Type\InputFile;
use Vjik\TelegramBot\Api\Type\MessageEntity;
use Vjik\TelegramBot\Api\Type\ReplyParameters;

final class SendAnimationTest extends TestCase
{
public function testBase(): void
{
$method = new SendAnimation(12, 'https://example.com/anime.gif');

$this->assertSame(HttpMethod::POST, $method->getHttpMethod());
$this->assertSame('sendAnimation', $method->getApiMethod());
$this->assertSame(
[
'chat_id' => 12,
'animation' => 'https://example.com/anime.gif',
],
$method->getData(),
);
$this->assertSame([], $method->getFiles());
}

public function testFull(): void
{
$animation = new InputFile((new StreamFactory())->createStream('test'), 'test.gif');
$thumbnail = new InputFile((new StreamFactory())->createStream('test'), 'test.jpg');
$entity = new MessageEntity('bold', 0, 5);
$replyParameters = new ReplyParameters(23);
$replyMarkup = new ForceReply();
$method = new SendAnimation(
12,
$animation,
'bcid1',
99,
100,
240,
320,
$thumbnail,
'Caption',
'HTML',
[$entity],
true,
false,
false,
true,
'meID',
$replyParameters,
$replyMarkup,
);

$this->assertSame(
[
'business_connection_id' => 'bcid1',
'chat_id' => 12,
'message_thread_id' => 99,
'duration' => 100,
'width' => 240,
'height' => 320,
'caption' => 'Caption',
'parse_mode' => 'HTML',
'caption_entities' => [$entity->toRequestArray()],
'show_caption_above_media' => true,
'has_spoiler' => false,
'disable_notification' => false,
'protect_content' => true,
'message_effect_id' => 'meID',
'reply_parameters' => $replyParameters->toRequestArray(),
'reply_markup' => $replyMarkup->toRequestArray(),
],
$method->getData(),
);
$this->assertSame(
[
'animation' => $animation,
'thumbnail' => $thumbnail,
],
$method->getFiles(),
);
}

public function testPrepareResult(): void
{
$method = new SendAnimation(12, 'https://example.com/anime.gif');

$preparedResult = $method->prepareResult([
'message_id' => 7,
'date' => 1620000000,
'chat' => [
'id' => 1,
'type' => 'private',
],
]);

$this->assertSame(7, $preparedResult->messageId);
}
}
20 changes: 20 additions & 0 deletions tests/TelegramBotApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,26 @@ public function testLogOut(): void
$this->assertTrue($result);
}

public function testSendAnimation(): void
{
$api = $this->createApi([
'ok' => true,
'result' => [
'message_id' => 7,
'date' => 1620000000,
'chat' => [
'id' => 1,
'type' => 'private',
],
],
]);

$result = $api->sendAnimation(12, 'https://example.com/anime.gif');

$this->assertInstanceOf(Message::class, $result);
$this->assertSame(7, $result->messageId);
}

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