Skip to content

Commit

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

Expand Down
55 changes: 55 additions & 0 deletions src/Method/ForwardMessage.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;

use Vjik\TelegramBot\Api\Request\HttpMethod;
use Vjik\TelegramBot\Api\Request\TelegramRequestWithResultPreparingInterface;
use Vjik\TelegramBot\Api\Type\Message;

/**
* @see https://core.telegram.org/bots/api#forwardmessage
*/
final readonly class ForwardMessage implements TelegramRequestWithResultPreparingInterface
{
public function __construct(
private int|string $chatId,
private int|string $fromChatId,
private int $messageId,
private ?int $messageThreadId = null,
private ?bool $disableNotification = null,
private ?bool $protectContent = null,
) {
}

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

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

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

public function prepareResult(mixed $result): Message
{
return Message::fromTelegramResult($result);
}
}
66 changes: 66 additions & 0 deletions src/Method/ForwardMessages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?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#forwardmessages
*/
final readonly class ForwardMessages 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,
) {
}

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

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

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,
],
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
);
}
}
57 changes: 57 additions & 0 deletions src/TelegramBotApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use Vjik\TelegramBot\Api\Client\TelegramResponse;
use Vjik\TelegramBot\Api\Method\Close;
use Vjik\TelegramBot\Api\Method\DeleteMyCommands;
use Vjik\TelegramBot\Api\Method\ForwardMessage;
use Vjik\TelegramBot\Api\Method\ForwardMessages;
use Vjik\TelegramBot\Api\Method\GetChat;
use Vjik\TelegramBot\Api\Method\GetChatMenuButton;
use Vjik\TelegramBot\Api\Method\GetFile;
Expand Down Expand Up @@ -54,6 +56,7 @@
use Vjik\TelegramBot\Api\Type\MenuButton;
use Vjik\TelegramBot\Api\Type\Message;
use Vjik\TelegramBot\Api\Type\MessageEntity;
use Vjik\TelegramBot\Api\Type\MessageId;
use Vjik\TelegramBot\Api\Type\Payments\StarTransactions;
use Vjik\TelegramBot\Api\Type\ReplyKeyboardMarkup;
use Vjik\TelegramBot\Api\Type\ReplyKeyboardRemove;
Expand Down Expand Up @@ -128,6 +131,60 @@ public function deleteMyCommands(?BotCommandScope $scope = null, ?string $langua
return $this->send(new DeleteMyCommands($scope, $languageCode));
}

/**
* @see https://core.telegram.org/bots/api#forwardmessage
*
* @psalm-suppress MixedInferredReturnType,MixedReturnStatement
*/
public function forwardMessage(
int|string $chatId,
int|string $fromChatId,
int $messageId,
?int $messageThreadId = null,
?bool $disableNotification = null,
?bool $protectContent = null,
): FailResult|Message
{
return $this->send(
new ForwardMessage(
$chatId,
$fromChatId,
$messageId,
$messageThreadId,
$disableNotification,
$protectContent,
)
);
}

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

/**
* @see https://core.telegram.org/bots/api#deletewebhook
*
Expand Down
26 changes: 26 additions & 0 deletions src/Type/MessageId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Vjik\TelegramBot\Api\Type;

use Vjik\TelegramBot\Api\ParseResult\ValueHelper;

/**
* https://core.telegram.org/bots/api#messageid
*/
final readonly class MessageId
{
public function __construct(
public int $messageId,
) {
}

public static function fromTelegramResult(mixed $result): self
{
ValueHelper::assertArrayResult($result);
return new self(
ValueHelper::getInteger($result, 'message_id'),
);
}
}
68 changes: 68 additions & 0 deletions tests/Method/ForwardMessageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);

namespace Vjik\TelegramBot\Api\Tests\Method;

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

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

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

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

$this->assertSame(
[
'chat_id' => 1,
'message_thread_id' => 4,
'from_chat_id' => 2,
'disable_notification' => true,
'protect_content' => false,
'message_id' => 3,
],
$method->getData()
);
}

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

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

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

declare(strict_types=1);

namespace Vjik\TelegramBot\Api\Tests\Method;

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

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

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

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

$this->assertSame(HttpMethod::POST, $method->getHttpMethod());
$this->assertSame('forwardMessages', $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,
],
$method->getData(),
);
}

public function testPrepareResult(): void
{
$method = new ForwardMessages(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 8f6ae76

Please sign in to comment.