/* USAGE
* =============================================== */
// Setup your endpoint somewhere
defined( 'GOOD_SLACK_WEBHOOK_ENDPOINT' ) or define( 'GOOD_SLACK_WEBHOOK_ENDPOINT', 'https://hooks.slack.com/services/XXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXX' );
// Create a rich payload
$sample_payload = array(
'channel' => '#incoming-webhooks',
'text' => 'This text is ' . good_slack_link( "click-able", 'http://example.com' ) . ' !!!!',
'icon_emoji' => ':slack:',
'attachments' => array(
array(
"title" => "This is an attachment title link",
"title_link" => "http://example.com/",
"text" => "This is some text for the attachment and a link below.\nhttp://example.com/ticket/123456",
),
),
);
// Send to Slack channel
$result = good_slack( "Payload 'text' will override this 'text'", $sample_payload );
/* GOOD SLACK
* =============================================== */
/**
* Sends a rich message to Slack.
* REQUIRES https://slack.com/apps/A0F7XDUAZ-incoming-webhooks to be installed and configured.
*
* @author Jesse Graupmann | http://www.justgooddesign.com
*
* @param string $message to be passed to Slack, can be overridden in $payload.
* @param array $payload Overrides - allows for more complex data.
* @param string $slack_webhook_endpoint Webhook endpoint
*
* @return string result of the curl or error message.
*
* ------
* @usage:
*
* defined( 'GOOD_SLACK_WEBHOOK_ENDPOINT' ) or
* define( 'GOOD_SLACK_WEBHOOK_ENDPOINT', 'https://hooks.slack.com/services/XXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXX' );
*
* $result = good_slack ( "Payload 'text' will override this 'text'", array( 'text'=>'This is the new message' ) );
* ------
* @see https://api.slack.com/incoming-webhooks
*
* PAYLOAD OPTIONS
* text = "This is a <http://example.com|link>"
* icon_emoji => :slack:, :beryl:, :+1:, :sunglasses:, etc...
* channel => #random
* attachments => array ( array( title, title_link, text ) )
*
*/
function good_slack( $message, $payload = array(), $slack_webhook_endpoint = '' ) {
// Requires an endpoint like: https://hooks.slack.com/services/XXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXX
// Login to your settings and pass as a param, set as constant or edit the $slack_webhook_endpoint default arg
if ( empty( $slack_webhook_endpoint ) ) {
if ( ! empty ( $payload[ 'webhook_endpoint' ] ) ) {
// payload contains endpoint
$slack_webhook_endpoint = $payload[ 'webhook_endpoint' ];
} else if ( defined( 'GOOD_SLACK_WEBHOOK_ENDPOINT' ) ) {
// endpoint was defined in constant
$slack_webhook_endpoint = GOOD_SLACK_WEBHOOK_ENDPOINT;
}
}
$payload_defaults = array(
'channel' => '#random',
'text' => $message,
);
// WordPress support
if ( $allow_filters = function_exists( 'apply_filters' ) ) {
// You can get your webhook endpoint from your Slack settings
// filter the endpoint
$slack_webhook_endpoint = apply_filters( 'good_slack_webhook_endpoint', $slack_webhook_endpoint, $message, $payload, $payload_defaults );
// allow you to turn off slack notifications if FALSE is returned
if ( ! $allow_slack = apply_filters( 'good_slack_allowed', true, $payload_defaults, $message, $payload, $slack_webhook_endpoint ) ) {
return $allow_slack;
}
// filter defaults
$payload_defaults = apply_filters( 'good_slack_payload_defaults', $payload_defaults, $message, $payload, $slack_webhook_endpoint );
// filter payload before merge
$payload = apply_filters( 'good_slack_payload_pre', $payload, $payload_defaults, $message, $slack_webhook_endpoint );
}
// clean the overrides
$payload = is_array( $payload ) ? $payload : array();
// merge payload over defaults
$payload = array_filter( array_merge( $payload_defaults, $payload ) );
// channel needs to start with #
if ( ! ( '#' === "" || strrpos( $payload[ 'channel' ], '#', - strlen( $payload[ 'channel' ] ) ) !== false ) ) {
$payload[ 'channel' ] = '#' . $payload[ 'channel' ];
}
if ( $allow_filters ) {
// filter final payload
$payload = apply_filters( 'good_slack_payload_post', $payload, $message, $slack_webhook_endpoint );
}
// Last check for endpoint
if ( empty ( $slack_webhook_endpoint ) ) {
return 'Nothing to do! Slack Webhook EndPoint is not set';
}
// prep the payload
$data = json_encode( $payload );
$ch = curl_init( $slack_webhook_endpoint );
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 );
return $result;
}
/**
* Convert link to Slack style for use in messages
*
* @param string $text Text to wrap in link
* @param string $link Link to wrap text
*
* @return string
*/
function good_slack_link( $text, $link ) {
$esc_url = esc_url( $link );
$slack_link = "<$esc_url|$text>";
if ( function_exists( 'apply_filters' ) ) {
$slack_link = apply_filters( 'good_slack_link', $slack_link, $text, $link );
}
return $slack_link;
}
Send rich messages to Slack using https://slack.com/apps/A0F7XDUAZ-incoming-webhooks.
Optional filters added for WordPress.
Optional filters added for WordPress.
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.