Skip to content

Commit

Permalink
Merge pull request #25 from cwilby/master
Browse files Browse the repository at this point in the history
Add ability to set token / chat id per log channel
  • Loading branch information
grkamil authored Oct 16, 2021
2 parents d2a73a8 + 3546dcf commit 7e2cba3
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,32 @@ config(['telegram-logger.template'=>'laravel-telegram-logging::custom'])
```
2. Use `Log` as usual.

## Configuring a different chat id or token per channel

1. Add `chat_id` or `token` to channels in `config/logging.php`. Overrides `config('telegram.chat_id')`.
```php
[
'channels' => [
[
'company' => [
'driver' => 'custom',
'via' => TelegramLogger::class,
'chat_id' => env('TELEGRAM_COMPANY_CHAT_ID'),
'token' => env('TELEGRAM_COMPANY_BOT_TOKEN')
],

'operations' => [
'driver' => 'custom',
'via' => TelegramLogger::class,
'chat_id' => env('TELEGRAM_OPERATIONS_CHAT_ID'),
'token' => env('TELEGRAM_OPERATIONS_BOT_TOKEN')
]
]
]
]
```

2. Use `Log` as usual.
## Lumen support

To make it work with Lumen, you need also run two steps:
Expand Down
30 changes: 26 additions & 4 deletions src/TelegramHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
*/
class TelegramHandler extends AbstractProcessingHandler
{
/**
* Logger config
*
* @var array
*/
private $config;

/**
* Bot API token
*
Expand Down Expand Up @@ -46,15 +53,16 @@ class TelegramHandler extends AbstractProcessingHandler
* TelegramHandler constructor.
* @param int $level
*/
public function __construct($level)
public function __construct(array $config)
{
$level = Logger::toMonologLevel($level);
$level = Logger::toMonologLevel($config['level']);

parent::__construct($level, true);

// define variables for making Telegram request
$this->botToken = config('telegram-logger.token');
$this->chatId = config('telegram-logger.chat_id');
$this->config = $config;
$this->botToken = $this->getConfigValue('token');
$this->chatId = $this->getConfigValue('chat_id');

// define variables for text message
$this->appName = config('app.name');
Expand Down Expand Up @@ -116,4 +124,18 @@ private function sendMessage(string $text): void

file_get_contents('https://api.telegram.org/bot'.$this->botToken.'/sendMessage?' . $httpQuery);
}

/**
* @param string $key
* @param string $defaultConfigKey
* @return string
*/
private function getConfigValue($key, $defaultConfigKey = null): string
{
if (isset($this->config[$key])) {
return $this->config[$key];
}

return config($defaultConfigKey ?: "telegram-logger.$key");
}
}
2 changes: 1 addition & 1 deletion src/TelegramLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __invoke(array $config)
return new Logger(
config('app.name'),
[
new TelegramHandler($config['level'])
new TelegramHandler($config),
]
);
}
Expand Down

0 comments on commit 7e2cba3

Please sign in to comment.