diff --git a/src/EventHandler/AbstractMessage.php b/src/EventHandler/AbstractMessage.php index 905d3e294..30da81101 100644 --- a/src/EventHandler/AbstractMessage.php +++ b/src/EventHandler/AbstractMessage.php @@ -80,7 +80,9 @@ abstract class AbstractMessage extends Update implements SimpleFilters $this->mentioned = $rawMessage['mentioned'] ?? false; $this->silent = $rawMessage['silent'] ?? $decryptedMessage['silent']; $this->ttlPeriod = $rawMessage['ttl_period'] ?? $decryptedMessage['ttl'] ?? null; - + if ($this instanceof SecretMessage && isset($decryptedMessage['reply_to_random_id'])) { + $this->replyToMsgId = $decryptedMessage['reply_to_random_id']; + } if (isset($rawMessage['reply_to']) && $rawMessage['reply_to']['_'] === 'messageReplyHeader') { $replyTo = $rawMessage['reply_to']; $this->replyToScheduled = $replyTo['reply_to_scheduled']; diff --git a/src/EventHandler/Media.php b/src/EventHandler/Media.php index d74913ceb..5b49d955a 100644 --- a/src/EventHandler/Media.php +++ b/src/EventHandler/Media.php @@ -61,6 +61,8 @@ abstract class Media extends IpcCapable implements JsonSerializable /** @internal Media location */ public readonly array $location; + /** Encrypted file (only for secret chats). */ + private readonly ?array $file; /** @internal */ public function __construct( @@ -88,6 +90,7 @@ abstract class Media extends IpcCapable implements JsonSerializable $this->creationDate = ($rawMedia['document'] ?? $rawMedia['photo'])['date']; $this->ttl = $rawMedia['ttl_seconds'] ?? null; $this->spoiler = $rawMedia['spoiler'] ?? false; + $this->file = $rawMedia['file']; } /** diff --git a/src/EventHandler/Message.php b/src/EventHandler/Message.php index 253d7dca2..3491b5b58 100644 --- a/src/EventHandler/Message.php +++ b/src/EventHandler/Message.php @@ -116,7 +116,7 @@ abstract class Message extends AbstractMessage $this->forwards = $rawMessage['forwards'] ?? null; $this->signature = $rawMessage['post_author'] ?? null; - $this->entities = MessageEntity::fromRawEntities($rawMessage['entities'] ?? []); + $this->entities = MessageEntity::fromRawEntities($rawMessage['entities'] ?? $decryptedMessage['entities']?? []); $this->message = $rawMessage['message'] ?? $decryptedMessage['message']; $this->fromScheduled = $rawMessage['from_scheduled'] ?? false; $this->viaBotId = $rawMessage['via_bot_id'] ?? $this->getClient()->getIdInternal($rawMessage['via_bot_name']) ?? null; @@ -150,6 +150,7 @@ abstract class Message extends AbstractMessage $this->protected = $this instanceof SecretMessage ? true : $rawMessage['noforwards']; $media = $rawMessage['media'] ?? $decryptedMessage['media'] ?? null; + $media['file'] = $rawMessage['file'] ?? null; $this->media = isset($media) ? $API->wrapMedia($media, $this->protected) : null; diff --git a/src/EventHandler/Message/SecretMessage.php b/src/EventHandler/Message/SecretMessage.php index 0adb7508e..5c9ab7f3a 100644 --- a/src/EventHandler/Message/SecretMessage.php +++ b/src/EventHandler/Message/SecretMessage.php @@ -38,8 +38,7 @@ class SecretMessage extends AbstractPrivateMessage $this->noWebpage = $decryptedMessage['no_webpage'] ?? null; $this->replyToRandomId = $decryptedMessage['reply_to_random_id'] ?? null; } - //TODO implement it using getSecretChat - /*public function getReply(string $class = AbstractMessage::class) : ?AbstractMessage + public function getReply(string $class = AbstractMessage::class): ?AbstractMessage { if ($class !== AbstractMessage::class && !\is_subclass_of($class, AbstractMessage::class)) { throw new AssertionError("A class that extends AbstractMessage was expected."); @@ -53,5 +52,16 @@ class SecretMessage extends AbstractPrivateMessage } return $this->replyCache; } - }*/ + $message = $this->getClient()->getSecretMessage( + chatId: $this->chatId, + randomId: $this->id + ); + /** @psalm-suppress InaccessibleProperty */ + $this->replyCache = $message; + $this->replyCached = true; + if (!$this->replyCache instanceof $class) { + return null; + } + return $this->replyCache; + } } diff --git a/src/EventHandler/Message/Service/SecretChat/ActionDeleteMessages.php b/src/EventHandler/Message/Service/SecretChat/ActionDeleteMessages.php index 0186e3838..f4edc7265 100644 --- a/src/EventHandler/Message/Service/SecretChat/ActionDeleteMessages.php +++ b/src/EventHandler/Message/Service/SecretChat/ActionDeleteMessages.php @@ -24,6 +24,7 @@ use danog\MadelineProto\MTProto; */ class ActionDeleteMessages extends ServiceMessage { + /** @internal */ public function __construct( MTProto $API, array $rawMessage, diff --git a/src/EventHandler/Message/Service/SecretChat/ActionFlushHistory.php b/src/EventHandler/Message/Service/SecretChat/ActionFlushHistory.php index 33e5afb0d..33d0933a6 100644 --- a/src/EventHandler/Message/Service/SecretChat/ActionFlushHistory.php +++ b/src/EventHandler/Message/Service/SecretChat/ActionFlushHistory.php @@ -24,6 +24,7 @@ use danog\MadelineProto\MTProto; */ class ActionFlushHistory extends ServiceMessage { + /** @internal */ public function __construct(MTProto $API, array $rawMessage, array $info) { parent::__construct($API, $rawMessage, $info); diff --git a/src/EventHandler/Message/Service/SecretChat/ActionReadMessages.php b/src/EventHandler/Message/Service/SecretChat/ActionReadMessages.php index 04d681b23..0788278ea 100644 --- a/src/EventHandler/Message/Service/SecretChat/ActionReadMessages.php +++ b/src/EventHandler/Message/Service/SecretChat/ActionReadMessages.php @@ -24,6 +24,7 @@ use danog\MadelineProto\MTProto; */ class ActionReadMessages extends ServiceMessage { + /** @internal */ public function __construct( MTProto $API, array $rawMessage, diff --git a/src/EventHandler/Message/Service/SecretChat/ActionScreenshotMessages.php b/src/EventHandler/Message/Service/SecretChat/ActionScreenshotMessages.php index b6a258b22..4050c0e33 100644 --- a/src/EventHandler/Message/Service/SecretChat/ActionScreenshotMessages.php +++ b/src/EventHandler/Message/Service/SecretChat/ActionScreenshotMessages.php @@ -24,6 +24,7 @@ use danog\MadelineProto\MTProto; */ class ActionScreenshotMessages extends ServiceMessage { + /** @internal */ public function __construct( MTProto $API, array $rawMessage, diff --git a/src/EventHandler/Message/Service/SecretChat/ActionSetMessageTTL.php b/src/EventHandler/Message/Service/SecretChat/ActionSetMessageTTL.php index 5ffe6faad..c14b7b0c9 100644 --- a/src/EventHandler/Message/Service/SecretChat/ActionSetMessageTTL.php +++ b/src/EventHandler/Message/Service/SecretChat/ActionSetMessageTTL.php @@ -27,6 +27,7 @@ use danog\MadelineProto\MTProto; */ class ActionSetMessageTTL extends ServiceMessage { + /** @internal */ public function __construct( MTProto $API, array $rawMessage, diff --git a/src/EventHandler/Message/Service/SecretChat/SecretUserTyping.php b/src/EventHandler/Message/Service/SecretChat/SecretUserTyping.php index 0c520d661..f65dfcfaf 100644 --- a/src/EventHandler/Message/Service/SecretChat/SecretUserTyping.php +++ b/src/EventHandler/Message/Service/SecretChat/SecretUserTyping.php @@ -25,6 +25,7 @@ use danog\MadelineProto\MTProto; */ class SecretUserTyping extends Typing { + /** @internal */ public function __construct( MTProto $API, array $rawMessage, diff --git a/src/InternalDoc.php b/src/InternalDoc.php index e032087ca..701459824 100644 --- a/src/InternalDoc.php +++ b/src/InternalDoc.php @@ -35,6 +35,7 @@ use danog\MadelineProto\EventHandler\Message\Entities\Phone; use danog\MadelineProto\EventHandler\Message\Entities\Pre; use danog\MadelineProto\EventHandler\Message\Entities\Spoiler; use danog\MadelineProto\EventHandler\Message\Entities\Url; +use danog\MadelineProto\EventHandler\Message\SecretMessage; use danog\MadelineProto\EventHandler\Participant\Admin; use danog\MadelineProto\EventHandler\Participant\Member; use danog\MadelineProto\EventHandler\Update; @@ -1027,7 +1028,7 @@ abstract class InternalDoc * @param integer $chatId Secret chat ID. * @param integer $randomId Secret chat message ID. */ - public function getSecretMessage(int $chatId, int $randomId): array + public function getSecretMessage(int $chatId, int $randomId): SecretMessage { return $this->wrapper->getAPI()->getSecretMessage($chatId, $randomId); } diff --git a/src/MTProtoTools/UpdateHandler.php b/src/MTProtoTools/UpdateHandler.php index 4ca20bceb..3c5e10d56 100644 --- a/src/MTProtoTools/UpdateHandler.php +++ b/src/MTProtoTools/UpdateHandler.php @@ -33,6 +33,7 @@ use danog\MadelineProto\EventHandler\Message; use danog\MadelineProto\EventHandler\Message\ChannelMessage; use danog\MadelineProto\EventHandler\Message\GroupMessage; use danog\MadelineProto\EventHandler\Message\PrivateMessage; +use danog\MadelineProto\EventHandler\Message\SecretMessage; use danog\MadelineProto\EventHandler\Message\Service\DialogChannelCreated; use danog\MadelineProto\EventHandler\Message\Service\DialogChannelMigrateFrom; use danog\MadelineProto\EventHandler\Message\Service\DialogChatJoinedByLink; @@ -669,6 +670,7 @@ trait UpdateHandler return null; } return match ($info['type']) { + API::PEER_TYPE_USER && $message['_'] === 'encryptedMessage' => new SecretMessage($this, $message, $info), API::PEER_TYPE_BOT, API::PEER_TYPE_USER => new PrivateMessage($this, $message, $info), API::PEER_TYPE_GROUP, API::PEER_TYPE_SUPERGROUP => new GroupMessage($this, $message, $info), API::PEER_TYPE_CHANNEL => new ChannelMessage($this, $message, $info), diff --git a/src/SecretChats/AuthKeyHandler.php b/src/SecretChats/AuthKeyHandler.php index 6a8d5090b..69e84092d 100644 --- a/src/SecretChats/AuthKeyHandler.php +++ b/src/SecretChats/AuthKeyHandler.php @@ -21,6 +21,7 @@ declare(strict_types=1); namespace danog\MadelineProto\SecretChats; use AssertionError; +use danog\MadelineProto\EventHandler\Message\SecretMessage; use danog\MadelineProto\Logger; use danog\MadelineProto\Loop\Update\UpdateLoop; use danog\MadelineProto\MTProtoTools\Crypt; @@ -157,9 +158,9 @@ trait AuthKeyHandler * @param integer $chatId Secret chat ID. * @param integer $randomId Secret chat message ID. */ - public function getSecretMessage(int $chatId, int $randomId): array + public function getSecretMessage(int $chatId, int $randomId): SecretMessage { - return $this->getSecretChatController($chatId)->getMessage($randomId); + return $this->wrapMessage($this->getSecretChatController($chatId)->getMessage($randomId)['message']); } /**