Sending Slack notifications with Ruby on Rails
Tags: rails, ruby, slack, beginners
Published: 2019-03-27 14:43:56

---
title: Sending Slack notifications with Ruby on Rails
published: true
description: Send Slack notifications when you have a new user, new purchase or whenever you want.
cover_image: https://thepracticaldev.s3.amazonaws.com/i/vkakdrmy9plyri3wpna5.png
tags: rails, ruby, slack, beginners
---

In my latest article I showed you how to [integrate Stripe on Ruby on Rails](https://dev.to/ksushiva/integrate-stripe-in-your-ruby-on-rails-app-3dc4). Today I'm writing a quick tutorial about how to get Slack notification every time someone makes a purchase on your site. It's very easy and simple. I chose Slack notification because as for me this platform is the best to share short messages instantly with your collaborators.

For the first part the [Slack tutorial](https://api.slack.com/incoming-webhooks) is quite explicite, so I suggest you to read it and do what they say. Firstly you need to create a Slack app, then activate Incoming Webhooks and choose a channel to post your notifications. After doing the Slack tutorial, navigate to the page of your __app => Features => Incoming Webhooks__ and you should see something like this:

![alt text](https://thepracticaldev.s3.amazonaws.com/i/76agc9az74ypqzgdxgwa.png "Incoming Webhooks")

Now, go to your Ruby on Rails application and in the terminal open the nano editor to add the Webhook URL in encrypted rails credentials:
```
$ EDITOR=nano rails credentials:edit
```
In the editor, add:
```yaml
slack:
   token: 'https://hooks.slack.com/services/your/webhook/url'
```
Use Ctrl+X to exit editor.

Now in our _Gemfile_ add a new gem:
```ruby
gem 'slack-notifier'
``` 
Then, run the command 'bundle install' in the terminal to install it. Now, in _config/initializers_ create _slack_notifier.rb_:
```ruby
module SlackNotifier
  CLIENT = Slack::Notifier.new Rails.application.credentials.slack[:token]
end
```
Finally, the last step is to send the notification. I want that every time someone pays I get a notification on slack. So we need to add this message in the action that subscribes the user to a plan. If you followed [my previous tutorial](https://dev.to/ksushiva/integrate-stripe-in-your-ruby-on-rails-app-3dc4) it will be in the action __subscribe__ in the view _views/controllers/billing_controller.rb_:

```ruby
def subscribe
    #code which you can see in my previous tutorial

      subscription.save
      SlackNotifier::CLIENT.ping "💸 Boom! New purchase for #{subscription.items.data[0].plan.amount / 100}€ from #{current_user.email}! 💸"
end
```
Now I also want to have a notification when a user registers. In order to do it we go to _app/models/user.rb_:

```ruby
after_create :notify_slack

  def notify_slack
    SlackNotifier::CLIENT.ping "🎉 New user: #{email} 🎉"
  end
```
Now we are going to test if everything works well. So we are going on our application, create a new user and subscribe him to one of the plans. You should get 2 notifications which look something like this:
![alt text](https://thepracticaldev.s3.amazonaws.com/i/o20b258fc51q426rofuw.png "Notifications Slack")

That's it! I hope you will see this notifications very often!