Add forwardMessage method

This commit is contained in:
netkas 2024-10-07 19:39:18 -04:00
parent 4d45add53e
commit 17ff223461
4 changed files with 98 additions and 0 deletions

View file

@ -29,6 +29,7 @@
* @method bool logOut() Use this method to log out from the cloud Bot API server before launching the bot locally. You must log out the bot before running it locally, otherwise there is no guarantee that the bot will receive updates. After a successful call, you can immediately log in on a local server, but will not be able to log in back to the cloud Bot API server for 10 minutes. Returns True on success. Requires no parameters.
* @method bool close() Use this method to close the bot instance before moving it from one local server to another. You need to delete the webhook before calling this method to ensure that the bot isn't launched again after server restart. The method will return error 429 in the first 10 minutes after the bot is launched. Returns True on success. Requires no parameters.
* @method Message sendMessage(string|int $chat_id, string $text, ?string $business_connection_id=null, ?int $message_thread_id=null, string|ParseMode|null $parse_mode=null, ?array $entities=null, ?LinkPreviewOptions $link_preview_options=null, ?bool $disable_notification=null, ?bool $protect_content=null, ?string $message_effect_id=null, ?ReplyParameters $reply_parameters=null, InlineKeyboardMarkup|ReplyKeyboardMarkup|ReplyKeyboardRemove|ForceReply|null $reply_markup=null) Use this method to send text messages. On success, the sent Message is returned.
* @method Message forwardMessage(string|int $chat_id, string|int $from_chat_id, int $message_id, ?int $message_thread_id=null, ?bool $disable_notification=null, ?bool $protect_content=null) Use this method to forward messages of any kind. Service messages and messages with protected content can't be forwarded. On success, the sent Message is returned.
*/
class Bot
{

View file

@ -6,6 +6,7 @@
use TgBotLib\Exceptions\TelegramException;
use TgBotLib\Interfaces\ObjectTypeInterface;
use TgBotLib\Methods\Close;
use TgBotLib\Methods\ForwardMessage;
use TgBotLib\Methods\GetMe;
use TgBotLib\Methods\Logout;
use TgBotLib\Methods\SendMessage;
@ -16,6 +17,7 @@
case LOGOUT = 'logOut';
case CLOSE = 'close';
case SEND_MESSAGE = 'sendMessage';
case FORWARD_MESSAGE = 'forwardMessage';
/**
* Executes a command on the provided bot with the given parameters.
@ -33,6 +35,7 @@
self::LOGOUT => LogOut::execute($bot, $parameters),
self::CLOSE => Close::execute($bot, $parameters),
self::SEND_MESSAGE => SendMessage::execute($bot, $parameters),
self::FORWARD_MESSAGE => ForwardMessage::execute($bot, $parameters),
};
}
}

View file

@ -0,0 +1,44 @@
<?php
namespace TgBotLib\Methods;
use InvalidArgumentException;
use TgBotLib\Abstracts\Method;
use TgBotLib\Bot;
use TgBotLib\Enums\Methods;
use TgBotLib\Objects\Message;
class ForwardMessage extends Method
{
/**
* @inheritDoc
*/
public static function execute(Bot $bot, array $parameters = []): Message
{
return Message::fromArray(self::executeCurl(self::buildPost($bot, Methods::FORWARD_MESSAGE->value, $parameters)));
}
/**
* @inheritDoc
*/
public static function getRequiredParameters(): array
{
return [
'chat_id',
'from_chat_id',
'message_id'
];
}
/**
* @inheritDoc
*/
public static function getOptionalParameters(): array
{
return [
'message_thread_id',
'disable_notification',
'protect_content'
];
}
}

View file

@ -0,0 +1,50 @@
<?php
namespace TgBotLib\Methods;
use PHPUnit\Framework\TestCase;
use TgBotLib\Bot;
use TgBotLib\Enums\Types\ParseMode;
use TgBotLib\Objects\LinkPreviewOptions;
use TgBotLib\Objects\Message;
class ForwardMessageTest extends TestCase
{
const int TEST_CHAT_ID = -1001301191379;
private static Bot $bot;
/**
* @return void
*/
public static function setUpBeforeClass(): void
{
self::$bot = new Bot(BOT_TOKEN);
}
/**
* Tests the `sendMessage` function of the bot instance.
*
* @return void
*/
public function testForwardMessage(): void
{
$result = self::$bot->sendMessage(
chat_id: self::TEST_CHAT_ID,
text: 'Test Unit: testForwardMessage'
);
$this->assertInstanceOf(Message::class, $result);
$this->assertEquals(self::TEST_CHAT_ID, $result->getChat()->getId());
$this->assertEquals('Test Unit: testForwardMessage', $result->getText());
$result = self::$bot->forwardMessage(
chat_id: self::TEST_CHAT_ID,
from_chat_id: self::TEST_CHAT_ID,
message_id: $result->getMessageId()
);
$this->assertInstanceOf(Message::class, $result);
$this->assertEquals(self::TEST_CHAT_ID, $result->getChat()->getId());
$this->assertEquals('Test Unit: testForwardMessage', $result->getText());
}
}