diff --git a/bot.php b/bot.php index 7722ec80..10b41187 100755 --- a/bot.php +++ b/bot.php @@ -25,11 +25,11 @@ try { } //var_dump($MadelineProto->API->get_config([], ['datacenter' => $MadelineProto->API->datacenter->curdc])); //var_dump($MadelineProto->API->settings['connection']); -$MadelineProto->serialize = 'bot.madeline'; +$MadelineProto->session = 'bot.madeline'; echo 'Wrote '.\danog\MadelineProto\Serialization::serialize('bot.madeline', $MadelineProto).' bytes'.PHP_EOL; $offset = 0; while (true) { - $updates = $MadelineProto->API->get_updates(['offset' => $offset, 'limit' => 50, 'timeout' => 0]); // Just like in the bot API, you can specify an offset, a limit and a timeout + $updates = $MadelineProto->get_updates(['offset' => $offset, 'limit' => 50, 'timeout' => 0]); // Just like in the bot API, you can specify an offset, a limit and a timeout \danog\MadelineProto\Logger::log([$updates]); foreach ($updates as $update) { $offset = $update['update_id'] + 1; // Just like in the bot API, the offset must be set to the last update_id @@ -54,7 +54,7 @@ while (true) { if (isset($update['update']['message']['media']) && ($update['update']['message']['media']['_'] == 'messageMediaPhoto' || $update['update']['message']['media']['_'] == 'messageMediaDocument')) { $time = time(); $file = $MadelineProto->download_to_dir($update['update']['message']['media'], '/tmp'); - $MadelineProto->messages->sendMessage(['peer' => $update['update']['message']['from_id'], 'message' => 'Downloaded to '.$file.' in '.(time() - $time).' seconds', 'reply_to_msg_id' => $update['update']['message']['id'], 'entities' => [['_' => 'messageEntityPre', 'offset' => 0, 'length' => strlen($res), 'language' => 'json']]]); + $MadelineProto->messages->sendMessage(['peer' => isset($update['update']['message']['from_id']) ? $update['update']['message']['from_id'] : $update['update']['message']['to_id'], 'message' => 'Downloaded to '.$file.' in '.(time() - $time).' seconds', 'reply_to_msg_id' => $update['update']['message']['id'], 'entities' => [['_' => 'messageEntityPre', 'offset' => 0, 'length' => strlen($res), 'language' => 'json']]]); } } catch (\danog\MadelineProto\RPCErrorException $e) { $MadelineProto->messages->sendMessage(['peer' => '@danogentili', 'message' => $e->getCode().': '.$e->getMessage().PHP_EOL.$e->getTraceAsString()]); diff --git a/src/danog/MadelineProto/API.php b/src/danog/MadelineProto/API.php index 583f4966..6006881f 100644 --- a/src/danog/MadelineProto/API.php +++ b/src/danog/MadelineProto/API.php @@ -16,6 +16,7 @@ class API extends APIFactory { use \danog\Serializable; public $session; + public $serialized = 0; public function ___construct($params = []) { @@ -96,7 +97,6 @@ class API extends APIFactory public function serialize($filename) { Logger::log([\danog\MadelineProto\Lang::$current_lang['serializing_madelineproto']]); - return Serialization::serialize($filename, $this); } } diff --git a/src/danog/MadelineProto/APIFactory.php b/src/danog/MadelineProto/APIFactory.php index b2d6dd89..394aa2b7 100644 --- a/src/danog/MadelineProto/APIFactory.php +++ b/src/danog/MadelineProto/APIFactory.php @@ -131,10 +131,13 @@ class APIFactory $this->API->get_config([], ['datacenter' => $this->API->datacenter->curdc]); $aargs = isset($arguments[1]) && is_array($arguments[1]) ? $arguments[1] : []; $aargs['datacenter'] = $this->API->datacenter->curdc; + if ($name === 'get_updates' && isset($this->session) && !is_null($this->session) && time() - $this->serialized > 10) { + $this->serialize($this->session); + Logger::log(["Didn't serialize in a while, doing that now..."]); + } if ($this->lua === false) { return method_exists($this->API, $this->namespace.$name) ? $this->API->{$this->namespace.$name}(...$arguments) : $this->API->method_call($this->namespace.$name, (isset($arguments[0]) && is_array($arguments[0])) ? $arguments[0] : [], $aargs); } - try { $deserialized = method_exists($this->API, $this->namespace.$name) ? $this->API->{$this->namespace.$name}(...$arguments) : $this->API->method_call($this->namespace.$name, (isset($arguments[0]) && is_array($arguments[0])) ? $arguments[0] : [], $aargs); Lua::convert_objects($deserialized); diff --git a/src/danog/MadelineProto/MTProtoTools/PeerHandler.php b/src/danog/MadelineProto/MTProtoTools/PeerHandler.php index cbf8a4ee..8e3d6918 100644 --- a/src/danog/MadelineProto/MTProtoTools/PeerHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/PeerHandler.php @@ -535,7 +535,7 @@ trait PeerHandler public function store_db($res, $force = false) { - if (!isset($this->settings['pwr']) || $this->settings['pwr']['pwr'] === false) { + if (!isset($this->settings['pwr']) || $this->settings['pwr']['pwr'] === false || (isset($this->settings['connection_settings'][$this->datacenter->curdc]) ? $this->settings['connection_settings'][$this->datacenter->curdc] : $this->settings['connection_settings']['all'])['test_mode']) { /* try { if (isset($res['username'])) { diff --git a/src/danog/MadelineProto/Serialization.php b/src/danog/MadelineProto/Serialization.php index 51c13652..28a367b9 100644 --- a/src/danog/MadelineProto/Serialization.php +++ b/src/danog/MadelineProto/Serialization.php @@ -32,6 +32,7 @@ class Serialization $instance->API->setdem = false; $instance->API->__construct($instance->API->settings); } + $instance->serialized = time(); if (!file_exists($lock = $filename.'.lock')) { touch($lock); clearstatcache();