|
3 | 3 | namespace TheCoder\MonologTelegram; |
4 | 4 |
|
5 | 5 | use GuzzleHttp\Client; |
| 6 | +use Illuminate\Routing\Router; |
| 7 | +use Illuminate\Support\Facades\Route; |
6 | 8 | use Monolog\Handler\AbstractProcessingHandler; |
7 | | -use Monolog\Handler\HandlerInterface; |
8 | 9 | use Monolog\Logger; |
| 10 | +use ReflectionMethod; |
| 11 | +use TheCoder\MonologTelegram\Attributes\TopicLogInterface; |
9 | 12 |
|
10 | | -class TelegramBotHandler extends AbstractProcessingHandler implements HandlerInterface |
| 13 | +class TelegramBotHandler extends AbstractProcessingHandler |
11 | 14 | { |
| 15 | + |
| 16 | + protected Router $router; |
| 17 | + |
12 | 18 | /** |
13 | 19 | * text parameter in sendMessage method |
14 | 20 | * @see https://core.telegram.org/bots/api#sendmessage |
@@ -49,39 +55,47 @@ class TelegramBotHandler extends AbstractProcessingHandler implements HandlerInt |
49 | 55 | */ |
50 | 56 | protected $topicId; |
51 | 57 |
|
| 58 | + protected $topicsLevel; |
| 59 | + |
52 | 60 | /** |
53 | 61 | * @param string $token Telegram bot access token provided by BotFather |
54 | 62 | * @param string $channel Telegram channel name |
55 | 63 | * @inheritDoc |
56 | 64 | */ |
57 | 65 | public function __construct( |
58 | | - string $token, |
59 | | - string $chat_id, |
| 66 | + Router $router, |
| 67 | + string $token, |
| 68 | + string $chat_id, |
60 | 69 | ?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) |
65 | 75 | { |
66 | 76 | parent::__construct($level, $bubble); |
67 | 77 |
|
| 78 | + $this->router = $router; |
68 | 79 | $this->token = $token; |
69 | 80 | $this->botApi = $bot_api; |
70 | 81 | $this->chatId = $chat_id; |
71 | 82 | $this->topicId = $topic_id; |
| 83 | + $this->topicsLevel = $topics_level; |
72 | 84 | $this->level = $level; |
73 | 85 | $this->bubble = $bubble; |
74 | 86 | $this->proxy = $proxy; |
75 | 87 | } |
76 | 88 |
|
77 | 89 | /** |
78 | 90 | * @inheritDoc |
| 91 | + * @throws \ReflectionException |
79 | 92 | */ |
80 | 93 | protected function write($record): void |
81 | 94 | { |
| 95 | + $topicId = $this->getTopicByAttribute(); |
82 | 96 | $token = $record['context']['token'] ?? null; |
83 | 97 | $chatId = $record['context']['chat_id'] ?? null; |
84 | | - $topicId = $record['context']['topic_id'] ?? null; |
| 98 | + $topicId = $topicId ?? $record['context']['topic_id'] ?? null; |
85 | 99 |
|
86 | 100 | $this->send($record['formatted'], $token, $chatId, $topicId); |
87 | 101 | } |
@@ -141,6 +155,41 @@ protected function send(string $message, $token = null, $chatId = null, $topicId |
141 | 155 | } |
142 | 156 | } |
143 | 157 |
|
| 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 | + |
144 | 193 | public function setToken(string $token): static |
145 | 194 | { |
146 | 195 | $this->token = $token; |
|
0 commit comments