From 36d1ab7ee5c535de6d91a1acfa5ae4f51aec4124 Mon Sep 17 00:00:00 2001 From: Mahdi Date: Tue, 11 Jun 2024 15:09:02 +0330 Subject: [PATCH] Add support for @replies messages - Add CommentReply class - Add FilterCommentReply class - Some changes for chatId ,senderId,messageId (named id here) --- src/EventHandler/AbstractMessage.php | 9 ++--- .../Filter/FilterCommentReply.php | 33 +++++++++++++++++++ src/EventHandler/Message/CommentReply.php | 24 ++++++++++++++ src/EventHandler/Message/GroupMessage.php | 2 +- src/MTProtoTools/UpdateHandler.php | 3 +- 5 files changed, 65 insertions(+), 6 deletions(-) create mode 100644 src/EventHandler/Filter/FilterCommentReply.php create mode 100644 src/EventHandler/Message/CommentReply.php diff --git a/src/EventHandler/AbstractMessage.php b/src/EventHandler/AbstractMessage.php index 6b3f4ea77..eb214f90c 100644 --- a/src/EventHandler/AbstractMessage.php +++ b/src/EventHandler/AbstractMessage.php @@ -93,12 +93,13 @@ abstract class AbstractMessage extends Update implements SimpleFilters } else { $secretChat = null; } + $fromReplies = (($info['User']['username'] ?? '') === 'replies'); $this->out = $rawMessage['out'] ?? false; - $this->id = $rawMessage['id'] ?? $rawMessage['random_id']; - $this->chatId = isset($secretChat) ? $secretChat->chatId : $info['bot_api_id']; - $this->senderId = isset($secretChat) ? $secretChat->otherID : (isset($rawMessage['from_id']) + $this->id = $fromReplies ? $rawMessage['fwd_from']['saved_from_msg_id'] : $rawMessage['id'] ?? $rawMessage['random_id']; + $this->chatId = isset($secretChat) ? $secretChat->chatId : ($fromReplies ? $rawMessage['reply_to']['reply_to_peer_id'] : $info['bot_api_id']); + $this->senderId = isset($secretChat) ? $secretChat->otherID : ($fromReplies ? $this->getClient()->getIdInternal($rawMessage['fwd_from']['from_id']) : (isset($rawMessage['from_id']) ? $this->getClient()->getIdInternal($rawMessage['from_id']) - : $this->chatId); + : $this->chatId)); $this->date = $rawMessage['date']; $this->mentioned = $rawMessage['mentioned'] ?? false; $this->silent = $rawMessage['silent'] ?? false; diff --git a/src/EventHandler/Filter/FilterCommentReply.php b/src/EventHandler/Filter/FilterCommentReply.php new file mode 100644 index 000000000..718677260 --- /dev/null +++ b/src/EventHandler/Filter/FilterCommentReply.php @@ -0,0 +1,33 @@ +. + * + * @author Mahdi + * @copyright 2016-2023 Mahdi + * @license https://opensource.org/licenses/AGPL-3.0 AGPLv3 + * @link https://docs.madelineproto.xyz MadelineProto documentation + */ + +namespace danog\MadelineProto\EventHandler\Filter; + +use Attribute; +use danog\MadelineProto\EventHandler\Message\CommentReply; +use danog\MadelineProto\EventHandler\Update; + +/** + * Allow messages that coming from @replies. + */ +#[Attribute(Attribute::TARGET_METHOD)] +class FilterCommentReply extends Filter +{ + public function apply(Update $update): bool + { + return $update instanceof CommentReply; + } +} diff --git a/src/EventHandler/Message/CommentReply.php b/src/EventHandler/Message/CommentReply.php new file mode 100644 index 000000000..a78dff3f7 --- /dev/null +++ b/src/EventHandler/Message/CommentReply.php @@ -0,0 +1,24 @@ +. + * + * @author Mahdi + * @copyright 2016-2023 Mahdi + * @license https://opensource.org/licenses/AGPL-3.0 AGPLv3 + * @link https://docs.madelineproto.xyz MadelineProto documentation + */ + +namespace danog\MadelineProto\EventHandler\Message; + +/** + * Represents a reply message that in channel comments. + */ +final class CommentReply extends GroupMessage +{ +} diff --git a/src/EventHandler/Message/GroupMessage.php b/src/EventHandler/Message/GroupMessage.php index c3809c01e..511b40ddb 100644 --- a/src/EventHandler/Message/GroupMessage.php +++ b/src/EventHandler/Message/GroupMessage.php @@ -29,7 +29,7 @@ use Webmozart\Assert\InvalidArgumentException; /** * Represents an incoming or outgoing group message. */ -final class GroupMessage extends Message +class GroupMessage extends Message { /** * Get info about a [channel/supergroup](https://core.telegram.org/api/channel) participant. diff --git a/src/MTProtoTools/UpdateHandler.php b/src/MTProtoTools/UpdateHandler.php index 0a55b547a..dd7f1f828 100644 --- a/src/MTProtoTools/UpdateHandler.php +++ b/src/MTProtoTools/UpdateHandler.php @@ -49,6 +49,7 @@ use danog\MadelineProto\EventHandler\Delete\DeleteScheduledMessages; use danog\MadelineProto\EventHandler\InlineQuery; use danog\MadelineProto\EventHandler\Message; use danog\MadelineProto\EventHandler\Message\ChannelMessage; +use danog\MadelineProto\EventHandler\Message\CommentReply; use danog\MadelineProto\EventHandler\Message\GroupMessage; use danog\MadelineProto\EventHandler\Message\PrivateMessage; use danog\MadelineProto\EventHandler\Message\SecretMessage; @@ -778,7 +779,7 @@ trait UpdateHandler }; } if (($info['User']['username'] ?? '') === 'replies') { - return null; + return new CommentReply($this, $message, $info, $scheduled); } if ($message['_'] === 'encryptedMessage') { return new SecretMessage($this, $message, $info, $scheduled);