Added method SendDice

This commit is contained in:
netkas 2024-10-31 00:07:09 -04:00
parent 64e40f62cb
commit b8f6c81833
3 changed files with 162 additions and 0 deletions

View file

@ -15,6 +15,7 @@
use TgBotLib\Methods\SendAnimation;
use TgBotLib\Methods\SendAudio;
use TgBotLib\Methods\SendContact;
use TgBotLib\Methods\SendDice;
use TgBotLib\Methods\SendDocument;
use TgBotLib\Methods\SendLocation;
use TgBotLib\Methods\SendMediaGroup;
@ -50,6 +51,7 @@
case SEND_VENUE = 'sendVenue';
case SEND_CONTACT = 'sendContact';
case SEND_POLL = 'sendPoll';
case SEND_DICE = 'sendDice';
/**
* Executes a command on the provided bot with the given parameters.
@ -84,6 +86,7 @@
self::SEND_VENUE => SendVenue::execute($bot, $parameters),
self::SEND_CONTACT => SendContact::execute($bot, $parameters),
self::SEND_POLL => SendPoll::execute($bot, $parameters),
self::SEND_DICE => SendDice::execute($bot, $parameters),
};
}
}

View file

@ -0,0 +1,60 @@
<?php
namespace TgBotLib\Methods;
use TgBotLib\Abstracts\Method;
use TgBotLib\Bot;
use TgBotLib\Enums\Methods;
use TgBotLib\Objects\Message;
use TgBotLib\Objects\ReplyParameters;
class SendDice extends Method
{
/**
* @inheritDoc
*/
public static function execute(Bot $bot, array $parameters = []): mixed
{
// 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();
}
// Make request
return Message::fromArray(self::executeCurl(self::buildPost($bot, Methods::SEND_DICE->value, $parameters)));
}
/**
* @inheritDoc
*/
public static function getRequiredParameters(): ?array
{
return [
'chat_id'
];
}
/**
* @inheritDoc
*/
public static function getOptionalParameters(): ?array
{
return [
'business_connection_id',
'message_thread_id',
'emoji',
'disable_notification',
'protect_content',
'message_effect_id',
'reply_parameters',
'reply_markup'
];
}
}

View file

@ -0,0 +1,99 @@
<?php
namespace TgBotLib\Methods;
use PHPUnit\Framework\TestCase;
use TgBotLib\Bot;
use TgBotLib\Exceptions\TelegramException;
use TgBotLib\Objects\Message;
class SendDiceTest 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 testSendDice(): void
{
$result = self::$bot->sendDice(
chat_id: TEST_CHAT_ID,
emoji: '🎲'
);
$this->assertInstanceOf(Message::class, $result);
$this->assertNotNull($result->getDice());
$this->assertEquals('🎲', $result->getDice()->getEmoji());
}
public function testSendTargetTest(): void
{
$result = self::$bot->sendDice(
chat_id: TEST_CHAT_ID,
emoji: '🎯'
);
$this->assertInstanceOf(Message::class, $result);
$this->assertNotNull($result->getDice());
$this->assertEquals('🎯', $result->getDice()->getEmoji());
}
public function testSendBasketballTest(): void
{
$result = self::$bot->sendDice(
chat_id: TEST_CHAT_ID,
emoji: '🏀'
);
$this->assertInstanceOf(Message::class, $result);
$this->assertNotNull($result->getDice());
$this->assertEquals('🏀', $result->getDice()->getEmoji());
}
public function testSendCousinsBowlingTest(): void
{
$result = self::$bot->sendDice(
chat_id: TEST_CHAT_ID,
emoji: '🎳'
);
$this->assertInstanceOf(Message::class, $result);
$this->assertNotNull($result->getDice());
$this->assertEquals('🎳', $result->getDice()->getEmoji());
}
public function testSendSoccerTest(): void
{
$result = self::$bot->sendDice(
chat_id: TEST_CHAT_ID,
emoji: '⚽'
);
$this->assertInstanceOf(Message::class, $result);
$this->assertNotNull($result->getDice());
$this->assertEquals('⚽', $result->getDice()->getEmoji());
}
public function testSlotMachineTest(): void
{
$result = self::$bot->sendDice(
chat_id: TEST_CHAT_ID,
emoji: '🎰'
);
$this->assertInstanceOf(Message::class, $result);
$this->assertNotNull($result->getDice());
$this->assertEquals('🎰', $result->getDice()->getEmoji());
}
}