From f2f26998adb55974c1be2d029dcfe870c730588e Mon Sep 17 00:00:00 2001 From: netkas Date: Fri, 1 Nov 2024 13:24:11 -0400 Subject: [PATCH] Fix object instantiation and add getUpdate functionality --- src/TgBotLib/Bot.php | 2 ++ src/TgBotLib/Methods/GetUpdates.php | 11 ++++-- src/TgBotLib/Objects/Message.php | 2 +- src/TgBotLib/Objects/Update.php | 3 +- src/TgBotLib/Objects/User.php | 9 ++--- tests/TgBotLib/Methods/GetUpdatesTest.php | 42 +++++++++++++++++++++++ 6 files changed, 57 insertions(+), 12 deletions(-) create mode 100644 tests/TgBotLib/Methods/GetUpdatesTest.php diff --git a/src/TgBotLib/Bot.php b/src/TgBotLib/Bot.php index 55b402c..f4ecb15 100644 --- a/src/TgBotLib/Bot.php +++ b/src/TgBotLib/Bot.php @@ -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. diff --git a/src/TgBotLib/Methods/GetUpdates.php b/src/TgBotLib/Methods/GetUpdates.php index bba4ec2..20ce41a 100644 --- a/src/TgBotLib/Methods/GetUpdates.php +++ b/src/TgBotLib/Methods/GetUpdates.php @@ -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', + ]; } } \ No newline at end of file diff --git a/src/TgBotLib/Objects/Message.php b/src/TgBotLib/Objects/Message.php index fb46c30..fb10119 100644 --- a/src/TgBotLib/Objects/Message.php +++ b/src/TgBotLib/Objects/Message.php @@ -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; diff --git a/src/TgBotLib/Objects/Update.php b/src/TgBotLib/Objects/Update.php index c0ff71f..8f89a41 100644 --- a/src/TgBotLib/Objects/Update.php +++ b/src/TgBotLib/Objects/Update.php @@ -331,7 +331,8 @@ /** * @inheritDoc */ - public static function fromArray(?array $data): ?Update + public static function + fromArray(?array $data): ?Update { if($data === null) { diff --git a/src/TgBotLib/Objects/User.php b/src/TgBotLib/Objects/User.php index 24b804b..4efb3ab 100644 --- a/src/TgBotLib/Objects/User.php +++ b/src/TgBotLib/Objects/User.php @@ -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; diff --git a/tests/TgBotLib/Methods/GetUpdatesTest.php b/tests/TgBotLib/Methods/GetUpdatesTest.php new file mode 100644 index 0000000..4a58b8a --- /dev/null +++ b/tests/TgBotLib/Methods/GetUpdatesTest.php @@ -0,0 +1,42 @@ +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); + } +}