From c944942b21698f5f1897b0d360a502b38a0a4c77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D9=8EAhJ?= Date: Tue, 8 Aug 2023 19:42:29 +0330 Subject: [PATCH 01/21] Add Inline Class --- src/EventHandler/InlineQuery.php | 51 ++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/EventHandler/InlineQuery.php diff --git a/src/EventHandler/InlineQuery.php b/src/EventHandler/InlineQuery.php new file mode 100644 index 000000000..b3490df1b --- /dev/null +++ b/src/EventHandler/InlineQuery.php @@ -0,0 +1,51 @@ + Regex matches, if a filter regex is present + */ + public ?array $matches = null; + + /** @internal */ + public function __construct(MTProto $API, array $rawInlineQuery) + { + parent::__construct($API); + $this->queryId = $rawInlineQuery['query_id']; + $this->query = $rawInlineQuery['query']; + $this->userId = $rawInlineQuery['user_id']; + $this->offset = $rawInlineQuery['offset']; + $this->geo = isset($rawInlineQuery['geo']) ? new GeoPoint($rawInlineQuery['geo']) : null; + $this->peerType = match ($rawInlineQuery['peerType']['_']) { + 'inlineQueryPeerTypeBotPM' => new InlineQueryPeerType\InlineQueryPeerTypeBotPM, + 'inlineQueryPeerTypeBroadcast' => new InlineQueryPeerType\InlineQueryPeerTypeBroadcast, + 'inlineQueryPeerTypeChat' => new InlineQueryPeerType\InlineQueryPeerTypeChat, + 'inlineQueryPeerTypeMegagroup' => new InlineQueryPeerType\InlineQueryPeerTypeMegagroup, + 'inlineQueryPeerTypePM' => new InlineQueryPeerType\InlineQueryPeerTypePM, + 'inlineQueryPeerTypeSameBotPM' => new InlineQueryPeerType\InlineQueryPeerTypeSameBotPM, + }; + } +} \ No newline at end of file From 7399c19f997568cbda9999328dc7804fb7a3c752 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D9=8EAhJ?= Date: Tue, 8 Aug 2023 19:42:48 +0330 Subject: [PATCH 02/21] Add Inline Query Peer Types --- src/EventHandler/InlineQueryPeerType.php | 14 ++++++++++++++ .../InlineQueryPeerTypeBotPM.php | 10 ++++++++++ .../InlineQueryPeerTypeBroadcast.php | 10 ++++++++++ .../InlineQueryPeerTypeChat.php | 10 ++++++++++ .../InlineQueryPeerTypeMegagroup.php | 10 ++++++++++ .../InlineQueryPeerType/InlineQueryPeerTypePM.php | 10 ++++++++++ .../InlineQueryPeerTypeSameBotPM.php | 10 ++++++++++ 7 files changed, 74 insertions(+) create mode 100644 src/EventHandler/InlineQueryPeerType.php create mode 100644 src/EventHandler/InlineQueryPeerType/InlineQueryPeerTypeBotPM.php create mode 100644 src/EventHandler/InlineQueryPeerType/InlineQueryPeerTypeBroadcast.php create mode 100644 src/EventHandler/InlineQueryPeerType/InlineQueryPeerTypeChat.php create mode 100644 src/EventHandler/InlineQueryPeerType/InlineQueryPeerTypeMegagroup.php create mode 100644 src/EventHandler/InlineQueryPeerType/InlineQueryPeerTypePM.php create mode 100644 src/EventHandler/InlineQueryPeerType/InlineQueryPeerTypeSameBotPM.php diff --git a/src/EventHandler/InlineQueryPeerType.php b/src/EventHandler/InlineQueryPeerType.php new file mode 100644 index 000000000..b354ac433 --- /dev/null +++ b/src/EventHandler/InlineQueryPeerType.php @@ -0,0 +1,14 @@ + static::class]; + } +} \ No newline at end of file diff --git a/src/EventHandler/InlineQueryPeerType/InlineQueryPeerTypeBotPM.php b/src/EventHandler/InlineQueryPeerType/InlineQueryPeerTypeBotPM.php new file mode 100644 index 000000000..73ddac480 --- /dev/null +++ b/src/EventHandler/InlineQueryPeerType/InlineQueryPeerTypeBotPM.php @@ -0,0 +1,10 @@ + Date: Tue, 8 Aug 2023 19:43:01 +0330 Subject: [PATCH 03/21] Add GeoPoint class --- src/EventHandler/Media/GeoPoint.php | 33 +++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/EventHandler/Media/GeoPoint.php diff --git a/src/EventHandler/Media/GeoPoint.php b/src/EventHandler/Media/GeoPoint.php new file mode 100644 index 000000000..b715f0fe7 --- /dev/null +++ b/src/EventHandler/Media/GeoPoint.php @@ -0,0 +1,33 @@ +long = $rawGeoPoint['long']; + $this->lat = $rawGeoPoint['lat']; + $this->accessHash = $rawGeoPoint['accessHash']; + $this->accuracyRadius = $rawGeoPoint['accuracyRadius'] ?? null; + } + + /** @internal */ + public function jsonSerialize(): mixed + { + $v = \get_object_vars($this); + $v['_'] = static::class; + return $v; + } +} \ No newline at end of file From b5f9a69d8bba76e5485f523bfad3caae81dedf8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D9=8EAhJ?= Date: Tue, 8 Aug 2023 19:43:28 +0330 Subject: [PATCH 04/21] Admit InlineQuery with wrapUpdate method --- src/MTProtoTools/UpdateHandler.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/MTProtoTools/UpdateHandler.php b/src/MTProtoTools/UpdateHandler.php index d8541340e..331a4815f 100644 --- a/src/MTProtoTools/UpdateHandler.php +++ b/src/MTProtoTools/UpdateHandler.php @@ -27,6 +27,7 @@ use Amp\Http\Client\Response; use Amp\TimeoutException; use danog\MadelineProto\API; use danog\MadelineProto\EventHandler\AbstractMessage; +use danog\MadelineProto\EventHandler\InlineQuery; use danog\MadelineProto\EventHandler\Message; use danog\MadelineProto\EventHandler\Message\ChannelMessage; use danog\MadelineProto\EventHandler\Message\GroupMessage; @@ -344,6 +345,7 @@ trait UpdateHandler 'updateInlineBotCallbackQuery' => isset($update['game_short_name']) ? new InlineGameQuery($this, $update) : new InlineButtonQuery($this, $update), + 'updateBotInlineQuery' => new InlineQuery($this, $update), default => null }; } From d5093e2b6f54d9fc70f650a1f12de25965cf143b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D9=8EAhJ?= Date: Tue, 8 Aug 2023 19:43:44 +0330 Subject: [PATCH 05/21] admit Filters with InlineQuery class --- src/EventHandler/Filter/FilterFromAdmin.php | 4 +++- src/EventHandler/Filter/FilterRegex.php | 6 ++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/EventHandler/Filter/FilterFromAdmin.php b/src/EventHandler/Filter/FilterFromAdmin.php index c1edbe3f7..2d75485bd 100644 --- a/src/EventHandler/Filter/FilterFromAdmin.php +++ b/src/EventHandler/Filter/FilterFromAdmin.php @@ -6,6 +6,7 @@ use Attribute; use danog\MadelineProto\EventHandler; use danog\MadelineProto\EventHandler\AbstractMessage; use danog\MadelineProto\EventHandler\Query\ButtonQuery; +use danog\MadelineProto\EventHandler\InlineQuery; use danog\MadelineProto\EventHandler\Update; /** @@ -24,6 +25,7 @@ final class FilterFromAdmin extends Filter public function apply(Update $update): bool { return ($update instanceof AbstractMessage && \in_array($update->senderId, $this->adminIds, true)) || - ($update instanceof ButtonQuery && \in_array($update->userId, $this->adminIds, true)); + ($update instanceof ButtonQuery && \in_array($update->userId, $this->adminIds, true)) || + ($update instanceof InlineQuery && \in_array($update->userId, $this->adminIds, true)); } } diff --git a/src/EventHandler/Filter/FilterRegex.php b/src/EventHandler/Filter/FilterRegex.php index a7ca76148..c285643ac 100644 --- a/src/EventHandler/Filter/FilterRegex.php +++ b/src/EventHandler/Filter/FilterRegex.php @@ -5,6 +5,7 @@ namespace danog\MadelineProto\EventHandler\Filter; use Attribute; use danog\MadelineProto\EventHandler\Message; use danog\MadelineProto\EventHandler\Query\ButtonQuery; +use danog\MadelineProto\EventHandler\InlineQuery; use danog\MadelineProto\EventHandler\Update; use Webmozart\Assert\Assert; @@ -32,6 +33,11 @@ final class FilterRegex extends Filter $update->matches = $matches; return true; } + if ($update instanceof InlineQuery && \preg_match($this->regex, $update->query, $matches)) { + /** @psalm-suppress InaccessibleProperty */ + $update->matches = $matches; + return true; + } return false; } } From 94cc11410fe764a9f10b3de4c6e006b587dddc38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D9=8EAhJ?= Date: Tue, 29 Aug 2023 11:42:16 +0330 Subject: [PATCH 06/21] Convert InlineQueryPeerType to enum --- src/EventHandler/InlineQueryPeerType.php | 36 +++++++++++++++---- .../InlineQueryPeerTypeBotPM.php | 10 ------ .../InlineQueryPeerTypeBroadcast.php | 10 ------ .../InlineQueryPeerTypeChat.php | 10 ------ .../InlineQueryPeerTypeMegagroup.php | 10 ------ .../InlineQueryPeerTypePM.php | 10 ------ .../InlineQueryPeerTypeSameBotPM.php | 10 ------ 7 files changed, 30 insertions(+), 66 deletions(-) delete mode 100644 src/EventHandler/InlineQueryPeerType/InlineQueryPeerTypeBotPM.php delete mode 100644 src/EventHandler/InlineQueryPeerType/InlineQueryPeerTypeBroadcast.php delete mode 100644 src/EventHandler/InlineQueryPeerType/InlineQueryPeerTypeChat.php delete mode 100644 src/EventHandler/InlineQueryPeerType/InlineQueryPeerTypeMegagroup.php delete mode 100644 src/EventHandler/InlineQueryPeerType/InlineQueryPeerTypePM.php delete mode 100644 src/EventHandler/InlineQueryPeerType/InlineQueryPeerTypeSameBotPM.php diff --git a/src/EventHandler/InlineQueryPeerType.php b/src/EventHandler/InlineQueryPeerType.php index b354ac433..a6b9e4b5f 100644 --- a/src/EventHandler/InlineQueryPeerType.php +++ b/src/EventHandler/InlineQueryPeerType.php @@ -2,13 +2,37 @@ namespace danog\MadelineProto\EventHandler; -use JsonSerializable; - -abstract class InlineQueryPeerType implements JsonSerializable +/** + * Inline query peer type + */ +enum InlineQueryPeerType { - /** @internal */ - public function jsonSerialize(): mixed + /** private chat */ + case InlineQueryPeerTypePM; + /** [chat](https://core.telegram.org/api/channel) */ + case InlineQueryPeerTypeChat; + /** private chat with a bot. */ + case InlineQueryPeerTypeBotPM; + /** [channel](https://core.telegram.org/api/channel) */ + case InlineQueryPeerTypeBroadcast; + /** [supergroup](https://core.telegram.org/api/channel) */ + case InlineQueryPeerTypeMegagroup; + /** private chat with the bot itself */ + case InlineQueryPeerTypeSameBotPM; + + /** + * Get InlineQueryPeerType from update + * + * @param string Type of the chat from which the inline query was sent. + */ + public static function fromString(string $name): ?InlineQueryPeerType { - return ['_' => static::class]; + $name = ucfirst($name); + foreach(InlineQueryPeerType::cases() as $status) + { + if($status->name === $name) + return $status; + } + return null; } } \ No newline at end of file diff --git a/src/EventHandler/InlineQueryPeerType/InlineQueryPeerTypeBotPM.php b/src/EventHandler/InlineQueryPeerType/InlineQueryPeerTypeBotPM.php deleted file mode 100644 index 73ddac480..000000000 --- a/src/EventHandler/InlineQueryPeerType/InlineQueryPeerTypeBotPM.php +++ /dev/null @@ -1,10 +0,0 @@ - Date: Tue, 29 Aug 2023 11:42:38 +0330 Subject: [PATCH 07/21] Handle peerType with static enum method --- src/EventHandler/InlineQuery.php | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/EventHandler/InlineQuery.php b/src/EventHandler/InlineQuery.php index b3490df1b..4dbd0d567 100644 --- a/src/EventHandler/InlineQuery.php +++ b/src/EventHandler/InlineQuery.php @@ -39,13 +39,6 @@ final class InlineQuery extends Update $this->userId = $rawInlineQuery['user_id']; $this->offset = $rawInlineQuery['offset']; $this->geo = isset($rawInlineQuery['geo']) ? new GeoPoint($rawInlineQuery['geo']) : null; - $this->peerType = match ($rawInlineQuery['peerType']['_']) { - 'inlineQueryPeerTypeBotPM' => new InlineQueryPeerType\InlineQueryPeerTypeBotPM, - 'inlineQueryPeerTypeBroadcast' => new InlineQueryPeerType\InlineQueryPeerTypeBroadcast, - 'inlineQueryPeerTypeChat' => new InlineQueryPeerType\InlineQueryPeerTypeChat, - 'inlineQueryPeerTypeMegagroup' => new InlineQueryPeerType\InlineQueryPeerTypeMegagroup, - 'inlineQueryPeerTypePM' => new InlineQueryPeerType\InlineQueryPeerTypePM, - 'inlineQueryPeerTypeSameBotPM' => new InlineQueryPeerType\InlineQueryPeerTypeSameBotPM, - }; + $this->peerType = InlineQueryPeerType::fromString($rawInlineQuery['peerType']['_']); } } \ No newline at end of file From 6829add3c3d95d162b764671c398593551909643 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D9=8EAhJ?= Date: Tue, 29 Aug 2023 11:42:47 +0330 Subject: [PATCH 08/21] fix some issue --- src/EventHandler/Media/GeoPoint.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/EventHandler/Media/GeoPoint.php b/src/EventHandler/Media/GeoPoint.php index b715f0fe7..cc695cf91 100644 --- a/src/EventHandler/Media/GeoPoint.php +++ b/src/EventHandler/Media/GeoPoint.php @@ -19,8 +19,8 @@ final class GeoPoint implements JsonSerializable { $this->long = $rawGeoPoint['long']; $this->lat = $rawGeoPoint['lat']; - $this->accessHash = $rawGeoPoint['accessHash']; - $this->accuracyRadius = $rawGeoPoint['accuracyRadius'] ?? null; + $this->accessHash = $rawGeoPoint['access_hash']; + $this->accuracyRadius = $rawGeoPoint['accuracy_radius'] ?? null; } /** @internal */ From 255f9ffcdb3ee1b60038a7df144dbdde02030467 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D9=8EAhJ?= Date: Tue, 29 Aug 2023 12:25:53 +0330 Subject: [PATCH 09/21] some refactor for InlineQueryPeerType enum & fromString method --- src/EventHandler/InlineQueryPeerType.php | 27 ++++++++++++------------ 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/src/EventHandler/InlineQueryPeerType.php b/src/EventHandler/InlineQueryPeerType.php index a6b9e4b5f..ae2b9c1d9 100644 --- a/src/EventHandler/InlineQueryPeerType.php +++ b/src/EventHandler/InlineQueryPeerType.php @@ -2,37 +2,36 @@ namespace danog\MadelineProto\EventHandler; +use AssertionError; + /** * Inline query peer type */ enum InlineQueryPeerType { /** private chat */ - case InlineQueryPeerTypePM; + case PM; /** [chat](https://core.telegram.org/api/channel) */ - case InlineQueryPeerTypeChat; + case Chat; /** private chat with a bot. */ - case InlineQueryPeerTypeBotPM; + case BotPM; /** [channel](https://core.telegram.org/api/channel) */ - case InlineQueryPeerTypeBroadcast; + case Broadcast; /** [supergroup](https://core.telegram.org/api/channel) */ - case InlineQueryPeerTypeMegagroup; + case Megagroup; /** private chat with the bot itself */ - case InlineQueryPeerTypeSameBotPM; + case SameBotPM; /** * Get InlineQueryPeerType from update * * @param string Type of the chat from which the inline query was sent. + * @throws AssertionError */ - public static function fromString(string $name): ?InlineQueryPeerType + public static function fromString(string $name): InlineQueryPeerType { - $name = ucfirst($name); - foreach(InlineQueryPeerType::cases() as $status) - { - if($status->name === $name) - return $status; - } - return null; + $name = ltrim($name, "inlineQueryPeerType"); + return (InlineQueryPeerType::cases())[$name] + ?? throw new AssertionError("Undefined case InlineQueryPeerType::".$name); } } \ No newline at end of file From 05c5875bb4957770d9559aa88329e398a12ca0b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D9=8EAhJ?= Date: Tue, 29 Aug 2023 12:36:54 +0330 Subject: [PATCH 10/21] fix fromString method --- src/EventHandler/InlineQueryPeerType.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/EventHandler/InlineQueryPeerType.php b/src/EventHandler/InlineQueryPeerType.php index ae2b9c1d9..f28739447 100644 --- a/src/EventHandler/InlineQueryPeerType.php +++ b/src/EventHandler/InlineQueryPeerType.php @@ -28,10 +28,13 @@ enum InlineQueryPeerType * @param string Type of the chat from which the inline query was sent. * @throws AssertionError */ - public static function fromString(string $name): InlineQueryPeerType + public static function fromString(string $name) { - $name = ltrim($name, "inlineQueryPeerType"); - return (InlineQueryPeerType::cases())[$name] - ?? throw new AssertionError("Undefined case InlineQueryPeerType::".$name); + $newName = ltrim($name, "inlineQueryPeerType"); + foreach (InlineQueryPeerType::cases() as $case) { + if ($case->name === $newName) + return $case; + } + throw new AssertionError("Undefined case InlineQueryPeerType::".$name); } } \ No newline at end of file From e3af33c8e0a7548ce5c0f69628a6f4f200a87277 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D9=8EAhJ?= Date: Tue, 29 Aug 2023 12:49:04 +0330 Subject: [PATCH 11/21] implement InlineQueryPeerType from JsonSerializable --- src/EventHandler/InlineQueryPeerType.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/EventHandler/InlineQueryPeerType.php b/src/EventHandler/InlineQueryPeerType.php index f28739447..3bb874370 100644 --- a/src/EventHandler/InlineQueryPeerType.php +++ b/src/EventHandler/InlineQueryPeerType.php @@ -3,11 +3,12 @@ namespace danog\MadelineProto\EventHandler; use AssertionError; +use JsonSerializable; /** * Inline query peer type */ -enum InlineQueryPeerType +enum InlineQueryPeerType implements JsonSerializable { /** private chat */ case PM; @@ -37,4 +38,10 @@ enum InlineQueryPeerType } throw new AssertionError("Undefined case InlineQueryPeerType::".$name); } + + /** @internal */ + public function jsonSerialize(): mixed + { + return $this->name; + } } \ No newline at end of file From 14699d1d52485683f1657d50a1652eda938157cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D9=8EAhJ?= Date: Tue, 29 Aug 2023 12:51:14 +0330 Subject: [PATCH 12/21] add return type fromString method --- src/EventHandler/InlineQueryPeerType.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/EventHandler/InlineQueryPeerType.php b/src/EventHandler/InlineQueryPeerType.php index 3bb874370..77c57c20f 100644 --- a/src/EventHandler/InlineQueryPeerType.php +++ b/src/EventHandler/InlineQueryPeerType.php @@ -29,7 +29,7 @@ enum InlineQueryPeerType implements JsonSerializable * @param string Type of the chat from which the inline query was sent. * @throws AssertionError */ - public static function fromString(string $name) + public static function fromString(string $name): InlineQueryPeerType { $newName = ltrim($name, "inlineQueryPeerType"); foreach (InlineQueryPeerType::cases() as $case) { From 9bf39b55eb2e95a022c1fa7f601bf86bd227da9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D9=8EAhJ?= Date: Tue, 29 Aug 2023 12:52:19 +0330 Subject: [PATCH 13/21] change return type jsonSerialize to string --- src/EventHandler/InlineQueryPeerType.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/EventHandler/InlineQueryPeerType.php b/src/EventHandler/InlineQueryPeerType.php index 77c57c20f..b03b0a907 100644 --- a/src/EventHandler/InlineQueryPeerType.php +++ b/src/EventHandler/InlineQueryPeerType.php @@ -40,7 +40,7 @@ enum InlineQueryPeerType implements JsonSerializable } /** @internal */ - public function jsonSerialize(): mixed + public function jsonSerialize(): string { return $this->name; } From a06d636b221db8719c8a49d4e57149e01152a063 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D9=8EAhJ?= Date: Tue, 29 Aug 2023 13:11:44 +0330 Subject: [PATCH 14/21] change peer_type to peerType --- src/EventHandler/InlineQuery.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/EventHandler/InlineQuery.php b/src/EventHandler/InlineQuery.php index 4dbd0d567..92a428ae4 100644 --- a/src/EventHandler/InlineQuery.php +++ b/src/EventHandler/InlineQuery.php @@ -39,6 +39,6 @@ final class InlineQuery extends Update $this->userId = $rawInlineQuery['user_id']; $this->offset = $rawInlineQuery['offset']; $this->geo = isset($rawInlineQuery['geo']) ? new GeoPoint($rawInlineQuery['geo']) : null; - $this->peerType = InlineQueryPeerType::fromString($rawInlineQuery['peerType']['_']); + $this->peerType = InlineQueryPeerType::fromString($rawInlineQuery['peer_type']['_']); } } \ No newline at end of file From 9e8cf2cf7cea4ef0f3f42fa36942e5ac693d7739 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D9=8EAhJ?= Date: Tue, 29 Aug 2023 13:12:03 +0330 Subject: [PATCH 15/21] fix $name paramter --- src/EventHandler/InlineQueryPeerType.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/EventHandler/InlineQueryPeerType.php b/src/EventHandler/InlineQueryPeerType.php index b03b0a907..5048ac517 100644 --- a/src/EventHandler/InlineQueryPeerType.php +++ b/src/EventHandler/InlineQueryPeerType.php @@ -31,7 +31,7 @@ enum InlineQueryPeerType implements JsonSerializable */ public static function fromString(string $name): InlineQueryPeerType { - $newName = ltrim($name, "inlineQueryPeerType"); + $newName = substr($name, 19); foreach (InlineQueryPeerType::cases() as $case) { if ($case->name === $newName) return $case; From 53cfd28ad0ea3ffcdc1111102fa047eb77d0c8f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D9=8EAhJ?= Date: Tue, 29 Aug 2023 13:29:24 +0330 Subject: [PATCH 16/21] Add supporting InlineQuery & ButtonQuery for Sender Filters --- src/EventHandler/Filter/FilterFromSender.php | 6 +++++- src/EventHandler/Filter/FilterFromSenders.php | 6 +++++- src/EventHandler/Filter/FilterSender.php | 6 +++++- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/EventHandler/Filter/FilterFromSender.php b/src/EventHandler/Filter/FilterFromSender.php index 86bb6ced8..d0fce9902 100644 --- a/src/EventHandler/Filter/FilterFromSender.php +++ b/src/EventHandler/Filter/FilterFromSender.php @@ -19,6 +19,8 @@ namespace danog\MadelineProto\EventHandler\Filter; use Attribute; use danog\MadelineProto\EventHandler; use danog\MadelineProto\EventHandler\Message\GroupMessage; +use danog\MadelineProto\EventHandler\Query\ButtonQuery; +use danog\MadelineProto\EventHandler\InlineQuery; use danog\MadelineProto\EventHandler\Update; /** @@ -39,6 +41,8 @@ final class FilterFromSender extends Filter } public function apply(Update $update): bool { - return $update instanceof GroupMessage && $update->senderId === $this->peerResolved; + return ($update instanceof GroupMessage && $update->senderId === $this->peerResolved) || + ($update instanceof ButtonQuery && $update->userId === $this->peerResolved) || + ($update instanceof InlineQuery && $update->userId === $this->peerResolved); } } diff --git a/src/EventHandler/Filter/FilterFromSenders.php b/src/EventHandler/Filter/FilterFromSenders.php index e2440249f..cb21a2444 100644 --- a/src/EventHandler/Filter/FilterFromSenders.php +++ b/src/EventHandler/Filter/FilterFromSenders.php @@ -19,6 +19,8 @@ namespace danog\MadelineProto\EventHandler\Filter; use Attribute; use danog\MadelineProto\EventHandler; use danog\MadelineProto\EventHandler\Message\GroupMessage; +use danog\MadelineProto\EventHandler\Query\ButtonQuery; +use danog\MadelineProto\EventHandler\InlineQuery; use danog\MadelineProto\EventHandler\Update; /** @@ -50,6 +52,8 @@ final class FilterFromSenders extends Filter } public function apply(Update $update): bool { - return $update instanceof GroupMessage && \in_array($update->senderId, $this->peersResolved, true); + return ($update instanceof GroupMessage && \in_array($update->senderId, $this->peersResolved, true)) || + ($update instanceof ButtonQuery && \in_array($update->userId, $this->peersResolved, true)) || + ($update instanceof InlineQuery && \in_array($update->userId, $this->peersResolved, true)); } } diff --git a/src/EventHandler/Filter/FilterSender.php b/src/EventHandler/Filter/FilterSender.php index 1d052d678..610a55a67 100644 --- a/src/EventHandler/Filter/FilterSender.php +++ b/src/EventHandler/Filter/FilterSender.php @@ -19,6 +19,8 @@ namespace danog\MadelineProto\EventHandler\Filter; use Attribute; use danog\MadelineProto\EventHandler; use danog\MadelineProto\EventHandler\Message\GroupMessage; +use danog\MadelineProto\EventHandler\Query\ButtonQuery; +use danog\MadelineProto\EventHandler\InlineQuery; use danog\MadelineProto\EventHandler\Update; /** @@ -39,6 +41,8 @@ final class FilterSender extends Filter } public function apply(Update $update): bool { - return $update instanceof GroupMessage && $update->senderId === $this->peerResolved; + return ($update instanceof GroupMessage && $update->senderId === $this->peerResolved) || + ($update instanceof ButtonQuery && $update->userId === $this->peerResolved) || + ($update instanceof InlineQuery && $update->userId === $this->peerResolved); } } From acf802a99c4df495d4ba16b631bcc178186504dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D9=8EAhJ?= Date: Tue, 29 Aug 2023 14:19:06 +0330 Subject: [PATCH 17/21] Change FilterSender for AbstractMessage --- src/EventHandler/Filter/FilterSender.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/EventHandler/Filter/FilterSender.php b/src/EventHandler/Filter/FilterSender.php index 610a55a67..d61ebeb00 100644 --- a/src/EventHandler/Filter/FilterSender.php +++ b/src/EventHandler/Filter/FilterSender.php @@ -18,7 +18,7 @@ namespace danog\MadelineProto\EventHandler\Filter; use Attribute; use danog\MadelineProto\EventHandler; -use danog\MadelineProto\EventHandler\Message\GroupMessage; +use danog\MadelineProto\EventHandler\AbstractMessage; use danog\MadelineProto\EventHandler\Query\ButtonQuery; use danog\MadelineProto\EventHandler\InlineQuery; use danog\MadelineProto\EventHandler\Update; @@ -41,7 +41,7 @@ final class FilterSender extends Filter } public function apply(Update $update): bool { - return ($update instanceof GroupMessage && $update->senderId === $this->peerResolved) || + return ($update instanceof AbstractMessage && $update->senderId === $this->peerResolved) || ($update instanceof ButtonQuery && $update->userId === $this->peerResolved) || ($update instanceof InlineQuery && $update->userId === $this->peerResolved); } From 4fe5a7f7de0afe1c18c8b378fe33fbe93588a70b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D9=8EAhJ?= Date: Tue, 29 Aug 2023 14:19:17 +0330 Subject: [PATCH 18/21] Add FilterSenders --- src/EventHandler/Filter/FilterSenders.php | 59 +++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 src/EventHandler/Filter/FilterSenders.php diff --git a/src/EventHandler/Filter/FilterSenders.php b/src/EventHandler/Filter/FilterSenders.php new file mode 100644 index 000000000..b0a6a9ed9 --- /dev/null +++ b/src/EventHandler/Filter/FilterSenders.php @@ -0,0 +1,59 @@ +. + * + * @author Daniil Gentili + * @copyright 2016-2023 Daniil Gentili + * @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; +use danog\MadelineProto\EventHandler\AbstractMessage; +use danog\MadelineProto\EventHandler\Query\ButtonQuery; +use danog\MadelineProto\EventHandler\InlineQuery; +use danog\MadelineProto\EventHandler\Update; + +/** + * Allow incoming or outgoing group messages made by a certain list of senders. + */ +#[Attribute(Attribute::TARGET_METHOD)] +final class FilterSender extends Filter +{ + /** @var array */ + private readonly array $peers; + /** @var list */ + private readonly array $peersResolved; + public function __construct(string|int ...$idOrUsername) + { + $this->peers = \array_unique($idOrUsername); + } + public function initialize(EventHandler $API): Filter + { + if (\count($this->peers) === 1) { + return (new FilterSender(\array_values($this->peers)[0]))->initialize($API); + } + $res = []; + foreach ($this->peers as $peer) { + $res []= $API->getId($peer); + } + /** @psalm-suppress InaccessibleProperty */ + $this->peersResolved = $res; + return $this; + } + public function apply(Update $update): bool + { + return ($update instanceof AbstractMessage && \in_array($update->senderId, $this->peersResolved, true)) || + ($update instanceof ButtonQuery && \in_array($update->userId, $this->peersResolved, true)) || + ($update instanceof InlineQuery && \in_array($update->userId, $this->peersResolved, true)); + } +} From a94c1e59601a77093358d915f95d2009e2bc3967 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D9=8EAhJ?= Date: Tue, 29 Aug 2023 14:20:30 +0330 Subject: [PATCH 19/21] fixing --- src/EventHandler/InlineQueryPeerType.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/EventHandler/InlineQueryPeerType.php b/src/EventHandler/InlineQueryPeerType.php index 5048ac517..f3328caa3 100644 --- a/src/EventHandler/InlineQueryPeerType.php +++ b/src/EventHandler/InlineQueryPeerType.php @@ -5,9 +5,7 @@ namespace danog\MadelineProto\EventHandler; use AssertionError; use JsonSerializable; -/** - * Inline query peer type - */ +/** @internal */ enum InlineQueryPeerType implements JsonSerializable { /** private chat */ From 0d70c985cef81f27874e730bc8bf60500cae8d44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D9=8EAhJ?= Date: Tue, 29 Aug 2023 23:18:08 +0330 Subject: [PATCH 20/21] Resolve confilits --- .../Filter/AbstractFilterFromSender.php | 43 ++++++++++++++ .../Filter/AbstractFilterFromSenders.php | 56 +++++++++++++++++++ src/EventHandler/Filter/FilterFromSender.php | 22 +------- src/EventHandler/Filter/FilterFromSenders.php | 33 +---------- src/EventHandler/Filter/FilterSender.php | 24 +------- src/EventHandler/Filter/FilterSenders.php | 35 +----------- 6 files changed, 109 insertions(+), 104 deletions(-) create mode 100644 src/EventHandler/Filter/AbstractFilterFromSender.php create mode 100644 src/EventHandler/Filter/AbstractFilterFromSenders.php diff --git a/src/EventHandler/Filter/AbstractFilterFromSender.php b/src/EventHandler/Filter/AbstractFilterFromSender.php new file mode 100644 index 000000000..b42603677 --- /dev/null +++ b/src/EventHandler/Filter/AbstractFilterFromSender.php @@ -0,0 +1,43 @@ +. + * + * @author Daniil Gentili + * @copyright 2016-2023 Daniil Gentili + * @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; +use danog\MadelineProto\EventHandler\Message\GroupMessage; +use danog\MadelineProto\EventHandler\Update; + +/** + * Allow incoming or outgoing group messages made by a certain sender. + */ +abstract class AbstractFilterFromSender extends Filter +{ + private readonly int $peerResolved; + public function __construct(private readonly string|int $peer) + { + } + public function initialize(EventHandler $API): Filter + { + /** @psalm-suppress InaccessibleProperty */ + $this->peerResolved = $API->getId($this->peer); + return $this; + } + public function apply(Update $update): bool + { + return $update instanceof GroupMessage && $update->senderId === $this->peerResolved; + } +} \ No newline at end of file diff --git a/src/EventHandler/Filter/AbstractFilterFromSenders.php b/src/EventHandler/Filter/AbstractFilterFromSenders.php new file mode 100644 index 000000000..72375dee4 --- /dev/null +++ b/src/EventHandler/Filter/AbstractFilterFromSenders.php @@ -0,0 +1,56 @@ +. + * + * @author Daniil Gentili + * @copyright 2016-2023 Daniil Gentili + * @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; +use danog\MadelineProto\EventHandler\Message\GroupMessage; +use danog\MadelineProto\EventHandler\Update; + +/** + * Allow incoming or outgoing group messages made by a certain list of senders. + * + * @internal + */ +abstract class AbstractFilterFromSenders extends Filter +{ + /** @var array */ + private readonly array $peers; + /** @var list */ + private readonly array $peersResolved; + public function __construct(string|int ...$idOrUsername) + { + $this->peers = \array_unique($idOrUsername); + } + public function initialize(EventHandler $API): Filter + { + if (\count($this->peers) === 1) { + return (new FilterFromSender(\array_values($this->peers)[0]))->initialize($API); + } + $res = []; + foreach ($this->peers as $peer) { + $res []= $API->getId($peer); + } + /** @psalm-suppress InaccessibleProperty */ + $this->peersResolved = $res; + return $this; + } + public function apply(Update $update): bool + { + return $update instanceof GroupMessage && \in_array($update->senderId, $this->peersResolved, true); + } +} \ No newline at end of file diff --git a/src/EventHandler/Filter/FilterFromSender.php b/src/EventHandler/Filter/FilterFromSender.php index d0fce9902..b5e8a5add 100644 --- a/src/EventHandler/Filter/FilterFromSender.php +++ b/src/EventHandler/Filter/FilterFromSender.php @@ -19,30 +19,12 @@ namespace danog\MadelineProto\EventHandler\Filter; use Attribute; use danog\MadelineProto\EventHandler; use danog\MadelineProto\EventHandler\Message\GroupMessage; -use danog\MadelineProto\EventHandler\Query\ButtonQuery; -use danog\MadelineProto\EventHandler\InlineQuery; use danog\MadelineProto\EventHandler\Update; /** * Allow incoming or outgoing group messages made by a certain sender. */ #[Attribute(Attribute::TARGET_METHOD)] -final class FilterFromSender extends Filter +final class FilterFromSender extends AbstractFilterFromSender { - private readonly int $peerResolved; - public function __construct(private readonly string|int $peer) - { - } - public function initialize(EventHandler $API): Filter - { - /** @psalm-suppress InaccessibleProperty */ - $this->peerResolved = $API->getId($this->peer); - return $this; - } - public function apply(Update $update): bool - { - return ($update instanceof GroupMessage && $update->senderId === $this->peerResolved) || - ($update instanceof ButtonQuery && $update->userId === $this->peerResolved) || - ($update instanceof InlineQuery && $update->userId === $this->peerResolved); - } -} +} \ No newline at end of file diff --git a/src/EventHandler/Filter/FilterFromSenders.php b/src/EventHandler/Filter/FilterFromSenders.php index cb21a2444..4a0f63317 100644 --- a/src/EventHandler/Filter/FilterFromSenders.php +++ b/src/EventHandler/Filter/FilterFromSenders.php @@ -19,41 +19,12 @@ namespace danog\MadelineProto\EventHandler\Filter; use Attribute; use danog\MadelineProto\EventHandler; use danog\MadelineProto\EventHandler\Message\GroupMessage; -use danog\MadelineProto\EventHandler\Query\ButtonQuery; -use danog\MadelineProto\EventHandler\InlineQuery; use danog\MadelineProto\EventHandler\Update; /** * Allow incoming or outgoing group messages made by a certain list of senders. */ #[Attribute(Attribute::TARGET_METHOD)] -final class FilterFromSenders extends Filter +final class FilterFromSenders extends AbstractFilterFromSenders { - /** @var array */ - private readonly array $peers; - /** @var list */ - private readonly array $peersResolved; - public function __construct(string|int ...$idOrUsername) - { - $this->peers = \array_unique($idOrUsername); - } - public function initialize(EventHandler $API): Filter - { - if (\count($this->peers) === 1) { - return (new FilterFromSender(\array_values($this->peers)[0]))->initialize($API); - } - $res = []; - foreach ($this->peers as $peer) { - $res []= $API->getId($peer); - } - /** @psalm-suppress InaccessibleProperty */ - $this->peersResolved = $res; - return $this; - } - public function apply(Update $update): bool - { - return ($update instanceof GroupMessage && \in_array($update->senderId, $this->peersResolved, true)) || - ($update instanceof ButtonQuery && \in_array($update->userId, $this->peersResolved, true)) || - ($update instanceof InlineQuery && \in_array($update->userId, $this->peersResolved, true)); - } -} +} \ No newline at end of file diff --git a/src/EventHandler/Filter/FilterSender.php b/src/EventHandler/Filter/FilterSender.php index d61ebeb00..64b25ea7b 100644 --- a/src/EventHandler/Filter/FilterSender.php +++ b/src/EventHandler/Filter/FilterSender.php @@ -18,31 +18,13 @@ namespace danog\MadelineProto\EventHandler\Filter; use Attribute; use danog\MadelineProto\EventHandler; -use danog\MadelineProto\EventHandler\AbstractMessage; -use danog\MadelineProto\EventHandler\Query\ButtonQuery; -use danog\MadelineProto\EventHandler\InlineQuery; +use danog\MadelineProto\EventHandler\Message\GroupMessage; use danog\MadelineProto\EventHandler\Update; /** * Allow incoming or outgoing group messages made by a certain sender. */ #[Attribute(Attribute::TARGET_METHOD)] -final class FilterSender extends Filter +final class FilterSender extends AbstractFilterFromSender { - private readonly int $peerResolved; - public function __construct(private readonly string|int $peer) - { - } - public function initialize(EventHandler $API): Filter - { - /** @psalm-suppress InaccessibleProperty */ - $this->peerResolved = $API->getId($this->peer); - return $this; - } - public function apply(Update $update): bool - { - return ($update instanceof AbstractMessage && $update->senderId === $this->peerResolved) || - ($update instanceof ButtonQuery && $update->userId === $this->peerResolved) || - ($update instanceof InlineQuery && $update->userId === $this->peerResolved); - } -} +} \ No newline at end of file diff --git a/src/EventHandler/Filter/FilterSenders.php b/src/EventHandler/Filter/FilterSenders.php index b0a6a9ed9..5b1f078af 100644 --- a/src/EventHandler/Filter/FilterSenders.php +++ b/src/EventHandler/Filter/FilterSenders.php @@ -18,42 +18,13 @@ namespace danog\MadelineProto\EventHandler\Filter; use Attribute; use danog\MadelineProto\EventHandler; -use danog\MadelineProto\EventHandler\AbstractMessage; -use danog\MadelineProto\EventHandler\Query\ButtonQuery; -use danog\MadelineProto\EventHandler\InlineQuery; +use danog\MadelineProto\EventHandler\Message\GroupMessage; use danog\MadelineProto\EventHandler\Update; /** * Allow incoming or outgoing group messages made by a certain list of senders. */ #[Attribute(Attribute::TARGET_METHOD)] -final class FilterSender extends Filter +final class FilterSenders extends AbstractFilterFromSenders { - /** @var array */ - private readonly array $peers; - /** @var list */ - private readonly array $peersResolved; - public function __construct(string|int ...$idOrUsername) - { - $this->peers = \array_unique($idOrUsername); - } - public function initialize(EventHandler $API): Filter - { - if (\count($this->peers) === 1) { - return (new FilterSender(\array_values($this->peers)[0]))->initialize($API); - } - $res = []; - foreach ($this->peers as $peer) { - $res []= $API->getId($peer); - } - /** @psalm-suppress InaccessibleProperty */ - $this->peersResolved = $res; - return $this; - } - public function apply(Update $update): bool - { - return ($update instanceof AbstractMessage && \in_array($update->senderId, $this->peersResolved, true)) || - ($update instanceof ButtonQuery && \in_array($update->userId, $this->peersResolved, true)) || - ($update instanceof InlineQuery && \in_array($update->userId, $this->peersResolved, true)); - } -} +} \ No newline at end of file From 5bec0abf9a2773bdeb20328b0de0fd65403d7639 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D9=8EAhJ?= Date: Tue, 29 Aug 2023 23:29:03 +0330 Subject: [PATCH 21/21] Support for inlineQuery --- src/EventHandler/Filter/AbstractFilterFromSender.php | 6 +++++- src/EventHandler/Filter/AbstractFilterFromSenders.php | 4 ++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/EventHandler/Filter/AbstractFilterFromSender.php b/src/EventHandler/Filter/AbstractFilterFromSender.php index 1a24821cb..434d0590c 100644 --- a/src/EventHandler/Filter/AbstractFilterFromSender.php +++ b/src/EventHandler/Filter/AbstractFilterFromSender.php @@ -19,6 +19,8 @@ namespace danog\MadelineProto\EventHandler\Filter; use Attribute; use danog\MadelineProto\EventHandler; use danog\MadelineProto\EventHandler\Message\GroupMessage; +use danog\MadelineProto\EventHandler\Query\ButtonQuery; +use danog\MadelineProto\EventHandler\InlineQuery; use danog\MadelineProto\EventHandler\Update; /** @@ -40,6 +42,8 @@ abstract class AbstractFilterFromSender extends Filter } public function apply(Update $update): bool { - return $update instanceof GroupMessage && $update->senderId === $this->peerResolved; + return ($update instanceof GroupMessage && $update->senderId === $this->peerResolved) || + ($update instanceof ButtonQuery && $update->userId === $this->peerResolved) || + ($update instanceof InlineQuery && $update->userId === $this->peerResolved); } } diff --git a/src/EventHandler/Filter/AbstractFilterFromSenders.php b/src/EventHandler/Filter/AbstractFilterFromSenders.php index eb8b331c4..d3aba0345 100644 --- a/src/EventHandler/Filter/AbstractFilterFromSenders.php +++ b/src/EventHandler/Filter/AbstractFilterFromSenders.php @@ -19,6 +19,8 @@ namespace danog\MadelineProto\EventHandler\Filter; use Attribute; use danog\MadelineProto\EventHandler; use danog\MadelineProto\EventHandler\Message\GroupMessage; +use danog\MadelineProto\EventHandler\Query\ButtonQuery; +use danog\MadelineProto\EventHandler\InlineQuery; use danog\MadelineProto\EventHandler\Update; /** @@ -52,5 +54,7 @@ abstract class AbstractFilterFromSenders extends Filter public function apply(Update $update): bool { return $update instanceof GroupMessage && \in_array($update->senderId, $this->peersResolved, true); + ($update instanceof ButtonQuery && \in_array($update->userId, $this->peerResolved, true)) || + ($update instanceof InlineQuery && \in_array($update->userId, $this->peerResolved, true)); } }