Rails to Slack announcements

Img 6939
Steve Brewer
2 months ago, 15 Jan
Until we've added notifications to the site, this handy gem lets you post messages to Slack from Rails.

By adding a line to a controller, we now get a message in our Slack #general when someone comments on the site. It posts their name, comment and a link to the comment so we can reply to it easily.

I've also put recent comments on the home page.

Here's the after_action I've set on the comment 'create' action in the comments_controller, which will fire after each comment is created: 
  def slack
    Slacked.post_async "#{@comment.author.name} #{@comment.body} - #{comment_url}",
                        {channel: 'general', username: 'Comment Bot'}
  end
When a new post is published, we'll be notified about that too. I've added this after_action to the 'create' action in the posts_controller:   
  def slack
    Slacked.post "#{@post.created_by.name} published a post: #{@post.title} - #{post_url(@post)}",
                  {channel: 'notifications', username: 'Blogger Bot'}
  end
If you're not on Slack yet, grab an invite here

Improvements/suggestions and, of course, comments welcome 😄

Rails

Slack

Slackbot

Webhook

Comments

  • Img 6939
    Steve Brewer
    2 months ago, 15 Jan
  • Love Rails!!
  • Img 6939
    Steve Brewer
    2 months ago, 15 Jan
  • When we get spammed to death I'll turn it off 😆

Sign in with GitHub to comment
Sign in