Add SendContact method with corresponding test.

This commit is contained in:
netkas 2024-10-10 12:47:37 -04:00
parent 012fc4fe0f
commit 561542bc62
3 changed files with 115 additions and 0 deletions

View file

@ -14,6 +14,7 @@
use TgBotLib\Methods\Logout;
use TgBotLib\Methods\SendAnimation;
use TgBotLib\Methods\SendAudio;
use TgBotLib\Methods\SendContact;
use TgBotLib\Methods\SendDocument;
use TgBotLib\Methods\SendLocation;
use TgBotLib\Methods\SendMediaGroup;
@ -46,6 +47,7 @@
case SEND_MEDIA_GROUP = 'sendMediaGroup';
case SEND_LOCATION = 'sendLocation';
case SEND_VENUE = 'sendVenue';
case SEND_CONTACT = 'sendContact';
/**
* Executes a command on the provided bot with the given parameters.
@ -78,6 +80,7 @@
self::SEND_MEDIA_GROUP => SendMediaGroup::execute($bot, $parameters),
self::SEND_LOCATION => SendLocation::execute($bot, $parameters),
self::SEND_VENUE => SendVenue::execute($bot, $parameters),
self::SEND_CONTACT => SendContact::execute($bot, $parameters),
};
}
}

View file

@ -0,0 +1,72 @@
<?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 SendContact extends Method
{
/**
* Use this method to send phone contacts. 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();
}
// Make request
$curl = self::buildPost($bot, Methods::SEND_CONTACT->value, $parameters);
$result = self::executeCurl($curl);
return Message::fromArray($result);
}
/**
* @inheritDoc
*/
public static function getRequiredParameters(): ?array
{
return [
'chat_id',
'phone_number',
'first_name',
];
}
/**
* @inheritDoc
*/
public static function getOptionalParameters(): ?array
{
return [
'business_connection_id',
'message_thread_id',
'last_name',
'vcard',
'disable_notification',
'protect_content',
'message_effect_id',
'reply_parameters',
'reply_markup',
];
}
}

View file

@ -0,0 +1,40 @@
<?php
namespace TgBotLib\Methods;
use PHPUnit\Framework\TestCase;
use TgBotLib\Bot;
use TgBotLib\Exceptions\TelegramException;
use TgBotLib\Objects\Message;
class SendContactTest 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 sendContact method.
*
* @return void
*/
public function testSendContact(): void
{
// Execute the method and retrieve the result
$result = self::$bot->sendContact(
chat_id: TEST_CHAT_ID,
phone_number: '18884074747',
first_name: 'FBI',
);
// Assert that the result is an instance of Message
$this->assertInstanceOf(Message::class, $result);
}
}