diff --git a/docs/docs/UPDATES.md b/docs/docs/UPDATES.md index ddf00e5e..f7abe391 100644 --- a/docs/docs/UPDATES.md +++ b/docs/docs/UPDATES.md @@ -77,7 +77,7 @@ $MadelineProto->loop(); This will create an event handler class `EventHandler`, create a MadelineProto session, and set the event handler class to our newly created event handler. -When an [Update](https://docs.madelineproto.xyz/types/Update.html) is received, the corresponding `onUpdateType` event handler method is called. To get a list of all possible update types, [click here](https://docs.madelineproto.xyz/types/Update.html). +When an [Update](https://docs.madelineproto.xyz/API_docs/types/Update.html) is received, the corresponding `onUpdateType` event handler method is called. To get a list of all possible update types, [click here](https://docs.madelineproto.xyz/API_docs/types/Update.html). If such a method does not exist, the `onAny` event handler method is called. If the `onAny` event handler method does not exist, the update is ignored. @@ -90,6 +90,9 @@ If you intend to use your own constructor in the event handler, make sure to cal The update handling loop is started by the `$MadelineProto->loop()` method, and it will automatically restart the script if execution time runs out. +To break out of the loop just call `die();` + + ## Event driven multithreaded To enable multithreaded update handling, pass `-1` to the `$MadelineProto->loop` method: @@ -117,7 +120,7 @@ $MadelineProto->setWebhook('http://mybot.eu.org/madelinehook.php'); $MadelineProto->loop(); ``` -When an [Update](https://docs.madelineproto.xyz/types/Update.html) is received, a POST request is made to the provided webhook URL, with json-encoded payload containing the Update. To get a list of all possible update types, [click here](https://docs.madelineproto.xyz/types/Update.html). +When an [Update](https://docs.madelineproto.xyz/API_docs/types/Update.html) is received, a POST request is made to the provided webhook URL, with json-encoded payload containing the Update. To get a list of all possible update types, [click here](https://docs.madelineproto.xyz/API_docs/types/Update.html). The webhook can also respond with a JSON payload containing the name of a method to call and the arguments: ```json {"method":"messages->sendMessage", "peer":"@danogentili", "message":"hi"} @@ -183,9 +186,11 @@ $MadelineProto->start(); $MadelineProto->setCallback(function ($update) use ($MadelineProto) { \danog\MadelineProto\Logger::log("Received an update of type ".$update['_']); }); $MadelineProto->loop(); ``` -When an [Update](https://docs.madelineproto.xyz/types/Update.html) is received, the provided callback function is called. +When an [Update](https://docs.madelineproto.xyz/API_docs/types/Update.html) is received, the provided callback function is called. -The update handling loop is started by the `$MadelineProto->loop()` method, and it will automatically restart the script if execution time runs out. +The update handling loop is started by the `$MadelineProto->loop()` method, and it will automatically restart the script if execution time runs out. + +To break out of the loop just call `die();` ## Callback multithreaded diff --git a/index.php b/index.php index d560a4ca..3b2dbf4a 100644 --- a/index.php +++ b/index.php @@ -2,7 +2,7 @@ require 'vendor/autoload.php'; -$MadelineProto = new \danog\MadelineProto\API('session.madeline', ['app_info' => ['api_id' => 6, 'api_hash' => 'eb06d4abfb49dc3eeb1aeb98ae0f581e']]); +$MadelineProto = new \danog\MadelineProto\API('session.madeline');//, ['app_info' => ['api_id' => 6, 'api_hash' => 'eb06d4abfb49dc3eeb1aeb98ae0f581e']]); $me = $MadelineProto->start(); diff --git a/makephar.php b/makephar.php index 2b5dac67..3c58e07b 100755 --- a/makephar.php +++ b/makephar.php @@ -25,7 +25,7 @@ $p->addFromString('.git/refs/heads/master', $argv[3]); $p->setStub('getMessage(), "Erroneous data format for unserializing 'phpseclib\\Math\\BigInteger'") === 0) { $tounserialize = str_replace('phpseclib\\Math\\BigInteger', 'phpseclib\\Math\\BigIntegor', $unserialized); @@ -67,23 +75,29 @@ class API extends APIFactory if ($unserialized === false) { throw new Exception(\danog\MadelineProto\Lang::$current_lang['deserialization_error']); } + $this->web_api_template = $unserialized->web_api_template; + $this->my_telegram_org_wrapper = $unserialized->my_telegram_org_wrapper; + $this->getting_api_id = $unserialized->getting_api_id; + if (isset($unserialized->API)) { $this->API = $unserialized->API; $this->APIFactory(); + return; } - - return; } $params = $settings; } + if (!isset($params['app_info']['api_id']) || !$params['app_info']['api_id']) { + $app = $this->api_start(); + $params['app_info']['api_id'] = $app['api_id']; + $params['app_info']['api_hash'] = $app['api_hash']; + } $this->API = new MTProto($params); \danog\MadelineProto\Logger::log(\danog\MadelineProto\Lang::$current_lang['apifactory_start'], Logger::VERBOSE); $this->APIFactory(); \danog\MadelineProto\Logger::log('Ping...', Logger::ULTRA_VERBOSE); $pong = $this->ping(['ping_id' => 3]); \danog\MadelineProto\Logger::log('Pong: '.$pong['ping_id'], Logger::ULTRA_VERBOSE); - //\danog\MadelineProto\Logger::log('Getting future salts...', Logger::ULTRA_VERBOSE); - //$this->future_salts = $this->get_future_salts(['num' => 3]); \danog\MadelineProto\Logger::log(\danog\MadelineProto\Lang::$current_lang['madelineproto_ready'], Logger::NOTICE); } @@ -97,15 +111,13 @@ class API extends APIFactory if (\danog\MadelineProto\Logger::$has_thread && is_object(\Thread::getCurrentThread()) || Logger::is_fork()) { return; } - if (!is_null($this->session)) { - $this->serialize($this->session); - } + $this->serialize(); restore_error_handler(); } public function __sleep() { - return ['API']; + return ['API', 'web_api_template', 'getting_api_id', 'my_telegram_org_wrapper']; } public function &__get($name) @@ -149,10 +161,12 @@ class API extends APIFactory public function APIFactory() { - foreach ($this->API->get_method_namespaces() as $namespace) { - $this->{$namespace} = new APIFactory($namespace, $this->API); + if ($this->API) { + foreach ($this->API->get_method_namespaces() as $namespace) { + $this->{$namespace} = new APIFactory($namespace, $this->API); + } + $this->API->wrapper = $this; } - $this->API->wrapper = $this; } public function serialize($params = '') @@ -161,8 +175,9 @@ class API extends APIFactory $params = $this->session; } Logger::log(\danog\MadelineProto\Lang::$current_lang['serializing_madelineproto']); - $this->serialized = time(); - + if (is_null($this->session)) { + return; + } return Serialization::serialize($params, $this); } } diff --git a/src/danog/MadelineProto/MTProto.php b/src/danog/MadelineProto/MTProto.php index afd87cfb..e2ea297d 100644 --- a/src/danog/MadelineProto/MTProto.php +++ b/src/danog/MadelineProto/MTProto.php @@ -543,9 +543,6 @@ class MTProto if (isset(Lang::$lang[$settings['app_info']['lang_code']])) { Lang::$current_lang = &Lang::$lang[$settings['app_info']['lang_code']]; } - if (!isset($settings['app_info']['api_id'])) { - throw new \danog\MadelineProto\Exception(\danog\MadelineProto\Lang::$current_lang['api_not_set'], 0, null, 'MadelineProto', 1); - } /*if ($settings['app_info']['api_id'] < 20) { $settings['connection_settings']['all']['protocol'] = 'obfuscated2'; }*/ @@ -571,6 +568,9 @@ class MTProto } $this->settings = $settings; if (!$this->settings['updates']['handle_updates']) $this->updates = []; + if (!isset($settings['app_info']['api_id']) || !$settings['app_info']['api_id']) { + throw new \danog\MadelineProto\Exception(\danog\MadelineProto\Lang::$current_lang['api_not_set'], 0, null, 'MadelineProto', 1); + } // Setup logger $this->setup_logger(); } diff --git a/src/danog/MadelineProto/MTProtoTools/Files.php b/src/danog/MadelineProto/MTProtoTools/Files.php index 251b3555..b1ae43b9 100644 --- a/src/danog/MadelineProto/MTProtoTools/Files.php +++ b/src/danog/MadelineProto/MTProtoTools/Files.php @@ -88,7 +88,7 @@ trait Files return $this->upload($file, $file_name, $cb, true); } public function gen_all_file($media) { - $res = [$this->constructors->find_by_predicate($constructor['_'])['type'] => $constructor]; + $res = [$this->constructors->find_by_predicate($media['_'])['type'] => $media]; switch ($media['_']) { case 'messageMediaPhoto': if (!isset($media['photo']['access_hash'])) { diff --git a/src/danog/MadelineProto/Serialization.php b/src/danog/MadelineProto/Serialization.php index 54c207a8..5956444f 100644 --- a/src/danog/MadelineProto/Serialization.php +++ b/src/danog/MadelineProto/Serialization.php @@ -48,11 +48,15 @@ class Serialization */ public static function serialize($filename, $instance, $force = false) { + if ($filename == '') { + throw new \danog\MadelineProto\Exception('Empty filename'); + } + if (isset($instance->API->setdem) && $instance->API->setdem) { $instance->API->setdem = false; $instance->API->__construct($instance->API->settings); } - if ($instance->API === null) { + if ($instance->API === null && !$instance->getting_api_id) { return false; } $instance->serialized = time(); @@ -86,6 +90,7 @@ class Serialization * * @return API */ + /* public static function deserialize($filename, $no_updates = false) { $realpaths = self::realpaths($filename); @@ -142,5 +147,5 @@ class Serialization } return $unserialized; - } + }*/ } diff --git a/src/danog/MadelineProto/Wrappers/Start.php b/src/danog/MadelineProto/Wrappers/Start.php index 48477413..de06a9dc 100644 --- a/src/danog/MadelineProto/Wrappers/Start.php +++ b/src/danog/MadelineProto/Wrappers/Start.php @@ -88,6 +88,7 @@ trait Start } } + public function web_phone_login() { try { @@ -147,4 +148,5 @@ trait Start $this->web_echo('ERROR: '.$e->getMessage().'. Try again.'); } } + }