From 3a4325f691fbc4e84699c58b29ce5df2c22bd118 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Wed, 5 Jun 2019 16:45:33 +0200 Subject: [PATCH] Add async echo --- CHANGELOG.md | 42 ++++++++++++++--------------- README.md | 32 ++++++++++++---------- docs | 2 +- src/danog/MadelineProto/MTProto.php | 2 +- src/danog/MadelineProto/Tools.php | 6 +++++ 5 files changed, 46 insertions(+), 38 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7d1c6438..c9de10dc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,15 +25,15 @@ Just `yield $MadelineProto->messages->sendMessage` instead of `$MadelineProto->m And now, on to the **API changes**: * First of all, we've got several bucketloads of telegram API changes, that can be viewed in the first posts. -* Dropped support for PHP 5 and PHP 7.0: these versions of PHP have [officially reached their EOL](http://php.net/eol.php), so MadelineProto will not support them anymore. -You can use MadelineProto with PHP 7.3 (or PHP 7.2, PHP 7.1 is supported but not recommended). +* **Very important**, I wrote [documentation](https://docs.madelineproto.xyz/docs/LOGIN.html#getting-permission-to-use-the-telegram-api) on what to do if your account gets banned. * Dropped support for PHP 5 and PHP 7.0: these versions of PHP have [officially reached their EOL](http://php.net/eol.php), so MadelineProto will not support them anymore. +You should use MadelineProto with PHP 7.3 (or PHP 7.2; PHP 7.1 is supported but not recommended). * **Dropped support for get_updates**: it won't work properly on async, and I really recommend you stop using it * You can now use the `@support` username in sendMessage and other methods to send messages to the support user! * Now MadelineProto will automatically try to get the access hash of users not present in the internal peer database (this should reduce errors)! * If any file cannot be downloaded to due issues with the tg media server that is hosting it, it will be automatically sent to the `@support` user ([settings](https://docs.madelineproto.xyz/docs/SETTINGS.html#settingsdownloadreport_broken_media)). * Documented the [MyTelegramOrgWrapper](https://docs.madelineproto.xyz/docs/LOGIN.html#api-id) API, that can be used to login programmaticaly to the [my.telegram.org](https://my.telegram.org management page). * Added an [update_2fa](https://docs.madelineproto.xyz/update_2fa.html) method to update the login password -* Added a [get_full_dialogs](https://docs.madelineproto.xyz/docs/DIALOGS.html#get_full_dialogs-now-fully-async) method to get a full list of all chats you’re member of, including dialog info (such as the pinned/last message ID, unread count, tag count, notification settings and message drafts). +* Added a [get_full_dialogs](https://docs.madelineproto.xyz/docs/DIALOGS.html#get_full_dialogs-now-fully-async) method to get a full list of all chats you’re member of, including **dialog info** (such as the pinned/last message ID, unread count, tag count, notification settings and message drafts). * [Added support for automatic file uploads by name in secret chats (as with normal chats); you can also now send secret chat messages using the sendMessage method as if it were a normal chat](https://github.com/danog/MadelineProto/blob/master/secret_bot.php) * Added a [resetUpdateState](https://docs.madelineproto.xyz/docs/UPDATES.html#fetch-all-updates-from-the-beginning) method to reset the update state and fetch ALL updates from the beginning * Improved chat message splitting algorithm (if the message you're trying to send is too long): performance improvements, and it will now notify you via the logs if there are too many entities in the logs, or if the entities are too long. @@ -96,7 +96,7 @@ The update state is now stored using a custom `UpdatesState` API, that will simp Possibly the most __exciting__ thing to work on in this version of MadelineProto was the new **update management system**: I whipped it up in merely two days a few weeks ago, and it has **absolutely improved** the overall reliability of MadelineProto. Huge thanks to Aliaksei Levin, the developer of tdlib, for explaining to me how exactly does the MTProto update API work: he saved me a lot of time, and was really nice <3<3<3. -While thinking of an easy way I could implement the new update system, I had an inspiration: +While thinking of an easy way I could implement the new update system, **I had an inspiration**: I created a **unique**, **reliable**, **fast** and **extremely simple** update handling system based on [MadelineProto's loop API](https://docs.madelineproto.xyz/docs/ASYNC.html#async-loop-apis), **not present in any** other MTProto client, **not even tdlib**. @@ -127,7 +127,7 @@ The same cached method mapping system is also used for the **event handler**, wh *** And now, let's elaborate on async: -With **MadelineProto 4.0**, each update is handled in **parallel** using a separate **thread**, and everything is done in **parallel** (even on restricted webhosts!). +With **MadelineProto 4.0**, each update is handled in **parallel** using a separate **thread**, and everything is done in **parallel** (even on restricted webhosts, perfect for creating **file downloader bots**!). When I say **thread**, I actually mean **green thread** ([wikipedia](https://en.wikipedia.org/wiki/Green_threads)), often called **strand**. **Strands** are behave exactly like normal **threads**, except that they're implemented in user-space, and they're much **faster**, **more reliable**, and **do not suffer** from synchronization issues present with normal threads. @@ -144,34 +144,32 @@ In * async construct * async readline * async filegetco +* async echo Things to expect in the next releases: -docs for get mime funcs -docs for HTML parser (div to avoid escaping) -docs for update_2fa +ton +video calls +group calls +native calls +DNS over HTTPS optional max_id and min_id async iterators +#phase1 + +telegram passport +get sponsor of proxies +docs for get mime funcs +docs for HTML parser Method name changes #MadelineProtoForNode async lua async improved get_pwr_chat gzip no defer logs -recover@tg docs startedLoop docs - +arrayaccess on promises +web files no error setting, madelineproto does that for you - -do not use manual +tell about restart tell about madeline.php loading in the same dire -arrayaccess on promises -get sponsor of -ton -video calls -group calls -native calls -dnssec -mytelegramorg docs -web files -telegram passport \ No newline at end of file diff --git a/README.md b/README.md index 2cfade65..86c80b51 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ It can login with a phone number (MTProto API), or with a bot token (MTProto API [It is now fully async](https://docs.madelineproto.xyz/docs/ASYNC.html)! -## Getting started +## Getting started ([now fully async!](https://docs.madelineproto.xyz/docs/ASYNC.html)) ```php start(); +$MadelineProto->async(true); +$MadelineProto->loop(function () use ($MadelineProto) { + yield $MadelineProto->start(); -$me = $MadelineProto->get_self(); + $me = yield $MadelineProto->get_self(); -\danog\MadelineProto\Logger::log($me); + $MadelineProto->logger($me); -if (!$me['bot']) { - $MadelineProto->messages->sendMessage(['peer' => '@danogentili', 'message' => "Hi!\nThanks for creating MadelineProto! <3"]); - $MadelineProto->channels->joinChannel(['channel' => '@MadelineProto']); + if (!$me['bot']) { + yield $MadelineProto->messages->sendMessage(['peer' => '@danogentili', 'message' => "Hi!\nThanks for creating MadelineProto! <3"]); + yield $MadelineProto->channels->joinChannel(['channel' => '@MadelineProto']); - try { - $MadelineProto->messages->importChatInvite(['hash' => 'https://t.me/joinchat/Bgrajz6K-aJKu0IpGsLpBg']); - } catch (\danog\MadelineProto\RPCErrorException $e) { + try { + yield $MadelineProto->messages->importChatInvite(['hash' => 'https://t.me/joinchat/Bgrajz6K-aJKu0IpGsLpBg']); + } catch (\danog\MadelineProto\RPCErrorException $e) { + $MadelineProto->logger($e); + } + + yield $MadelineProto->messages->sendMessage(['peer' => 'https://t.me/joinchat/Bgrajz6K-aJKu0IpGsLpBg', 'message' => 'Testing MadelineProto!']); } - - $MadelineProto->messages->sendMessage(['peer' => 'https://t.me/joinchat/Bgrajz6K-aJKu0IpGsLpBg', 'message' => 'Testing MadelineProto!']); -} -echo 'OK, done!'.PHP_EOL; + yield $MadelineProto->echo('OK, done!'); +}); ``` [Try this code now!](https://try.madelineproto.xyz) or run this code in a browser or in a console. diff --git a/docs b/docs index 67df53f4..4aa913bc 160000 --- a/docs +++ b/docs @@ -1 +1 @@ -Subproject commit 67df53f4660009faa7e01fb5cfce2a9751c8ac72 +Subproject commit 4aa913bc973371c46295b2dd6cb87ab07f8bdf1a diff --git a/src/danog/MadelineProto/MTProto.php b/src/danog/MadelineProto/MTProto.php index 2c8d3d21..78611c86 100644 --- a/src/danog/MadelineProto/MTProto.php +++ b/src/danog/MadelineProto/MTProto.php @@ -30,6 +30,7 @@ use danog\MadelineProto\MTProtoTools\UpdatesState; use danog\MadelineProto\Stream\MTProtoTransport\HttpsStream; use danog\MadelineProto\Stream\MTProtoTransport\HttpStream; use danog\MadelineProto\TL\TLCallback; +use function Amp\ByteStream\getOutputBufferStream; /** * Manages all of the mtproto stuff. @@ -233,7 +234,6 @@ class MTProto extends AsyncConstruct implements TLCallback { return $this->logger->logger(...$params); } - public function isAltervista() { return Magic::$altervista; diff --git a/src/danog/MadelineProto/Tools.php b/src/danog/MadelineProto/Tools.php index f7420857..289706a1 100644 --- a/src/danog/MadelineProto/Tools.php +++ b/src/danog/MadelineProto/Tools.php @@ -31,6 +31,7 @@ use function Amp\Promise\timeout; use function Amp\Promise\wait; use function Amp\ByteStream\getStdin; use function Amp\ByteStream\getStdout; +use function Amp\ByteStream\getOutputBufferStream; /** * Some tools. @@ -353,6 +354,11 @@ trait Tools return array_shift($lines); } + + public function echo($string) + { + return getOutputBufferStream()->write($string); + } public function is_array_or_alike($var) { return is_array($var) ||