Add forwardMessages method for batch forwarding
This commit is contained in:
parent
5e52ceea4b
commit
2e58fcf108
6 changed files with 162 additions and 7 deletions
|
@ -44,17 +44,13 @@
|
|||
* @param array|null $parameters An array of parameters to be sent in the POST request.
|
||||
* @return CurlHandle The configured cURL handle ready for execution.
|
||||
*/
|
||||
protected static function buildPost(Bot $bot, string $method, ?array $parameters=null): CurlHandle
|
||||
{
|
||||
protected static function buildPost(Bot $bot, string $method, ?array $parameters = null): CurlHandle {
|
||||
$curl = curl_init(sprintf('%s/bot%s/%s', $bot->getEndpoint(), $bot->getToken(), $method));
|
||||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
|
||||
|
||||
if($parameters === null)
|
||||
{
|
||||
if ($parameters === null) {
|
||||
curl_setopt($curl, CURLOPT_POST, false);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
curl_setopt($curl, CURLOPT_POST, true);
|
||||
curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
|
||||
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($parameters));
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
use TgBotLib\Objects\InlineKeyboardMarkup;
|
||||
use TgBotLib\Objects\LinkPreviewOptions;
|
||||
use TgBotLib\Objects\Message;
|
||||
use TgBotLib\Objects\MessageId;
|
||||
use TgBotLib\Objects\ReplyKeyboardMarkup;
|
||||
use TgBotLib\Objects\ReplyKeyboardRemove;
|
||||
use TgBotLib\Objects\ReplyParameters;
|
||||
|
@ -30,6 +31,7 @@
|
|||
* @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.
|
||||
* @method MessageId[] forwardMessages(string|int $chat_id, string|int $from_chat_id, int[] $message_ids, ?int $message_thread_id=null, ?bool $disable_notification=null, ?bool $protect_content=null) Use this method to forward multiple messages of any kind. If some of the specified messages can't be found or forwarded, they are skipped. Service messages and messages with protected content can't be forwarded. Album grouping is kept for forwarded messages. On success, an array of MessageId of the sent messages is returned.
|
||||
*/
|
||||
class Bot
|
||||
{
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
use TgBotLib\Interfaces\ObjectTypeInterface;
|
||||
use TgBotLib\Methods\Close;
|
||||
use TgBotLib\Methods\ForwardMessage;
|
||||
use TgBotLib\Methods\ForwardMessages;
|
||||
use TgBotLib\Methods\GetMe;
|
||||
use TgBotLib\Methods\Logout;
|
||||
use TgBotLib\Methods\SendMessage;
|
||||
|
@ -18,6 +19,7 @@
|
|||
case CLOSE = 'close';
|
||||
case SEND_MESSAGE = 'sendMessage';
|
||||
case FORWARD_MESSAGE = 'forwardMessage';
|
||||
case FORWARD_MESSAGES = 'forwardMessages';
|
||||
|
||||
/**
|
||||
* Executes a command on the provided bot with the given parameters.
|
||||
|
@ -36,6 +38,7 @@
|
|||
self::CLOSE => Close::execute($bot, $parameters),
|
||||
self::SEND_MESSAGE => SendMessage::execute($bot, $parameters),
|
||||
self::FORWARD_MESSAGE => ForwardMessage::execute($bot, $parameters),
|
||||
self::FORWARD_MESSAGES => ForwardMessages::execute($bot, $parameters),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
53
src/TgBotLib/Methods/ForwardMessages.php
Normal file
53
src/TgBotLib/Methods/ForwardMessages.php
Normal file
|
@ -0,0 +1,53 @@
|
|||
<?php
|
||||
|
||||
namespace TgBotLib\Methods;
|
||||
|
||||
use TgBotLib\Abstracts\Method;
|
||||
use TgBotLib\Bot;
|
||||
use TgBotLib\Enums\Methods;
|
||||
use TgBotLib\Exceptions\TelegramException;
|
||||
use TgBotLib\Objects\MessageId;
|
||||
|
||||
class ForwardMessages extends Method
|
||||
{
|
||||
/**
|
||||
* Use this method to forward multiple messages of any kind. If some of the specified messages can't be found or
|
||||
* forwarded, they are skipped. Service messages and messages with protected content can't be forwarded.
|
||||
* Album grouping is kept for forwarded messages. On success, an array of MessageId of the sent messages is returned.
|
||||
*
|
||||
* @param Bot $bot
|
||||
* @param array $parameters
|
||||
* @return MessageId[]
|
||||
* @throws TelegramException
|
||||
*/
|
||||
public static function execute(Bot $bot, array $parameters = []): array
|
||||
{
|
||||
return array_map(fn($messageId) => MessageId::fromArray($messageId),
|
||||
self::executeCurl(self::buildPost($bot, Methods::FORWARD_MESSAGES->value, $parameters))
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public static function getRequiredParameters(): ?array
|
||||
{
|
||||
return [
|
||||
'chat_id',
|
||||
'from_chat_id',
|
||||
'message_ids'
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public static function getOptionalParameters(): ?array
|
||||
{
|
||||
return [
|
||||
'message_thread_id',
|
||||
'disable_notification',
|
||||
'protect_content'
|
||||
];
|
||||
}
|
||||
}
|
46
src/TgBotLib/Objects/MessageId.php
Normal file
46
src/TgBotLib/Objects/MessageId.php
Normal file
|
@ -0,0 +1,46 @@
|
|||
<?php
|
||||
|
||||
namespace TgBotLib\Objects;
|
||||
|
||||
use TgBotLib\Interfaces\ObjectTypeInterface;
|
||||
|
||||
class MessageId implements ObjectTypeInterface
|
||||
{
|
||||
private int $message_id;
|
||||
|
||||
/**
|
||||
* Unique message identifier
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getMessageId(): int
|
||||
{
|
||||
return $this->message_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function toArray(): ?array
|
||||
{
|
||||
return [
|
||||
'message_id' => $this->message_id
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public static function fromArray(?array $data): ?MessageId
|
||||
{
|
||||
if (empty($data))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
$object = new self();
|
||||
$object->message_id = $data['message_id'];
|
||||
|
||||
return $object;
|
||||
}
|
||||
}
|
55
tests/TgBotLib/Methods/ForwardMessagesTest.php
Normal file
55
tests/TgBotLib/Methods/ForwardMessagesTest.php
Normal file
|
@ -0,0 +1,55 @@
|
|||
<?php
|
||||
|
||||
namespace TgBotLib\Methods;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use TgBotLib\Bot;
|
||||
use TgBotLib\Enums\Types\ParseMode;
|
||||
use TgBotLib\Objects\LinkPreviewOptions;
|
||||
use TgBotLib\Objects\Message;
|
||||
use TgBotLib\Objects\MessageId;
|
||||
|
||||
class ForwardMessagesTest 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
|
||||
{
|
||||
$message1 = self::$bot->sendMessage(
|
||||
chat_id: self::TEST_CHAT_ID,
|
||||
text: 'This is a test message.'
|
||||
);
|
||||
|
||||
$message2 = self::$bot->sendMessage(
|
||||
chat_id: self::TEST_CHAT_ID,
|
||||
text: 'This is another test message.'
|
||||
);
|
||||
|
||||
$result = self::$bot->forwardMessages(
|
||||
chat_id: self::TEST_CHAT_ID,
|
||||
from_chat_id: self::TEST_CHAT_ID,
|
||||
message_ids: [$message1->getMessageId(), $message2->getMessageId()]
|
||||
);
|
||||
|
||||
$this->assertIsArray($result);
|
||||
$this->assertCount(2, $result);
|
||||
foreach ($result as $messageId)
|
||||
{
|
||||
$this->assertInstanceOf(MessageId::class, $messageId);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue