Created
July 31, 2015 03:00
-
-
Save anonymous/9cee6dc1f3ef8477d4cb to your computer and use it in GitHub Desktop.
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 | |
// settings | |
$slackHook = 'YOUR_SLACK-ISSUED_WEBHOOK_URL_HERE'; | |
$discourseHost = 'YOUR_DISCOURSE_HOSTNAME'; | |
// decode webhook payload | |
list($post, $topic, $user) = json_decode(file_get_contents('php://input'), true); | |
// compose message for Slack | |
$messageText = "New topic on $discourseHost"; | |
#// This doesn't work because the category is a number... we'll need to map them on our own to category names or slack rooms | |
#if (!empty($topic['category'])) { | |
# $messageText .= " in $topic[category]"; | |
#} | |
$messageText .= ": <http://$discourseHost/t/$post[topic_id]|$topic[title]>"; | |
$messageText .= "\n\n> " . str_replace("\n", "\n> ", $post['raw']) . "\n\n\n" . json_encode(['post' => $post, 'topic' => $topic, 'user' => $user], JSON_PRETTY_PRINT);; | |
// confugre cURL | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $slackHook); | |
curl_setopt($ch, CURLOPT_POST, true); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, [ | |
'payload' => json_encode([ | |
'username' => $user['username'] . '@discourse', | |
'icon_url' => str_replace('{size}', 100, "http://$discourseHost/$user[avatar_template]"), | |
'text' => $messageText | |
]) | |
]); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
// execute request | |
$response = curl_exec($ch); | |
curl_close($ch); | |
// write slack response to output | |
die($response); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment