Skip to content

Instantly share code, notes, and snippets.

@abrudtkuhl
Last active March 11, 2016 22:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abrudtkuhl/74490a81ae5e67686162 to your computer and use it in GitHub Desktop.
Save abrudtkuhl/74490a81ae5e67686162 to your computer and use it in GitHub Desktop.
Extending the WordPress REST API to return message to Slack
<?php
/**
* Plugin Name: WP Slack Slash Command Example
* Description: An example of using the WordPress REST API as a backend for a Slack Slash Command
* Author: Andy Brudtkuhl
* Author URI: http://youmetandy.com
* Version: 0.1
* Plugin URI: https://github.com/abrudtkuhl/wp-slack-slash-command
* License: GPL2+
*/
add_action( 'rest_api_init', function () {
register_rest_route( 'api', '/slash', array(
'methods' => 'GET',
'callback' => 'get_content',
) );
}
function get_content() {
if( isset( $_GET['token'] ) ) {
// /heykramer gif
if( isset( $_GET['command'] ) && $_GET['command'] == 'gif' ) {
$post = get_posts( array(
'category_name' => 'gifs',
'orderby' => 'rand',
'posts_per_page' => 1,
) );
}
// /heykramer quote
if( isset( $_GET['command'] ) && $_GET['command'] == 'quote' ) {
$post = get_posts( array(
'category_name' => 'quotes',
'orderby' => 'rand',
'posts_per_page' => 1,
) );
}
// no command given? just give something random
if( !isset($post) ) {
$post = get_posts( array(
'orderby' => 'rand',
'posts_per_page' => 1,
) );
}
// slack formatted response
$response = array('response_type' => 'in_channel', 'text' => $post[0]->post_content);
return $response;
}
// huh?
return "these arent the droids you are looking for :wave:";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment