Creating a Slack chat bot
In this use case, you'll learn how to use serverless technologies to create a Slack bot that will run commands in a chat and respond to user messages.
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 and assign it the
editor
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 a custom app.
- In the Name field, enter the app name:
ServerlessBotApp
. - Select the available workspace and click Create app.
- Grant permissions to the
ServerlessBotApp
app:- In the app management menu, select OAuth & Permissions.
- Under Bot Token Scopes, add the following permissions:
chat:write
,commands
, andim:history
. - After updating the permissions, reinstall the app: you'll see a notification at the top of the page. To reinstall the app, follow the link in the notification and click Allow.
Set up a link between Slack and Yandex Cloud
-
Create an API gateway named
for-slack-bot
. -
Create a function named
for-slack-bot-challenge
. Make sure it is private. -
Create a function version:
- Create a file named
index.py
and paste the contents of the0_for-slack-bot-challenge.py
file from the archive into it. - Specify the following parameters:
- Runtime environment:
python37
- Entrypoint:
index.handler
- Timeout, sec:
5
- Service account: Service account you created earlier
- Runtime environment:
- Create a file named
-
Edit the
for-slack-bot
API gateway. Add to thepaths
parameter thePOST
method configuration: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
Parameters:
<Function ID>
: ID of thefor-slack-bot-challenge
function.<Service account ID>
: ID of the service account.
Test the link between Slack and Yandex Cloud
- Copy the service domain of the
for-slack-bot
API gateway. - Select
theServerlessBotApp
app. - In the app management menu, select Event Subscriptions.
- Set Enable Events to on.
- In the Request URL field, insert the API gateway's address and wait for the Verified entry to appear.
Get a Token and Secret for the app
SelectServerlessBotApp
app:
- The Bot User OAuth Token value from the OAuth & Permissions section will be used for the
SLACK_BOT_TOKEN
environment variable. - The Signing Secret value from the Basic Information section will be used for the
SLACK_SIGNING_SECRET
environment variable.
Create a database
The bot will use responses to chat commands and messages taken from Yandex Managed Service for YDB. To enable this, prepare a table:
- Create a database named
for-slack-bot
in Serverless mode. - In the
for-slack-bot
database, createa YDB table namedcoffee
:-
Go to the Navigation tab.
-
In the top-right corner, click New SQL query. The Query page opens.
-
In the Query field, enter:
CREATE TABLE coffee ( id Utf8, name Utf8, PRIMARY KEY (id) );
-
Click Run.
-
- Add an entry to the table. For example, specify the coffee variety name and id = 1.
Create functions
Using functions, you can configure the bot's reactions to user actions in the chat. You'll create the following functions in this use case:
- For messaging between the bot and the user.
- For getting the bot's response to a simple command.
- For the bot to select a response to a command from Managed Service for YDB.
A function for messaging
-
Create a function named
for-slack-bot-small-talk
. Make sure it is private. -
Create a function version:
-
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. -
Specify the following parameters:
- Runtime environment:
python37
- Entrypoint:
index.handler
- Timeout, sec:
5
- Service account: Service account you created earlier
- Runtime environment:
-
SLACK_BOT_TOKEN
SLACK_SIGNING_SECRET
-
A function for responding to commands
-
Create a function named
for-slack-bot-hello-from-serverless
. Make sure it is private. -
Create a function version:
-
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. -
Specify the following parameters:
- Runtime environment:
python37
- Entrypoint:
index.handler
- Timeout, sec:
5
- Service account: Service account you created earlier
- Runtime environment:
-
SLACK_BOT_TOKEN
SLACK_SIGNING_SECRET
-
A function for selecting responses to commands
-
Create a function named
for-slack-bot-what-kind-of-coffee
. Make sure it is private. -
Create a function version:
-
Create a file named
requirements.txt
and specify the libraries and the version of Managed Service for YDB in it:slack_sdk slack_bolt boto3 ydb==0.0.41
-
Create a file named
index.py
and paste the contents of the3_for-slack-bot-what-kind-of-coffee.py
file from the archive into it. -
Specify the following parameters:
- Runtime environment:
python37
- Entrypoint:
index.handler
- Timeout, sec:
5
- Service account: Service account you created earlier
- Runtime environment:
-
SLACK_BOT_TOKEN
SLACK_SIGNING_SECRET
-
Add the variables to work with Managed Service for YDB:
ENDPOINT
: First part of the Endpoint field value from the Overview section in thefor-slack-bot
database properties (the one preceding/?database=
), e.g.,grpcs://ydb.serverless.yandexcloud.net:2135
.DATABASE
: Second part of the Endpoint field value from the Overview section in thefor-slack-bot
database properties (the one following/?database=
), e.g.,/ru-central1/r1gra875baommfd5leds/g5n22e7ejfr16h9oif9d
.USE_METADATA_CREDENTIALS
= 1.
-
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 and add to the paths
parameter the POST
method configurations:
paths:
/:
post:
x-yc-apigateway-integration:
type: cloud_functions
function_id: <ID_of_function_1>
service_account_id: <Service account ID>
operationId: slack-challenge
/hello-from-serverless:
post:
x-yc-apigateway-integration:
type: cloud_functions
function_id: <ID_of_function_2>
service_account_id: <Service account ID>
operationId: hello-from-serverless
/what-kind-of-coffee:
post:
x-yc-apigateway-integration:
type: cloud_functions
function_id: <ID_of_function_3>
service_account_id: <Service account ID>
operationId: /what-kind-of-coffee
Parameters:
<Service account ID>
: ID of the 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.<ID_of_function_3>
: ID of thefor-slack-bot-what-kind-of-coffee
function.
Add commands to Slack
Using commands/
character and their list is always visible to the user.
Note
A command will not work if the corresponding method is not configured for it in the API gateway.
- Select
theServerlessBotApp
app. - In the app management menu, select 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
- Add a command for the
for-slack-bot-what-kind-of-coffee
function:- In the Command field, enter
/what-kind-of-coffee
. - In the Request URL field, paste the
url
from thefor-slack-bot
API gateway specification, adding the/what-kind-of-coffee
command's URL to it. - In the Short descriptions field, enter any short description of the command.
- Click Save.
- In the Command field, enter
- After adding the new commands, reinstall the app: you'll see a notification at the top of the page. To reinstall the app, follow the link in the notification and click Allow.
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 respondHi there, @<username>!
. - Send a message saying
knock knock
in the chat. The bot should respondWho'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 respondThanks!
.
- Send the
- To test the
for-slack-bot-what-kind-of-coffee
function:- Send the
/what-kind-of-coffee
command in the chat. The bot should respondToday we use <entry from the coffee table>
.
- Send the