Skip to content

Commit

Permalink
Add copyMessage and copyMessages methods (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik authored Jun 22, 2024
1 parent 8f6ae76 commit 728b851
Show file tree
Hide file tree
Showing 7 changed files with 409 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
`RevenueWithdrawalState` types.
- New #23: Add `logOut` and `close` methods.
- New #25: Add `forwardMessage` and `forwardMessages` methods, and `MessageId` type.
- New #26: Add `copyMessage` and `copyMessages` methods.
- Chg #24: Move update methods to `Vjik\TelegramBot\Api\Method\Update` namespace, and update types to
`Vjik\TelegramBot\Api\Type\Update` namespace.

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

declare(strict_types=1);

namespace Vjik\TelegramBot\Api\Method;

use Vjik\TelegramBot\Api\Request\HttpMethod;
use Vjik\TelegramBot\Api\Request\TelegramRequestWithResultPreparingInterface;
use Vjik\TelegramBot\Api\Type\ForceReply;
use Vjik\TelegramBot\Api\Type\InlineKeyboardMarkup;
use Vjik\TelegramBot\Api\Type\MessageEntity;
use Vjik\TelegramBot\Api\Type\MessageId;
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#copymessage
*/
final readonly class CopyMessage implements TelegramRequestWithResultPreparingInterface
{
/**
* @param MessageEntity[]|null $captionEntities
*/
public function __construct(
private int|string $chatId,
private int|string $fromChatId,
private int $messageId,
private ?int $messageThreadId = null,
private ?string $caption = null,
private ?string $parseMode = null,
private ?array $captionEntities = null,
private ?bool $showCaptionAboveMedia = null,
private ?bool $disableNotification = null,
private ?bool $protectContent = 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 'copyMessage';
}

public function getData(): array
{
return array_filter(
[
'chat_id' => $this->chatId,
'message_thread_id' => $this->messageThreadId,
'from_chat_id' => $this->fromChatId,
'message_id' => $this->messageId,
'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,
'disable_notification' => $this->disableNotification,
'protect_content' => $this->protectContent,
'reply_parameters' => $this->replyParameters?->toRequestArray(),
'reply_markup' => $this->replyMarkup?->toRequestArray(),
],
static fn(mixed $value): bool => $value !== null,
);
}

public function prepareResult(mixed $result): MessageId
{
return MessageId::fromTelegramResult($result);
}
}
68 changes: 68 additions & 0 deletions src/Method/CopyMessages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?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;
use Vjik\TelegramBot\Api\Type\MessageId;

/**
* @see https://core.telegram.org/bots/api#copymessages
*/
final readonly class CopyMessages implements TelegramRequestWithResultPreparingInterface
{
/**
* @param int[] $messageIds
*/
public function __construct(
private int|string $chatId,
private int|string $fromChatId,
private array $messageIds,
private ?int $messageThreadId = null,
private ?bool $disableNotification = null,
private ?bool $protectContent = null,
private ?bool $removeCaption = null,
) {
}

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

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

public function getData(): array
{
return array_filter(
[
'chat_id' => $this->chatId,
'message_thread_id' => $this->messageThreadId,
'from_chat_id' => $this->fromChatId,
'message_ids' => $this->messageIds,
'disable_notification' => $this->disableNotification,
'protect_content' => $this->protectContent,
'remove_caption' => $this->removeCaption,
],
static fn(mixed $value): bool => $value !== null,
);
}

/**
* @return MessageId[]
*/
public function prepareResult(mixed $result): array
{
ValueHelper::assertArrayResult($result);
return array_map(
static fn($item) => MessageId::fromTelegramResult($item),
$result
);
}
}
71 changes: 71 additions & 0 deletions src/TelegramBotApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use JsonException;
use Vjik\TelegramBot\Api\Client\TelegramResponse;
use Vjik\TelegramBot\Api\Method\Close;
use Vjik\TelegramBot\Api\Method\CopyMessage;
use Vjik\TelegramBot\Api\Method\CopyMessages;
use Vjik\TelegramBot\Api\Method\DeleteMyCommands;
use Vjik\TelegramBot\Api\Method\ForwardMessage;
use Vjik\TelegramBot\Api\Method\ForwardMessages;
Expand Down Expand Up @@ -121,6 +123,75 @@ public function close(): FailResult|true
return $this->send(new Close());
}

/**
* @see https://core.telegram.org/bots/api#copymessage
*
* @param MessageEntity[]|null $captionEntities
*
* @psalm-suppress MixedInferredReturnType,MixedReturnStatement
*/
public function copyMessage(
int|string $chatId,
int|string $fromChatId,
int $messageId,
?int $messageThreadId = null,
?string $caption = null,
?string $parseMode = null,
?array $captionEntities = null,
?bool $showCaptionAboveMedia = null,
?bool $disableNotification = null,
?bool $protectContent = null,
?ReplyParameters $replyParameters = null,
InlineKeyboardMarkup|ReplyKeyboardMarkup|ReplyKeyboardRemove|ForceReply|null $replyMarkup = null,
): FailResult|MessageId {
return $this->send(
new CopyMessage(
$chatId,
$fromChatId,
$messageId,
$messageThreadId,
$caption,
$parseMode,
$captionEntities,
$showCaptionAboveMedia,
$disableNotification,
$protectContent,
$replyParameters,
$replyMarkup,
)
);
}

