Created
November 14, 2017 11:36
-
-
Save mtrimarchi/becf84e362d9960041edb0926855e3ae to your computer and use it in GitHub Desktop.
Wrapper from Papertrail to Telegram using webhooks
This file contains hidden or 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 | |
/** | |
* alertPapertrail.php | |
*/ | |
// Require https://github.com/Eleirbag89/TelegramBotPHP | |
include 'Telegram.php'; | |
// Set the bot TOKEN | |
$bot_token = 'xxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; | |
// Set "Papertrail Alert" group ID | |
$chat_group = 'xxxxxxxxxxxxxx'; | |
/** | |
* Main Routine | |
*/ | |
// Get payload data from Papertrail | |
$payload = $_POST['payload']; | |
// Instance the Telegram Object | |
$telegram = new Telegram($bot_token); | |
// Get all events from Papertrail | |
$events = papertrailDecoder( $payload ); | |
// Send all events to chat group | |
foreach($events as $event){ | |
$content = ['chat_id' => $chat_group, 'text' => $event, 'parse_mode' => 'html']; | |
$telegram->sendMessage($content); | |
} | |
// End of routine | |
exit(); | |
/** | |
* All functions | |
*/ | |
// Papertrail Events Decoder | |
// @return Array() of strings with key ID of events | |
function papertrailDecoder( $payload ){ | |
$return = Array(); | |
$papertrail = json_decode( $payload, 1 ); | |
foreach($papertrail['events'] as $event){ | |
$event_line = ""; | |
foreach($event as $key => $value ){ | |
$event_line .= "<b>" . $key . "</b> : <code>" . $value . "</code>\r\n"; | |
} | |
$return[$event['id']] = $event_line; | |
} | |
return $return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment