diff --git a/CHANGELOG.md b/CHANGELOG.md index 52190e529..9dfbbbc45 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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! diff --git a/docs b/docs index 220cc31d4..376e44aad 160000 --- a/docs +++ b/docs @@ -1 +1 @@ -Subproject commit 220cc31d45634acd45f2697d3b7ac519c7fb7f71 +Subproject commit 376e44aad589eaa9263cf6a1135f4ae24da6c882 diff --git a/examples/bot.php b/examples/bot.php index 1acb2d175..1cfebae27 100755 --- a/examples/bot.php +++ b/examples/bot.php @@ -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 ); } diff --git a/src/EventHandler.php b/src/EventHandler.php index e61a4cea1..814089998 100644 --- a/src/EventHandler.php +++ b/src/EventHandler.php @@ -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))) { diff --git a/src/MTProto.php b/src/MTProto.php index d0304d695..f8d8bb3be 100644 --- a/src/MTProto.php +++ b/src/MTProto.php @@ -1571,7 +1571,7 @@ final class MTProto implements TLCallback, LoggerGetter /** * IDs of peers where to report errors. * - * @var array + * @var list */ private array $reportDest = []; /** @@ -1631,7 +1631,7 @@ final class MTProto implements TLCallback, LoggerGetter } } /** @var array $userOrId */ - return $userOrId; + return \array_values($userOrId); } /** * Set peer(s) where to send errors occurred in the event loop. diff --git a/src/MTProtoTools/PeerHandler.php b/src/MTProtoTools/PeerHandler.php index 648ba6b53..84ed08c29 100644 --- a/src/MTProtoTools/PeerHandler.php +++ b/src/MTProtoTools/PeerHandler.php @@ -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', []);