-
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
9 changed files
with
411 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,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); | ||
} | ||
} |
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,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 | ||
); | ||
} | ||
} |
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,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'), | ||
); | ||
} | ||
} |
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\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); | ||
} | ||
} |
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\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); | ||
} | ||
} |
Oops, something went wrong.