Skip to content

Commit

Permalink
enable use of Monolog\Handlers\TelegramBotHandler under type 'telegra…
Browse files Browse the repository at this point in the history
…m' (#406)


Co-authored-by: Jordi Boggiano <j.boggiano@seld.be>
  • Loading branch information
gassan and Seldaek committed May 10, 2022
1 parent 4694fc2 commit 6134b24
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
36 changes: 36 additions & 0 deletions DependencyInjection/Configuration.php
Expand Up @@ -344,6 +344,17 @@
* - [level]: level name or int value, defaults to DEBUG
* - [bubble]: bool, defaults to true
*
* - telegram:
* - token: Telegram bot access token provided by BotFather
* - channel: Telegram channel name
* - [level]: level name or int value, defaults to DEBUG
* - [bubble]: bool, defaults to true
* - [parse_mode]: optional the kind of formatting that is used for the message
* - [disable_webpage_preview]: bool, defaults to false, disables link previews for links in the message
* - [disable_notification]: bool, defaults to false, sends the message silently. Users will receive a notification with no sound
* - [split_long_messages]: bool, defaults to false, split messages longer than 4096 bytes into multiple messages
* - [delay_between_messages]: bool, defaults to false, adds a 1sec delay/sleep between sending split messages
*
* All handlers can also be marked with `nested: true` to make sure they are never added explicitly to the stack
*
* @author Jordi Boggiano <j.boggiano@seld.be>
Expand Down Expand Up @@ -564,6 +575,11 @@ public function getConfigTreeBuilder()
->scalarNode('release')->defaultNull()->end() // raven_handler, sentry_handler
->scalarNode('environment')->defaultNull()->end() // raven_handler, sentry_handler
->scalarNode('message_type')->defaultValue(0)->end() // error_log
->scalarNode('parse_mode')->defaultNull()->end() // telegram
->booleanNode('disable_webpage_preview')->defaultNull()->end() // telegram
->booleanNode('disable_notification')->defaultNull()->end() // telegram
->booleanNode('split_long_messages')->defaultFalse()->end() // telegram
->booleanNode('delay_between_messages')->defaultFalse()->end() // telegram
->arrayNode('tags') // loggly
->beforeNormalization()
->ifString()
Expand Down Expand Up @@ -600,6 +616,7 @@ public function getConfigTreeBuilder()
$this->addMongoSection($handlerNode);
$this->addElasticsearchSection($handlerNode);
$this->addRedisSection($handlerNode);
$this->addTelegramSection($handlerNode);
$this->addPredisSection($handlerNode);
$this->addMailerSection($handlerNode);
$this->addVerbosityLevelSection($handlerNode);
Expand Down Expand Up @@ -865,6 +882,25 @@ private function addElasticsearchSection(ArrayNodeDefinition $handerNode)
;
}

private function addTelegramSection(ArrayNodeDefinition $handerNode)
{
$handerNode
->children()
->arrayNode('telegram')
->canBeUnset()
->children()
->scalarNode('token')->end()
->scalarNode('channel')->end()
->end()
->end()
->end()
->validate()
->ifTrue(function ($v) { return 'telegram' === $v['type'] && empty($v['id']) && (empty($v['token']) || empty($v['channel'])); })
->thenInvalid('The token and channel have to be specified to use a TelegramBotHandler')
->end()
;
}

private function addRedisSection(ArrayNodeDefinition $handerNode)
{
$handerNode
Expand Down
20 changes: 20 additions & 0 deletions DependencyInjection/MonologExtension.php
Expand Up @@ -346,6 +346,25 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler
$handler['bubble'],
]);
break;

case 'telegram':
if (!class_exists('Monolog\Handler\TelegramBotHandler')) {
throw new \RuntimeException('The TelegramBotHandler is not available. Please update "monolog/monolog" to 2.2.0');
}

$definition->setArguments([
$handler['token'],
$handler['channel'],
$handler['level'],
$handler['bubble'],
$handler['parse_mode'],
$handler['disable_webpage_preview'],
$handler['disable_notification'],
$handler['split_long_messages'],
$handler['delay_between_messages'],
]);
break;

case 'redis':
case 'predis':
if (isset($handler['redis']['id'])) {
Expand Down Expand Up @@ -994,6 +1013,7 @@ private function getHandlerClassByType($handlerType)
'filter' => 'Monolog\Handler\FilterHandler',
'mongo' => 'Monolog\Handler\MongoDBHandler',
'elasticsearch' => 'Monolog\Handler\ElasticSearchHandler',
'telegram' => 'Monolog\Handler\TelegramBotHandler',
'server_log' => 'Symfony\Bridge\Monolog\Handler\ServerLogHandler',
'redis' => 'Monolog\Handler\RedisHandler',
'predis' => 'Monolog\Handler\RedisHandler',
Expand Down
20 changes: 20 additions & 0 deletions Tests/DependencyInjection/ConfigurationTest.php
Expand Up @@ -243,6 +243,26 @@ public function testWithElasticsearchHandler()
$this->assertEquals('my-index', $config['handlers']['elasticsearch']['index']);
}

public function testWithTelegramBotHandler()
{
$configs = [
[
'handlers' => [
'telegram' => [
'type' => 'telegram',
'token' => 'bot-token',
'channel' => '-100',
]
]
]
];

$config = $this->process($configs);

$this->assertEquals('bot-token', $config['handlers']['telegram']['token']);
$this->assertEquals('-100', $config['handlers']['telegram']['channel']);
}

public function testWithConsoleHandler()
{
$configs = [
Expand Down

0 comments on commit 6134b24

Please sign in to comment.