Last active
October 11, 2016 21:45
-
-
Save vitormattos/bb60520e4a4350d22fcde921be5f6437 to your computer and use it in GitHub Desktop.
Send logs from application to a Telegram group
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use App\Handler\MonologTelegramHandler; | |
$app['telegram_bot.token'] = getenv('TELEGRAM_BOT_TOKEN'); | |
$app['telegram_bot.log_chat'] = getenv('TELEGRAM_BOT_LOG_CHAT'); | |
$app['telegram_bot.contact_chat'] = getenv('TELEGRAM_BOT_CONTACT_CHAT'); | |
$app['telegram_bot.log_chat.enable'] = $app['log.level'] == Monolog\Logger::DEBUG; | |
$app['monolog'] = $app->share($app->extend('monolog', function($monolog, $app) { | |
if($app['telegram_bot.log_chat.enable'] && $app['log.level'] == Monolog\Logger::DEBUG ) { | |
$monolog->pushHandler(new MonologTelegramHandler([ | |
'token' => $app['telegram_bot.token'], | |
'chat_id' => $app['telegram_bot.log_chat'], | |
'command' => $app['cli.sendmessage'] | |
])); | |
} | |
return $monolog; | |
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"require-dev" : { | |
"irazasyed/telegram-bot-sdk": "dev-master" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Handler; | |
use Monolog\Handler\MailHandler; | |
use Monolog\Handler\AbstractHandler; | |
use Telegram\Bot\Api; | |
class MonologTelegramHandler extends MailHandler { | |
/** | |
* | |
* @var Api | |
*/ | |
private $telegram; | |
public function __construct($arguments, $level = \Monolog\Logger::ERROR, $bubble = true) { | |
$this->arguments = $arguments; | |
} | |
protected function send($content, array $records) | |
{ | |
pclose(popen('php '.$this->arguments['command']. | |
base64_encode(serialize([ | |
'params' => [ | |
'chat_id' => $this->arguments['chat_id'], | |
'text' => getenv('APP_ENV').'->'.$content, | |
'disable_web_page_preview' => true | |
], | |
'token' => $this->arguments['token'] | |
])).' &', 'r' | |
)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Cli\Command; | |
use Symfony\Component\Console\Command\Command; | |
use Symfony\Component\Console\Input\InputInterface; | |
use Symfony\Component\Console\Input\InputArgument; | |
use Symfony\Component\Console\Output\OutputInterface; | |
use Telegram\Bot\Api; | |
class SendMessageCommand extends Command { | |
protected function configure() | |
{ | |
$this | |
->setName('sendmsg') | |
->setDescription('Send a message to telegram') | |
->addArgument( | |
'arguments', | |
InputArgument::REQUIRED, | |
'Arguments to method sendMessage of Telegram bot api' | |
); | |
} | |
protected function execute(InputInterface $input, OutputInterface $output) | |
{ | |
$arguments = $input->getArgument('arguments'); | |
$arguments = unserialize(base64_decode($arguments)); | |
if(!$arguments) { | |
$output->writeln('<error>Invalid params</error>'); | |
return; | |
} | |
$telegram = new Api($arguments['token']); | |
$telegram->sendMessage($arguments['params']); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
pclose(popen('php '.$this->app['cli.sendmessage']. | |
base64_encode(serialize([ | |
'params' => [ | |
'chat_id' => $this->app['telegram_bot.contact_chat'], | |
'text' => print_r($data, true) | |
], | |
'token' => $this->app['telegram_bot.token'] | |
])).' &', 'r' | |
)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment