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 for Yandex Cloud and create a billing account:
- Navigate to the management console
and log in to Yandex Cloud or create a new account. - On the Yandex Cloud Billing
page, make sure you have a billing account linked and it has theACTIVEorTRIAL_ACTIVEstatus. 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 create or select a folder for your infrastructure on the cloud page
Learn more about clouds and folders here.
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-slackand assign it theeditorrole 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-botas the name. -
In the Specification field, add the
POSTmethod configuration to thepathsparameter: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-challengeWhere:
function_id:for-slack-bot-challengefunction ID.service_account_id:sa-slackservice account ID.
-
Test the link between Slack and Yandex Cloud
- Copy the
for-slack-botAPI 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
Verifiedto 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_SECRETenvironment 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_TOKENenvironment 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.txtand specify these libraries in it:slack_sdk slack_bolt boto3 -
Create a file named
index.pyand paste into it the contents of the1_for-slack-bot-small-talk.pyfile 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.txtand specify these libraries in it:slack_sdk slack_bolt boto3 -
Create a file named
index.pyand paste into it the contents of the2_for-slack-bot-hello-from-serverless.pyfile 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-slackservice account ID.<function_1_ID>:for-slack-bot-small-talkfunction ID.<function_2_ID>:for-slack-bot-hello-from-serverlessfunction 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-serverlessfunction:- In the Command field, enter
/hello-from-serverless. - In the Request URL field, paste the
urlfrom thefor-slack-botAPI gateway specification, adding the/hello-from-serverlesscommand 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-talkfunction:- Send
:wave:to the chat. The bot should respond withHi there, @<username>!. - Send
knock knockto the chat. The bot should respond withWho's there?.
- Send
- To test the
for-slack-bot-hello-from-serverlessfunction, send the/hello-from-serverlesscommand to the chat. The bot should respond withThanks!.
How to delete the resources you created
To stop paying for the resources you created: