Get Mystery Box with random crypto!

MadelineProto 5.1 introduces one especially useful feature: na | MadelineProto | Official Channel

MadelineProto 5.1 introduces one especially useful feature: native error reporting.

Native error reporting greatly simplifies the development process of MadelineProto bots.
Simply by specifying the username of the bot admin, all errors raised by the bot (or MadelineProto itself!) are automatically reported to the admin, along with the logfile.

Starting with this version of MadelineProto, doing unlink('MadelineProto.log'); will crash MadelineProto.
If you need to reduce the size of the logfile, change $settings['logger']['max_size'] (minimum 100kb = 100*1000).

Here is an example source for MadelineProto 5.1:


if (!file_exists('madeline.php')) {
copy('https://phar.madelineproto.xyz/madeline.php', 'madeline.php');
}
include 'madeline.php';

use danog\MadelineProto\EventHandler;
use danog\MadelineProto\Tools;
use danog\MadelineProto\API;
use danog\MadelineProto\Logger;
use danog\MadelineProto\RPCErrorException;

/**
* Event handler class.
*/
class MyEventHandler extends EventHandler
{
/**
* @var int|string Username or ID of bot admin
*/
const ADMIN = "danogentili"; // Change this
/**
* Get peer(s) where to report errors
*
* @return int|string|array
*/
public function getReportPeers()
{
return [self::ADMIN];
}
/**
* Called on startup, can contain async calls for initialization of the bot
*
* @return void
*/
public function onStart()
{
}
/**
* Handle updates from supergroups and channels
*
* @param array $update Update
*
* @return void
*/
public function onUpdateNewChannelMessage(array $update): \Generator
{
return $this->onUpdateNewMessage($update);
}
/**
* Handle updates from users.
*
* @param array $update Update
*
* @return \Generator
*/
public function onUpdateNewMessage(array $update): \Generator
{
if ($update['message']['_'] === 'messageEmpty' || $update['message']['out'] ?? false) {
return;
}
$res = \json_encode($update, JSON_PRETTY_PRINT);

try {
yield $this->messages->sendMessage(['peer' => $update, 'message' => "$res", 'reply_to_msg_id' => $update['message']['id'] ?? null, 'parse_mode' => 'HTML']);
if (isset($update['message']['media']) && $update['message']['media']['_'] !== 'messageMediaGame') {
yield $this->messages->sendMedia(['peer' => $update, 'message' => $update['message']['message'], 'media' => $update]);
}
} catch (RPCErrorException $e) {
$this->report("Surfaced: $e");
} catch (Exception $e) {
if (\stripos($e->getMessage(), 'invalid constructor given') === false) {
$this->report("Surfaced: $e");
}
}
}
}

$settings = [];

$MadelineProto = new API('bot.madeline', $settings);
$MadelineProto->startAndLoop(MyEventHandler::class);


The new startAndLoop method automatically initializes MadelineProto, enables async, logs in the user/bot, initializes error reporting, catches and reports all errors surfacing from the event loop to the peers returned by the getReportPeers method.

It also contributes to slashing boilerplate, removing all the $MadelineProto->loop() stuff that cluttered even simple codebases.