diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 4a7b36a5..6e54bbfb 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -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 @@ -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() @@ -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); @@ -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 diff --git a/DependencyInjection/MonologExtension.php b/DependencyInjection/MonologExtension.php index cb884faf..d1f49d9a 100644 --- a/DependencyInjection/MonologExtension.php +++ b/DependencyInjection/MonologExtension.php @@ -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'])) { @@ -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', diff --git a/Tests/DependencyInjection/ConfigurationTest.php b/Tests/DependencyInjection/ConfigurationTest.php index 2d537eee..71d3601a 100644 --- a/Tests/DependencyInjection/ConfigurationTest.php +++ b/Tests/DependencyInjection/ConfigurationTest.php @@ -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 = [