If you are not familliar with OpenFaas, it’s definitely time that you should have a look at it, plus, they are doing some pretty awesome work!
From their documentation: “OpenFaaS (Functions as a Service) is a framework for building serverless functions with Docker and Kubernetes which has first class support for metrics. Any process can be packaged as a function enabling you to consume a range of web events without repetitive boiler-plate coding.”
Make sure to give them a visit at openfaas.com and while you are there, in the world of serverless, have a look at how Alex outlines architecture and patterns he applies in a real-world example, absolutely great read!
What are we doing today?
Today we will build a slack app using python which we will deploy as a function on OpenFaas!
Our slash command will make a request to our slack-request
function, which will respond with a json string, which will then be parsed in a slack attachment message, then based on your button decision, it will then invoke our slack-interaction
function, which will then respond with another message that will allow you to follow the embedded link.
The slack messages are really basic, but you can create a awesome workflow using slack apps. And the best of all, its running on OpenFaas!
Deploying OpenFaas
Docker Swarm and Kubernetes are supported, but since I am using Docker Swarm at the moment of writing, this tutorial will show how to deploy OpenFaas to your cluster. Have a look at OpenFaas Documentation for more detailed information.
Installing OpenFaas CLI for Mac:
1
|
|
Deploy the OpenFaas Stack:
1 2 3 |
|
Credentials: The default configuration will create credentials for you and returns instructions on how to authorize faas-cli, for demonstration it will look more or less like the following:
1
|
|
The UI will be available at: http://127.0.0.1:8080. For this demonstration we will only use the cli.
Create the Functions
I will create 2 python functions:
- The
slack-request
function, which will be associated to the slash command - The
slack-interactive
function, which will be used for interactivity
Create a home directory for your functions and create 2 functions:
1 2 3 |
|
Read the documentation if you’d like to learn more.
Configure the first function:
1
|
|
And our function code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
|
Since our response needs to be parsed as json, we need to set the content type for our environment in our yaml configuration. Read more on it here. Edit the slack-request.yml
:
1 2 3 4 5 6 7 8 9 10 |
|
Now we need to build our image, push it to our repository like dockerhub, then deploy to openfaas:
1 2 3 4 5 6 7 |
|
Configure the slack-interactive
function:
1
|
|
Note that whenever your interact with the first message, a post request will be made against the interactivity request url, you will notice that I decoded the payload (but not doing anything with it), where you will find the callback_id, request_url etc. But for simplicity, I am just using a static json message to respond. Our function code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
|
We also need to set the content type to json:
1 2 3 4 5 6 7 8 9 10 |
|
Build, deploy and ship:
1 2 3 4 5 6 7 8 |
|
When your functions are deployed, go ahead and create the slack app.
Create the Slack App
- Head over to https://api.slack.com/apps and create a new app
- Create a incoming webhook
- Head over to slash commands and create a new command, in my case it was
/supersam
, set the request url to the public endpoint of your function:http://pub-ip:8080/function/slack-request
- Head over to interactive components, set the request url for the interactivity:
http://pub-ip:8080/function/slack-interactive
- If you dont have a public routable address, have a look at ngrok
Once you are set, you should be able to see the slash command integration in your slack workspace, head over to slacks documentation if you run into any trouble.
Test your Slack App
Now that everything is good to go, its time to test your slack app running on OpenFaas!
Head over to slack and run your command /<your-slack-slash-command>
. You should see this output:
When you select one of the buttons, you will get a new message:
This is a real basic example of slack apps, but slack apps are really powerful. You can for example create a slack app that deploys ephemeral environments on swarm, or create change management approval workflows etc.
I hope this was informative, I am really enjoying OpenFaas at the moment and if your have not tested it, I encourage you to try it out, its really, really amazing!