Slash command and custom integration in Slack using AWS Chalice framework
As per official website of Slack:
Slack is where work flows. It's where the people you need, the information you share and the tools you use come together to get things done.
I have been using Slack since three years and can say that it works amazing in case of team collaboration. So there are so many different features you can use in Slack. But amongst those features my most favourite feature is '/slash-commands'. So you must be wondering what is so special about these slash commands, the best part about them is you can customise them according to your need.
Here are the steps to customise your slash-commands according to your need:
Login into Slack using your credentials.
Create an app by going to api.slack.com. Please refer below screenshot, click on 'your apps':
- Then you will be redirected to below window, click on create new app:
- Here you need to enter the app name and select the workspace in Slack wherever you want to install this app, I have already installed my 'slash_bot' app in 'trial' workspace, I am just showing below screenshot for demo purpose:
- After clicking on create app, below window will appear where we need to focus on slash commands tab:
- After clicking on slash command tab, you need to create your customised command, here for demo purpose I am creating the command which will identify if the entered domain is root domain or not. We can decide other activities based on that but for simplicity I am just printing the message. Root domain is nothing but the combination of domain name and high level domain. So for example, consider xyz.abc.com, in this domain abc.com is a root domain.
The request URL in above image is nothing but the URL to which the Slack will send http post request with the information you have provided while invoking this command in Slack.
So for getting this URL we need the help of our AWS Chalice framework. AWS Chalice allows you to quickly create and deploy applications that use Amazon API Gateway and AWS Lambda. So before using AWS chalice framework you need to complete some of the prerequisites:
Configure AWS access credentials in ~/.aws/config
Create a virtual environment
pip3 install chalice
So if you are not familiar with Chalice then you can refer their official documentation chalice.readthedocs.io/en/stable/quickstart..
After creating your basic app you will be able to see your document dictionary as below:
I am giving my app name as 'slash_bot'.
So now our basic boiler plate is ready, we need to add our specifications in it. We need to create the route named /root-domain in our app and this route will handle our functionality. Below is our code:
So as mentioned in above code it is checking if the domain is root domain or not by calling 'is_root_domain' function. We are calling this function from our helpers.py file which is included inside chalicelib folder. According to official documentation of Chalice,
chalicelib/ - This directory (if it exists) is included in the deployment package. This is where you can add config files and additional application modules if you prefer not to have all your app code in the app.py file.
Now we are ready with our code, we need to deploy this using 'chalice deploy' command. Once you run this command you will see some output like below in your terminal:
The URL which we receive after deploying our API that is REST API URL we need to enter in our Slack 'Request URL' field as mentioned earlier. There we will need to add the URL with route:
https://endpoint.execute-api.us-east-1.amazonaws.com/api/root-domain
Once we set this URL then we need to again reinstall the app and we are good to go with our actual usage. You need to enter the command as shown below in image:
Then result will be as below:
Pretty simple right!! If you wanted to know how I was able to find out if the domain is root domain or not then you can visit my github repository for this whole application. But before that I wanted to mention some of the issues which I have faced while deploying this application:
It is really tough in such type of applications to get the logs locally, so to check the logs you can login into your AWS console and can see the cloudwatch logs which will help you to debug the issues. While integrating with Slack, if there is anything wrong then it will just give one generic error message that is "dispatch_failed" and then Cloudwatch logs will help to debug this issue further.