Fix object instantiation and add getUpdate functionality

This commit is contained in:
netkas 2024-11-01 13:24:11 -04:00
parent 845a0d3b3c
commit f2f26998ad
6 changed files with 57 additions and 12 deletions

View file

@ -43,11 +43,13 @@
use TgBotLib\Objects\ReplyKeyboardRemove;
use TgBotLib\Objects\ReplyParameters;
use TgBotLib\Objects\Stickers\Sticker;
use TgBotLib\Objects\Update;
use TgBotLib\Objects\User;
use TgBotLib\Objects\UserChatBoosts;
use TgBotLib\Objects\UserProfilePhotos;
/**
* @method Update[] getUpdates(?int $offset=null, ?int $limit=null, ?int $timeout=null, ?string $allowed_updates=null) Use this method to receive incoming updates using long polling (wiki). An Array of Update objects is returned.
* @method User getMe() A simple method for testing your bot's authentication token. Requires no parameters. Returns basic information about the bot in form of a User object.
* @method bool logOut() Use this method to log out from the cloud Bot API server before launching the bot locally. You must log out the bot before running it locally, otherwise there is no guarantee that the bot will receive updates. After a successful call, you can immediately log in on a local server, but will not be able to log in back to the cloud Bot API server for 10 minutes. Returns True on success. Requires no parameters.
* @method bool close() Use this method to close the bot instance before moving it from one local server to another. You need to delete the webhook before calling this method to ensure that the bot isn't launched again after server restart. The method will return error 429 in the first 10 minutes after the bot is launched. Returns True on success. Requires no parameters.

View file

@ -17,7 +17,7 @@ class GetUpdates extends Method
{
return array_map(
fn($update) => Update::fromArray($update),
self::executeCurl(self::buildPost($bot, Methods::GET_UPDATES->value, $parameters))['result']
self::executeCurl(self::buildPost($bot, Methods::GET_UPDATES->value, $parameters))
);
}
@ -26,7 +26,7 @@ class GetUpdates extends Method
*/
public static function getRequiredParameters(): ?array
{
// TODO: Implement getRequiredParameters() method.
return null;
}
/**
@ -34,6 +34,11 @@ class GetUpdates extends Method
*/
public static function getOptionalParameters(): ?array
{
// TODO: Implement getOptionalParameters() method.
return [
'offset',
'limit',
'timeout',
'allowed_updates',
];
}
}

View file

@ -1142,7 +1142,7 @@
$object->poll = isset($data['poll']) ? Poll::fromArray($data['poll']) : null;
$object->venue = isset($data['venue']) ? Venue::fromArray($data['venue']) : null;
$object->location = isset($data['location']) ? Location::fromArray($data['location']) : null;
$object->new_chat_members = isset($data['new_chat_members']) ? array_map(fn($item) => User::fromArray($data), $data['new_chat_members']) : null;
$object->new_chat_members = isset($data['new_chat_members']) ? array_map(fn($item) => User::fromArray($item), $data['new_chat_members']) : null;
$object->left_chat_member = isset($data['left_chat_member']) ? User::fromArray($data['left_chat_member']) : null;
$object->new_chat_title = $data['new_chat_title'] ?? null;
$object->new_chat_photo = isset($data['new_chat_photo']) ? array_map(fn($item) => PhotoSize::fromArray($data), $data['new_chat_photo']): null;

View file

@ -331,7 +331,8 @@
/**
* @inheritDoc
*/
public static function fromArray(?array $data): ?Update
public static function
fromArray(?array $data): ?Update
{
if($data === null)
{

View file

@ -187,15 +187,10 @@
*/
public static function fromArray(?array $data): ?User
{
if($data === null)
{
return null;
}
$object = new self();
$object->id = $data['id'] ?? null;
$object->id = $data['id'];
$object->is_bot = $data['is_bot'] ?? false;
$object->first_name = $data['first_name'] ?? null;
$object->first_name = $data['first_name'];
$object->last_name = $data['last_name'] ?? null;
$object->username = $data['username'] ?? null;
$object->language_code = $data['language_code'] ?? null;

View file

@ -0,0 +1,42 @@
<?php
namespace TgBotLib\Methods;
use PHPUnit\Framework\TestCase;
use TgBotLib\Bot;
use TgBotLib\Enums\Types\ParseMode;
use TgBotLib\Objects\LinkPreviewOptions;
use TgBotLib\Objects\Message;
use TgBotLib\Objects\MessageId;
use TgBotLib\Objects\Update;
class GetUpdatesTest 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 `sendMessage` function of the bot instance.
*
* @return void
*/
public function testGetUpdatesTest(): void
{
$result = self::$bot->getUpdates();
if(count($result) > 0)
{
$this->assertInstanceOf(Update::class, $result[0]);
}
$this->assertIsArray($result);
}
}