fbpx

Slackbot using Lambda functions

Slackbot using Lambda functions

Why Slack commands?

Slack is currently the ubiquitous communication platform used in all industries. 

A slack command is an app triggered by a “/” followed by a keyword, which triggers a POST request to an API in the background. 

Here is a sample of the information sent by the command

What’s the catch?

Slack commands have a timeout of 3 seconds which is not enough to query the database and create and post the pdf. Have in mind Lambda functions need a few milliseconds to be up and running. All this makes it impossible to process everything on a single API call.

The good news is that we don’t need to respond to the slack command with the PDF itself! We could send it as a file to a channel shared between the user and the App.

At first, we tried to send a quick response to the command and leave the pdf creation process on a separated thread on the same Lambda function, but the process never ended correctly. We suspect Lambda functions have a limitation on the time they run after the response is sent, killing any other threads running in the background.

To solve this we call a second Lambda function which receives the request to create and post the PDF to the App channel without having to return any response.

Proposed architecture

API Gateway

The API gateway exposes a POST method pointing to the main Lambda function.

SlackCommand Lambda

The SlackCommand lambda receives the request creates a call to the SlackPoster function and returns a response message right away.

The key here is to set the Lambda invocation as an Event. We saw that not only the Lambda is called correctly but if an error happens it runs one more time until a result is returned.

Here is a snip of the code we used. Note we return an APIGatewayProxyResponse

Check the gist

⚠️ Note: you need to give permissions for Lambda functions to AssumeRole to call the second Lambda

SlackPoster Lambda

Once we have the information we need to create a PDF. Without the procession of returning a quick response, we can take our time to create the payload (less than 5 minutes, that’s the limit of Lambda functions.

Check the gist

The magic happens inside the method in charge of uploading the PDF. Using the parameters from the slack command we know the user name and the channel shared with the bot.

Check the gist

Conclusion

We discover that creating a slack bot is a simple task. They not only enhance our current communication workflows but also can help in other areas such as accounting, management, and production.

It only takes a little bit of effort to learn and deploy.

What do you think? Share with us your ideas of good implementations for a Slack bot!

547 Views
Author

I'm an Architect who decided to make his life easier by coding. Curious by nature, I approach challenges armed with lateral thinking and a few humble programming skills. Love to work with passioned people and push the boundaries of the industry. Bring me your problems/Impossible is possible, but it takes more time.