Rails to Slack announcements

Img 1040
Steve Brewer
Over 1 year ago
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'}
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'}
If you're not on Slack yet, grab an invite here

Improvements/suggestions and, of course, comments welcome 😄






Sign in with GitHub to comment
Sign in