Add SendPoll method with tests and enhance Poll object
This commit is contained in:
parent
33f7a094fc
commit
9a4e537294
6 changed files with 234 additions and 11 deletions
|
@ -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),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
96
src/TgBotLib/Methods/SendPoll.php
Normal file
96
src/TgBotLib/Methods/SendPoll.php
Normal file
|
@ -0,0 +1,96 @@
|
|||
<?php
|
||||
|
||||
namespace TgBotLib\Methods;
|
||||
|
||||
use TgBotLib\Abstracts\Method;
|
||||
use TgBotLib\Bot;
|
||||
use TgBotLib\Enums\Methods;
|
||||
use TgBotLib\Exceptions\TelegramException;
|
||||
use TgBotLib\Objects\Message;
|
||||
use TgBotLib\Objects\ReplyParameters;
|
||||
|
||||
class SendPoll extends Method
|
||||
{
|
||||
/**
|
||||
* Use this method to send a native poll. 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
|
||||
{
|
||||
// Handle reply parameters
|
||||
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']) && 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',
|
||||
];
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -4,7 +4,6 @@
|
|||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use TgBotLib\Bot;
|
||||
use TgBotLib\Exceptions\TelegramException;
|
||||
use TgBotLib\Objects\Message;
|
||||
|
||||
class SendContactTest extends TestCase
|
||||
|
|
74
tests/TgBotLib/Methods/SendPollTest.php
Normal file
74
tests/TgBotLib/Methods/SendPollTest.php
Normal file
|
@ -0,0 +1,74 @@
|
|||
<?php
|
||||
|
||||
namespace TgBotLib\Methods;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use TgBotLib\Bot;
|
||||
use TgBotLib\Objects\InputPollOption;
|
||||
use TgBotLib\Objects\Message;
|
||||
|
||||
class SendPollTest extends TestCase
|
||||
{
|
||||
private static Bot $bot;
|
||||
|
||||
public static function setUpBeforeClass(): void
|
||||
{
|
||||
// Initialize the Bot instance before tests
|
||||
self::$bot = new Bot(BOT_TOKEN);
|
||||
self::$bot->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);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Add table
Reference in a new issue