From d0aeadc5b5502ebba843bcfc87a3f0278fbfcd5b Mon Sep 17 00:00:00 2001 From: "S.H.Mahmoudzadeh" Date: Thu, 13 Jun 2024 15:09:44 +0330 Subject: [PATCH] feat: add topics level feature --- .../AbstractTopicLevelAttribute.php | 11 +++ src/Attributes/CriticalAttribute.php | 10 +++ src/Attributes/DebugAttribute.php | 10 +++ src/Attributes/EmergencyAttribute.php | 10 +++ src/Attributes/ImportantAttribute.php | 10 +++ src/Attributes/InformationAttribute.php | 10 +++ src/Attributes/LowPriorityAttribute.php | 10 +++ src/Attributes/TopicLogInterface.php | 8 +++ src/TelegramBotHandler.php | 67 ++++++++++++++++--- 9 files changed, 137 insertions(+), 9 deletions(-) create mode 100644 src/Attributes/AbstractTopicLevelAttribute.php create mode 100644 src/Attributes/CriticalAttribute.php create mode 100644 src/Attributes/DebugAttribute.php create mode 100644 src/Attributes/EmergencyAttribute.php create mode 100644 src/Attributes/ImportantAttribute.php create mode 100644 src/Attributes/InformationAttribute.php create mode 100644 src/Attributes/LowPriorityAttribute.php create mode 100644 src/Attributes/TopicLogInterface.php diff --git a/src/Attributes/AbstractTopicLevelAttribute.php b/src/Attributes/AbstractTopicLevelAttribute.php new file mode 100644 index 0000000..b04f486 --- /dev/null +++ b/src/Attributes/AbstractTopicLevelAttribute.php @@ -0,0 +1,11 @@ +router = $router; $this->token = $token; $this->botApi = $bot_api; $this->chatId = $chat_id; $this->topicId = $topic_id; + $this->topicsLevel = $topics_level; $this->level = $level; $this->bubble = $bubble; $this->proxy = $proxy; @@ -76,12 +88,14 @@ public function __construct( /** * @inheritDoc + * @throws \ReflectionException */ protected function write($record): void { + $topicId = $this->getTopicByAttribute(); $token = $record['context']['token'] ?? null; $chatId = $record['context']['chat_id'] ?? null; - $topicId = $record['context']['topic_id'] ?? null; + $topicId = $topicId ?? $record['context']['topic_id'] ?? null; $this->send($record['formatted'], $token, $chatId, $topicId); } @@ -141,6 +155,41 @@ protected function send(string $message, $token = null, $chatId = null, $topicId } } + + protected function getTopicByAttribute(): string|null + { + $route = Route::current(); + if ($route == null) { + return null; + } + + $action = $route->getAction(); + + if (!isset($action['controller'])) { + return null; + } + + try { + + [$controller, $method] = explode('@', $action['controller']); + $reflectionMethod = new ReflectionMethod($controller, $method); + + $attributes = $reflectionMethod->getAttributes(); + + if (empty($attributes)) { + return null; + } + + /** @var TopicLogInterface $notifyException */ + $notifyException = $attributes[0]->newInstance(); + + return $notifyException->getTopicId($this->topicsLevel); + + } catch (\Throwable) { + return null; + } + } + public function setToken(string $token): static { $this->token = $token;