Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/Attributes/AbstractTopicLevelAttribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace TheCoder\MonologTelegram\Attributes;

abstract class AbstractTopicLevelAttribute implements TopicLogInterface
{
public function getTopicID(array $topicsLevel): string|null
{
return $topicsLevel[static::class] ?? null;
}
}
10 changes: 10 additions & 0 deletions src/Attributes/CriticalAttribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace TheCoder\MonologTelegram\Attributes;

use Attribute;

#[Attribute]
final class CriticalAttribute extends AbstractTopicLevelAttribute
{
}
10 changes: 10 additions & 0 deletions src/Attributes/DebugAttribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace TheCoder\MonologTelegram\Attributes;

use Attribute;

#[Attribute]
final class DebugAttribute extends AbstractTopicLevelAttribute
{
}
10 changes: 10 additions & 0 deletions src/Attributes/EmergencyAttribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace TheCoder\MonologTelegram\Attributes;

use Attribute;

#[Attribute]
final class EmergencyAttribute extends AbstractTopicLevelAttribute
{
}
10 changes: 10 additions & 0 deletions src/Attributes/ImportantAttribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace TheCoder\MonologTelegram\Attributes;

use Attribute;

#[Attribute]
final class ImportantAttribute extends AbstractTopicLevelAttribute
{
}
10 changes: 10 additions & 0 deletions src/Attributes/InformationAttribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace TheCoder\MonologTelegram\Attributes;

use Attribute;

#[Attribute]
final class InformationAttribute extends AbstractTopicLevelAttribute
{
}
10 changes: 10 additions & 0 deletions src/Attributes/LowPriorityAttribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace TheCoder\MonologTelegram\Attributes;

use Attribute;

#[Attribute]
final class LowPriorityAttribute extends AbstractTopicLevelAttribute
{
}
8 changes: 8 additions & 0 deletions src/Attributes/TopicLogInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace TheCoder\MonologTelegram\Attributes;

interface TopicLogInterface
{
public function getTopicID(array $topicsLevel): string|null;
}
67 changes: 58 additions & 9 deletions src/TelegramBotHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@
namespace TheCoder\MonologTelegram;

use GuzzleHttp\Client;
use Illuminate\Routing\Router;
use Illuminate\Support\Facades\Route;
use Monolog\Handler\AbstractProcessingHandler;
use Monolog\Handler\HandlerInterface;
use Monolog\Logger;
use ReflectionMethod;
use TheCoder\MonologTelegram\Attributes\TopicLogInterface;

class TelegramBotHandler extends AbstractProcessingHandler implements HandlerInterface
class TelegramBotHandler extends AbstractProcessingHandler
{

protected Router $router;

/**
* text parameter in sendMessage method
* @see https://core.telegram.org/bots/api#sendmessage
Expand Down Expand Up @@ -49,39 +55,47 @@ class TelegramBotHandler extends AbstractProcessingHandler implements HandlerInt
*/
protected $topicId;

protected $topicsLevel;

/**
* @param string $token Telegram bot access token provided by BotFather
* @param string $channel Telegram channel name
* @inheritDoc
*/
public function __construct(
string $token,
string $chat_id,
Router $router,
string $token,
string $chat_id,
?string $topic_id = null,
$level = Logger::DEBUG,
bool $bubble = true,
$bot_api = 'https://api.telegram.org/bot',
$proxy = null)
$topics_level,
$level = Logger::DEBUG,
bool $bubble = true,
$bot_api = 'https://api.telegram.org/bot',
$proxy = null)
{
parent::__construct($level, $bubble);

$this->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;
}

/**
* @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);
}
Expand Down Expand Up @@ -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;
Expand Down