1
0
mirror of https://github.com/danog/MadelineProto.git synced 2024-11-30 08:18:59 +01:00

Add support for @admin and simple crons with the Periodic attribute

This commit is contained in:
Daniil Gentili 2023-07-09 18:07:23 +02:00
parent 363058f99d
commit d202ea173e
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
6 changed files with 35 additions and 8 deletions

View File

@ -4,12 +4,15 @@ Features:
- Thanks to the many translation contributors @ https://weblate.madelineproto.xyz/, MadelineProto is now fully localized in Hebrew, Persian, Kurdish, Uzbek and Italian, with WIP translations in Russian and French!
- You can now use `Tools::callFork` to fork a new green thread!
- You can now automatically pin messages broadcasted using `broadcastMessages`, `broadcastForwardMessages` by using the new `pin: true` parameter!
- You can now use `@admin` to send messages to the bot's admin, which will be the first peer returned by `getReportPeers`.
- Added `wrapUpdate`, `wrapMessage`, `wrapMedia`
- Added `Periodic`
- Added plugins, filters, simple filters
- The `waveform` attribute of `Voice` objects is now automatically encoded and decoded to an array of 100 integer values!
- Added a custom PeerNotInDbException class for "This peer is not present in the internal peer database" errors
- Added a `label` property to the Button class, directly indicating the button label (instead of manually fetching it as an array key).
- Added `isForum` method to check whether a given supergroup is a forum
- Added `entitiesToHtml` method to convert a message and a set of Telegram entities to an HTML string!
- Added `wrapUpdate`, `wrapMessage`, `wrapMedia`
- You can now use `reportMemoryProfile()` to generate and send a `pprof` memory profile to all report peers to debug the causes of high memory usage.
- Added support for `pay`, `login_url`, `web_app` and `tg://user?id=` buttons in bot API syntax!

2
docs

@ -1 +1 @@
Subproject commit 220cc31d45634acd45f2697d3b7ac519c7fb7f71
Subproject commit 376e44aad589eaa9263cf6a1135f4ae24da6c882

View File

@ -23,6 +23,7 @@ use danog\MadelineProto\API;
use danog\MadelineProto\Broadcast\Progress;
use danog\MadelineProto\Broadcast\Status;
use danog\MadelineProto\EventHandler;
use danog\MadelineProto\EventHandler\Attributes\Periodic;
use danog\MadelineProto\Logger;
use danog\MadelineProto\Settings;
use danog\MadelineProto\Settings\Database\Mysql;
@ -51,7 +52,7 @@ class MyEventHandler extends EventHandler
/**
* @var int|string Username or ID of bot admin
*/
const ADMIN = "@me"; // !!! Change this to your username !!!
const ADMIN = "@danogentili"; // !!! Change this to your username !!!
private int $adminId;
@ -90,11 +91,27 @@ class MyEventHandler extends EventHandler
return;
}
$this->messages->sendMessage(
peer: self::ADMIN,
peer: '@admin', // You can also use self::ADMIN, which does the same thing
message: "The bot was started!"
);
}
/**
* This function will be executed every 60 seconds.
*/
#[Periodic(period: 60.0)]
public function cron1(): bool
{
$stop = false;
$this->messages->sendMessage(
peer: '@admin',
message: "The bot is online, current time ".date(DATE_RFC850)."!"
);
return $stop;
}
private int $lastLog = 0;
/**
* Handles updates to an in-progress broadcast.
@ -104,7 +121,7 @@ class MyEventHandler extends EventHandler
if (time() - $this->lastLog > 5 || $progress->status === Status::FINISHED) {
$this->lastLog = time();
$this->messages->sendMessage(
peer: self::ADMIN,
peer: 'admin',
message: (string) $progress
);
}

View File

@ -178,13 +178,14 @@ abstract class EventHandler extends AbstractAPI
];
continue;
}
if (!($periodic = $methodRefl->getAttributes(Periodic::class))) {
if ($periodic = $methodRefl->getAttributes(Periodic::class)) {
$periodic = $periodic[0]->newInstance();
$this->periodicLoops[$method] = new PeriodicLoop(
$closure,
$method,
$periodic->period
);
$this->periodicLoops[$method]->start();
continue;
}
if (!($handler = $methodRefl->getAttributes(Handler::class))) {

View File

@ -1571,7 +1571,7 @@ final class MTProto implements TLCallback, LoggerGetter
/**
* IDs of peers where to report errors.
*
* @var array<int>
* @var list<int>
*/
private array $reportDest = [];
/**
@ -1631,7 +1631,7 @@ final class MTProto implements TLCallback, LoggerGetter
}
}
/** @var array<int> $userOrId */
return $userOrId;
return \array_values($userOrId);
}
/**
* Set peer(s) where to send errors occurred in the event loop.

View File

@ -700,6 +700,12 @@ trait PeerHandler
if ($id === 'me') {
return $this->getInfo($this->authorization['user']['id'], $type);
}
if ($id === 'admin') {
if (!isset($this->reportDest[0])) {
throw new Exception("No report peers were configured, can't send a message to the bot admin!");
}
return $this->getInfo($this->reportDest[0], $type);
}
if ($id === 'support') {
if (!$this->supportUser) {
$this->methodCallAsyncRead('help.getSupport', []);