What is Slack?
If you are new to Slack, It’s a great platform for team collaboration and instant messaging used in and out of organizations to help team communication and collaboration.
I first used slack for a study group. You can create different channels to separate messages and discussions. You can create private channels as well to keep messages private in a team.
The best functionality is that it also allows integrations on it’s platform. And this is what makes it different than other messaging and collaboration platforms.
You can integrate google calender, twitter, trello etc. It’s also let’s you create custom applications like bots.
In this post, I will walk you through building a MeetupBot for Slack using Nodejs. It will give you list of meetups going on nearby your location based on your interest.
It will use Slack’s slash commands. You can type /meetupbot from within Slack to call the MeetupBot and it will greet you along with the list of commands.
You will need basic knowledge of Nodejs and how APIs work. Let’s get started.
Steps for building MeetupBot for Slack
Step 1 - Project Setup
First fork my repository here
Then go to glitch.com and create a project and edit the project name to a shorter name.
Click on project name > advanced options and then click import the repo from github. You first need to grant access to import your repositories into glitch.
Go to Meetup Api here and click on API Key tab and save that as you will pass it with every request to Meetup API.
In your glitch project open .env file and set a variable SECRET as Your Meetup API Key as
Click on Show live in glitch and you will get your glitch project url.
Step 2 - Create a Slack App
- Go to https://api.slack.com/ and then click on “Your Apps > Create New App”. It will show you following screen.
Enter “App Name” and select “Development Slack Workspace” and click on Create App. Now we need to do 3 things to see it working in our slack workspace.
On the next screen you will see your App Configuration page with following things.
- Activate incoming webhooks.
- Create Slash commands.
- Install your app to your workspace.
- Now click on “Incoming Webhooks” and activate it. Incoming Webhooks allows you to post messages into slack.
Next thing click on “Slash Commands” and create one as /meetupbot. Command as /meetupbot,
Request url as <glitch-project-url>/meetupbot,
and a Short description and a Usage hint.
By activating incoming webhooks & creating a slash commands you should have already got a green tick on Permissions.
- Now 3rd and last click on install your app to your workspace and that will take you to next screen to confirm and authorize before installing. And now you are good to go.
Step 3 - Test it in your channel
Open your slack team channel and type /meetupbot and you should be able to see your commands popping up. Click enter and you will see a greeting message from meetupbot and list of commands that you can use.
Since you have created only one slash command go to your App page and create 1 more commands as /meetupbot-show with request url as <glitch-project-url>/meetupbot-show (Follow step 2 - create slack command).
So that’s it, Congratulations you have successfully created a MeetupBot for Slack using Nodejs.
Lets understand the code.
We are using Google Geocode API to get Latitude and Longitude from location/address parameter that is passed in the command. This latlong along with interest parameter is then passed to meetup api to get list of meetups.
What happens when you call /meetupbot ? It makes a Post request to glitch-project-url/meetupbot.
The request body contains user_name, text and other info. the reply object is the json response format for the slack API.
What happens when you call /meetupbot-show ? It makes a Post request to glitch-project-url/meetupbot. The request body contains user_name, text (i.e location and interest separated by “&”) and other info.
We first make sure the location and interest parameters sent with command are not blank.
The meetup api returns an array of meetup event object and we iterate through this array to make an array of event objects in slack response format and keep pushing it in attachments array which we created in the start.
And that reply object with event attachments is then returned as response and is shown in your slack.
This response will only be visible to you (i.e to the user who calls the bot ) and won’t disturb other members of channel.
In the above code we have 2 Promises as follows:
getGeoCode() - This take location as parameter and makes an API call to google geocode API with location as query string and returns latlong.
getMeetupEvents() - This takes location and interest as parameters and makes API call to meetup API containing key i.e API Key, Latitude, Longitude, text i.e interest and radius as query string parameters
Also helps in avoiding nested callbacks by using chainable then. If you have nested callbacks in code then it looks like pyramid structure also known as “callback hell”.
The official MeetupBot has one more command as /meetupbot-find to get list of meetup group in your location/area and also has Oauth code so that you can install it by clicking add to slack button.
Did you find this article useful? Write your comments below.
If you found this article helpful, do share with your friends. Thank you :)