From 6cdf32e5e1e0c3c7208b12c63b8176a6c1d8beab Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Fri, 4 Aug 2023 20:28:27 +0200 Subject: [PATCH] Improve bot API conversion --- README.md | 4 ++-- src/Loop/Update/UpdateLoop.php | 17 ++++++++++++++++- src/MTProtoTools/PeerHandler.php | 7 ++++++- src/TL/Conversion/BotAPI.php | 12 ++++++------ 4 files changed, 30 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index f4af94dcc..bae880d4e 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,7 @@ The following open source projects were created using MadelineProto: you can dir * [`tgstories_dl_bot.php`](https://github.com/danog/MadelineProto/blob/v8/examples/tgstories_dl_bot.php) - Source code of [@tgstories_dl_bot](https://t.me/tgstories_dl_bot) - Bot to download any Telegram Story! * [`downloadRenameBot.php`](https://github.com/danog/MadelineProto/blob/v8/examples/downloadRenameBot.php) - Download files by URL and rename Telegram files using this async parallelized bot! * [`secret_bot.php`](https://github.com/danog/MadelineProto/blob/v8/examples/secret_bot.php) - Secret chat bot! -* [`pipesbot.php`](https://github.com/danog/MadelineProto/blob/v8/examples/pipesbot.php) - Creating inline bots and using other inline bots via a userbot! +* [`pipesbot.php`](https://github.com/danog/pipesbot) - Creating inline bots and using other inline bots via a userbot! * [`bot.php`](https://github.com/danog/MadelineProto/blob/v8/examples/bot.php) - Examples for sending normal messages, downloading any media! Want to add your own open-source project to this list? [Click here!](https://docs.madelineproto.xyz/FOSS.html) @@ -435,7 +435,7 @@ Want to add your own open-source project to this list? [Click here!](https://doc * Fork a new green thread and execute the passed function in the background: callFork * Forwards a list of messages to all peers (users, chats, channels) of the bot: broadcastForwardMessages * Forwards messages by their IDs: messages.forwardMessages - * Generate MTProto vector hash: genVectorHash + * Generate MTProto vector hash: genVectorHash * Generate a login token, for login via QR code. : auth.exportLoginToken * Generate an invoice deep link: payments.exportInvoice * Generates a temporary profile link for the currently logged-in user: contacts.exportContactToken diff --git a/src/Loop/Update/UpdateLoop.php b/src/Loop/Update/UpdateLoop.php index 2730705b7..4c96e133a 100644 --- a/src/Loop/Update/UpdateLoop.php +++ b/src/Loop/Update/UpdateLoop.php @@ -29,6 +29,8 @@ use danog\MadelineProto\PeerNotInDbException; use danog\MadelineProto\PTSException; use danog\MadelineProto\RPCErrorException; +use function Amp\delay; + /** * Update loop. * @@ -92,6 +94,10 @@ final class UpdateLoop extends Loop try { $difference = $this->API->methodCallAsyncRead('updates.getChannelDifference', ['channel' => $this->API->toSupergroup($this->channelId), 'filter' => ['_' => 'channelMessagesFilterEmpty'], 'pts' => $request_pts, 'limit' => $limit, 'force' => true], ['datacenter' => $this->API->datacenter->currentDatacenter, 'postpone' => $this->first, 'FloodWaitLimit' => 86400]); } catch (RPCErrorException $e) { + if ($e->rpc === '-503') { + delay(1.0); + continue; + } if (\in_array($e->rpc, ['CHANNEL_PRIVATE', 'CHAT_FORBIDDEN', 'CHANNEL_INVALID', 'USER_BANNED_IN_CHANNEL'], true)) { $this->feeder->stop(); unset($this->API->updaters[$this->channelId], $this->API->feeders[$this->channelId]); @@ -151,7 +157,16 @@ final class UpdateLoop extends Loop } } else { $this->logger->logger('Resumed and fetching normal difference...', Logger::ULTRA_VERBOSE); - $difference = $this->API->methodCallAsyncRead('updates.getDifference', ['pts' => $state->pts(), 'date' => $state->date(), 'qts' => $state->qts()], ['datacenter' => $this->API->authorized_dc]); + do { + try { + $difference = $this->API->methodCallAsyncRead('updates.getDifference', ['pts' => $state->pts(), 'date' => $state->date(), 'qts' => $state->qts()], ['datacenter' => $this->API->authorized_dc]); + break; + } catch (RPCErrorException $e) { + if ($e->rpc !== '-503') { + throw $e; + } + } + } while (true); $this->logger->logger('Got '.$difference['_'], Logger::ULTRA_VERBOSE); $timeout = self::DEFAULT_TIMEOUT; switch ($difference['_']) { diff --git a/src/MTProtoTools/PeerHandler.php b/src/MTProtoTools/PeerHandler.php index 769f38998..b9ffa1e42 100644 --- a/src/MTProtoTools/PeerHandler.php +++ b/src/MTProtoTools/PeerHandler.php @@ -36,6 +36,7 @@ use danog\MadelineProto\SecretPeerNotInDbException; use danog\MadelineProto\Settings; use danog\MadelineProto\Tools; use InvalidArgumentException; +use Throwable; use Webmozart\Assert\Assert; use const danog\Decoder\PHOTOSIZE_SOURCE_DIALOGPHOTO_BIG; @@ -983,7 +984,11 @@ trait PeerHandler */ public function getPwrChat(mixed $id, bool $fullfetch = true): array { - $full = $fullfetch && $this->getSettings()->getDb()->getEnableFullPeerDb() ? $this->getFullInfo($id) : $this->getInfo($id); + try { + $full = $fullfetch && $this->getSettings()->getDb()->getEnableFullPeerDb() ? $this->getFullInfo($id) : $this->getInfo($id); + } catch (Throwable) { + $full = $this->getInfo($id); + } $res = ['id' => $full['bot_api_id'], 'type' => $full['type']]; switch ($full['type']) { case 'user': diff --git a/src/TL/Conversion/BotAPI.php b/src/TL/Conversion/BotAPI.php index 6b8aa0301..6e16fdc80 100644 --- a/src/TL/Conversion/BotAPI.php +++ b/src/TL/Conversion/BotAPI.php @@ -187,9 +187,9 @@ trait BotAPI $newd['post'] = $data['post']; $newd['silent'] = $data['silent']; if (isset($data['from_id'])) { - $newd['from'] = ($this->getPwrChat($data['from_id'])); + $newd['from'] = ($this->getPwrChat($data['from_id'], false)); } - $newd['chat'] = ($this->getPwrChat($data['peer_id'])); + $newd['chat'] = ($this->getPwrChat($data['peer_id'], false)); if (isset($data['entities'])) { $newd['entities'] = ($this->MTProtoToBotAPI($data['entities'])); } @@ -200,14 +200,14 @@ trait BotAPI $newd['edit_date'] = $data['edit_date']; } if (isset($data['via_bot_id'])) { - $newd['via_bot'] = ($this->getPwrChat($data['via_bot_id'])); + $newd['via_bot'] = ($this->getPwrChat($data['via_bot_id'], false)); } if (isset($data['fwd_from']['from_id'])) { - $newd['forward_from'] = ($this->getPwrChat($data['fwd_from']['from_id'])); + $newd['forward_from'] = ($this->getPwrChat($data['fwd_from']['from_id'], false)); } if (isset($data['fwd_from']['channel_id'])) { try { - $newd['forward_from_chat'] = $this->getPwrChat(MTProto::toSupergroup($data['fwd_from']['channel_id'])); + $newd['forward_from_chat'] = $this->getPwrChat(MTProto::toSupergroup($data['fwd_from']['channel_id']), false); } catch (Throwable $e) { } } @@ -285,7 +285,7 @@ trait BotAPI case 'messageEntityMentionName': unset($data['_']); $data['type'] = 'text_mention'; - $data['user'] = ($this->getPwrChat($data['user_id'])); + $data['user'] = ($this->getPwrChat($data['user_id'], false)); unset($data['user_id']); return $data; case 'messageMediaPhoto':