Plugin

HameSlack

A perfect integration helper for Slack ♡ WordPress. You can post notification to Slack from WordPress. You can make post from Slack. Everything is up to you.

Core Concept

By default, this plugin does nothing. It’s true. Slack has many API intergrations, but hameslack uses 3 of them.

Upper is easier. This plugin helps the connection between Slack and WordPress and you can concentrate on what you should do with slack.

Setup

Everything you need can be set up on admin screen. Go to Setting > HameSlack Setting and save credentials.

How To Use

Most simple is posting to slack. There is a simple way to do it.

<?php do_action( 'hameslack', $content, $attachments, $channel ) ?>

Why do_action? Because in WordPress environment, PHP functions causes Fatal Error when plugin is deactivated. do_action affects nothing even if plugin  suddenly disappears. $attachments is an array of associative array and you can specify image, title, color and so on. Please see Slack Documentation for more detail.

Okay, so when should you do it?

Case 1: Request Review

Let’s consider the case that you are managing a news media on WordPress. There are some editors and many writers. On your work flow, novice writers cannot publish posts without review because they are not reliable. This is very usual on popular news sites.When novice writers finished their post, they change post status to ‘pending‘. Now editors go to admin screen and change its status to ‘publish’.

This workflow sometimes stacks up at editor’s review. So it’s nice to send notification to slack. Incoming Webhooks enable this.

<?php
// Send notification if post status is changed.
add_action( 'transition_post_status', function ( $new_status, $old_status, $post ) {
  if ( 'post' !== $post->post_type ) {
    // If this is not post, ignore.
    return;
  }
  if ( ( 'pending' === $new_status ) && ( 'pending' !== $old_status ) ) {
    // Post is newly waiting review
    $edit_link = admin_url(  "post.php?post={$post->ID}&action=edit" );
    $title = get_the_title( $post );
    $content = "@channel  new post is waiting review: {$title} {$edit_link}";
    do_action( 'hameslack',  $content ) );
  }
}, 10, 3 );

Case 2: Make draft from slack

If you use Slack as communication tool and use WordPress as publishing platform, probably you spend more time on Slack. But the conversations on Slack are ‘flow’ and difficult to find in future. If someone post nice idea, save it as draft.

Outgoing Webhooks enable this.

<?php

// This filter triggered on outgoing hook.
add_filter( 'hameslack_rest_response', function ( $response, $request, $post ) {
    // Detect what endpoint is hit with post slug
    switch ( $post->post_name ) {
        case 'save-draft':
                // Separate text and 1st line will be title.
                $lines = explode( "", $request['text'] );
                $title  = array_shift( $lines );
                $content = implode( "\n", $lines );
                $post_id = wp_insert_post( [
                    'post_title' => $title,
                    'post_content' => $content,
                    'post_status' => 'draft',
                    'post_type' => 'post',
                ] );
                // Make response. This will be post as Bot.
                $edit_link = admin_url( "post.php?post={$post_id}&action=edit" );
                $response['text'] = "New draft is created: {$edit_link}";
                break;
    }
    return $response;
} );

If you have periodical notice, this flow becomes much better(e.g. “7 days have been past since a draft xxx created, what’s up?”).

Case 3: Log Weekly Chat with Specific Keywords

Let’s consider the case you have music review site and make a weekly list of tweets. But you don’t want include negatives tweets, so your team members post twitter URL to Slack by hand with keywords “GT”. You can get the list of “good tweets” by searching chats with “GT”.

To get list of users, messages or chats, Bot works well. HameSlack has integration for it. Register a bot on slack and enter API token on WP admin screen, that’s all.

Bot can interact Slack API easily. In this case, we need channels.histroy endpoint. Notice that this api’s channel parameter means channel id(e.g. CX92810), not channel name(e.g. general). Don’t worry, HameSlack has short cut for this. Let’s see the code below.

<?php

// Register Weekly Cron.
add_action( 'init', function () {
  if ( ! wp_next_scheduled( 'weekly_tweet_summary' ) ) {
    wp_schedule_event( current_time( 'timestamp', true ), 'weekly', 'weekly_tweet_summary' );
  }
} );

// Add Cron action.
add_action( 'weekly_tweet_summary', function () {
  // Grab API.
  $latest   = current_time( 'timestamp', true );
  $oldest   = $latest - 60 * 60 * 24 * 7;
  $messages = hameslack_channel_history( 'general', $latest, $oldest, [
    'count' => 100,
  ] );
  if ( is_wp_error( $messages ) ) {
    // Failed.
    return;
  }
  // Create content if.
  $content = implode( "\n\n", array_filter( array_map( function ( $message ) {
    // $message is an object.
    $text = trim( $message->text );
    if ( 0 !== strpos( $text, 'GT' ) ) {
      return false;
    }
    $text = trim( str_replace( 'GT', '', $text ) );
    if ( 0 === strpos( $text, 'https://twitter.com' ) ) {
      return $text;
    } else {
      return false;
    }
  }, $messages ) ) );
  // Check content.
  if ( ! $content ) {
    return;
  }
  // Make post and notify.
  $post_id   = wp_insert_post( [
    'post_title'   => sprintf( 'Twitter log: %s', date_i18n( 'Y-m-d', $now ) ),
    'post_content' => $content,
    'post_type'    => 'post',
    'post_status'  => 'draft',
  ] );
  $edit_link = admin_url( "post.php?post={$post_id}&action=edit" );
  hameslack_post( "Created twitter log: {$edit_link}" );
} );

Bit long code? But you can do many thing with customization.

Requirements

PHP 5.4 and over.

Download

Yes, they are open source and any pull requests are welcome!