Skip to content

Commit d0aeadc

Browse files
feat: add topics level feature
1 parent bbf0da5 commit d0aeadc

9 files changed

+137
-9
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace TheCoder\MonologTelegram\Attributes;
4+
5+
abstract class AbstractTopicLevelAttribute implements TopicLogInterface
6+
{
7+
public function getTopicID(array $topicsLevel): string|null
8+
{
9+
return $topicsLevel[static::class] ?? null;
10+
}
11+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace TheCoder\MonologTelegram\Attributes;
4+
5+
use Attribute;
6+
7+
#[Attribute]
8+
final class CriticalAttribute extends AbstractTopicLevelAttribute
9+
{
10+
}

src/Attributes/DebugAttribute.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace TheCoder\MonologTelegram\Attributes;
4+
5+
use Attribute;
6+
7+
#[Attribute]
8+
final class DebugAttribute extends AbstractTopicLevelAttribute
9+
{
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace TheCoder\MonologTelegram\Attributes;
4+
5+
use Attribute;
6+
7+
#[Attribute]
8+
final class EmergencyAttribute extends AbstractTopicLevelAttribute
9+
{
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace TheCoder\MonologTelegram\Attributes;
4+
5+
use Attribute;
6+
7+
#[Attribute]
8+
final class ImportantAttribute extends AbstractTopicLevelAttribute
9+
{
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace TheCoder\MonologTelegram\Attributes;
4+
5+
use Attribute;
6+
7+
#[Attribute]
8+
final class InformationAttribute extends AbstractTopicLevelAttribute
9+
{
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace TheCoder\MonologTelegram\Attributes;
4+
5+
use Attribute;
6+
7+
#[Attribute]
8+
final class LowPriorityAttribute extends AbstractTopicLevelAttribute
9+
{
10+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace TheCoder\MonologTelegram\Attributes;
4+
5+
interface TopicLogInterface
6+
{
7+
public function getTopicID(array $topicsLevel): string|null;
8+
}

src/TelegramBotHandler.php

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,18 @@
33
namespace TheCoder\MonologTelegram;
44

55
use GuzzleHttp\Client;
6+
use Illuminate\Routing\Router;
7+
use Illuminate\Support\Facades\Route;
68
use Monolog\Handler\AbstractProcessingHandler;
7-
use Monolog\Handler\HandlerInterface;
89
use Monolog\Logger;
10+
use ReflectionMethod;
11+
use TheCoder\MonologTelegram\Attributes\TopicLogInterface;
912

10-
class TelegramBotHandler extends AbstractProcessingHandler implements HandlerInterface
13+
class TelegramBotHandler extends AbstractProcessingHandler
1114
{
15+
16+
protected Router $router;
17+
1218
/**
1319
* text parameter in sendMessage method
1420
* @see https://core.telegram.org/bots/api#sendmessage
@@ -49,39 +55,47 @@ class TelegramBotHandler extends AbstractProcessingHandler implements HandlerInt
4955
*/
5056
protected $topicId;
5157

58+
protected $topicsLevel;
59+
5260
/**
5361
* @param string $token Telegram bot access token provided by BotFather
5462
* @param string $channel Telegram channel name
5563
* @inheritDoc
5664
*/
5765
public function __construct(
58-
string $token,
59-
string $chat_id,
66+
Router $router,
67+
string $token,
68+
string $chat_id,
6069
?string $topic_id = null,
61-
$level = Logger::DEBUG,
62-
bool $bubble = true,
63-
$bot_api = 'https://api.telegram.org/bot',
64-
$proxy = null)
70+
$topics_level,
71+
$level = Logger::DEBUG,
72+
bool $bubble = true,
73+
$bot_api = 'https://api.telegram.org/bot',
74+
$proxy = null)
6575
{
6676
parent::__construct($level, $bubble);
6777

78+
$this->router = $router;
6879
$this->token = $token;
6980
$this->botApi = $bot_api;
7081
$this->chatId = $chat_id;
7182
$this->topicId = $topic_id;
83+
$this->topicsLevel = $topics_level;
7284
$this->level = $level;
7385
$this->bubble = $bubble;
7486
$this->proxy = $proxy;
7587
}
7688

7789
/**
7890
* @inheritDoc
91+
* @throws \ReflectionException
7992
*/
8093
protected function write($record): void
8194
{
95+
$topicId = $this->getTopicByAttribute();
8296
$token = $record['context']['token'] ?? null;
8397
$chatId = $record['context']['chat_id'] ?? null;
84-
$topicId = $record['context']['topic_id'] ?? null;
98+
$topicId = $topicId ?? $record['context']['topic_id'] ?? null;
8599

86100
$this->send($record['formatted'], $token, $chatId, $topicId);
87101
}
@@ -141,6 +155,41 @@ protected function send(string $message, $token = null, $chatId = null, $topicId
141155
}
142156
}
143157

158+
159+
protected function getTopicByAttribute(): string|null
160+
{
161+
$route = Route::current();
162+
if ($route == null) {
163+
return null;
164+
}
165+
166+
$action = $route->getAction();
167+
168+
if (!isset($action['controller'])) {
169+
return null;
170+
}
171+
172+
try {
173+
174+
[$controller, $method] = explode('@', $action['controller']);
175+
$reflectionMethod = new ReflectionMethod($controller, $method);
176+
177+
$attributes = $reflectionMethod->getAttributes();
178+
179+
if (empty($attributes)) {
180+
return null;
181+
}
182+
183+
/** @var TopicLogInterface $notifyException */
184+
$notifyException = $attributes[0]->newInstance();
185+
186+
return $notifyException->getTopicId($this->topicsLevel);
187+
188+
} catch (\Throwable) {
189+
return null;
190+
}
191+
}
192+
144193
public function setToken(string $token): static
145194
{
146195
$this->token = $token;

0 commit comments

Comments
 (0)