September 22, 2017

Asap: Slack command to notify about things as soon as possible

Slack ASAP is a Slack Slash Command we use at Sophilabs to notify our coworkers about things as soon as possible. The command will send the contact SMS messages, emails and Slack private messages. It can be configured to add more notifiers, or even remove them.

Asap's logo
Asap's logo

It works as a Cowboy Server listening for the Slack Slash Command which is referenced by the Slack ASAP application. The slack app registers a slash command, this calls the server which in turn calls each of the notifiers passing the slack parsed message. A Notifier can do anything with the message, and can even add attachments to the command result. The project comes with some common actions like sending emails using Bamboo, and SMS messages with ExTwilio. Some other notifiers that can be included are: making phone calls, sending a Google Hangouts message, or even playing a synthesized voice with the message through the speakers. The package itself fetches the profile information about the receiver and takes care of the heavy lifting of exposing all the information behind the SlackAsap.Message struct.

A basic workflow is described below:

  1. Cowboy receives the Slash Command request
  2. Transforms the JSON parameters into a SlackAsap.Message _struct_
  3. Calls the SlackAsap.Core notifier (checks Slack token and parses the command)
  4. If it is a help command, it simply prints the usage information
  5. If it is a message, it calls the other notifiers one by one passing the message from the previous Notifier to the next.
  6. It replies to the request with the resulting message after being handled by the last Notifier.
Asap slack command demo
Asap slack command demo

The project itself started as part of our R&D on Elixir here at Sophilabs. It was impressive how fast we were able to build a Slash Command, and make it easier to test doing a TDD approach. The Lasse Ebert article on Mocks in Elixir shows how to quickly make mocks without using any library. I found there are already enough libraries and tools to make things work efficiently for the large majority of common problems like interacting with Slack, sending emails or even SMS. The only problem I found during development is that it’s difficult to narrow down programming or configuration errors. The iex stack traces are not clear enough, which means that the functions need to communicate errors in the most clear manner possible. That way the invoker can interpret possible errors and make necessary corrections. For example, I made a mistake on a configuration in the ExTwilio package, which in the end came down to changing the API configuration syntax from a Map to a string. However due to the lack of error handling, I got an unspecific error like the one below when calling the ExTwilio SMS API function.

{:error, "The requested resource /2010-04-01/Messages.json was not found", 404}

Luckily, Elixir comes with an integrated debugger, available in the Elixir shell: iex. This article shows some techniques on how to to debug modules.

In the end, it was a nice experience to make this package. The code is available at Github. As always, any contributions are welcome.

"Asap: Slack command to notify about things as soon as possible" by Ignacio Avas is licensed under CC BY SA. Source code examples are licensed under MIT. Categorized under open source, research & learning and elixir.

We are sophilabs
A software design and development agency that helps companies build and grow products by delivering high-quality software through agile practices and perfectionist teams.
Check Our Services