Thumbnail

Send a notification message from Drupal to Slack

Thumbnail

Campbell Tilley

|

I recently created a custom module for the Lil Engine website, that will send a message to slack when a new comment is published. This blog will guide you through integrating Slack and Drupal and the tools needed.

Many businesses are now using Slack for their daily communications and one of the great benefits of Slack is the abundance of app integrations available and the API to create your own. I was recently given the challenge to integrate Drupal and Slack, so that a notification message is sent each time a new comment is published on an article. This is the first "chat-ops" integration we've done, but it won't be the last.

How we created the integration

Create a new Incoming WebHook in your Slack workspace

Description of Slack incoming webhook

To create a custom webhook go to https://<your-workspace>.slack.com/apps/manage/custom-integrations then select Incoming WebHooks from the list. From here you will be able to ‘Add Configuration’ - this is where Slack allows us to set the channel, username and icon, however, we can change this in Drupal so don’t spend too much time worrying about it. The webhook URL is the most important item on this page as this is what we will input into Drupal.

Setup the Slack module on your site

Screenshot of the slack module configuration page

I was able to find an active module on Drupal.org that uses Slack webhooks to configure an integration between Drupal and Slack. The Slack module provides a great starting point for building complex solutions. It also integrates nicely with Rules, if a purely UI approach is preferable.

Install and enable the module on your site. Navigate to Configuration -> Web Services -> Slack -> Configuration. Enter in the webhook URL from step 1 then add the channel name, username and set an icon. If you use an emoji, you can input the code for any emoji in your workspace.

Once you’ve setup your configuration have a go at sending a test message to your Slack channel to make sure everything is working.

Screenshot of the slack test message page

The code

We now have our Drupal site sending messages to Slack, great! But the real purpose of this is to send a message when a comment is published. We could achieve this using the Rules module but we wanted some more custom functionality in our message.

The following code will send a message to Slack each time a comment is published as well as providing the author of the comment, a link to the node, links to reply, review or delete the comment and the comment text.

use Drupal\comment\Entity\Comment;
use Drupal\comment\CommentInterface;
use Drupal\Core\Link;
use Drupal\Core\Url;

/**
* Implements hook_ENTITY_TYPE_insert() for comment entities.
*/

function <my_module>_comment_insert($comment) {

 if ($comment->isPublished()) {
  $config = \Drupal::config('slack.settings');
  $channel = $config->get('slack_channel');
  $username = $config->get('slack_username');
  $node = $comment->getCommentedEntity();
  $nid = $node->id();
  $cid = $comment->id();

  $new_comment = strip_tags($comment->get('comment_body')->value);
  $author = strip_tags($comment->getAuthorName());
  $page_title = $node->title->value;
  $page_url = Url::fromRoute('entity.node.canonical', ['node' => $nid], ['absolute' => TRUE])->toString();
  $reply_url =  Url::fromUri('https://www.<mysite.com>/comment/reply/node/' . $nid . '/<field_article_comments>')->toString();
  $review_url = 'https://www.<mysite.com>/admin/content/comment';
  $delete_url = Url::fromUri('https://www.<mysite.com>/comment/' . $cid .  '/delete?destination=/admin/content/comment')->toString();

  $output = [];
  $output[] = "A new comment has been posted to my site";
  $output[] = "_Page_: <$page_url | $page_title>";
  $output[] = "_Comment author_: $author";
  $output[] = "_Links_: <$reply_url | Reply>, <$review_url | Review>, <$delete_url | Delete>";
  $output[] = "_Comment_: \n $new_comment";

  \Drupal::service('slack.slack_service')
    ->sendMessage(implode("\n", $output), $channel, $username);
 }
}

This will require a bit of tweaking to make it compatible with your site. I have wrapped the code that will need to change in arrows <> for your convenience.

Add new comment

The content of this field is kept private and will not be shown publicly.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.

Comments

  • Allowed HTML tags: <em> <strong> <cite> <blockquote cite> <ul type> <ol start type> <li> <dl> <dt> <dd> <p>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.
  • Use [gist:#####] where ##### is your gist number to embed the gist
    You may also include a specific file within a multi-file gist with [gist:####:my_file].

Spread the word