Automating office tasks using Slack bots
Automation allows to do a lot of things faster, even repetitive office tasks. If you are using Slack as a communication tool, using bots may save you a lot of time, specifically in regards to administrative tasks – and we have proven this with a real world example.
On Fridays we order lunch for the whole team using an old-school approach: a spreadsheet that required manual data entry, which was prone to errors. The most important part of this process was to keep track of the orders in a way in which every team member would be able to request a specific meal only once and also ensure that team members would not overwrite each others’ meals. So we decided to implement a bot to simplify this process.
Solving the routine task using a Slack app
From a user’s point of view, we would like the bot interaction to behave in the same way a restaurant’s waiter/waitress does when it received customers:
- The waiter/waitress shows the menu,
- The client makes an order
- If the order has not been confirmed, it can be edited, refined or cancelled
- All the orders can be listed all the times that is required – in our case, it was required to send all orders to our lunch provider.
Our bot is architectured in three basic pieces
- Configure Slack to declare the commands required
- Configure Slack to respond to users’ orders
- A REST API that answers to Slack requests and stores the orders in our backend
Step 1: Defining the Slack command
This can be easily done through the Slack UI, specifying the command name and the URL that will be hit when the command is triggered
Step 2: First response when a user triggers the command
The conversation starts when a user triggers an action using slash command “/lunch-menu”. As described in the previous step, this hits our REST API, which returns another message (a JSON content) with the action and meal selection user interface that allows a user to choose an option for luch. As a user interface, we decided to show a list of the different meals and an “Order” button using Slack message buttons https://api.slack.com/docs/message-buttons
On the backend side, the code that generates the interactive UI using Slack API (https://api.slack.com/messaging/interactivity) is listed below.
IMPORTANT NOTE: before using message buttons, interactive components support has to be enabled in Slack
Step 3: First response when a user triggers the command
Once the UI is rendered, the user just presses the “Order” button that matches her/his desired meal, which in turn will hit the REST API with the necessary information – in this case, basically the name of the meal requested by the user. The REST API now processes the lunch order and return the result of operation
As a result, Slack will send a JSON payload through an HTTP POST request to our API, encoded as a field named payload in a form (content type application/x-www-urlencoded).
To process the result, we need to grab the URL-encoded content of the payload field and parse it – we have used the Jackson JSON Library (https://github.com/FasterXML/jackson) to parse such payload. In the Slack API context, callback_id acts like a topic about the context you are handling. ref: https://api.slack.com/docs/interactive-message-field-guide:
The only remaining piece is the order listing that it is very similar to the flow described in Step 1. For this last use case, we defined a new command called
/list-orders, that generates the list of meals and the quantities ordered by our team members.
With this last command, our bot is complete from the functional perspective. Any other new interaction can be added just creating new commands and including the proper responses in our REST API.