How to create a Slack chat bot
In this tutorial, you will learn how to use serverless technology to create a Slack bot that will run commands in a chat and respond to user messages.
To create a bot:
- Set up your environment.
- Create an application and connect it to Yandex Cloud.
- Get a token and a secret for your app.
- Create functions.
- Modify the API gateway.
- Add commands to Slack.
- Test your Slack bot.
Getting started
Sign up in Yandex Cloud and create a billing account:
- Navigate to the management console
and log in to Yandex Cloud or register a new account. - On the Yandex Cloud Billing
page, make sure you have a billing account linked and it has theACTIVE
orTRIAL_ACTIVE
status. If you do not have a billing account, create one and link a cloud to it.
If you have an active billing account, you can navigate to the cloud page
Learn more about clouds and folders.
Required paid resources
The cost of bot support includes:
- Fee for using the API gateway (see Yandex API Gateway pricing).
- Fee for using functions (see Yandex Cloud Functions pricing).
Set up your environment
- Download
the file archive required to create a bot. - If you do not have a folder yet, create one.
- Create a service account named
sa-slack
and assign it theeditor
role for your folder.
Create an application and connect it to Yandex Cloud
Register your Slack app
-
Log in to Slack
. To create a bot, make sure you have workspace admin permissions. -
Create an app
:- Click Create an App.
- Select From scratch.
- In the App Name field, enter the name for your app:
ServerlessBotApp
. - Select the available workspace and click Create App.
-
Grant permissions to
ServerlessBotApp
:- In the app menu, select Features → OAuth & Permissions.
- Under Scopes → Bot Token Scopes, click Add an OAuth Scope and add the following permissions:
chat:write
,commands
, andim:history
.
-
Install the application: in the app menu, select Settings → Install App and click Install to Workspace → Allow.
Configure a link between Slack and Yandex Cloud
-
Create a function named
for-slack-bot-challenge
. Make sure it is private. -
Create a function version:
-
Create an API gateway:
-
Enter
for-slack-bot
as the name. -
In the Specification field, add the
POST
method configuration to thepaths
parameter:paths: /: get: x-yc-apigateway-integration: type: dummy content: '*': Hello, World! http_code: 200 http_headers: Content-Type: text/plain post: x-yc-apigateway-integration: type: cloud_functions function_id: <function_ID> service_account_id: <service_account_ID> operationId: slack-challenge
Where:
function_id
:for-slack-bot-challenge
function ID.service_account_id
:sa-slack
service account ID.
-
Test the link between Slack and Yandex Cloud
- Copy the
for-slack-bot
API gateway service domain. - Select the app
you created:ServerlessBotApp
. - In the app menu, select Features → Event Subscriptions.
- Check Enable Events.
- In the Request URL field, paste the API gateway address and wait for
Verified
to appear. - Under Subscribe to bot events, click Add Bot User Event and select
message.im
. - Click Save Changes.
Get a token and a secret for your app
Select the appServerlessBotApp
.
- In the app menu, select Settings → Basic Information.
- Copy the Signing Secret value from the App Credentials section: this value will be used for the
SLACK_SIGNING_SECRET
environment variable. - In the app menu, select Features → OAuth & Permissions.
- In the OAuth Tokens for Your Workspace section, copy the Bot User OAuth Token value: it will be used for the
SLACK_BOT_TOKEN
environment variable.
Create functions
Using functions, you can set up the bot’s responses to user actions within the chat. In this tutorial, you will create the following functions:
- For messaging between the bot and the user.
- For getting the bot's response to a simple command.
Messaging function
-
Create a function named
for-slack-bot-small-talk
. Make sure it is private. -
Create a function version:
-
Specify the following settings:
- Runtime environment:
python312
. - Entry point:
index.handler
. - Timeout:
5
. - Service account: Service account you created earlier (
sa-slack
).
- Runtime environment:
-
Create a file named
requirements.txt
and specify these libraries in it:slack_sdk slack_bolt boto3
-
Create a file named
index.py
and paste into it the contents of the1_for-slack-bot-small-talk.py
file from the archive. -
Add these environment variables:
SLACK_BOT_TOKEN
.SLACK_SIGNING_SECRET
.
-
Command response function
-
Create a function named
for-slack-bot-hello-from-serverless
. Make sure it is private. -
Create a function version:
-
Specify the following settings:
- Runtime environment:
python312
. - Entry point:
index.handler
. - Timeout:
5
. - Service account: Service account you created earlier (
sa-slack
).
- Runtime environment:
-
Create a file named
requirements.txt
and specify these libraries in it:slack_sdk slack_bolt boto3
-
Create a file named
index.py
and paste into it the contents of the2_for-slack-bot-hello-from-serverless.py
file from the archive. -
Add these environment variables:
SLACK_BOT_TOKEN
.SLACK_SIGNING_SECRET
.
-
Modify the API gateway
To make sure the bot starts responding to user messages, link the created functions to your app. To do this, update the for-slack-bot
API gateway specification and add the POST
method configurations to the paths
parameter:
/:
post:
x-yc-apigateway-integration:
type: cloud_functions
function_id: <function_1_ID>
service_account_id: <service_account_ID>
operationId: small-talk
/hello-from-serverless:
post:
x-yc-apigateway-integration:
type: cloud_functions
function_id: <function_2_ID>
service_account_id: <service_account_ID>
operationId: hello-from-serverless
Where:
service_account_id
:sa-slack
service account ID.<function_1_ID>
:for-slack-bot-small-talk
function ID.<function_2_ID>
:for-slack-bot-hello-from-serverless
function ID.
Add a command to Slack
Using commands/
and users can always see their full list.
Note
The command will not work without the relevant method configured in the API gateway.
-
Select the app
you created:ServerlessBotApp
. -
In the app menu, select Features → Slash Commands and click Create New Command.
-
Add a command for the
for-slack-bot-hello-from-serverless
function:- In the Command field, enter
/hello-from-serverless
. - In the Request URL field, paste the
url
from thefor-slack-bot
API gateway specification, adding the/hello-from-serverless
command URL to it. - In the Short descriptions field, enter a brief description for the command.
- Click Save.
- In the Command field, enter
-
Re-install the application: in the app menu, select Settings → Install App and click Reinstall to Workspace → Allow.
-
Enable users to send messages: in the app menu, select Features → App Home and enable Allow users to send Slash commands and messages from the messages tab under Show Tabs.
Test your Slack bot
Open Slack and select a chat with ServerlessBotApp
under Apps.
- To test the
for-slack-bot-small-talk
function:- Send
:wave:
to the chat. The bot should respond withHi there, @<username>!
. - Send
knock knock
to the chat. The bot should respond withWho's there?
.
- Send
- To test the
for-slack-bot-hello-from-serverless
function, send the/hello-from-serverless
command to the chat. The bot should respond withThanks!
.
How to delete the resources you created
To stop paying for the resources you created: