Creating a Slack chat bot
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](https://storage.yandexcloud.net/doc-files/slackbot.zip) 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 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
ServerlessBotApp
:- 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:
-
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
ID.service_account_id
:sa-slack
service account ID
-
Test the link between Slack and Yandex Cloud
- Copy the
for-slack-bot
API gateway's service domain. - Select the
ServerlessBotApp
app . - In the app control menu, select Features → Event Subscriptions.
- Enable the Enable Events option.
- In the Request URL field, paste the API gateway address and wait for the
Verified
entry to appear. - Under Subscribe to bot events, 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: this value 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: 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
-
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: 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
-
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 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 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 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 select a chat with the ServerlessBotApp
bot under Apps.
- To test the
for-slack-bot-small-talk
function:- Send the
:wave:
message to the chat. The bot should respond with:Hi there, @<username>!
. - Send the
knock knock
message to the chat. The bot should respond with:Who's there?
.
- Send the
- To test the
for-slack-bot-hello-from-serverless
function, send the/hello-from-serverless
command in the chat. The bot should respond with:Thanks!
.
How to delete the resources you created
To stop paying for the resources you created: