From 012fc4fe0fba21078d638beee84413f56e4fb137 Mon Sep 17 00:00:00 2001 From: netkas Date: Thu, 10 Oct 2024 12:26:12 -0400 Subject: [PATCH] Add SendVenue method to send venue information --- src/TgBotLib/Enums/Methods.php | 3 + src/TgBotLib/Methods/SendVenue.php | 95 +++++++++++++++++++++ tests/TgBotLib/Methods/SendVenueTest.php | 102 +++++++++++++++++++++++ 3 files changed, 200 insertions(+) create mode 100644 src/TgBotLib/Methods/SendVenue.php create mode 100644 tests/TgBotLib/Methods/SendVenueTest.php diff --git a/src/TgBotLib/Enums/Methods.php b/src/TgBotLib/Enums/Methods.php index 968d454..a90e085 100644 --- a/src/TgBotLib/Enums/Methods.php +++ b/src/TgBotLib/Enums/Methods.php @@ -20,6 +20,7 @@ use TgBotLib\Methods\SendMessage; use TgBotLib\Methods\SendPaidMedia; use TgBotLib\Methods\SendPhoto; + use TgBotLib\Methods\SendVenue; use TgBotLib\Methods\SendVideo; use TgBotLib\Methods\SendVideoNote; use TgBotLib\Methods\SendVoice; @@ -44,6 +45,7 @@ case SEND_PAID_MEDIA = 'sendPaidMedia'; case SEND_MEDIA_GROUP = 'sendMediaGroup'; case SEND_LOCATION = 'sendLocation'; + case SEND_VENUE = 'sendVenue'; /** * 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_MEDIA_GROUP => SendMediaGroup::execute($bot, $parameters), self::SEND_LOCATION => SendLocation::execute($bot, $parameters), + self::SEND_VENUE => SendVenue::execute($bot, $parameters), }; } } diff --git a/src/TgBotLib/Methods/SendVenue.php b/src/TgBotLib/Methods/SendVenue.php new file mode 100644 index 0000000..f02741d --- /dev/null +++ b/src/TgBotLib/Methods/SendVenue.php @@ -0,0 +1,95 @@ +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' + ]; + } + } \ No newline at end of file diff --git a/tests/TgBotLib/Methods/SendVenueTest.php b/tests/TgBotLib/Methods/SendVenueTest.php new file mode 100644 index 0000000..d1354fb --- /dev/null +++ b/tests/TgBotLib/Methods/SendVenueTest.php @@ -0,0 +1,102 @@ +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: '' + ); + } +} \ No newline at end of file