/**
* @see https://core.telegram.org/bots/api#copymessages
*
* @param int[] $messageIds
* @return FailResult|MessageId[]
*
* @psalm-suppress MixedInferredReturnType,MixedReturnStatement
*/
public function copyMessages(
int|string $chatId,
int|string $fromChatId,
array $messageIds,
?int $messageThreadId = null,
?bool $disableNotification = null,
?bool $protectContent = null,
?bool $removeCaption = null,
): FailResult|array {
return $this->send(
new CopyMessages(
$chatId,
$fromChatId,
$messageIds,
$messageThreadId,
$disableNotification,
$protectContent,
$removeCaption,
)
);
}

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

declare(strict_types=1);

namespace Method;

use PHPUnit\Framework\TestCase;
use Vjik\TelegramBot\Api\Method\CopyMessage;
use Vjik\TelegramBot\Api\Request\HttpMethod;
use Vjik\TelegramBot\Api\Type\ForceReply;
use Vjik\TelegramBot\Api\Type\MessageEntity;
use Vjik\TelegramBot\Api\Type\ReplyParameters;

final class CopyMessageTest extends TestCase
{
public function testBase(): void
{
$method = new CopyMessage(1,2,3);

$this->assertSame(HttpMethod::POST, $method->getHttpMethod());
$this->assertSame('copyMessage', $method->getApiMethod());
$this->assertSame(
[
'chat_id' => 1,
'from_chat_id' => 2,
'message_id' => 3,
],
$method->getData(),
);
}

public function testFull(): void
{
$messageEntity = new MessageEntity('bold', 0, 4);
$replyParameters = new ReplyParameters(23);
$replyMarkup = new ForceReply();
$method = new CopyMessage(
1,
2,
3,
4,
'test',
'MarkdownV2',
[$messageEntity],
true,
false,
true,
$replyParameters,
$replyMarkup,
);

$this->assertSame(
[
'chat_id' => 1,
'message_thread_id' => 4,
'from_chat_id' => 2,
'message_id' => 3,
'caption' => 'test',
'parse_mode' => 'MarkdownV2',
'caption_entities' => [$messageEntity->toRequestArray()],
'show_caption_above_media' => true,
'disable_notification' => false,
'protect_content' => true,
'reply_parameters' => $replyParameters->toRequestArray(),
'reply_markup' => $replyMarkup->toRequestArray(),
],
$method->getData()
);
}

public function testPrepareResult(): void
{
$method = new CopyMessage(1, 2, 3);

$preparedResult = $method->prepareResult([
'message_id' => 7,
]);

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

declare(strict_types=1);

namespace Method;

use PHPUnit\Framework\TestCase;
use Vjik\TelegramBot\Api\Method\CopyMessages;
use Vjik\TelegramBot\Api\Request\HttpMethod;
use Vjik\TelegramBot\Api\Type\MessageId;

final class CopyMessagesTest extends TestCase
{
public function testBase(): void
{
$method = new CopyMessages(1, 2, [3, 4]);

$this->assertSame(HttpMethod::POST, $method->getHttpMethod());
$this->assertSame('copyMessages', $method->getApiMethod());
$this->assertSame(
[
'chat_id' => 1,
'from_chat_id' => 2,
'message_ids' => [3, 4],
],
$method->getData(),
);
}

public function testFull(): void
{
$method = new CopyMessages(1, 2, [3, 4], 5, true, false, true);

$this->assertSame(HttpMethod::POST, $method->getHttpMethod());
$this->assertSame('copyMessages', $method->getApiMethod());
$this->assertSame(
[
'chat_id' => 1,
'message_thread_id' => 5,
'from_chat_id' => 2,
'message_ids' => [3, 4],
'disable_notification' => true,
'protect_content' => false,
'remove_caption' => true,
],
$method->getData(),
);
}

public function testPrepareResult(): void
{
$method = new CopyMessages(1, 2, [3, 4]);

$preparedResult = $method->prepareResult([
[
'message_id' => 7,
],
[
'message_id' => 8,
],
]);

$this->assertCount(2, $preparedResult);
$this->assertInstanceOf(MessageId::class, $preparedResult[0]);
$this->assertInstanceOf(MessageId::class, $preparedResult[1]);
$this->assertSame(7, $preparedResult[0]->messageId);
$this->assertSame(8, $preparedResult[1]->messageId);
}
}
Loading

0 comments on commit 728b851

Please sign in to comment.