-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
409 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.