From 9267d711f3e647538a774d6754e40224c46c9c03 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Sun, 24 Sep 2017 22:26:06 +0000 Subject: [PATCH] Readable errors --- .env.example | 1 - magna.php | 52 +++++++++---------- secret_bot.php | 9 +--- src/danog/MadelineProto/Lua.php | 4 +- src/danog/MadelineProto/MTProto.php | 1 + .../MTProtoTools/PeerHandler.php | 30 +++++------ .../MTProtoTools/UpdateHandler.php | 10 +++- 7 files changed, 52 insertions(+), 55 deletions(-) diff --git a/.env.example b/.env.example index 2af7d453..43453030 100644 --- a/.env.example +++ b/.env.example @@ -1,4 +1,3 @@ -MTPROTO_NUMBER=+39394838932 MTPROTO_SETTINGS={"app_info":{"api_id":6,"api_hash":"eb06d4abfb49dc3eeb1aeb98ae0f581e"}} TEST_USERNAME=@danogentili TEST_DESTINATION_GROUPS=["@pwrtelegramgroup","@pwrtelegramgroupita"] diff --git a/magna.php b/magna.php index 6a71536d..eeba7f89 100755 --- a/magna.php +++ b/magna.php @@ -41,14 +41,7 @@ if ($MadelineProto === false) { echo 'Loading MadelineProto...'.PHP_EOL; $MadelineProto = new \danog\MadelineProto\API($settings); if (getenv('TRAVIS_COMMIT') == '') { - $checkedPhone = $MadelineProto->auth->checkPhone(// auth.checkPhone becomes auth->checkPhone - [ - 'phone_number' => getenv('MTPROTO_NUMBER'), - ] - ); - - \danog\MadelineProto\Logger::log([$checkedPhone], \danog\MadelineProto\Logger::NOTICE); - $sentCode = $MadelineProto->phone_login(getenv('MTPROTO_NUMBER')); + $sentCode = $MadelineProto->phone_login(readline('Enter your phone number: ')); \danog\MadelineProto\Logger::log([$sentCode], \danog\MadelineProto\Logger::NOTICE); echo 'Enter the code you received: '; $code = fgets(STDIN, (isset($sentCode['type']['length']) ? $sentCode['type']['length'] : 5) + 1); @@ -80,7 +73,9 @@ if ($MadelineProto === false) { \danog\MadelineProto\Logger::log(['hey'], \danog\MadelineProto\Logger::FATAL_ERROR); $message = (getenv('TRAVIS_COMMIT') == '') ? 'I iz works always (io laborare sembre) (yo lavorar siempre) (mi labori ĉiam) (я всегда работать) (Ik werkuh altijd) (Ngimbonga ngaso sonke isikhathi ukusebenza)' : ('Travis ci tests in progress: commit '.getenv('TRAVIS_COMMIT').', job '.getenv('TRAVIS_JOB_NUMBER').', PHP version: '.getenv('TRAVIS_PHP_VERSION')); -if (!isset($MadelineProto->programmed_call)) $MadelineProto->programmed_call = []; +if (!isset($MadelineProto->programmed_call)) { + $MadelineProto->programmed_call = []; +} echo 'Serializing MadelineProto to session.madeline...'.PHP_EOL; echo 'Wrote '.\danog\MadelineProto\Serialization::serialize('session.madeline', $MadelineProto).' bytes'.PHP_EOL; /* @@ -98,25 +93,24 @@ $users = []; if ($time < time()) { if (!isset($calls[$user])) { try { - include 'songs.php'; - $call = $MadelineProto->request_call($user); - $call->configuration['enable_NS'] = false; - $call->configuration['enable_AGC'] = false; - $call->configuration['enable_AEC'] = false; - $call->configuration['shared_config'] = [ + include 'songs.php'; + $call = $MadelineProto->request_call($user); + $call->configuration['enable_NS'] = false; + $call->configuration['enable_AGC'] = false; + $call->configuration['enable_AEC'] = false; + $call->configuration['shared_config'] = [ 'audio_init_bitrate' => 70 * 1000, 'audio_max_bitrate' => 100 * 1000, 'audio_min_bitrate' => 15 * 1000, //'audio_bitrate_step_decr' => 0, //'audio_bitrate_step_incr' => 2000, ]; - $call->parseConfig(); - $call->playOnHold($songs); - $calls[$call->getOtherID()] = $call; - $times[$call->getOtherID()] = [time(), $MadelineProto->messages->sendMessage(['peer' => $call->getOtherID(), 'message' => 'Total running calls: '.count($calls).PHP_EOL.PHP_EOL.$call->getDebugString()])['id']]; - + $call->parseConfig(); + $calls[$call->getOtherID()] = $call; + $times[$call->getOtherID()] = [time(), $MadelineProto->messages->sendMessage(['peer' => $call->getOtherID(), 'message' => 'Total running calls: '.count($calls).PHP_EOL.PHP_EOL.$call->getDebugString()])['id']]; + $call->playOnHold($songs); } catch (\danog\MadelineProto\RPCErrorException $e) { -echo $e; + echo $e; } } unset($MadelineProto->programmed_call[$key]); @@ -127,10 +121,11 @@ echo $e; unset($calls[$key]); } else if (isset($times[$call->getOtherID()])&&$times[$call->getOtherID()][0] < time()) { $times[$call->getOtherID()][0] += 10; + try { $MadelineProto->messages->editMessage(['id' => $times[$call->getOtherID()][1], 'peer' => $call->getOtherID(), 'message' => 'Total running calls: '.count($calls).PHP_EOL.PHP_EOL.$call->getDebugString()]); } catch (\danog\MadelineProto\RPCErrorException $e) { -echo $e; + echo $e; } } } @@ -145,7 +140,7 @@ echo $e; } try { - if (!isset($users[$update['update']['message']['from_id']]) ||isset($update['update']['message']['message']) && $update['update']['message']['message'] === '/start') { + if (!isset($users[$update['update']['message']['from_id']]) || isset($update['update']['message']['message']) && $update['update']['message']['message'] === '/start') { $users[$update['update']['message']['from_id']] = true; $update['update']['message']['message'] = '/call'; $MadelineProto->messages->sendMessage(['peer' => $update['update']['message']['from_id'], 'message' => "Hi, I'm @magnaluna the webradio. @@ -183,15 +178,14 @@ Propic art by @magnaluna on deviantart.", 'parse_mode' => 'Markdown']); $calls[$call->getOtherID()] = $call; $times[$call->getOtherID()] = [time(), $MadelineProto->messages->sendMessage(['peer' => $call->getOtherID(), 'message' => 'Total running calls: '.count($calls).PHP_EOL.PHP_EOL.$call->getDebugString()])['id']]; - } if (isset($update['update']['message']['message']) && strpos($update['update']['message']['message'], '/program') === 0) { $time = strtotime(str_replace('/program ', '', $update['update']['message']['message'])); if ($time === false) { - $MadelineProto->messages->sendMessage(['peer' => $update['update']['message']['from_id'], 'message' => "Invalid time provided"]); + $MadelineProto->messages->sendMessage(['peer' => $update['update']['message']['from_id'], 'message' => 'Invalid time provided']); } else { - $MadelineProto->programmed_call[]= [$update['update']['message']['from_id'], $time]; - $MadelineProto->messages->sendMessage(['peer' => $update['update']['message']['from_id'], 'message' => "OK"]); + $MadelineProto->programmed_call[] = [$update['update']['message']['from_id'], $time]; + $MadelineProto->messages->sendMessage(['peer' => $update['update']['message']['from_id'], 'message' => 'OK']); } } } catch (\danog\MadelineProto\RPCErrorException $e) { @@ -228,9 +222,11 @@ Propic art by @magnaluna on deviantart.", 'parse_mode' => 'Markdown']); echo 'DID NOT ACCEPT A CALL'; } $calls[$update['update']['phone_call']->getOtherID()] = $update['update']['phone_call']; + try { $times[$update['update']['phone_call']->getOtherID()] = [time(), $MadelineProto->messages->sendMessage(['peer' => $update['update']['phone_call']->getOtherID(), 'message' => 'Total running calls: '.count($calls).PHP_EOL.PHP_EOL.$update['update']['phone_call']->getDebugString()])['id']]; - } catch (\danog\MadelineProto\RPCErrorException $e) { ; } + } catch (\danog\MadelineProto\RPCErrorException $e) { + } $update['update']['phone_call']->playOnHold($songs); } diff --git a/secret_bot.php b/secret_bot.php index 8c86f865..038e4aab 100755 --- a/secret_bot.php +++ b/secret_bot.php @@ -37,14 +37,7 @@ if ($MadelineProto === false) { echo 'Loading MadelineProto...'.PHP_EOL; $MadelineProto = new \danog\MadelineProto\API($settings); if (getenv('TRAVIS_COMMIT') == '') { - $checkedPhone = $MadelineProto->auth->checkPhone(// auth.checkPhone becomes auth->checkPhone - [ - 'phone_number' => getenv('MTPROTO_NUMBER'), - ] - ); - - \danog\MadelineProto\Logger::log([$checkedPhone], \danog\MadelineProto\Logger::NOTICE); - $sentCode = $MadelineProto->phone_login(getenv('MTPROTO_NUMBER')); + $sentCode = $MadelineProto->phone_login(readline('Enter your phone number: ')); \danog\MadelineProto\Logger::log([$sentCode], \danog\MadelineProto\Logger::NOTICE); echo 'Enter the code you received: '; $code = fgets(STDIN, (isset($sentCode['type']['length']) ? $sentCode['type']['length'] : 5) + 1); diff --git a/src/danog/MadelineProto/Lua.php b/src/danog/MadelineProto/Lua.php index 5c95da05..f1d43afb 100644 --- a/src/danog/MadelineProto/Lua.php +++ b/src/danog/MadelineProto/Lua.php @@ -42,7 +42,7 @@ class Lua $this->Lua->registerCallback('tdcli_function', [$this, 'tdcli_function']); $this->Lua->registerCallback('madeline_function', [$this, 'madeline_function']); foreach (get_class_methods($this->MadelineProto->API) as $method) { - $this->Lua->registerCallback($method, [$this->MadelineProto->API, $method]); + $this->Lua->registerCallback($method, [$this->MadelineProto, $method]); } $methods = []; foreach ($this->MadelineProto->get_methods_namespaced() as $pair) { @@ -159,7 +159,7 @@ class Lua public static function convert_objects(&$data) { array_walk_recursive($data, function (&$value, $key) { - if (is_object($value)) { + if (is_object($value) && !($value instanceof \phpseclib\Math\BigInteger)) { $newval = []; foreach (get_class_methods($value) as $name) { $newval[$name] = [$value, $name]; diff --git a/src/danog/MadelineProto/MTProto.php b/src/danog/MadelineProto/MTProto.php index 5def5505..3c358da2 100644 --- a/src/danog/MadelineProto/MTProto.php +++ b/src/danog/MadelineProto/MTProto.php @@ -362,6 +362,7 @@ class MTProto if ($this->authorized === self::LOGGED_IN) { $this->get_self(); $this->get_cdn_config($this->datacenter->curdc); + $this->setup_logger(); } if ($this->authorized === self::LOGGED_IN && !$this->authorization['user']['bot']) { $this->get_dialogs($force); diff --git a/src/danog/MadelineProto/MTProtoTools/PeerHandler.php b/src/danog/MadelineProto/MTProtoTools/PeerHandler.php index b75fed0d..cbf8a4ee 100644 --- a/src/danog/MadelineProto/MTProtoTools/PeerHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/PeerHandler.php @@ -21,6 +21,9 @@ trait PeerHandler { foreach ($users as $key => $user) { if (!isset($user['access_hash'])) { + if (isset($user['username']) && !isset($this->chats[$user['id']])) { + $this->get_pwr_chat($user['username'], false, true); + } continue; } switch ($user['_']) { @@ -71,10 +74,13 @@ trait PeerHandler break; case 'channel': case 'channelForbidden': + $bot_api_id = $this->to_supergroup($chat['id']); if (!isset($chat['access_hash'])) { + if (isset($chat['username']) && !isset($this->chats[$bot_api_id])) { + $this->get_pwr_chat($chat['username'], true, true); + } continue; } - $bot_api_id = $this->to_supergroup($chat['id']); if (!isset($this->chats[$bot_api_id]) || $this->chats[$bot_api_id] !== $chat) { $this->chats[$bot_api_id] = $chat; @@ -130,7 +136,7 @@ trait PeerHandler if (isset($fwd['user_id']) && !$this->peer_isset($fwd['user_id'])) { return false; } - if (isset($fwd['channel_id']) && !$this->peer_isset('channel#'.$fwd['channel_id'])) { + if (isset($fwd['channel_id']) && !$this->peer_isset($this->to_supergroup($fwd['channel_id']))) { return false; } } catch (\danog\MadelineProto\Exception $e) { @@ -161,19 +167,16 @@ trait PeerHandler break; case 'chat': - $id = -$id['id']; - break; case 'chatFull': $id = -$id['id']; break; - case 'inputPeerChat': + + case 'inputPeerChat': case 'peerChat': $id = -$id['chat_id']; break; case 'channel': - $id = $this->to_supergroup($id['id']); - break; case 'channelFull': $id = $this->to_supergroup($id['id']); break; @@ -204,15 +207,12 @@ trait PeerHandler if (is_string($id)) { $id = \danog\MadelineProto\Logger::$bigint ? ((float) $id) : ((int) $id); } + if (!isset($this->chats[$id]) && $id < 0 && !preg_match('/^-100/', $id)) { + $this->method_call('messages.getFullChat', ['chat_id' => -$id], ['datacenter' => $this->datacenter->curdc]); + } if (isset($this->chats[$id])) { return $this->gen_all($this->chats[$id]); } - if ($id < 0 && !preg_match('/^-100/', $id)) { - $this->method_call('messages.getFullChat', ['chat_id' => -$id], ['datacenter' => $this->datacenter->curdc]); - if (isset($this->chats[$id])) { - return $this->gen_all($this->chats[$id]); - } - } if (!isset($this->settings['pwr']['requests']) || $this->settings['pwr']['requests'] === true) { $dbres = json_decode(@file_get_contents('https://id.pwrtelegram.xyz/db/getusername?id='.$id, false, stream_context_create(['http'=> [ 'timeout' => 2, @@ -225,9 +225,9 @@ trait PeerHandler throw new \danog\MadelineProto\Exception('This peer is not present in the internal peer database'); } - $id = str_replace('@', '', $id); + $id = strtolower(str_replace('@', '', $id)); foreach ($this->chats as $chat) { - if (isset($chat['username']) && strtolower($chat['username']) === strtolower($id)) { + if (isset($chat['username']) && strtolower($chat['username']) === $id) { return $this->gen_all($chat); } } diff --git a/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php b/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php index b9373b24..a47e1863 100644 --- a/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php @@ -79,7 +79,15 @@ trait UpdateHandler }); $time = microtime(true); - $this->get_updates_difference(); + + try { + $this->get_updates_difference(); + } catch (\danog\MadelineProto\RPCErrorException $e) { + if ($e->rpc !== 'RPC_CALL_FAIL') { + throw $e; + } + } + $default_params = ['offset' => 0, 'limit' => null, 'timeout' => 0]; foreach ($default_params as $key => $default) { if (!isset($params[$key])) {