exception_notification: Slack mentions
Tags: ruby, rails
Published: 2019-01-20 20:43:15

---
title: exception_notification: Slack mentions
published: true
description: How to add mentions to Slack messages that are generated by exception_notification gem
tags: ruby, rails
cover_image: https://thepracticaldev.s3.amazonaws.com/i/90zyzeozm3jzgxgqpfzr.JPG
canonical_url: 
---

Have you ever tried [`exception_notification` gem](https://github.com/smartinez87/exception_notification)? It can help you to catch errors in your rails _(not only)_ app and send them to you via email, Slack or other supported notification methods. 

The problem I encountered is that if you use [Slack as notifier](https://github.com/smartinez87/exception_notification/blob/master/docs/notifiers/slack.md), it will send a message to channel silently, without any mentions. Well, it is not a problem if you have Slack notifications set up to send you notification on each message. But if you receive notifications only for mentioned messages, you can potentially loose some time before seeing this message, which is not very good.

In this article I will show you how you can add mentions to Slack messages that are generated by `exception_notification` gem. 

So, first let's look how basic config looks like if you want to use Slack as messages receiver. 

```ruby
Rails.application.configure do
  config.middleware.use ExceptionNotification::Rack,
    slack: {
      webhook_url: ENV['SLACK_WEBHOOK_URL'],
      channel:     ENV['SLACK_CHANNEL_NAME'],
      additional_parameters: {
        mrkdwn: true
      }
    }
end
```

And how generated message looks like in Slack.

![exception_notification slack notifier](https://thepracticaldev.s3.amazonaws.com/i/7ji1o7q2e8yu3ub8hy37.png)


You can see that message consists of several parts. First is the main text, and then a number of fields like **Exception**, **Hostname** and **Backtrace**. All those fields are automatically generated by `exception_notification`, but you can add your own custom fields too.

In our config file, besides `webhook_url`, `channel`, and `additional_parameters`, you can also add `additional_fields` option, which will accept array of hashes with `title` and `value` keys. That option is responsible for adding more **Backtrace** like fields to Slack message. 

So, we can add field with `title`: `Mentions` and `value`: `<!channel>`. 

```ruby
Rails.application.configure do
  config.middleware.use ExceptionNotification::Rack,
    slack: {
      webhook_url: ENV['SLACK_WEBHOOK_URL'],
      channel:     ENV['SLACK_CHANNEL_NAME'],
      additional_parameters: {
        mrkdwn: true
      },
      additional_fields: [
        { title: 'Mentions', value: '<!channel>' }
      ]
    }
end
```

And that will add one more field to our Slack message which will raise `@channel` mention and all users in this channel will receive notification. 

![](https://thepracticaldev.s3.amazonaws.com/i/mcb5m9vvcif2gkxz7bp1.png)

In the `value` field we wrote `<!channel>`, no just `@channel`, because Slack uses [special message formatting](https://api.slack.com/docs/message-formatting#linking_to_channels_and_users). If you want to mention some user, for example, you will need to write it like this: `<@username>`.