Serverless Interactive Slack Bot: Part One
In this article, I will demonstrate how we integrate the slack bot into google cloud functions and we will understand how slack functionalities are working which is receiving a simple message you send on the slack.
Why do we need to slack bot, slack bot enables us to automate our daily work interactively according to the dynamics within the company we work for. For example, a new person joins your team and you need to explain the onboarding processes to him, everything is ready, but you just need to guide us. Or, as a very different event, you can do all your deployment processes with slack bots, you can create automatic merge requests using Github and GitLab APIs depending on the version control system you use, or you can create commands that will trigger deployments these merge requests.
Create Slack App
First of all, we need to create an app through the slack environment we use for our slack bot, you can simply create your app by following the steps below.
When we go to api.slack.com/apps, the Apps page greets us here, where we can create our first app by clicking Create New App.
Slack offers us two options, we will continue with the first one because we have to create it from scratch, but to explain the second option if you have different apps, it automatically creates an app with the same features by copying the manifests there.
We continue the from scratch option and we enter the app name and select workspace.
After clicking Create App, we can now start using our app. As the next step, there are two things we need to connect us to the app, one is Bot Token and the other is Signin Secret. We can get Signin Secret from the Basic Information page that opens first. For Bot Token, we need to define a scope first, about what our app will access, you can do this by clicking Add New Scope in the Scope field in the OAuth & Permissions tab. We will use chat:write for now. After adding the scope, you can add your app workspace by clicking Install to Workspace and see that your token is created automatically.
You can find more information about scope and what scopes do here. https://api.slack.com/scopes
Create Cloud Function
First of all, why do we need to use a serverless structure, let’s take a look at it first? When using Slack apps, all processes are built on request-response infrastructure, we need to trigger a slack bot somehow over a structure, and after triggering, we need to be able to perform the necessary actions by listening to the requests. The serverless structure will help us here.
Let’s create the first cloud function, first, we go to the cloud functions over the console. Then we click on Create Function, Configuration page welcomes us, here we set Function Name and Authentication.
Here we get our URL. We will use it in Slack bot integration. Then, we switch to the Code tab, here we choose python for our bot, since we will write with Python, two automatically generated files are automatically sent to us as the default cloud function, main.py and requirement.txt, and we deploy them together with them, now the cloud function is ready.
Integrate Cloud Function and Slack
We go back to Slack App and enable event subscription from the Event Subscription tab here, then enter the cloud function URL we got above here. Thus, our cloud function will be triggered with every event we do on slack.
You can choose which events to subscribe to from the Subscribe to bot events field on this page. These events are the same as the scopes we mentioned above, you can subscribe to events according to your own needs.
Everything is ready, let’s write our bot.
Bolt-Python Slack Bot
We will create our bot with Bolt. Bolt is simply a Python framework with which you can create and manage your Slack Apps. After importing Bolt, we connect to our bot with the bot token and signin secret that we received when we first created our slack app with the App, then let’s make our first example with our bot with the mentioned event. In order to do this, we specify which event we will access with the app.event tag is provided by the bolt framework.
You can easily access and process which events you can use here. Then, we can enable our bot to react with the say function that Bolt offers us.
The first two parameters that are important for the count function are channel and the other is message or blocks. You can determine where you can send a message with the value you give in the channel. You need to give channel_id or user_id here. You can automatically call the body parameter of the function you created to get these ids. You can take the event values that you sent slack together with the body, and you can use them by getting the channel id and user id from here, we will proceed with the channel id. Then we will use blocks that allow slack to send us interactive messages.
Here you can create your own slack blocks and see how interactive messages can be created. https://app.slack.com/block-kit-builder/
After creating our block via block-kit-builder, we can now deploy our app. We can easily deploy our app with the command below.
gcloud functions deploy slack-bot --runtime python39 --trigger-http --allow-unauthenticated
After the app is deployed, let’s mention our app.
As you can see, our bot was triggered and got back to us for mentioning it. Well, can we only trigger it with a mention, of course, no, let’s see how we can trigger it with the message.
If ‘hey’ is mentioned in the message, let the app be triggered, we use the app_message event for this and add a keyword we want into it, and trigger it after deploying it in the same way.
Yes, we triggered our bot with the ‘hey’ message. You can use the keywords you want here. Don’t forget to take a look at the bolt document for more listeners or to create interactive messages or actions.
Wish you to create enjoyable slack bots 🤓