Add SendVenue method to send venue information

This commit is contained in:
netkas 2024-10-10 12:26:12 -04:00
parent e4787b90a1
commit 012fc4fe0f
3 changed files with 200 additions and 0 deletions

View file

@ -20,6 +20,7 @@
use TgBotLib\Methods\SendMessage; use TgBotLib\Methods\SendMessage;
use TgBotLib\Methods\SendPaidMedia; use TgBotLib\Methods\SendPaidMedia;
use TgBotLib\Methods\SendPhoto; use TgBotLib\Methods\SendPhoto;
use TgBotLib\Methods\SendVenue;
use TgBotLib\Methods\SendVideo; use TgBotLib\Methods\SendVideo;
use TgBotLib\Methods\SendVideoNote; use TgBotLib\Methods\SendVideoNote;
use TgBotLib\Methods\SendVoice; use TgBotLib\Methods\SendVoice;
@ -44,6 +45,7 @@
case SEND_PAID_MEDIA = 'sendPaidMedia'; case SEND_PAID_MEDIA = 'sendPaidMedia';
case SEND_MEDIA_GROUP = 'sendMediaGroup'; case SEND_MEDIA_GROUP = 'sendMediaGroup';
case SEND_LOCATION = 'sendLocation'; case SEND_LOCATION = 'sendLocation';
case SEND_VENUE = 'sendVenue';
/** /**
* Executes a command on the provided bot with the given parameters. * Executes a command on the provided bot with the given parameters.
@ -75,6 +77,7 @@
self::SEND_PAID_MEDIA => SendPaidMedia::execute($bot, $parameters), self::SEND_PAID_MEDIA => SendPaidMedia::execute($bot, $parameters),
self::SEND_MEDIA_GROUP => SendMediaGroup::execute($bot, $parameters), self::SEND_MEDIA_GROUP => SendMediaGroup::execute($bot, $parameters),
self::SEND_LOCATION => SendLocation::execute($bot, $parameters), self::SEND_LOCATION => SendLocation::execute($bot, $parameters),
self::SEND_VENUE => SendVenue::execute($bot, $parameters),
}; };
} }
} }

View file

@ -0,0 +1,95 @@
<?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\ReplyParameters;
class SendVenue extends Method
{
/**
* Use this method to send information about a venue.
* On success, the sent Message is returned.
*
* @param Bot $bot
* @param array $parameters
* @return Message
* @throws TelegramException
*/
public static function execute(Bot $bot, array $parameters = []): Message
{
// Validate required float parameters
foreach (['latitude', 'longitude'] as $param)
{
if (!isset($parameters[$param]) || !is_numeric($parameters[$param]))
{
throw new TelegramException("Parameter '$param' must be a numeric value");
}
}
// Validate required string parameters
foreach (['title', 'address'] as $param)
{
if (!isset($parameters[$param]) || !is_string($parameters[$param]) || empty(trim($parameters[$param])))
{
throw new TelegramException("Parameter '$param' must be a non-empty string");
}
}
// Handle ReplyParameters
if (isset($parameters['reply_parameters']) && $parameters['reply_parameters'] instanceof ReplyParameters)
{
$parameters['reply_parameters'] = $parameters['reply_parameters']->toArray();
}
// Handle reply_markup
if (isset($parameters['reply_markup']))
{
if ($parameters['reply_markup'] instanceof ObjectTypeInterface)
{
$parameters['reply_markup'] = json_encode($parameters['reply_markup']->toArray());
}
}
return Message::fromArray(self::executeCurl(self::buildPost($bot, Methods::SEND_VENUE->value, $parameters)));
}
/**
* @inheritDoc
*/
public static function getRequiredParameters(): ?array
{
return [
'chat_id',
'latitude',
'longitude',
'title',
'address'
];
}
/**
* @inheritDoc
*/
public static function getOptionalParameters(): ?array
{
return [
'business_connection_id',
'message_thread_id',
'foursquare_id',
'foursquare_type',
'google_place_id',
'google_place_type',
'disable_notification',
'protect_content',
'message_effect_id',
'reply_parameters',
'reply_markup'
];
}
}

View file

@ -0,0 +1,102 @@
<?php
namespace TgBotLib\Methods;
use PHPUnit\Framework\TestCase;
use TgBotLib\Bot;
use TgBotLib\Exceptions\TelegramException;
use TgBotLib\Objects\Message;
class SendVenueTest extends TestCase
{
private static Bot $bot;
/**
* @return void
*/
public static function setUpBeforeClass(): void
{
self::$bot = new Bot(BOT_TOKEN);
self::$bot->setAutoRetry(true);
}
/**
* Tests the basic `sendVenue` functionality
*
* @return void
*/
public function testSendVenue(): void
{
$result = self::$bot->sendVenue(
chat_id: TEST_CHAT_ID,
latitude: 51.5074,
longitude: -0.1278,
title: 'Big Ben',
address: 'Westminster, London SW1A 0AA'
);
$this->assertInstanceOf(Message::class, $result);
$this->assertNotNull($result->getVenue());
$this->assertEquals('Big Ben', $result->getVenue()->getTitle());
$this->assertEquals('Westminster, London SW1A 0AA', $result->getVenue()->getAddress());
$this->assertEquals(51.507394, $result->getVenue()->getLocation()->getLatitude());
$this->assertEquals(-0.127813, $result->getVenue()->getLocation()->getLongitude());
}
/**
* Tests sending venue with additional parameters
*
* @return void
*/
public function testSendVenueWithAdditionalParams(): void
{
$result = self::$bot->sendVenue(
chat_id: TEST_CHAT_ID,
latitude: 51.5074,
longitude: -0.1278,
title: 'Big Ben',
address: 'Westminster, London SW1A 0AA',
);
$this->assertInstanceOf(Message::class, $result);
$this->assertNotNull($result->getVenue());
$this->assertEquals('Big Ben', $result->getVenue()->getTitle());
$this->assertEquals('Westminster, London SW1A 0AA', $result->getVenue()->getAddress());
}
/**
* Tests sending venue with invalid latitude
*
* @return void
*/
public function testSendVenueInvalidLatitude(): void
{
$this->expectException(TelegramException::class);
self::$bot->sendVenue(
chat_id: TEST_CHAT_ID,
latitude: 'invalid',
longitude: -0.1278,
title: 'Big Ben',
address: 'Westminster, London SW1A 0AA'
);
}
/**
* Tests sending venue with empty address
*
* @return void
*/
public function testSendVenueEmptyAddress(): void
{
$this->expectException(TelegramException::class);
self::$bot->sendVenue(
chat_id: TEST_CHAT_ID,
latitude: 51.5074,
longitude: -0.1278,
title: 'Big Ben',
address: ''
);
}
}