1
0
mirror of https://github.com/danog/MadelineProto.git synced 2024-11-26 19:04:40 +01:00

Add sendMessageToAdmins

This commit is contained in:
Daniil Gentili 2023-07-13 16:40:56 +02:00
parent caecd1ba41
commit be22716329
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
7 changed files with 106 additions and 28 deletions

View File

@ -1,10 +1,10 @@
MadelineProto was updated (8.0.0-beta100)!
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!
- Thanks to the many translation contributors @ https://weblate.madelineproto.xyz/, MadelineProto is now localized in Hebrew, Persian, Kurdish, Uzbek, Russian, French and Italian!
- 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`.
- You can now use `sendMessageToAdmins` to send messages to the bot's admin (the peers returned by `getReportPeers`).
- Added `wrapUpdate`, `wrapMessage`, `wrapMedia`
- Added `Cron`
- Added plugins, filters, simple filters

View File

@ -648,6 +648,8 @@ Want to add your own open-source project to this list? [Click here!](https://doc
* <a href="https://docs.madelineproto.xyz/API_docs/methods/messages.requestWebView.html" name="messages.requestWebView">Open a bot web app, sending over user information after user confirmation: messages.requestWebView</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/phone.receivedCall.html" name="phone.receivedCall">Optional: notify the server that the user is currently busy in a call: this will automatically refuse all incoming phone calls until the current phone call is ended: phone.receivedCall</a>
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#updatesettings-settingsabstract-settings-void" name="updateSettings">Parse, update and store settings: updateSettings</a>
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#validateeventhandlerclass-class-string-eventhandler-class-void" name="validateEventHandlerClass">Perform static analysis on a certain event handler class, to make sure it satisfies some performance requirements: validateEventHandlerClass</a>
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#validateeventhandlercode-string-code-void" name="validateEventHandlerCode">Perform static analysis on a certain event handler class, to make sure it satisfies some performance requirements: validateEventHandlerCode</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/messages.updatePinnedMessage.html" name="messages.updatePinnedMessage">Pin a message: messages.updatePinnedMessage</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/channels.updatePinnedForumTopic.html" name="channels.updatePinnedForumTopic">Pin or unpin forum topics: channels.updatePinnedForumTopic</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/messages.toggleDialogPin.html" name="messages.toggleDialogPin">Pin/unpin a dialog: messages.toggleDialogPin</a>
@ -680,7 +682,7 @@ Want to add your own open-source project to this list? [Click here!](https://doc
* <a href="https://docs.madelineproto.xyz/API_docs/methods/account.reportPeer.html" name="account.reportPeer">Report a peer for violation of telegram's Terms of Service: account.reportPeer</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/account.reportProfilePhoto.html" name="account.reportProfilePhoto">Report a profile photo of a dialog: account.reportProfilePhoto</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/messages.reportEncryptedSpam.html" name="messages.reportEncryptedSpam">Report a secret chat for spam: messages.reportEncryptedSpam</a>
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#report-string-message-string-parsemode-void" name="report">Report an error to the previously set peer: report</a>
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#report-string-message-string-parsemode-bool-sendlogs-void" name="report">Report an error to the previously set peer: report</a>
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#reportmemoryprofile-void" name="reportMemoryProfile">Report memory profile with memprof: reportMemoryProfile</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/channels.reportSpam.html" name="channels.reportSpam">Reports some messages from a user in a supergroup as spam; requires administrator rights in the supergroup: channels.reportSpam</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/messages.getEmojiGroups.html" name="messages.getEmojiGroups">Represents a list of emoji categories, to be used when selecting custom emojis: messages.getEmojiGroups</a>
@ -752,10 +754,13 @@ Want to add your own open-source project to this list? [Click here!](https://doc
* <a href="https://docs.madelineproto.xyz/API_docs/methods/account.acceptAuthorization.html" name="account.acceptAuthorization">Sends a Telegram Passport authorization form, effectively sharing data with the service: account.acceptAuthorization</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/messages.setTyping.html" name="messages.setTyping">Sends a current user typing event (see SendMessageAction for all event types) to a conversation partner or group: messages.setTyping</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/bots.sendCustomRequest.html" name="bots.sendCustomRequest">Sends a custom request; for bots only: bots.sendCustomRequest</a>
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#senddocument-int-string-peer-message-media-localfile-remoteurl-botapifileid-readablestream-file-message-media-localfile-remoteurl-botapifileid-readablestream-null-thumb-string-caption-html-markdown-null-parsemode-callable-callback-string-filename-string-mimetype-int-ttl-bool-spoiler-int-null-replytomsgid-int-null-topmsgid-array-null-replymarkup-int-null-sendas-int-null-scheduledate-bool-silent-bool-noforwards-bool-background-bool-cleardraft-bool-updatestickersetsorder-danog-madelineproto-eventhandler-message" name="sendDocument">Sends a document: sendDocument</a>
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#broadcastmessages-array-messages-danog-madelineproto-broadcast-filter-filter-bool-pin-int" name="broadcastMessages">Sends a list of messages to all peers (users, chats, channels) of the bot: broadcastMessages</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/messages.sendMessage.html" name="messages.sendMessage">Sends a message to a chat: messages.sendMessage</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/messages.sendEncryptedFile.html" name="messages.sendEncryptedFile">Sends a message with a file attachment to a secret chat: messages.sendEncryptedFile</a>
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#sendmessage-int-string-peer-string-message-html-markdown-null-parsemode-int-null-replytomsgid-int-null-topmsgid-array-null-replymarkup-int-null-sendas-int-null-scheduledate-bool-silent-bool-noforwards-bool-background-bool-cleardraft-bool-nowebpage-bool-updatestickersetsorder-danog-madelineproto-eventhandler-message" name="sendMessage">Sends a message: sendMessage</a>
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#sendmessagetoadmins-string-message-html-markdown-null-parsemode-array-null-replymarkup-int-null-scheduledate-bool-silent-bool-noforwards-bool-background-bool-cleardraft-bool-nowebpage-list-message" name="sendMessageToAdmins">Sends a message: sendMessageToAdmins</a>
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#sendphoto-int-string-peer-message-media-localfile-remoteurl-botapifileid-readablestream-file-string-caption-html-markdown-null-parsemode-callable-callback-string-filename-int-ttl-bool-spoiler-int-null-replytomsgid-int-null-topmsgid-array-null-replymarkup-int-null-sendas-int-null-scheduledate-bool-silent-bool-noforwards-bool-background-bool-cleardraft-bool-updatestickersetsorder-danog-madelineproto-eventhandler-message" name="sendPhoto">Sends a photo: sendPhoto</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/messages.sendEncryptedService.html" name="messages.sendEncryptedService">Sends a service message to a secret chat: messages.sendEncryptedService</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/messages.sendEncrypted.html" name="messages.sendEncrypted">Sends a text message to a secret chat: messages.sendEncrypted</a>
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#sendcustomevent-mixed-payload-void" name="sendCustomEvent">Sends an updateCustomEvent update to the event handler: sendCustomEvent</a>
@ -828,7 +833,7 @@ Want to add your own open-source project to this list? [Click here!](https://doc
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#uploadfromcallable-mixed-callable-int-size-string-mime-string-filename-callable-cb-bool-seekable-bool-encrypted-mixed" name="uploadFromCallable">Upload file from callable: uploadFromCallable</a>
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#uploadfromstream-mixed-stream-int-size-string-mime-string-filename-callable-cb-bool-encrypted-mixed" name="uploadFromStream">Upload file from stream: uploadFromStream</a>
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#uploadencrypted-filecallbackinterface-string-array-file-string-filename-callable-cb-mixed" name="uploadEncrypted">Upload file to secret chat: uploadEncrypted</a>
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#upload-filecallbackinterface-localfile-string-array-resource-file-string-filename-callable-cb-bool-encrypted-mixed" name="upload">Upload file: upload</a>
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#upload-filecallbackinterface-localfile-remoteurl-botapifileid-string-array-file-string-filename-callable-cb-bool-encrypted-mixed" name="upload">Upload file: upload</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/account.uploadRingtone.html" name="account.uploadRingtone">Upload notification sound, use account.saveRingtone to convert it and add it to the list of saved notification sounds: account.uploadRingtone</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/account.uploadTheme.html" name="account.uploadTheme">Upload theme: account.uploadTheme</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/messages.setGameScore.html" name="messages.setGameScore">Use this method to set the score of the specified user in a game sent as a normal message (bots only): messages.setGameScore</a>
@ -849,7 +854,7 @@ Want to add your own open-source project to this list? [Click here!](https://doc
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#isipc-bool" name="isIpc">Whether we're an IPC client instance: isIpc</a>
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#isipcworker-bool" name="isIpcWorker">Whether we're an IPC server process (as opposed to an event handler): isIpcWorker</a>
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#wrapmessage-array-message-danog-madelineproto-eventhandler-message" name="wrapMessage">Wrap a Message constructor into an abstract Message object: wrapMessage</a>
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#wrapmedia-array-media-danog-madelineproto-eventhandler-media" name="wrapMedia">Wrap a media constructor into an abstract Media object: wrapMedia</a>
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#wrapmedia-array-media-bool-protected-danog-madelineproto-eventhandler-media" name="wrapMedia">Wrap a media constructor into an abstract Media object: wrapMedia</a>
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#wrapupdate-array-update-danog-madelineproto-eventhandler-update" name="wrapUpdate">Wrap an Update constructor into an abstract Update object: wrapUpdate</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/messages.transcribeAudio.html" name="messages.transcribeAudio">Transcribe voice message: messages.transcribeAudio</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/messages.unpinAllMessages.html" name="messages.unpinAllMessages">Unpin all pinned messages: messages.unpinAllMessages</a>

View File

@ -57,7 +57,7 @@ class MyEventHandler extends SimpleEventHandler
/**
* @var int|string Username or ID of bot admin
*/
const ADMIN = "@danogentili"; // !!! Change this to your username !!!
const ADMIN = "@me"; // !!! Change this to your username !!!
private int $adminId;
@ -92,13 +92,7 @@ class MyEventHandler extends SimpleEventHandler
$this->logger($this->getFullInfo('MadelineProto'));
$this->adminId = $this->getId(self::ADMIN);
if ($this->getSelf()['bot'] && $this->getSelf()['id'] === $this->adminId) {
return;
}
$this->messages->sendMessage(
peer: '@admin', // You can also use self::ADMIN, which does the same thing
message: "The bot was started!"
);
$this->sendMessageToAdmins("The bot was started!");
}
/**
@ -107,10 +101,7 @@ class MyEventHandler extends SimpleEventHandler
#[Cron(period: 60.0)]
public function cron1(): void
{
$this->messages->sendMessage(
peer: '@admin',
message: "The bot is online, current time ".date(DATE_RFC850)."!"
);
$this->sendMessageToAdmins("The bot is online, current time ".date(DATE_RFC850)."!");
}
private int $lastLog = 0;
@ -121,10 +112,7 @@ class MyEventHandler extends SimpleEventHandler
{
if (time() - $this->lastLog > 5 || $progress->status === Status::FINISHED) {
$this->lastLog = time();
$this->messages->sendMessage(
peer: 'admin',
message: (string) $progress
);
$this->sendMessageToAdmins((string) $progress);
}
}

View File

@ -329,7 +329,7 @@ abstract class EventHandler extends AbstractAPI
*/
private static function internalGetDirectoryPlugins(string $class): array
{
if (is_subclass_of($class, PluginEventHandler::class)) {
if (\is_subclass_of($class, PluginEventHandler::class)) {
return [];
}

View File

@ -1399,10 +1399,11 @@ abstract class InternalDoc
*
* @param string $message Error to report
* @param string $parseMode Parse mode
* @param bool $sendLogs Whether to also send logs
*/
public function report(string $message, string $parseMode = ''): void
public function report(string $message, string $parseMode = '', bool $sendLogs = true): void
{
$this->wrapper->getAPI()->report($message, $parseMode);
$this->wrapper->getAPI()->report($message, $parseMode, $sendLogs);
}
/**
* Report memory profile with memprof.
@ -1535,7 +1536,25 @@ abstract class InternalDoc
return $this->wrapper->getAPI()->sendMessage($peer, $message, $parseMode, $replyToMsgId, $topMsgId, $replyMarkup, $sendAs, $scheduleDate, $silent, $noForwards, $background, $clearDraft, $noWebpage, $updateStickersetsOrder);
}
/**
* Sends a document.
* Sends a message.
*
* @param string $message Message to send
* @param "html"|"markdown"|null $parseMode Parse mode
* @param array|null $replyMarkup Keyboard information.
* @param integer|null $scheduleDate Schedule date.
* @param boolean $silent Whether to send the message silently, without triggering notifications.
* @param boolean $background Send this message as background message
* @param boolean $clearDraft Clears the draft field
* @param boolean $noWebpage Set this flag to disable generation of the webpage preview
*
* @return list<Message>
*/
public function sendMessageToAdmins(string $message, ?string $parseMode = null, ?array $replyMarkup = null, ?int $scheduleDate = null, bool $silent = false, bool $noForwards = false, bool $background = false, bool $clearDraft = false, bool $noWebpage = false): array
{
return $this->wrapper->getAPI()->sendMessageToAdmins($message, $parseMode, $replyMarkup, $scheduleDate, $silent, $noForwards, $background, $clearDraft, $noWebpage);
}
/**
* Sends a photo.
*
* Please use named arguments to call this method.
*
@ -1891,6 +1910,28 @@ abstract class InternalDoc
{
return $this->wrapper->getAPI()->uploadFromUrl($url, $size, $fileName, $cb, $encrypted);
}
/**
* Perform static analysis on a certain event handler class, to make sure it satisfies some performance requirements.
*
* @param class-string<EventHandler> $class Class name
*
* @throws AssertionError If validation fails.
*/
public static function validateEventHandlerClass(string $class): void
{
\danog\MadelineProto\Tools::validateEventHandlerClass($class);
}
/**
* Perform static analysis on a certain event handler class, to make sure it satisfies some performance requirements.
*
* @param string $code Code of the class.
*
* @throws AssertionError If validation fails.
*/
public static function validateEventHandlerCode(string $code): void
{
\danog\MadelineProto\Tools::validateEventHandlerCode($code);
}
/**
* Mark sponsored message as read.
*

View File

@ -1679,13 +1679,56 @@ final class MTProto implements TLCallback, LoggerGetter
}
}
private ?LocalMutex $reportMutex = null;
/**
* Sends a message to all report peers (admins of the bot).
*
* @param string $message Message to send
* @param "html"|"markdown"|null $parseMode Parse mode
* @param array|null $replyMarkup Keyboard information.
* @param integer|null $scheduleDate Schedule date.
* @param boolean $silent Whether to send the message silently, without triggering notifications.
* @param boolean $background Send this message as background message
* @param boolean $clearDraft Clears the draft field
* @param boolean $noWebpage Set this flag to disable generation of the webpage preview
*
* @return list<Message>
*/
public function sendMessageToAdmins(
string $message,
?string $parseMode = null,
?array $replyMarkup = null,
?int $scheduleDate = null,
bool $silent = false,
bool $noForwards = false,
bool $background = false,
bool $clearDraft = false,
bool $noWebpage = false,
): array {
$result = [];
foreach ($this->reportDest as $report) {
$result []= $this->sendMessage(
peer: $report,
message: $message,
parseMode: $parseMode,
replyMarkup: $replyMarkup,
scheduleDate: $scheduleDate,
silent: $silent,
noForwards: $noForwards,
background: $background,
clearDraft: $clearDraft,
noWebpage: $noWebpage
);
}
return $result;
}
/**
* Report an error to the previously set peer.
*
* @param string $message Error to report
* @param string $parseMode Parse mode
* @param bool $sendLogs Whether to also send logs
*/
public function report(string $message, string $parseMode = ''): void
public function report(string $message, string $parseMode = '', bool $sendLogs = true): void
{
if (!$this->reportDest) {
return;
@ -1694,7 +1737,8 @@ final class MTProto implements TLCallback, LoggerGetter
$lock = $this->reportMutex->acquire();
try {
$file = null;
if ($this->settings->getLogger()->getType() === Logger::FILE_LOGGER
if ($sendLogs
&& $this->settings->getLogger()->getType() === Logger::FILE_LOGGER
&& $path = $this->settings->getLogger()->getExtra()) {
$temp = \tempnam(\sys_get_temp_dir(), 'madelinelog');
\copy($path, $temp);

View File

@ -202,7 +202,7 @@ trait FilesAbstraction
);
}
/**
* Sends a document.
* Sends a photo.
*
* Please use named arguments to call this method.
*