From 9a4e5372941a4d4ea8d79f137010f42bb6868209 Mon Sep 17 00:00:00 2001 From: netkas Date: Thu, 10 Oct 2024 13:20:40 -0400 Subject: [PATCH] Add SendPoll method with tests and enhance Poll object --- src/TgBotLib/Enums/Methods.php | 3 + src/TgBotLib/Methods/SendPoll.php | 96 ++++++++++++++++++++++ src/TgBotLib/Objects/InputPollOption.php | 59 ++++++++++++- src/TgBotLib/Objects/Poll.php | 12 +-- tests/TgBotLib/Methods/SendContactTest.php | 1 - tests/TgBotLib/Methods/SendPollTest.php | 74 +++++++++++++++++ 6 files changed, 234 insertions(+), 11 deletions(-) create mode 100644 src/TgBotLib/Methods/SendPoll.php create mode 100644 tests/TgBotLib/Methods/SendPollTest.php diff --git a/src/TgBotLib/Enums/Methods.php b/src/TgBotLib/Enums/Methods.php index d15e582..aa84c61 100644 --- a/src/TgBotLib/Enums/Methods.php +++ b/src/TgBotLib/Enums/Methods.php @@ -21,6 +21,7 @@ use TgBotLib\Methods\SendMessage; use TgBotLib\Methods\SendPaidMedia; use TgBotLib\Methods\SendPhoto; + use TgBotLib\Methods\SendPoll; use TgBotLib\Methods\SendVenue; use TgBotLib\Methods\SendVideo; use TgBotLib\Methods\SendVideoNote; @@ -48,6 +49,7 @@ case SEND_LOCATION = 'sendLocation'; case SEND_VENUE = 'sendVenue'; case SEND_CONTACT = 'sendContact'; + case SEND_POLL = 'sendPoll'; /** * Executes a command on the provided bot with the given parameters. @@ -81,6 +83,7 @@ self::SEND_LOCATION => SendLocation::execute($bot, $parameters), self::SEND_VENUE => SendVenue::execute($bot, $parameters), self::SEND_CONTACT => SendContact::execute($bot, $parameters), + self::SEND_POLL => SendPoll::execute($bot, $parameters), }; } } diff --git a/src/TgBotLib/Methods/SendPoll.php b/src/TgBotLib/Methods/SendPoll.php new file mode 100644 index 0000000..d45673e --- /dev/null +++ b/src/TgBotLib/Methods/SendPoll.php @@ -0,0 +1,96 @@ +toArray(); + } + + // Handle reply markup + if (isset($parameters['reply_markup']) && method_exists($parameters['reply_markup'], 'toArray')) + { + $parameters['reply_markup'] = $parameters['reply_markup']->toArray(); + } + + // Handle question entities + if (isset($parameters['question_entities']) && is_array($parameters['question_entities'])) + { + $parameters['question_entities'] = json_encode(array_map(function ($entity) {return $entity->toArray();}, $parameters['question_entities'])); + } + + // Handle explanation entities + if (isset($parameters['explanation_entities']) && is_array($parameters['explanation_entities'])) + { + $parameters['explanation_entities'] = json_encode(array_map(function ($entity) {return $entity->toArray();}, $parameters['explanation_entities'])); + } + + if (isset($parameters['options']) && is_array($parameters['options'])) + { + $parameters['options'] = json_encode(array_map(function ($option) {return $option->toArray();}, $parameters['options'])); + } + + // Make request + return Message::fromArray(self::executeCurl(self::buildPost($bot, Methods::SEND_POLL->value, $parameters))); + } + + /** + * @inheritDoc + */ + public static function getRequiredParameters(): ?array + { + return [ + 'chat_id', + 'question', + 'options', + ]; + } + + /** + * @inheritDoc + */ + public static function getOptionalParameters(): ?array + { + return [ + 'business_connection_id', + 'message_thread_id', + 'question_parse_mode', + 'question_entities', + 'is_anonymous', + 'type', + 'allows_multiple_answers', + 'correct_option_id', + 'explanation', + 'explanation_parse_mode', + 'explanation_entities', + 'open_period', + 'close_date', + 'is_closed', + 'disable_notification', + 'protect_content', + 'message_effect_id', + 'reply_parameters', + 'reply_markup', + ]; + } + } diff --git a/src/TgBotLib/Objects/InputPollOption.php b/src/TgBotLib/Objects/InputPollOption.php index 9243152..056f884 100644 --- a/src/TgBotLib/Objects/InputPollOption.php +++ b/src/TgBotLib/Objects/InputPollOption.php @@ -14,6 +14,17 @@ */ private ?array $text_entities; + + /** + * InputPollOption constructor. + */ + public function __construct() + { + $this->text = (string)null; + $this->text_parse_mode = null; + $this->text_entities = null; + } + /** * Option text, 1-100 characters * @@ -24,6 +35,16 @@ return $this->text; } + /** + * @param string $text + * @return InputPollOption + */ + public function setText(string $text): InputPollOption + { + $this->text = $text; + return $this; + } + /** * Optional. Mode for parsing entities in the text. See formatting options for more details. * Currently, only custom emoji entities are allowed @@ -35,6 +56,16 @@ return $this->text_parse_mode; } + /** + * @param ParseMode|null $text_parse_mode + * @return InputPollOption + */ + public function setTextParseMode(?ParseMode $text_parse_mode): InputPollOption + { + $this->text_parse_mode = $text_parse_mode; + return $this; + } + /** * Optional. A JSON-serialized list of special entities that appear in the poll option text. * It can be specified instead of text_parse_mode @@ -46,16 +77,36 @@ return $this->text_entities; } + /** + * @param MessageEntity[]|null $text_entities + * @return InputPollOption + */ + public function setTextEntities(?array $text_entities): InputPollOption + { + $this->text_entities = $text_entities; + return $this; + } + /** * @inheritDoc */ public function toArray(): ?array { - return [ - 'text' => $this->text, - 'text_parse_mode' => $this->text_parse_mode?->value, - 'text_entities' => array_map(fn(MessageEntity $item) => $item->toArray(), $this->text_entities) + $array = [ + 'text' => $this->text ]; + + if($this->text_parse_mode !== null) + { + $array['text_parse_mode'] = $this->text_parse_mode->value; + } + + if($this->text_entities !== null) + { + $array['text_entities'] = array_map(fn($item) => $item->toArray(), $this->text_entities); + } + + return $array; } /** diff --git a/src/TgBotLib/Objects/Poll.php b/src/TgBotLib/Objects/Poll.php index 7229a51..96f6956 100644 --- a/src/TgBotLib/Objects/Poll.php +++ b/src/TgBotLib/Objects/Poll.php @@ -19,7 +19,7 @@ private string $type; private bool $allow_multiple_answers; private ?int $correct_option_id; - private string $explanation; + private ?string $explanation; /** * @var MessageEntity[]|null */ @@ -122,9 +122,9 @@ * Optional. Text that is shown when a user chooses an incorrect answer or taps on the lamp icon in * a quiz-style poll, 0-200 characters * - * @return string + * @return string|null */ - public function getExplanation(): string + public function getExplanation(): ?string { return $this->explanation; } @@ -201,10 +201,10 @@ $object->id = $data['id'] ?? null; $object->question = $data['question'] ?? null; $object->total_voter_count = $data['total_voter_count'] ?? null; - $object->is_closed = $data['is_closed'] ?? null; - $object->is_anonymous = $data['is_anonymous'] ?? null; + $object->is_closed = $data['is_closed'] ?? false; + $object->is_anonymous = $data['is_anonymous'] ?? false; $object->type = $data['type'] ?? null; - $object->allow_multiple_answers = $data['allow_multiple_answers'] ?? null; + $object->allow_multiple_answers = $data['allow_multiple_answers'] ?? false; $object->correct_option_id = $data['correct_option_id'] ?? null; $object->explanation = $data['explanation'] ?? null; $object->open_period = $data['open_period'] ?? null; diff --git a/tests/TgBotLib/Methods/SendContactTest.php b/tests/TgBotLib/Methods/SendContactTest.php index 8a8ea1e..0502875 100644 --- a/tests/TgBotLib/Methods/SendContactTest.php +++ b/tests/TgBotLib/Methods/SendContactTest.php @@ -4,7 +4,6 @@ use PHPUnit\Framework\TestCase; use TgBotLib\Bot; - use TgBotLib\Exceptions\TelegramException; use TgBotLib\Objects\Message; class SendContactTest extends TestCase diff --git a/tests/TgBotLib/Methods/SendPollTest.php b/tests/TgBotLib/Methods/SendPollTest.php new file mode 100644 index 0000000..48c03de --- /dev/null +++ b/tests/TgBotLib/Methods/SendPollTest.php @@ -0,0 +1,74 @@ +setAutoRetry(true); + } + + /** + * Tests the sendPoll method with basic parameters. + * + * @return void + */ + public function testSendPollBasic(): void + { + + // Execute the method and retrieve the result + $result = self::$bot->sendPoll( + chat_id: TEST_CHAT_ID, + question: 'What is your favorite color?', + options: [ + (new InputPollOption())->setText('Red'), + (new InputPollOption())->setText('Blue'), + (new InputPollOption())->setText('Green'), + ] + ); + + // Assert that the result is an instance of Message + $this->assertInstanceOf(Message::class, $result); + } + + /** + * Tests the sendPoll method with advanced options. + * + * @return void + */ + public function testSendPollWithAdvancedOptions(): void + { + // Execute the method and retrieve the result + $result = self::$bot->sendPoll( + chat_id: TEST_CHAT_ID, + question: 'What is your favorite color?', + options: [ + (new InputPollOption())->setText('Red'), + (new InputPollOption())->setText('Blue'), + (new InputPollOption())->setText('Green'), + ], + is_anonymous: false, + allows_multiple_answers: true, + correct_option_id: 1, + explanation: 'The correct answer is Blue.', + explanation_parse_mode: 'MarkdownV2', + open_period: 60, + close_date: strtotime('+1 day') + ); + + // Assert that the result is an instance of Message + $this->assertInstanceOf(Message::class, $result); + } + +} +