DEV Community

loading...
Cover image for exception_notification: Slack mentions

exception_notification: Slack mentions

tetiross profile image Rostyslav Semanyshyn Originally published at dev.to ・Updated on ・2 min read

Have you ever tried exception_notification gem? 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, 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.

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

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>.

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.

In the value field we wrote <!channel>, no just @channel, because Slack uses special message formatting. If you want to mention some user, for example, you will need to write it like this: <@username>.

Discussion (1)

pic
Editor guide
Collapse
defman profile image
Sergey Kislyakov πŸ‡·πŸ‡Ί πŸ‡ΊπŸ‡Έ

Looks like a lightweight variant of Sentry.