Skip to content

Instantly share code, notes, and snippets.

@pelusium
Last active August 29, 2015 14:15
Show Gist options
  • Save pelusium/9e5da1e2cad16b792c0b to your computer and use it in GitHub Desktop.
Save pelusium/9e5da1e2cad16b792c0b to your computer and use it in GitHub Desktop.
L4 - Write to Slack
App::error(function(Exception $exception, $code)
{
Log::error($exception);
if(!\Config::get('app.debug')){
$slack = new WriteToSlack();
$params = [
'exception' => $exception,
'code' => $code,
'type' => 'error'
];
$slack->write($params);
}
});
<?php
class WriteToSlack {
public function write($params){
$type = $params['type'];
if($type == "error")
$this->registerError($params);
}
private function registerError($params){
$message = $params['exception'];
$code = $params['code'];
$url = \Config::get('settings.slack_hook');
$attach = array(
"fallback" => "Required text summary of the attachment that is shown by clients that understand attachments but choose not to show them.",
//"pretext" => "<http://". \Config::get('app.url') . "|" . \Config::get('app.url') . ">\n" . "Exception Code: " . $message->getCode(),
//"text" => "Exception Code: " . $message->getCode(),
"color" => "danger", // Can either be one of 'good', 'warning', 'danger', or any hex color code
"fields" => array(
array(
"title" => "File",
"value" => $message->getFile() . ":" . $message->getLine(),
"short" => true
),
array(
"title" => "Message",
"value" => "Código:".$code." -".$message->getMessage(),
"short" => true
),
)
);
$icon = ":bell:";
/*if ($params['status'] == "alert") {
$icon = ":no_entry:";
}*/
$x = array(
"username" => "App - Errors",
"text" => "Origin: " . \App::environment() . "\n",
"icon_emoji" => $icon,
"attachments" => array($attach)
);
$pedido_json = json_encode($x);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($pedido_json))
);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $pedido_json);
$curl_response = curl_exec($ch);
curl_close($ch);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment