Added SendVoice method

This commit is contained in:
netkas 2024-10-09 14:21:50 -04:00
parent d528993069
commit e7c438bc6a
4 changed files with 233 additions and 0 deletions

View file

@ -18,6 +18,7 @@
use TgBotLib\Methods\SendMessage; use TgBotLib\Methods\SendMessage;
use TgBotLib\Methods\SendPhoto; use TgBotLib\Methods\SendPhoto;
use TgBotLib\Methods\SendVideo; use TgBotLib\Methods\SendVideo;
use TgBotLib\Methods\SendVoice;
enum Methods : string enum Methods : string
{ {
@ -34,6 +35,7 @@
case SEND_DOCUMENT = 'sendDocument'; case SEND_DOCUMENT = 'sendDocument';
case SEND_VIDEO = 'sendVideo'; case SEND_VIDEO = 'sendVideo';
case SEND_ANIMATION = 'sendAnimation'; case SEND_ANIMATION = 'sendAnimation';
case SEND_VOICE = 'sendVoice';
/** /**
* Executes a command on the provided bot with the given parameters. * Executes a command on the provided bot with the given parameters.
@ -60,6 +62,7 @@
self::SEND_DOCUMENT => SendDocument::execute($bot, $parameters), self::SEND_DOCUMENT => SendDocument::execute($bot, $parameters),
self::SEND_VIDEO => SendVideo::execute($bot, $parameters), self::SEND_VIDEO => SendVideo::execute($bot, $parameters),
self::SEND_ANIMATION => SendAnimation::execute($bot, $parameters), self::SEND_ANIMATION => SendAnimation::execute($bot, $parameters),
self::SEND_VOICE => SendVoice::execute($bot, $parameters),
}; };
} }
} }

View file

@ -0,0 +1,110 @@
<?php
namespace TgBotLib\Methods;
use TgBotLib\Abstracts\Method;
use TgBotLib\Bot;
use TgBotLib\Enums\Methods;
use TgBotLib\Exceptions\TelegramException;
use TgBotLib\Interfaces\ObjectTypeInterface;
use TgBotLib\Objects\Message;
use TgBotLib\Objects\MessageEntity;
use TgBotLib\Objects\ReplyParameters;
class SendVoice extends Method
{
/**
* Use this method to send audio files, if you want Telegram clients to display the file as a playable voice
* message. For this to work, your audio must be in an .OGG file encoded with OPUS, or in .MP3 format, or in
* .M4A format (other formats may be sent as Audio or Document). On success, the sent Message is returned.
* Bots can currently send voice messages of up to 50 MB in size, this limit may be changed in the future.
*
* @param Bot $bot
* @param array $parameters
* @return Message
* @throws TelegramException
*/
public static function execute(Bot $bot, array $parameters = []): Message
{
// Handle object conversions
if (isset($parameters['caption_entities']) && $parameters['caption_entities'])
{
$entities = [];
foreach ($parameters['caption_entities'] as $entity)
{
if ($entity instanceof MessageEntity)
{
$entities[] = $entity->toArray();
}
else
{
$entities[] = $entity;
}
}
$parameters['caption_entities'] = $entities;
}
if (isset($parameters['reply_parameters']) && $parameters['reply_parameters'] instanceof ReplyParameters)
{
$parameters['reply_parameters'] = $parameters['reply_parameters']->toArray();
}
if (isset($parameters['reply_markup']))
{
if ($parameters['reply_markup'] instanceof ObjectTypeInterface)
{
$parameters['reply_markup'] = json_encode($parameters['reply_markup']->toArray());
}
elseif (is_array($parameters['reply_markup']))
{
$parameters['reply_markup'] = json_encode($parameters['reply_markup']);
}
}
// Handle different voice input types
if (isset($parameters['voice']))
{
$voice = $parameters['voice'];
// If voice is a file path and exists locally
if (is_string($voice) && file_exists($voice) && is_file($voice))
{
return Message::fromArray(self::executeCurl(self::buildUpload($bot, Methods::SEND_VOICE->value, 'voice', $voice, array_diff_key($parameters, ['voice' => null]))));
}
}
// If voice is a file_id or URL, use regular POST method
return Message::fromArray(self::executeCurl(self::buildPost($bot, Methods::SEND_VOICE->value, $parameters)));
}
/**
* @inheritDoc
*/
public static function getRequiredParameters(): ?array
{
return [
'chat_id',
'voice',
];
}
/**
* @inheritDoc
*/
public static function getOptionalParameters(): ?array
{
return [
'business_connection_id',
'message_thread_id',
'caption',
'parse_mode',
'caption_entities',
'duration',
'disable_notification',
'protect_content',
'message_effect_id',
'reply_parameters',
'reply_markup'
];
}
}

View file

@ -0,0 +1,120 @@
<?php
namespace TgBotLib\Methods;
use PHPUnit\Framework\TestCase;
use TgBotLib\Bot;
use TgBotLib\Exceptions\TelegramException;
use TgBotLib\Objects\InlineKeyboardButton;
use TgBotLib\Objects\InlineKeyboardMarkup;
use TgBotLib\Objects\Message;
use TgBotLib\Objects\MessageEntity;
class SendVoiceTest extends TestCase
{
private static Bot $bot;
/**
* Set up the bot instance before all tests.
*
* @return void
*/
public static function setUpBeforeClass(): void
{
self::$bot = new Bot(BOT_TOKEN);
}
/**
* Tests sending a voice file to a specified chat.
*
* @return void
*/
public function testSendVoice(): void
{
$result = self::$bot->sendVoice(
chat_id: TEST_CHAT_ID,
voice: __DIR__ . DIRECTORY_SEPARATOR . 'sample' . DIRECTORY_SEPARATOR . 'ted.ogg',
caption: 'Test Unit: testSendVoice',
duration: 30,
disable_notification: true
);
$this->assertInstanceOf(Message::class, $result);
$this->assertEquals(TEST_CHAT_ID, $result->getChat()->getId());
}
/**
* Tests sending a voice file using a URL.
*
* @return void
*/
public function testSendVoiceWithUrl(): void
{
$result = self::$bot->sendVoice(
chat_id: TEST_CHAT_ID,
voice: 'https://example.com/voice.ogg',
caption: 'Here is a voice message from a URL',
duration: 25
);
$this->assertInstanceOf(Message::class, $result);
$this->assertEquals(TEST_CHAT_ID, $result->getChat()->getId());
$this->assertEquals(25, $result->getVoice()->getDuration());
}
/**
* Tests sending a voice file with caption entities.
*
* @return void
*/
public function testSendVoiceWithCaptionEntities(): void
{
$captionEntities = [
new MessageEntity(type: 'bold', offset: 0, length: 4),
new MessageEntity(type: 'italic', offset: 5, length: 5)
];
$result = self::$bot->sendVoice(
chat_id: TEST_CHAT_ID,
voice: 'https://example.com/voice.ogg',
caption: 'This is a test',
caption_entities: $captionEntities
);
$this->assertInstanceOf(Message::class, $result);
$this->assertEquals(TEST_CHAT_ID, $result->getChat()->getId());
$this->assertEquals('This is a test', $result->getCaption());
}
/**
* Tests sending a voice file with custom reply markup.
*
* @return void
*/
public function testSendVoiceWithReplyMarkup(): void
{
$replyMarkup = new InlineKeyboardMarkup();
$replyMarkup->addRow(
InlineKeyboardButton::fromArray(['text' => 'Button 1', 'callback_data' => 'button1']),
InlineKeyboardButton::fromArray(['text' => 'Button 2', 'callback_data' => 'button2'])
);
try
{
$result = self::$bot->sendVoice(
chat_id: TEST_CHAT_ID,
voice: __DIR__ . DIRECTORY_SEPARATOR . 'sample' . DIRECTORY_SEPARATOR . 'ted.ogg',
reply_markup: $replyMarkup
);
$this->assertInstanceOf(Message::class, $result);
$this->assertEquals(TEST_CHAT_ID, $result->getChat()->getId());
}
catch (TelegramException $e)
{
$this->fail('Failed to send voice message: ' . $e->getMessage());
}
}
}

Binary file not shown.