Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?
function sendToSlack($to,$title,$text,$color = "good") {
$inHook = 'https://hooks.slack.com/services/###/###/###';
$data = json_encode(array(
"channel" => $to,
"username" => "Your App Name",
"icon_emoji" => ":beers:", // your app icon
"attachments" => array(array(
"fallback" => $title,
"pretext" => $title,
"mrkdwn" => true,
"mrkdwn_in" => array('text'),
"color" => $color,
"text" => $text
))
));
$ch = curl_init($inHook);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
}
// EXAMPLE USAGE
$message = "My oh my, something happened and it's now being pushed to Slack immediately.";
$message .= "Please be aware *formatting* works here, so take full advantage of it.\n";
$message .= "```"
$message .= "12:00 - Log : Details of something here\n";
$message .= "12:01 - Log : Details of something else here\n";
$message .= "```";
// go go go
sendToSlack('@davidsword', 'A Something Occurred', $message, '#fd8f31');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment