Simple Slack bot example using Yandex Cloud
-
First you need to Create your App:
- Click
Create New App
- In the
Name field
, enter the app name, in our case:MY_SLACK_BOT
- Select the workspace and click
Create app
- Grant permissions to the
MY_SLACK_BOT
app:- go to Features -> OAuth@Permissions -> Scopes -> Bot Token Scopes:
- add:
chat:write
commands
im:history
- go to Features -> OAuth@Permissions:
- click
Install to Workspace
- click
Allow
- at the top you will see pop-up
Success!
- below you can see
Bot User OAuth Token
. Copy to your notes
- click
- go to Settings -> Basic Information:
- copy
Signing Secret
to your notes
- copy
- Click
-
Go to Yandex Cloud Console
-
Create
Yandex Service account
in Yandex Cloud:- click
Create service account
- add name, in our case
for-slack-bot
- add role
editor
- click
Create
- copy
Service Account ID
to your notes
- click
-
Create
Yandex API-Gateway
in Yandex Cloud:- add name, in our case
api-python-slack-bot
- click
Create
- after publication in spec you can see
servers
section: - Copy
Server Url
to your noteshttps://d5dtglrhssdfgvms1i.apigw.yandexcloud.net
(url example from spec)
- add name, in our case
-
Create
Yandex Cloud Function
in Yandex Cloud:- add name, in our case
function-for-api-python-slack-bot
- click
Create
- choose programming language, in our case it will be
Python
: - create
index.py
file (If is not created as template):- add temp code in
index.py
file👇:
import json def handler(event, context): print(f"Received event:\n{event}\nWith context:\n{context}") slack_body = event.get("body") slack_event = json.loads(slack_body) challenge_answer = slack_event.get("challenge") return { 'statusCode': 200, 'body': challenge_answer }
- add temp code in
- select below
Yandex Service account
you created earlier - set
timeout settings
to 5 sec - set variable
SLACK_BOT_TOKEN
, with keyBot User OAuth Token
from your notes - set variable
SLACK_SIGNING_SECRET
, with keySigning Secret
from your notes - click
Create version
- copy
Function ID
to your notes
- add name, in our case
-
Go back to
Yandex API-Gateway
Settings in Yandex Cloud with namefor-slack-bot
:- add
POST
spec below👇: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
- add
-
Go back to
API Slack settings
:- go to Basic Information -> Add features and functionality -> -> Event Subscribtions -> Enable Events
- click
On
- paste
Server Url
from your API Gateway here - if it is
ok
you will see messageVerified
- below click
Subscribe to bot events
- then click
Add Bot User Event
and add event calledmessage.im
- click
Save Changes
- you will see pop-up
Success!
- go to Features -> App Home and select
Your App’s Presence in Slack
- click
Always Show My Bot as Online
- select
Show Tabs
- click
Allow users to send Slash commands and messages from the messages tab
-
Go back to
Yandex Cloud Function
in Yandex Cloud:- delete old temp code from
index.py
file - copy and paste code from
index.py
to fileindex.py
inYandex Cloud Function
Editor - create file
requirements.txt
inYandex Cloud Function
Editor - copy and paste data from
requirements.txt
to filerequirements.txt
inYandex Cloud Function
Editor - click
Create Version
- delete old temp code from
-
Reopen Your Slack APP on your Windows\Linux OS
-
Everything should work 🙂