From 71f392e2b954395a52b0615b50eb079f00cf048d Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Tue, 26 Sep 2017 15:08:25 +0200 Subject: [PATCH] Added userbot examples, improved performance of gzip compression --- README.md | 10 +++++----- src/danog/MadelineProto/MTProtoTools/CallHandler.php | 10 +++++----- .../MadelineProto/MTProtoTools/MessageHandler.php | 1 - {bots => userbots}/MadelineProto_bot.php | 0 {bots => userbots}/pipesbot.php | 0 {bots => userbots}/pwrtelegram_debug_bot.php | 0 6 files changed, 10 insertions(+), 11 deletions(-) rename {bots => userbots}/MadelineProto_bot.php (100%) rename {bots => userbots}/pipesbot.php (100%) rename {bots => userbots}/pwrtelegram_debug_bot.php (100%) diff --git a/README.md b/README.md index 14c39ebb..74458b9b 100644 --- a/README.md +++ b/README.md @@ -103,7 +103,7 @@ git submodule add https://github.com/danog/MadelineProto cd MadelineProto composer update cp .env.example .env -cp -a *php tests bots .env* .. +cp -a *php tests userbots .env* .. ``` Now open `.env` and edit its values as needed. @@ -115,9 +115,9 @@ You can find examples for nearly every MadelineProto function in * [`tests/testing.php`](https://github.com/danog/MadelineProto/blob/master/tests/testing.php) - examples for making/receiving calls, making secret chats, sending secret chat messages, videos, audios, voice recordings, gifs, stickers, photos, sending normal messages, videos, audios, voice recordings, gifs, stickers, photos. * [`bot.php`](https://github.com/danog/MadelineProto/blob/master/bot.php) - examples for sending normal messages, downloading any media * [`magna.php`](https://github.com/danog/MadelineProto/blob/master/magna.php) - examples for receiving calls -* [`bots/pipesbot.php`](https://github.com/danog/MadelineProto/blob/master/bots/pipesbot.php) - examples for creating inline bots and using other inline bots via a userbot -* [`bots/MadelineProto_bot.php`](https://github.com/danog/MadelineProto/blob/master/bots/MadelineProto_bot.php) - More fun shiz -* [`bots/pwrtelegram_debug_bot`](https://github.com/danog/MadelineProto/blob/master/bots/pwrtelegram_debug_bot.php) - More fun shiz +* [`userbots/pipesbot.php`](https://github.com/danog/MadelineProto/blob/master/userbots/pipesbot.php) - examples for creating inline bots and using other inline bots via a userbot +* [`userbots/MadelineProto_bot.php`](https://github.com/danog/MadelineProto/blob/master/userbots/MadelineProto_bot.php) - More fun shiz +* [`userbots/pwrtelegram_debug_bot`](https://github.com/danog/MadelineProto/blob/master/userbots/pwrtelegram_debug_bot.php) - More fun shiz ## Methods @@ -217,7 +217,7 @@ $MadelineProto = new \danog\MadelineProto\API(); If you have some questions about the usage of the methods of this library, you can join the [support group](https://telegram.me/pwrtelegramgroup) or contact [@danogentili](https://telegram.me/danogentili). -But first, please read this WHOLE page very carefully, follow all links to external documentation, and read all examples in the repo (bot.php, bots/, tests/testing.php). +But first, please read this WHOLE page very carefully, follow all links to external documentation, and read all examples in the repo (bot.php, userbots/, tests/testing.php). If you don't understand something, read everything again. diff --git a/src/danog/MadelineProto/MTProtoTools/CallHandler.php b/src/danog/MadelineProto/MTProtoTools/CallHandler.php index fe12a58b..e50f5b8b 100644 --- a/src/danog/MadelineProto/MTProtoTools/CallHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/CallHandler.php @@ -74,11 +74,9 @@ trait CallHandler if (isset($queue)) { $serialized = $this->serialize_method('invokeAfterMsgs', ['msg_ids' => $this->datacenter->sockets[$aargs['datacenter']]->call_queue[$queue], 'query' => $serialized]); } - $l = strlen($serialized); - $g = strlen($gzipped = gzencode($serialized)); - if ($l > 500 && $g < $l) { - \danog\MadelineProto\Logger::log(['Using GZIP compression for '.$method.', saved '.($l - $g).' bytes of data, reduced call size by '.($g * 100 / $l).'%'], \danog\MadelineProto\Logger::VERBOSE); + if (($l = strlen($serialized)) > 500 && ($g = strlen($gzipped = gzencode($serialized))) < $l) { $serialized = $this->serialize_object(['type' => 'gzip_packed'], ['packed_data' => $gzipped], 'gzipped data'); + \danog\MadelineProto\Logger::log(['Using GZIP compression for '.$method.', saved '.($l - $g).' bytes of data, reduced call size by '.($g * 100 / $l).'%'], \danog\MadelineProto\Logger::VERBOSE); } $last_recv = $this->last_recv; if ($canunset = !$this->updates_state['sync_loading'] && !$this->threads && !$this->run_workers) { @@ -282,12 +280,14 @@ trait CallHandler if (!isset($aargs['datacenter'])) { throw new \danog\MadelineProto\Exception('No datacenter provided'); } + $serialized = $this->serialize_object(['type' => $object], $args, $object); + for ($count = 1; $count <= $this->settings['max_tries']['query']; $count++) { try { if ($object !== 'msgs_ack') { \danog\MadelineProto\Logger::log(['Sending object (try number '.$count.' for '.$object.')...'], \danog\MadelineProto\Logger::ULTRA_VERBOSE); } - $message_id = $this->send_message($this->serialize_object(['type' => $object], $args, $object), $this->content_related($object), $aargs); + $message_id = $this->send_message($serialized, $this->content_related($object), $aargs); if ($object !== 'msgs_ack') { $this->datacenter->sockets[$aargs['datacenter']]->outgoing_messages[$message_id]['content'] = ['method' => $object, 'args' => $args]; } diff --git a/src/danog/MadelineProto/MTProtoTools/MessageHandler.php b/src/danog/MadelineProto/MTProtoTools/MessageHandler.php index da983e5d..4a4fe264 100644 --- a/src/danog/MadelineProto/MTProtoTools/MessageHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/MessageHandler.php @@ -119,7 +119,6 @@ trait MessageHandler $this->datacenter->sockets[$datacenter]->incoming_messages[$message_id]['response'] = -1; $this->datacenter->sockets[$datacenter]->new_incoming[$message_id] = $message_id; $this->last_recv = time(); - return true; } } diff --git a/bots/MadelineProto_bot.php b/userbots/MadelineProto_bot.php similarity index 100% rename from bots/MadelineProto_bot.php rename to userbots/MadelineProto_bot.php diff --git a/bots/pipesbot.php b/userbots/pipesbot.php similarity index 100% rename from bots/pipesbot.php rename to userbots/pipesbot.php diff --git a/bots/pwrtelegram_debug_bot.php b/userbots/pwrtelegram_debug_bot.php similarity index 100% rename from bots/pwrtelegram_debug_bot.php rename to userbots/pwrtelegram_debug_bot.php