Building a bot on Slack
The Problem
I try to find solutions for problems that I notice and that was when I came across something at my office . At JustRide , We get to have coffee/tea twice a day . One around 11 in the morning and one around evening 5 . Maari usually gets the count himself or somebody kind in the team would help him , and my friend Sanchit Jain would take a note everyday so that he’s paid correctly . I found this very inefficient and wanted to automate it .
That was when I happened to know about building bots on Slack . We use slack extensively for all our internal communications and thought it would be better to use the same as a platform .
What I wanted
Slack allows us to develop over their API’s using different ways . We can either create Slash commands like /<action>
which when triggered can hit some url that we specify and perform an action or create Incoming webhooks which lets communicate to slack using any of slacks methods. I instead wanted to create an installable Slack Bot that would do the below tasks .
- Send a message with Interactive Buttons to every users in a channel at the particular time asking if they need a coffee/tea or neither .
- When the user responds to the message by clicking any of the buttons , push it to Firebase .
- Once all of them makes a choice , Push the count as a message to a slack channel .
Requirements
I chose to develop using node as I was a little comfortable with javascript and wanted to push something on node . I founded a node-schedule package online that lets you schedule jobs at specific times and let us to specify optional recurrence rules and the love Firebase did not let me think of anything else for storing the data . Above all , you need to have an account on slack and have a team for which you have access to .
Development
- The first step was creating an app in the Your Apps section .
2. Add the necessary permissions .
The above screen is what you will see as soon as you create an App on slack . Since we are going to create a bot , Select Bots .
3. Specify bot username
4. Install the app to your slack team
At the time of developing this bot, an API was to be maintained to generate the Bot OAuth Access Token which we had to save for communicating with slack . The recent update has removed this requirement and lets you Install the App right from the app dashboard . Note this token and save it as a config variable when you deploy your node server .
5. Create Interactive Messages
Interactive buttons are a new and awesome update from slack , that lets attach buttons along with messages which when clicked can trigger the request URL that is specified when enabling it . It also sends along with the request parameters corresponding to the button the user has clicked .
5. Message Builder
Use the Message builder on slack for bulding and previewing your message . Above screenshot is what I want to be send to every user of some channel . Instead of using node-schedule to trigger sending messages to everybody in the channel , I maintained an API `/trigger` on my heroku server . Heroku allowed automatic deploys for any changes to the connected github account which saved my time . You could also use something like localtunnel if you need to run the server on your machine .
6. Configuring Variables
Exposing slack tokens like VerificationToken and Bot User OAuth Access Token online can cause harm . Heroku allows us to Config Variables as key value pairs which can access be accessed in the app as`process.env.<variable-name>`. Verification Tokens are used to verify that requests are actually coming from slack . Bot User OAuth Access Token instead allows us to communicate with slack or invoke a method , for eg: Sending a message to slack . Slack maintains API Methods which lists all the methods that can be used .
7. Sending Buttons
I have used channels.Info method to get the members of channel general (which has the id C3J6S2HGB) for my team . You can also use channels.info method to list all the channels in the team and get the id of what you prefer . After finding the members of the channel , we need to create a direct message channel with every user using the im.open method. And finally , we need to use chat.postMessage method to send the message with buttons to each of the created channel ,i.e to the user .
8. Responding to Buttons
When a user clicks on any of the buttons in the message , the request_url specified will receive a POST request with a `payload` body parameter as JSON.
Here , the first time I do a chat.postMessage to send a message to the channel which will return a ts (timestamp) value that I store in firebase so that I can update every upcoming requests .
And , thats It . Hope this will get you started . You could use a scheduler like node-schedule to schedule when the messages are to be send to the users and also add a Slash Command that will return the count of particular month . Before publishing , you will have to mention the scopes your app need to access of the user’s account .
👋 Helpful? Hit the clap button below as many times as you like ! That should encourage me to write more.