How to create a Slack chat bot using YDB
In this tutorial, you will learn how to use serverless technologies to create a Slack bot that will run commands in a chat and respond to user messages.
To create a bot:
- Prepare the environment.
- Create an application and connect it to Yandex Cloud.
- Get a token and a secret for the app.
- Create functions.
- Edit the API gateway.
- Add commands to Slack.
- Test the Slack bot.
Getting started
Sign up for Yandex Cloud and create a billing account:
- Go to the management console
and log in to Yandex Cloud or create an account if you do not have one yet. - 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.
If you have an active billing account, you can go 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).
Prepare the environment
- Download
the archive with the files 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 app and connect it to Yandex Cloud
Register the Slack app
-
Log in to Slack
. To create a bot, make sure you have workspace management permissions. -
Create an app
:- Click Create an App.
- Select From scratch.
- In the App Name field, enter the name for the app:
ServerlessBotApp
. - Select the available workspace and click Create App.
-
Grant permissions to the
ServerlessBotApp app
:- In the app control 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 control menu, select Settings → Install App and click Install to Workspace → Allow.
Set up 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:
-
Specify the name:
for-slack-bot
. -
Under Specification, 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
: ID of thefor-slack-bot-challenge
function.service_account_id
: ID of thesa-slack
service account.
-
Test the link between Slack and Yandex Cloud
- Copy the service domain of the
for-slack-bot
API gateway. - Select the
ServerlessBotApp
app . - In the app control menu, select Features → Event Subscriptions.
- Enable the Enable Events option.
- In the Request URL field, insert the API gateway's address and wait for the
Verified
entry to appear. - In the Subscribe to bot events section, click Add Bot User Event and select the
message.im
event. - Click Save Changes.
Get a token and a secret for the app
Select the ServerlessBotApp
app
- In the app control menu, select Settings → Basic Information.
- Copy the Signing Secret value from the App Credentials section: it will be used for the
SLACK_SIGNING_SECRET
environment variable. - In the app control 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 configure the bot's reactions to user actions in 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.
Function for messaging
-
Create a function named
for-slack-bot-small-talk
. Make sure it is private. -
Create a function version:
-
Specify the following parameters:
- Runtime environment:
python312
. - Entry point:
index.handler
. - Timeout, sec:
5
. - Service account:
sa-slack
service account you created earlier.
- Runtime environment:
-
Create a file named
requirements.txt
and specify the following libraries in it:slack_sdk slack_bolt boto3
-
Create a file named
index.py
and paste the contents of the1_for-slack-bot-small-talk.py
file from the archive into it. -
SLACK_BOT_TOKEN
SLACK_SIGNING_SECRET
-
Function for responding to commands
-
Create a function named
for-slack-bot-hello-from-serverless
. Make sure it is private. -
Create a function version:
-
Specify the following parameters:
- Runtime environment:
python312
. - Entry point:
index.handler
. - Timeout, sec:
5
. - Service account:
sa-slack
service account you created earlier.
- Runtime environment:
-
Create a file named
requirements.txt
and specify the following libraries in it:slack_sdk slack_bolt boto3
-
Create a file named
index.py
and paste the contents of the2_for-slack-bot-hello-from-serverless.py
file from the archive into it. -
SLACK_BOT_TOKEN
SLACK_SIGNING_SECRET
-
Edit the API gateway
To make sure the bot starts responding to user messages, link the created functions to the app. To do this, edit the specification of the for-slack-bot
API gateway 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
: ID of thesa-slack
service account.<ID_of_function_1>
: ID of thefor-slack-bot-small-talk
function.<ID_of_function_2>
: ID of thefor-slack-bot-hello-from-serverless
function.
Add a command to Slack
Using commands/
character and their list is always visible to the user.
Note
A command will not work if the relevant method is not configured for it in the API gateway.
-
Select the
ServerlessBotApp
app . -
In the app control 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's URL to it. - In the Short descriptions field, enter any short description of the command.
- Click Save.
- In the Command field, enter
-
Re-install the application: in the app control menu, select Settings → Install App and click Reinstall to Workspace → Allow.
-
Enable users to send messages: in the app control menu, select Features → App Home and enable Allow users to send Slash commands and messages from the messages tab in the Show Tabs section.
Test the Slack bot
Open the Slack client and choose a chat with the ServerlessBotApp
bot under Apps.
- To test the
for-slack-bot-small-talk
function:- Send a message saying
:wave:
in the chat. The bot should respond withHi there, @<username>!
. - Send a message saying
knock knock
in the chat. The bot should respond withWho's there?
.
- Send a message saying
- To test the
for-slack-bot-hello-from-serverless
function, send the/hello-from-serverless
command in the chat. The bot should respond withThanks!
.
How to delete the resources you created
To stop paying for the resources you created: