From 34f7047d73e1e4b53e3fbb787241c3570348c827 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Fri, 30 Mar 2018 16:07:54 +0000 Subject: [PATCH] Improve docs --- .env.example | 2 +- docs/docs/INLINE_BUTTONS.md | 6 ++---- docs/docs/UPDATES.md | 32 +++++++------------------------- multiprocess_bot.php | 9 +-------- secret_bot.php | 9 ++------- 5 files changed, 13 insertions(+), 45 deletions(-) diff --git a/.env.example b/.env.example index 43453030..ef2bd5a3 100644 --- a/.env.example +++ b/.env.example @@ -1,4 +1,4 @@ -MTPROTO_SETTINGS={"app_info":{"api_id":6,"api_hash":"eb06d4abfb49dc3eeb1aeb98ae0f581e"}} +MTPROTO_SETTINGS={} TEST_USERNAME=@danogentili TEST_DESTINATION_GROUPS=["@pwrtelegramgroup","@pwrtelegramgroupita"] TEST_SECRET_CHAT=@danogentili diff --git a/docs/docs/INLINE_BUTTONS.md b/docs/docs/INLINE_BUTTONS.md index 1d2acb3e..536717bb 100644 --- a/docs/docs/INLINE_BUTTONS.md +++ b/docs/docs/INLINE_BUTTONS.md @@ -27,9 +27,7 @@ class EventHandler extends \danog\MadelineProto\EventHandler } -$settings = ['app_info' => ['api_id' => 6, 'api_hash' => 'eb06d4abfb49dc3eeb1aeb98ae0f581e'], 'updates' => ['handle_updates' => true]]; - -$MadelineProto = new \danog\MadelineProto\API('session.madeline', $settings); +$MadelineProto = new \danog\MadelineProto\API('session.madeline'); $MadelineProto->start(); $MadelineProto->setEventHandler('\EventHandler'); @@ -50,4 +48,4 @@ And click them: $button->click(); ``` -
\ No newline at end of file +
diff --git a/docs/docs/UPDATES.md b/docs/docs/UPDATES.md index ad2130cc..98031c59 100644 --- a/docs/docs/UPDATES.md +++ b/docs/docs/UPDATES.md @@ -65,15 +65,8 @@ class EventHandler extends \danog\MadelineProto\EventHandler } -$settings = ['app_info' => ['api_id' => 6, 'api_hash' => 'eb06d4abfb49dc3eeb1aeb98ae0f581e']]; +$MadelineProto = new \danog\MadelineProto\API('bot.madeline'); -try { - $MadelineProto = new \danog\MadelineProto\API('bot.madeline', $settings); -} catch (\danog\MadelineProto\Exception $e) { - \danog\MadelineProto\Logger::log($e->getMessage()); - unlink('bot.madeline'); - $MadelineProto = new \danog\MadelineProto\API('bot.madeline', $settings); -} $MadelineProto->start(); $MadelineProto->setEventHandler('\EventHandler'); $MadelineProto->loop(); @@ -112,15 +105,8 @@ Note that multiprocessing is not the same as multithreading, and should be avoid ## Webhook ```php -$settings = ['app_info' => ['api_id' => 6, 'api_hash' => 'eb06d4abfb49dc3eeb1aeb98ae0f581e']]; +$MadelineProto = new \danog\MadelineProto\API('bot.madeline'); -try { - $MadelineProto = new \danog\MadelineProto\API('bot.madeline', $settings); -} catch (\danog\MadelineProto\Exception $e) { - \danog\MadelineProto\Logger::log($e->getMessage()); - unlink('bot.madeline'); - $MadelineProto = new \danog\MadelineProto\API('bot.madeline', $settings); -} $MadelineProto->start(); $MadelineProto->setWebhook('http://mybot.eu.org/madelinehook.php'); $MadelineProto->loop(); @@ -146,6 +132,9 @@ This way, each update could be managed faster. ## Long polling ```php +$MadelineProto = new \danog\MadelineProto\API('bot.madeline'); +$MadelineProto->start(); + while (true) { $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); @@ -163,7 +152,7 @@ while (true) { } try { - $MadelineProto->messages->sendMessage(['peer' => $update['update']['_'] === 'updateNewMessage' ? $update['update']['message']['from_id'] : $update['update']['message']['to_id'], 'message' => $res, 'reply_to_msg_id' => $update['update']['message']['id'], 'entities' => [['_' => 'messageEntityPre', 'offset' => 0, 'length' => strlen($res), 'language' => 'json']]]); + $MadelineProto->messages->sendMessage(['peer' => $update, 'message' => $res, '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()]); } @@ -179,15 +168,8 @@ The get_updates function accepts an array of options as the first parameter, and ## Callback ```php -$settings = ['app_info' => ['api_id' => 6, 'api_hash' => 'eb06d4abfb49dc3eeb1aeb98ae0f581e']]; +$MadelineProto = new \danog\MadelineProto\API('bot.madeline'); -try { - $MadelineProto = new \danog\MadelineProto\API('bot.madeline', $settings); -} catch (\danog\MadelineProto\Exception $e) { - \danog\MadelineProto\Logger::log($e->getMessage()); - unlink('bot.madeline'); - $MadelineProto = new \danog\MadelineProto\API('bot.madeline', $settings); -} $MadelineProto->start(); $MadelineProto->setCallback(function ($update) use ($MadelineProto) { \danog\MadelineProto\Logger::log("Received an update of type ".$update['_']); }); $MadelineProto->loop(); diff --git a/multiprocess_bot.php b/multiprocess_bot.php index b810f30f..d287be29 100644 --- a/multiprocess_bot.php +++ b/multiprocess_bot.php @@ -60,15 +60,8 @@ class EventHandler extends \danog\MadelineProto\EventHandler } } -$settings = ['app_info' => ['api_id' => 6, 'api_hash' => 'eb06d4abfb49dc3eeb1aeb98ae0f581e'], 'updates' => ['handle_updates' => true]]; //, 'connection_settings' => ['all' => ['test_mode' => true]]]; +$MadelineProto = new \danog\MadelineProto\API('bot.madeline'); -try { - $MadelineProto = new \danog\MadelineProto\API('bot.madeline', $settings); -} catch (\danog\MadelineProto\Exception $e) { - \danog\MadelineProto\Logger::log($e->getMessage()); - unlink('bot.madeline'); - $MadelineProto = new \danog\MadelineProto\API('bot.madeline', $settings); -} $MadelineProto->start(); $MadelineProto->setEventHandler('\EventHandler'); $MadelineProto->loop(-1); diff --git a/secret_bot.php b/secret_bot.php index f018254d..c69cf6b0 100755 --- a/secret_bot.php +++ b/secret_bot.php @@ -99,13 +99,8 @@ if (file_exists('.env')) { echo 'Loading settings...'.PHP_EOL; $settings = json_decode(getenv('MTPROTO_SETTINGS'), true) ?: []; -try { - $MadelineProto = new \danog\MadelineProto\API('s.madeline', $settings); -} catch (\danog\MadelineProto\Exception $e) { - \danog\MadelineProto\Logger::log($e->getMessage()); - unlink('s.madeline'); - $MadelineProto = new \danog\MadelineProto\API('s.madeline', $settings); -} +$MadelineProto = new \danog\MadelineProto\API('s.madeline', $settings); + $MadelineProto->start(); $MadelineProto->setEventHandler('\EventHandler'); $MadelineProto->loop();