This commit is contained in:
Daniil Gentili 2018-03-30 09:24:11 +00:00
commit 44d53b1c09
13 changed files with 228 additions and 192 deletions

View File

@ -67,13 +67,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]]]; $settings = ['app_info' => ['api_id' => 6, 'api_hash' => 'eb06d4abfb49dc3eeb1aeb98ae0f581e'], 'updates' => ['handle_updates' => true]]; //, 'connection_settings' => ['all' => ['test_mode' => true]]];
try { $MadelineProto = new \danog\MadelineProto\API('bot.madeline', $settings);
$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->start();
$MadelineProto->setEventHandler('\EventHandler'); $MadelineProto->setEventHandler('\EventHandler');
$MadelineProto->loop(); $MadelineProto->loop();

View File

@ -28,6 +28,10 @@ class EventHandler extends \danog\MadelineProto\EventHandler
{ {
\danog\MadelineProto\Logger::log("Received an update of type ".$update['_']); \danog\MadelineProto\Logger::log("Received an update of type ".$update['_']);
} }
public function onLoop()
{
\danog\MadelineProto\Logger::log("Working...");
}
public function onUpdateNewChannelMessage($update) public function onUpdateNewChannelMessage($update)
{ {
$this->onUpdateNewMessage($update); $this->onUpdateNewMessage($update);
@ -80,6 +84,8 @@ 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). 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 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`: To access the `$MadelineProto` instance inside of the event handler, simply access `$this`:
```php ```php

327
magna.php
View File

@ -12,47 +12,51 @@ If not, see <http://www.gnu.org/licenses/>.
*/ */
set_include_path(get_include_path().':'.realpath(dirname(__FILE__).'/MadelineProto/')); set_include_path(get_include_path().':'.realpath(dirname(__FILE__).'/MadelineProto/'));
require_once 'vendor/autoload.php'; if (!file_exists(__DIR__.'/vendor/autoload.php')) {
echo 'You did not run composer update, using madeline.php'.PHP_EOL;
if (!file_exists('madeline.php')) {
copy('https://phar.madelineproto.xyz/madeline.php', 'madeline.php');
}
include 'madeline.php';
} else {
require_once 'vendor/autoload.php';
}
if (file_exists('web_data.php')) { if (file_exists('web_data.php')) {
require_once 'web_data.php'; require_once 'web_data.php';
} }
echo 'Deserializing MadelineProto from session.madeline...'.PHP_EOL; echo 'Deserializing MadelineProto from session.madeline...'.PHP_EOL;
try { $MadelineProto = new \danog\MadelineProto\API('session.madeline', ['secret_chats' => ['accept_chats' => false]]);
$MadelineProto = new \danog\MadelineProto\API('session.madeline');
} catch (\danog\MadelineProto\Exception $e) {
echo $e.PHP_EOL;
unlink('session.madeline');
$MadelineProto = new \danog\MadelineProto\API('session.madeline');
}
$MadelineProto->start(); $MadelineProto->start();
if (!isset($MadelineProto->programmed_call)) { if (!isset($MadelineProto->programmed_call)) {
$MadelineProto->programmed_call = []; $MadelineProto->programmed_call = [];
} }
$MadelineProto->session = 'session.madeline'; $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->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->inputEncryptedFileGif = $MadelineProto->upload_encrypted('tests/pony.mp4');
$MadelineProto->inputEncryptedFileSticker = $MadelineProto->upload_encrypted('tests/lel.webp'); $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->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->inputEncryptedFileVideo = $MadelineProto->upload_encrypted('tests/swing.mp4');
$MadelineProto->inputEncryptedFileAudio = $MadelineProto->upload_encrypted('tests/mosconi.mp3'); $MadelineProto->inputEncryptedFileAudio = $MadelineProto->upload_encrypted('tests/mosconi.mp3');
} }*/
$times = []; class EventHandler extends \danog\MadelineProto\EventHandler
$calls = [];
$users = [];
function configureCall($call)
{ {
include 'songs.php'; private $times = [];
$call->configuration['enable_NS'] = false; private $calls = [];
$call->configuration['enable_AGC'] = false; private $my_users = [];
$call->configuration['enable_AEC'] = false;
$call->configuration['shared_config'] = [ 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_init_bitrate' => 100 * 1000,
'audio_max_bitrate' => 100 * 1000, 'audio_max_bitrate' => 100 * 1000,
'audio_min_bitrate' => 10 * 1000, 'audio_min_bitrate' => 10 * 1000,
@ -60,94 +64,17 @@ function configureCall($call)
//'audio_bitrate_step_decr' => 0, //'audio_bitrate_step_decr' => 0,
//'audio_bitrate_step_incr' => 2000, //'audio_bitrate_step_incr' => 2000,
]; ];
$call->parseConfig(); $call->parseConfig();
$call->playOnHold($songs); $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]);
}
} }
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 { public function handleMessage($chat_id, $from_id, $message)
$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) { try {
echo $e; 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.
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.
Call _me_ to listen to some **awesome** music, or send /call to make _me_ call _you_ (don't forget to disable call privacy settings!). Call _me_ to listen to some **awesome** music, or send /call to make _me_ call _you_ (don't forget to disable call privacy settings!).
@ -161,54 +88,148 @@ Send /start to see this message again.
I also provide advanced stats during calls! I also provide advanced stats during calls!
I'm a userbot powered by @MadelineProto, created by @danogentili. I can also work in secret chats!
Propic art by @magnaluna on deviantart.", 'parse_mode' => 'Markdown']);
}
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']];
}
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']);
}
}
} 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.";
}
$MadelineProto->messages->sendMessage(['peer' => $update['update']['message']['from_id'], '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'];
try { I'm a userbot powered by @MadelineProto, created by @danogentili.
$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']]; Propic art by @magnaluna on [deviantart](https://magnaluna.deviantart.com).", 'parse_mode' => 'Markdown']);
} catch (\danog\MadelineProto\RPCErrorException $e) { }
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;
} }
} }
} }
} 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';
}
$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);
$this->programmed_call[] = [$user, time() + 1 + $t];
$e = "I'll call you back in $t seconds.\nYou can also call me right now.";
}
$this->messages->sendMessage(['peer' => $user, 'message' => (string) $e]);
} catch (\danog\MadelineProto\RPCErrorException $e) {
}
}
}
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;
}
}
}
} }
} }
$MadelineProto->setEventHandler('\EventHandler');
$MadelineProto->loop();

View File

@ -134,6 +134,7 @@ class APIFactory
$this->API->get_config([], ['datacenter' => $this->API->datacenter->curdc]); $this->API->get_config([], ['datacenter' => $this->API->datacenter->curdc]);
$aargs = isset($arguments[1]) && is_array($arguments[1]) ? $arguments[1] : []; $aargs = isset($arguments[1]) && is_array($arguments[1]) ? $arguments[1] : [];
$aargs['datacenter'] = $this->API->datacenter->curdc; $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']) { 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..."); Logger::log("Didn't serialize in a while, doing that now...");
$this->serialize($this->session); $this->serialize($this->session);

View File

@ -172,7 +172,7 @@ class MTProto
public function __sleep() 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() public function __wakeup()

View File

@ -28,26 +28,19 @@ trait CallHandler
if (!is_array($aargs)) { if (!is_array($aargs)) {
throw new \danog\MadelineProto\Exception("Additonal arguments aren't an array."); 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') { if (isset($args['id']['_']) && isset($args['id']['dc_id']) && $args['id']['_'] === 'inputBotInlineMessageID') {
$aargs['datacenter'] = $args['id']['dc_id']; $aargs['datacenter'] = $args['id']['dc_id'];
} }
if (basename(debug_backtrace(0)[0]['file']) === 'APIFactory.php' && array_key_exists($method, self::DISALLOWED_METHODS)) { if (!isset($aargs['datacenter'])) {
if ($method === 'channels.getParticipants' && isset($args['filter']) && $args['filter'] === ['_' => 'channelParticipantsRecent']) { throw new \danog\MadelineProto\Exception('No datacenter provided');
\danog\MadelineProto\Logger::log(self::DISALLOWED_METHODS[$method], \danog\MadelineProto\Logger::FATAL_ERROR); }
} else { if (isset($aargs['apifactory']) && array_key_exists($method, self::DISALLOWED_METHODS)) {
throw new \danog\MadelineProto\Exception(self::DISALLOWED_METHODS[$method], 0, null, 'MadelineProto', 1); 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']) { 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..."); \danog\MadelineProto\Logger::log("Didn't serialize in a while, doing that now...");
$this->wrapper->serialize($this->wrapper->session); $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) { if (isset($args['message']) && is_string($args['message']) && $this->mb_strlen($args['message']) > 4096) {
$message_chunks = $this->split_to_chunks($args['message']); $message_chunks = $this->split_to_chunks($args['message']);
$args['message'] = array_shift($message_chunks); $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)) { 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']); $res = $this->get_info($args['chat_id']);
if ($res['type'] !== 'chat') { 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']; $args['chat_id'] = $res['chat_id'];
} }

View File

@ -274,6 +274,8 @@ trait PeerHandler
case 'updateNewChannelMessage': case 'updateNewChannelMessage':
case 'updateNewEncryptedMessage': case 'updateNewEncryptedMessage':
return $this->get_info($id['message']); return $this->get_info($id['message']);
case 'updateEncryption':
return $this->get_secret_chat($id['chat']['id']);
case 'chatForbidden': case 'chatForbidden':
case 'channelForbidden': case 'channelForbidden':
throw new \danog\MadelineProto\RPCErrorException('CHAT_FORBIDDEN'); throw new \danog\MadelineProto\RPCErrorException('CHAT_FORBIDDEN');

View File

@ -233,7 +233,7 @@ trait AuthKeyHandler
public function get_secret_chat($chat) 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) public function discard_secret_chat($chat)

View File

@ -400,6 +400,12 @@ trait TL
$arguments['hash'] = $matches[2]; $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 ($method === 'messages.sendEncryptedFile') {
if (isset($arguments['file'])) { if (isset($arguments['file'])) {
if (!is_array($arguments['file']) && $this->settings['upload']['allow_automatic_upload']) { if (!is_array($arguments['file']) && $this->settings['upload']['allow_automatic_upload']) {
@ -413,7 +419,6 @@ trait TL
} }
} }
} }
$tl = $this->methods->find_by_method($method); $tl = $this->methods->find_by_method($method);
if ($tl === false) { if ($tl === false) {
throw new Exception(\danog\MadelineProto\Lang::$current_lang['method_not_found'].$method); throw new Exception(\danog\MadelineProto\Lang::$current_lang['method_not_found'].$method);

View File

@ -18,7 +18,7 @@ trait DialogHandler
public function get_dialogs($force = true) 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'])) { 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; $this->updates_state['sync_loading'] = true;
$res = ['dialogs' => [0], 'count' => 1]; $res = ['dialogs' => [0], 'count' => 1];

View File

@ -23,20 +23,26 @@ trait Events
public function setEventHandler($event_handler) 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; $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']['callback'] = [$this, 'event_update_handler'];
$this->settings['updates']['handle_updates'] = true; $this->settings['updates']['handle_updates'] = true;
} }
public function event_update_handler($update) 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['_']); $method_name = 'on'.ucfirst($update['_']);
if (method_exists($this->event_handler_instance, $method_name)) { if (method_exists($this->event_handler_instance, $method_name)) {
$this->event_handler_instance->$method_name($update); $this->event_handler_instance->$method_name($update);

View File

@ -18,6 +18,13 @@ namespace danog\MadelineProto\Wrappers;
*/ */
trait Loop trait Loop
{ {
private $loop_callback;
public function setLoopCallback($callback)
{
$this->loop_callback = $callback;
}
public function loop($max_forks = 0) public function loop($max_forks = 0)
{ {
if (in_array($this->settings['updates']['callback'], [['danog\\MadelineProto\\API', 'get_updates_update_handler'], 'get_updates_update_handler'])) { if (in_array($this->settings['updates']['callback'], [['danog\\MadelineProto\\API', 'get_updates_update_handler'], 'get_updates_update_handler'])) {
@ -46,6 +53,9 @@ trait Loop
die; die;
} }
} }
if ($this->loop_callback !== null) {
($this->loop_callback)();
}
} }
} else { } else {
while (true) { while (true) {
@ -54,6 +64,9 @@ trait Loop
$offset = $update['update_id'] + 1; $offset = $update['update_id'] + 1;
$this->settings['updates']['callback']($update['update']); $this->settings['updates']['callback']($update['update']);
} }
if ($this->loop_callback !== null) {
($this->loop_callback)();
}
} }
} }
} }

View File

@ -43,13 +43,7 @@ $settings = json_decode(getenv('MTPROTO_SETTINGS'), true) ?: [];
*/ */
echo 'Loading MadelineProto...'.PHP_EOL; echo 'Loading MadelineProto...'.PHP_EOL;
try { $MadelineProto = new \danog\MadelineProto\API(getcwd().'/testing.madeline', $settings);
$MadelineProto = new \danog\MadelineProto\API(getcwd().'/testing.madeline', $settings);
} catch (\danog\MadelineProto\Exception $e) {
echo $e.PHP_EOL;
unlink(getcwd().'/testing.madeline');
$MadelineProto = new \danog\MadelineProto\API(getcwd().'/testing.madeline', $settings);
}
/* /*
* If this session is not logged in, login * If this session is not logged in, login