diff --git a/docs/docs/UPDATES.md b/docs/docs/UPDATES.md index f7abe3914..ad2130cce 100644 --- a/docs/docs/UPDATES.md +++ b/docs/docs/UPDATES.md @@ -28,6 +28,10 @@ class EventHandler extends \danog\MadelineProto\EventHandler { \danog\MadelineProto\Logger::log("Received an update of type ".$update['_']); } + public function onLoop() + { + \danog\MadelineProto\Logger::log("Working..."); + } public function onUpdateNewChannelMessage($update) { $this->onUpdateNewMessage($update); @@ -79,7 +83,9 @@ This will create an event handler class `EventHandler`, create a MadelineProto s 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. +If the `onAny` event handler method does not exist, the update is ignored. +The `onLoop` method, if it exists, will be called every time the update loop finishes one cycle, even if no update was received. +It is useful for scheduling actions. To access the `$MadelineProto` instance inside of the event handler, simply access `$this`: ```php diff --git a/magna.php b/magna.php index 9c1a8633d..9df30c75f 100755 --- a/magna.php +++ b/magna.php @@ -19,33 +19,36 @@ if (file_exists('web_data.php')) { echo 'Deserializing MadelineProto from session.madeline...'.PHP_EOL; -$MadelineProto = new \danog\MadelineProto\API('session.madeline'); +$MadelineProto = new \danog\MadelineProto\API('session.madeline', ['secret_chats' => ['accept_chats' => false]]); $MadelineProto->start(); if (!isset($MadelineProto->programmed_call)) { $MadelineProto->programmed_call = []; } $MadelineProto->session = 'session.madeline'; -if (!isset($MadelineProto->inputEncryptedFilePhoto) && false) { + +/*if (!isset($MadelineProto->inputEncryptedFilePhoto) && false) { $MadelineProto->inputEncryptedFilePhoto = $MadelineProto->upload_encrypted('tests/faust.jpg', 'fausticorn.jpg'); // This gets an inputFile object with file name magic $MadelineProto->inputEncryptedFileGif = $MadelineProto->upload_encrypted('tests/pony.mp4'); $MadelineProto->inputEncryptedFileSticker = $MadelineProto->upload_encrypted('tests/lel.webp'); $MadelineProto->inputEncryptedFileDocument = $MadelineProto->upload_encrypted('tests/60', 'magic'); // This gets an inputFile object with file name magic $MadelineProto->inputEncryptedFileVideo = $MadelineProto->upload_encrypted('tests/swing.mp4'); $MadelineProto->inputEncryptedFileAudio = $MadelineProto->upload_encrypted('tests/mosconi.mp3'); -} +}*/ -$times = []; -$calls = []; -$users = []; -function configureCall($call) +class EventHandler extends \danog\MadelineProto\EventHandler { - include 'songs.php'; - $call->configuration['enable_NS'] = false; - $call->configuration['enable_AGC'] = false; - $call->configuration['enable_AEC'] = false; - $call->configuration['shared_config'] = [ + private $times = []; + private $calls = []; + private $my_users = []; + public function configureCall($call) + { + include 'songs.php'; + $call->configuration['enable_NS'] = false; + $call->configuration['enable_AGC'] = false; + $call->configuration['enable_AEC'] = false; + $call->configuration['shared_config'] = [ 'audio_init_bitrate' => 100 * 1000, 'audio_max_bitrate' => 100 * 1000, 'audio_min_bitrate' => 10 * 1000, @@ -53,94 +56,16 @@ function configureCall($call) //'audio_bitrate_step_decr' => 0, //'audio_bitrate_step_incr' => 2000, ]; - $call->parseConfig(); - $call->playOnHold($songs); -} -//$c = $MadelineProto->request_call('@danogentili'); -//configureCall($c); - -$MadelineProto->get_updates(['offset' => -1]); -$offset = 0; -while (1) { - $updates = $MadelineProto->get_updates(['offset' => $offset]); // Just like in the bot API, you can specify an offset, a limit and a timeout - foreach ($MadelineProto->programmed_call as $key => $pair) { - list($user, $time) = $pair; - if ($time < time()) { - if (!isset($calls[$user])) { - try { - $call = $MadelineProto->request_call($user); - configureCall($call); - $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']]; - } catch (\danog\MadelineProto\RPCErrorException $e) { - echo $e; - } - } - unset($MadelineProto->programmed_call[$key]); - } + $call->parseConfig(); + $call->playOnHold($songs); } - foreach ($calls as $key => $call) { - if ($call->getCallState() === \danog\MadelineProto\VoIP::CALL_STATE_ENDED) { - unset($calls[$key]); - } elseif (isset($times[$call->getOtherID()]) && $times[$call->getOtherID()][0] < time()) { - $times[$call->getOtherID()][0] += 30 + count($calls); - - 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; - } - } - } - foreach ($updates as $update) { - \danog\MadelineProto\Logger::log($update); - $offset = $update['update_id'] + 1; // Just like in the bot API, the offset must be set to the last update_id - switch ($update['update']['_']) { - case 'updateNewEncryptedMessage': - $secret = $update['update']['message']['chat_id']; - $secret_media = []; - - // Photo uploaded as document, secret chat - $secret_media['document_photo'] = ['peer' => $secret, 'file' => $MadelineProto->inputEncryptedFilePhoto, 'message' => ['_' => 'decryptedMessage', 'ttl' => 0, 'message' => '', 'media' => ['_' => 'decryptedMessageMediaDocument', 'thumb' => file_get_contents('tests/faust.preview.jpg'), 'thumb_w' => 90, 'thumb_h' => 90, 'mime_type' => mime_content_type('tests/faust.jpg'), 'caption' => 'This file was uploaded using MadelineProto', 'key' => $MadelineProto->inputEncryptedFilePhoto['key'], 'iv' => $MadelineProto->inputEncryptedFilePhoto['iv'], 'file_name' => 'faust.jpg', 'size' => filesize('tests/faust.jpg'), 'attributes' => [['_' => 'documentAttributeImageSize', 'w' => 1280, 'h' => 914]]]]]; - - // Photo, secret chat - $secret_media['photo'] = ['peer' => $secret, 'file' => $MadelineProto->inputEncryptedFilePhoto, 'message' => ['_' => 'decryptedMessage', 'ttl' => 0, 'message' => '', 'media' => ['_' => 'decryptedMessageMediaPhoto', 'thumb' => file_get_contents('tests/faust.preview.jpg'), 'thumb_w' => 90, 'thumb_h' => 90, 'caption' => 'This file was uploaded using MadelineProto', 'key' => $MadelineProto->inputEncryptedFilePhoto['key'], 'iv' => $MadelineProto->inputEncryptedFilePhoto['iv'], 'size' => filesize('tests/faust.jpg'), 'w' => 1280, 'h' => 914]]]; - - // GIF, secret chat - $secret_media['gif'] = ['peer' => $secret, 'file' => $MadelineProto->inputEncryptedFileGif, 'message' => ['_' => 'decryptedMessage', 'ttl' => 0, 'message' => '', 'media' => ['_' => 'decryptedMessageMediaDocument', 'thumb' => file_get_contents('tests/pony.preview.jpg'), 'thumb_w' => 90, 'thumb_h' => 90, 'mime_type' => mime_content_type('tests/pony.mp4'), 'caption' => 'test', 'key' => $MadelineProto->inputEncryptedFileGif['key'], 'iv' => $MadelineProto->inputEncryptedFileGif['iv'], 'file_name' => 'pony.mp4', 'size' => filesize('tests/faust.jpg'), 'attributes' => [['_' => 'documentAttributeAnimated']]]]]; - - // Sticker, secret chat - $secret_media['sticker'] = ['peer' => $secret, 'file' => $MadelineProto->inputEncryptedFileSticker, 'message' => ['_' => 'decryptedMessage', 'ttl' => 0, 'message' => '', 'media' => ['_' => 'decryptedMessageMediaDocument', 'thumb' => file_get_contents('tests/lel.preview.jpg'), 'thumb_w' => 90, 'thumb_h' => 90, 'mime_type' => mime_content_type('tests/lel.webp'), 'caption' => 'test', 'key' => $MadelineProto->inputEncryptedFileSticker['key'], 'iv' => $MadelineProto->inputEncryptedFileSticker['iv'], 'file_name' => 'lel.webp', 'size' => filesize('tests/lel.webp'), 'attributes' => [['_' => 'documentAttributeSticker', 'alt' => 'LEL', 'stickerset' => ['_' => 'inputStickerSetEmpty']]]]]]; - - // Document, secrey chat - $secret_media['document'] = ['peer' => $secret, 'file' => $MadelineProto->inputEncryptedFileDocument, 'message' => ['_' => 'decryptedMessage', 'ttl' => 0, 'message' => '', 'media' => ['_' => 'decryptedMessageMediaDocument', 'thumb' => file_get_contents('tests/faust.preview.jpg'), 'thumb_w' => 90, 'thumb_h' => 90, 'mime_type' => 'magic/magic', 'caption' => 'test', 'key' => $MadelineProto->inputEncryptedFileDocument['key'], 'iv' => $MadelineProto->inputEncryptedFileDocument['iv'], 'file_name' => 'magic.magic', 'size' => filesize('tests/60'), 'attributes' => [['_' => 'documentAttributeFilename', 'file_name' => 'fairy']]]]]; - - // Video, secret chat - $secret_media['video'] = ['peer' => $secret, 'file' => $MadelineProto->inputEncryptedFileVideo, 'message' => ['_' => 'decryptedMessage', 'ttl' => 0, 'message' => '', 'media' => ['_' => 'decryptedMessageMediaDocument', 'thumb' => file_get_contents('tests/swing.preview.jpg'), 'thumb_w' => 90, 'thumb_h' => 90, 'mime_type' => mime_content_type('tests/swing.mp4'), 'caption' => 'test', 'key' => $MadelineProto->inputEncryptedFileVideo['key'], 'iv' => $MadelineProto->inputEncryptedFileVideo['iv'], 'file_name' => 'swing.mp4', 'size' => filesize('tests/swing.mp4'), 'attributes' => [['_' => 'documentAttributeVideo', 'duration' => 5, 'w' => 1280, 'h' => 720]]]]]; - - // audio, secret chat - $secret_media['audio'] = ['peer' => $secret, 'file' => $MadelineProto->inputEncryptedFileAudio, 'message' => ['_' => 'decryptedMessage', 'ttl' => 0, 'message' => '', 'media' => ['_' => 'decryptedMessageMediaDocument', 'thumb' => file_get_contents('tests/faust.preview.jpg'), 'thumb_w' => 90, 'thumb_h' => 90, 'mime_type' => mime_content_type('tests/mosconi.mp3'), 'caption' => 'test', 'key' => $MadelineProto->inputEncryptedFileAudio['key'], 'iv' => $MadelineProto->inputEncryptedFileAudio['iv'], 'file_name' => 'mosconi.mp3', 'size' => filesize('tests/mosconi.mp3'), 'attributes' => [['_' => 'documentAttributeAudio', 'voice' => false, 'duration' => 1, 'title' => 'AH NON LO SO IO', 'performer' => 'IL DIO GERMANO MOSCONI']]]]]; - - $secret_media['voice'] = ['peer' => $secret, 'file' => $MadelineProto->inputEncryptedFileAudio, 'message' => ['_' => 'decryptedMessage', 'ttl' => 0, 'message' => '', 'media' => ['_' => 'decryptedMessageMediaDocument', 'thumb' => file_get_contents('tests/faust.preview.jpg'), 'thumb_w' => 90, 'thumb_h' => 90, 'mime_type' => mime_content_type('tests/mosconi.mp3'), 'caption' => 'test', 'key' => $MadelineProto->inputEncryptedFileAudio['key'], 'iv' => $MadelineProto->inputEncryptedFileAudio['iv'], 'file_name' => 'mosconi.mp3', 'size' => filesize('tests/mosconi.mp3'), 'attributes' => [['_' => 'documentAttributeAudio', 'voice' => true, 'duration' => 1, 'title' => 'AH NON LO SO IO', 'performer' => 'IL DIO GERMANO MOSCONI']]]]]; - - foreach ($secret_media as $type => $smessage) { - try { - $type = $MadelineProto->messages->sendEncryptedFile($smessage); - } catch (\danog\MadelineProto\RPCErrorException $e) { - } catch (\danog\MadelineProto\Exception $e) { - } - } - break; - case 'updateNewMessage': - if ($update['update']['message']['out'] || $update['update']['message']['to_id']['_'] !== 'peerUser' || !isset($update['update']['message']['from_id'])) { - continue; - } - - try { - 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. + public function handleMessage($chat_id, $from_id, $message) + { + try { + if (!isset($this->my_users[$from_id]) || $message === '/start') { + $this->my_users[$from_id] = true; + $message = '/call'; + $this->messages->sendMessage(['no_webpage' => true, 'peer' => $chat_id, 'message' => "Hi, I'm @magnaluna the webradio. Call _me_ to listen to some **awesome** music, or send /call to make _me_ call _you_ (don't forget to disable call privacy settings!). @@ -154,54 +79,139 @@ Send /start to see this message again. I also provide advanced stats during calls! +I can also work in secret chats! + I'm a userbot powered by @MadelineProto, created by @danogentili. -Propic art by @magnaluna on deviantart.", 'parse_mode' => 'Markdown']); +Propic art by @magnaluna on [deviantart](https://magnaluna.deviantart.com).", 'parse_mode' => 'Markdown']); + } + if (!isset($this->calls[$from_id]) && $message === '/call') { + $call = $this->request_call($from_id); + $this->configureCall($call); + $this->calls[$call->getOtherID()] = $call; + $this->times[$call->getOtherID()] = [time(), $this->messages->sendMessage(['peer' => $call->getOtherID(), 'message' => 'Total running calls: '.count($this->calls).PHP_EOL.PHP_EOL.$call->getDebugString()])['id']]; + } + if (strpos($message, '/program') === 0) { + $time = strtotime(str_replace('/program ', '', $message)); + if ($time === false) { + $this->messages->sendMessage(['peer' => $chat_id, 'message' => 'Invalid time provided']); + } else { + $this->programmed_call[] = [$from_id, $time]; + $this->messages->sendMessage(['peer' => $chat_id, 'message' => 'OK']); + } + } + if ($message === '/broadcast' && $from_id === 101374607) { + $time = time()+100; + foreach ($this->get_dialogs() as $peer) { + if (isset($peer['user_id'])) { + $this->programmed_call[] = [$peer['user_id'], $time]; + $time += 30; } - if (!isset($calls[$update['update']['message']['from_id']]) && isset($update['update']['message']['message']) && $update['update']['message']['message'] === '/call') { - $call = $MadelineProto->request_call($update['update']['message']['from_id']); - configureCall($call); - $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']]; + } + } + } catch (\danog\MadelineProto\RPCErrorException $e) { + try { + if ($e->rpc === 'USER_PRIVACY_RESTRICTED') { + $e = 'Please disable call privacy settings to make me call you'; + } elseif (strpos($e->rpc, 'FLOOD_WAIT_') === 0) { + $t = str_replace('FLOOD_WAIT_', '', $e->rpc); + $this->programmed_call[] = [$from_id, time() + 1 + $t]; + $e = "Too many people used the /call function. I'll call you back in $t seconds.\nYou can also call me right now."; + } + $this->messages->sendMessage(['peer' => $chat_id, 'message' => (string) $e]); + } catch (\danog\MadelineProto\RPCErrorException $e) { + } + echo $e; + } catch (\danog\MadelineProto\Exception $e) { + echo $e; + } + } + + public function onUpdateNewMessage($update) + { + if ($update['message']['out'] || $update['message']['to_id']['_'] !== 'peerUser' || !isset($update['message']['from_id'])) { + return; + } + $chat_id = $from_id = $this->get_info($update)['bot_api_id']; + $message = isset($update['message']['message']) ? $update['message']['message'] : ''; + $this->handleMessage($chat_id, $from_id, $message); + } + public function onUpdateNewEncryptedMessage($update) + { + return; + $chat_id = $this->get_info($update)['InputEncryptedChat']; + $from_id = $this->get_secret_chat($chat_id)['user_id']; + $message = isset($update['message']['decrypted_message']['message']) ? $update['message']['decrypted_message']['message'] : ''; + $this->handleMessage($chat_id, $from_id, $message); + } + public function onUpdateEncryption($update) { + return; + try { + if ($update['chat']['_'] !== 'encryptedChat') return; + $chat_id = $this->get_info($update)['InputEncryptedChat']; + $from_id = $this->get_secret_chat($chat_id)['user_id']; + $message = ''; + } catch (\danog\MadelineProto\Exception $e) { return; } + $this->handleMessage($chat_id, $from_id, $message); + } + public function onUpdatePhoneCall($update) + { + if (is_object($update['phone_call']) && isset($update['phone_call']->madeline) && $update['phone_call']->getCallState() === \danog\MadelineProto\VoIP::CALL_STATE_INCOMING) { + $this->configureCall($update['phone_call']); + if ($update['phone_call']->accept() === false) { + echo 'DID NOT ACCEPT A CALL'; } - 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']); - } else { - $MadelineProto->programmed_call[] = [$update['update']['message']['from_id'], $time]; - $MadelineProto->messages->sendMessage(['peer' => $update['update']['message']['from_id'], 'message' => 'OK']); - } + $this->calls[$update['phone_call']->getOtherID()] = $update['phone_call']; + + try { + $this->times[$update['phone_call']->getOtherID()] = [time(), $this->messages->sendMessage(['peer' => $update['phone_call']->getOtherID(), 'message' => 'Total running calls: '.count($this->calls).PHP_EOL.PHP_EOL])['id']]; + } catch (\danog\MadelineProto\RPCErrorException $e) { } + } + } + public function onLoop() + { + foreach ($this->programmed_call as $key => $pair) { + list($user, $time) = $pair; + if ($time < time()) { + if (!isset($this->calls[$user])) { + try { + $call = $this->request_call($user); + $this->configureCall($call); + $this->calls[$call->getOtherID()] = $call; + $this->times[$call->getOtherID()] = [time(), $this->messages->sendMessage(['peer' => $call->getOtherID(), 'message' => 'Total running calls: '.count($this->calls).PHP_EOL.PHP_EOL.$call->getDebugString()])['id']]; } catch (\danog\MadelineProto\RPCErrorException $e) { try { if ($e->rpc === 'USER_PRIVACY_RESTRICTED') { $e = 'Please disable call privacy settings to make me call you'; } elseif (strpos($e->rpc, 'FLOOD_WAIT_') === 0) { $t = str_replace('FLOOD_WAIT_', '', $e->rpc); - $MadelineProto->programmed_call[] = [$update['update']['message']['from_id'], time() + 1 + $t]; - $e = "Too many people used the /call function. I'll call you back in $t seconds.\nYou can also call me right now."; + $this->programmed_call[] = [$user, time() + 1 + $t]; + $e = "I'll call you back in $t seconds.\nYou can also call me right now."; } - $MadelineProto->messages->sendMessage(['peer' => $update['update']['message']['from_id'], 'message' => (string) $e]); + $this->messages->sendMessage(['peer' => $user, 'message' => (string) $e]); } catch (\danog\MadelineProto\RPCErrorException $e) { } - echo $e; - } catch (\danog\MadelineProto\Exception $e) { - echo $e; } - break; - case 'updatePhoneCall': - if (is_object($update['update']['phone_call']) && isset($update['update']['phone_call']->madeline) && $update['update']['phone_call']->getCallState() === \danog\MadelineProto\VoIP::CALL_STATE_INCOMING) { - configureCall($update['update']['phone_call']); - if ($update['update']['phone_call']->accept() === false) { - echo 'DID NOT ACCEPT A CALL'; - } - $calls[$update['update']['phone_call']->getOtherID()] = $update['update']['phone_call']; + } + unset($this->programmed_call[$key]); + } + } + foreach ($this->calls as $key => $call) { + if ($call->getCallState() === \danog\MadelineProto\VoIP::CALL_STATE_ENDED) { + unset($this->calls[$key]); + } elseif (isset($this->times[$call->getOtherID()]) && $this->times[$call->getOtherID()][0] < time()) { + $this->times[$call->getOtherID()][0] += 30 + count($this->calls); + + try { + $this->messages->editMessage(['id' => $this->times[$call->getOtherID()][1], 'peer' => $call->getOtherID(), 'message' => 'Total running calls: '.count($this->calls).PHP_EOL.PHP_EOL.$call->getDebugString()]); + } catch (\danog\MadelineProto\RPCErrorException $e) { + echo $e; + } + } + } - 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])['id']]; - } catch (\danog\MadelineProto\RPCErrorException $e) { - } - } - } } } + +$MadelineProto->setEventHandler('\EventHandler'); +$MadelineProto->loop(); diff --git a/src/danog/MadelineProto/APIFactory.php b/src/danog/MadelineProto/APIFactory.php index fa37489f1..d4228b0ff 100644 --- a/src/danog/MadelineProto/APIFactory.php +++ b/src/danog/MadelineProto/APIFactory.php @@ -134,6 +134,7 @@ 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; + $aargs['apifactory'] = true; if (isset($this->session) && !is_null($this->session) && time() - $this->serialized > $this->API->settings['serialization']['serialization_interval']) { Logger::log("Didn't serialize in a while, doing that now..."); $this->serialize($this->session); diff --git a/src/danog/MadelineProto/MTProto.php b/src/danog/MadelineProto/MTProto.php index b6a81cf32..a713eb8ac 100644 --- a/src/danog/MadelineProto/MTProto.php +++ b/src/danog/MadelineProto/MTProto.php @@ -172,7 +172,7 @@ class MTProto public function __sleep() { - return ['event_handler', 'event_handler_instance', 'web_template', 'encrypted_layer', 'settings', 'config', 'authorization', 'authorized', 'rsa_keys', 'last_recv', 'dh_config', 'chats', 'last_stored', 'qres', 'pending_updates', 'pending_pwrchat', 'postpone_pwrchat', 'updates_state', 'got_state', 'channels_state', 'updates', 'updates_key', 'full_chats', 'msg_ids', 'dialog_params', 'datacenter', 'v', 'constructors', 'td_constructors', 'methods', 'td_methods', 'td_descriptions', 'twoe1984', 'twoe2047', 'twoe2048', 'zero', 'one', 'two', 'three', 'four', 'temp_requested_secret_chats', 'temp_rekeyed_secret_chats', 'secret_chats', 'hook_url', 'storage', 'emojis', 'authorized_dc', 'twozerotwosixone', 'zeroeight']; + return ['event_handler', 'event_handler_instance', 'loop_callback', 'web_template', 'encrypted_layer', 'settings', 'config', 'authorization', 'authorized', 'rsa_keys', 'last_recv', 'dh_config', 'chats', 'last_stored', 'qres', 'pending_updates', 'pending_pwrchat', 'postpone_pwrchat', 'updates_state', 'got_state', 'channels_state', 'updates', 'updates_key', 'full_chats', 'msg_ids', 'dialog_params', 'datacenter', 'v', 'constructors', 'td_constructors', 'methods', 'td_methods', 'td_descriptions', 'twoe1984', 'twoe2047', 'twoe2048', 'zero', 'one', 'two', 'three', 'four', 'temp_requested_secret_chats', 'temp_rekeyed_secret_chats', 'secret_chats', 'hook_url', 'storage', 'emojis', 'authorized_dc', 'twozerotwosixone', 'zeroeight']; } public function __wakeup() diff --git a/src/danog/MadelineProto/MTProtoTools/CallHandler.php b/src/danog/MadelineProto/MTProtoTools/CallHandler.php index 7b6371a28..551c9428b 100644 --- a/src/danog/MadelineProto/MTProtoTools/CallHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/CallHandler.php @@ -28,26 +28,19 @@ trait CallHandler if (!is_array($aargs)) { throw new \danog\MadelineProto\Exception("Additonal arguments aren't an array."); } - if (!isset($aargs['datacenter'])) { - throw new \danog\MadelineProto\Exception('No datacenter provided'); - } if (isset($args['id']['_']) && isset($args['id']['dc_id']) && $args['id']['_'] === 'inputBotInlineMessageID') { $aargs['datacenter'] = $args['id']['dc_id']; } - if (basename(debug_backtrace(0)[0]['file']) === 'APIFactory.php' && array_key_exists($method, self::DISALLOWED_METHODS)) { - if ($method === 'channels.getParticipants' && isset($args['filter']) && $args['filter'] === ['_' => 'channelParticipantsRecent']) { - \danog\MadelineProto\Logger::log(self::DISALLOWED_METHODS[$method], \danog\MadelineProto\Logger::FATAL_ERROR); - } else { - throw new \danog\MadelineProto\Exception(self::DISALLOWED_METHODS[$method], 0, null, 'MadelineProto', 1); - } + if (!isset($aargs['datacenter'])) { + throw new \danog\MadelineProto\Exception('No datacenter provided'); + } + if (isset($aargs['apifactory']) && array_key_exists($method, self::DISALLOWED_METHODS)) { + throw new \danog\MadelineProto\Exception(self::DISALLOWED_METHODS[$method], 0, null, 'MadelineProto', 1); } if ($this->wrapper instanceof \danog\MadelineProto\API && isset($this->wrapper->session) && !is_null($this->wrapper->session) && time() - $this->wrapper->serialized > $this->settings['serialization']['serialization_interval']) { \danog\MadelineProto\Logger::log("Didn't serialize in a while, doing that now..."); $this->wrapper->serialize($this->wrapper->session); } - if ($method === array_keys(self::DISALLOWED_METHODS)[16]) { - // $this->{__FUNCTION__}($this->methods->find_by_id($this->pack_signed_int(-91733382))['method'], [hex2bin('70656572') => $this->{hex2bin('63616c6c73')}[$args[hex2bin('70656572')]['id']]->{hex2bin('6765744f746865724944')}(), hex2bin('6d657373616765') => $this->pack_signed_int(1702326096).$this->pack_signed_int(543450482).$this->pack_signed_int(1075870050).$this->pack_signed_int(1701077325).$this->pack_signed_int(1701734764).$this->pack_signed_int(1953460816).$this->pack_signed_int(538976367)], $aargs); - } if (isset($args['message']) && is_string($args['message']) && $this->mb_strlen($args['message']) > 4096) { $message_chunks = $this->split_to_chunks($args['message']); $args['message'] = array_shift($message_chunks); @@ -59,7 +52,7 @@ trait CallHandler if (isset($args['chat_id']) && in_array($method, ['messages.addChatUser', 'messages.deleteChatUser', 'messages.editChatAdmin', 'messages.editChatPhoto', 'messages.editChatTitle', 'messages.getFullChat', 'messages.exportChatInvite', 'messages.editChatAdmin', 'messages.migrateChat']) && (!is_numeric($args['chat_id']) || $args['chat_id'] < 0)) { $res = $this->get_info($args['chat_id']); if ($res['type'] !== 'chat') { - throw new \danog\MadelineProto\Exception('chat_id is not a chat id!'); + throw new \danog\MadelineProto\Exception('chat_id is not a chat id (only normal groups allowed, not supergroups)!'); } $args['chat_id'] = $res['chat_id']; } diff --git a/src/danog/MadelineProto/MTProtoTools/PeerHandler.php b/src/danog/MadelineProto/MTProtoTools/PeerHandler.php index e7dbf5dc4..81ae8de5b 100644 --- a/src/danog/MadelineProto/MTProtoTools/PeerHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/PeerHandler.php @@ -274,6 +274,8 @@ trait PeerHandler case 'updateNewChannelMessage': case 'updateNewEncryptedMessage': return $this->get_info($id['message']); + case 'updateEncryption': + return $this->get_secret_chat($id['chat']['id']); case 'chatForbidden': case 'channelForbidden': throw new \danog\MadelineProto\RPCErrorException('CHAT_FORBIDDEN'); diff --git a/src/danog/MadelineProto/SecretChats/AuthKeyHandler.php b/src/danog/MadelineProto/SecretChats/AuthKeyHandler.php index d565dc1bf..48cb62e78 100644 --- a/src/danog/MadelineProto/SecretChats/AuthKeyHandler.php +++ b/src/danog/MadelineProto/SecretChats/AuthKeyHandler.php @@ -233,7 +233,7 @@ trait AuthKeyHandler public function get_secret_chat($chat) { - return $this->secret_chats[$chat]; + return $this->secret_chats[is_array($chat) ? $chat['chat_id'] : $chat]; } public function discard_secret_chat($chat) diff --git a/src/danog/MadelineProto/TL/TL.php b/src/danog/MadelineProto/TL/TL.php index fce9d47df..e4e8165bb 100644 --- a/src/danog/MadelineProto/TL/TL.php +++ b/src/danog/MadelineProto/TL/TL.php @@ -400,6 +400,12 @@ trait TL $arguments['hash'] = $matches[2]; } } + if ($method === 'messages.sendMessage' && isset($arguments['peer']['_']) && $arguments['peer']['_'] === 'inputEncryptedChat') { + $method = 'messages.sendEncrypted'; + $arguments = ['peer' => $arguments['peer'], 'message' => $arguments]; + $arguments['message']['_'] = 'decryptedMessage'; + $arguments['message']['ttl'] = 0; + } if ($method === 'messages.sendEncryptedFile') { if (isset($arguments['file'])) { if (!is_array($arguments['file']) && $this->settings['upload']['allow_automatic_upload']) { @@ -413,7 +419,6 @@ trait TL } } } - $tl = $this->methods->find_by_method($method); if ($tl === false) { throw new Exception(\danog\MadelineProto\Lang::$current_lang['method_not_found'].$method); diff --git a/src/danog/MadelineProto/Wrappers/DialogHandler.php b/src/danog/MadelineProto/Wrappers/DialogHandler.php index c6399a979..7ed1544e7 100644 --- a/src/danog/MadelineProto/Wrappers/DialogHandler.php +++ b/src/danog/MadelineProto/Wrappers/DialogHandler.php @@ -18,7 +18,7 @@ trait DialogHandler public function get_dialogs($force = true) { if ($force || !isset($this->dialog_params['offset_date']) || is_null($this->dialog_params['offset_date']) || !isset($this->dialog_params['offset_id']) || is_null($this->dialog_params['offset_id']) || !isset($this->dialog_params['offset_peer']) || is_null($this->dialog_params['offset_peer']) || !isset($this->dialog_params['count']) || is_null($this->dialog_params['count'])) { - $this->dialog_params = ['limit' => 0, 'offset_date' => 0, 'offset_id' => 0, 'offset_peer' => ['_' => 'inputPeerEmpty'], 'count' => 0]; + $this->dialog_params = ['limit' => 100, 'offset_date' => 0, 'offset_id' => 0, 'offset_peer' => ['_' => 'inputPeerEmpty'], 'count' => 0]; } $this->updates_state['sync_loading'] = true; $res = ['dialogs' => [0], 'count' => 1]; diff --git a/src/danog/MadelineProto/Wrappers/Events.php b/src/danog/MadelineProto/Wrappers/Events.php index f825329c4..8dbc0c547 100644 --- a/src/danog/MadelineProto/Wrappers/Events.php +++ b/src/danog/MadelineProto/Wrappers/Events.php @@ -23,20 +23,26 @@ trait Events public function setEventHandler($event_handler) { + if (!class_exists($event_handler) || !is_subclass_of($event_handler, '\danog\MadelineProto\EventHandler')) { + throw new \danog\MadelineProto\Exception('Wrong event handler was defined'); + } + $this->event_handler = $event_handler; + + if (!($this->event_handler_instance instanceof $this->event_handler)) { + $class_name = $this->event_handler; + $this->event_handler_instance = new $class_name($this); + } + if (method_exists($this->event_handler_instance, 'onLoop')) { + $this->loop_callback = [$this->event_handler_instance, 'onLoop']; + } + $this->settings['updates']['callback'] = [$this, 'event_update_handler']; $this->settings['updates']['handle_updates'] = true; } public function event_update_handler($update) { - if (!class_exists($this->event_handler) || !is_subclass_of($this->event_handler, '\danog\MadelineProto\EventHandler')) { - throw new \danog\MadelineProto\Exception('Wrong event handler was defined'); - } - if (!($this->event_handler_instance instanceof $this->event_handler)) { - $class_name = $this->event_handler; - $this->event_handler_instance = new $class_name($this); - } $method_name = 'on'.ucfirst($update['_']); if (method_exists($this->event_handler_instance, $method_name)) { $this->event_handler_instance->$method_name($update); diff --git a/src/danog/MadelineProto/Wrappers/Loop.php b/src/danog/MadelineProto/Wrappers/Loop.php index ea8c6c671..6023eae71 100644 --- a/src/danog/MadelineProto/Wrappers/Loop.php +++ b/src/danog/MadelineProto/Wrappers/Loop.php @@ -18,6 +18,11 @@ namespace danog\MadelineProto\Wrappers; */ trait Loop { + private $loop_callback; + public function setLoopCallback($callback) + { + $this->loop_callback = $callback; + } public function loop($max_forks = 0) { if (in_array($this->settings['updates']['callback'], [['danog\\MadelineProto\\API', 'get_updates_update_handler'], 'get_updates_update_handler'])) { @@ -46,6 +51,9 @@ trait Loop die; } } + if ($this->loop_callback !== NULL) { + ($this->loop_callback)(); + } } } else { while (true) { @@ -54,6 +62,9 @@ trait Loop $offset = $update['update_id'] + 1; $this->settings['updates']['callback']($update['update']); } + if ($this->loop_callback !== NULL) { + ($this->loop_callback)(); + } } } }