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_BOTapp:- go to Features -> OAuth@Permissions -> Scopes -> Bot Token Scopes:
- add:
chat:writecommandsim: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 Secretto your notes
- copy
- Click
-
Go to Yandex Cloud Console
-
Create
Yandex Service accountin Yandex Cloud:- click
Create service account - add name, in our case
for-slack-bot - add role
editor - click
Create - copy
Service Account IDto your notes
- click
-
Create
Yandex API-Gatewayin Yandex Cloud:- add name, in our case
api-python-slack-bot - click
Create - after publication in spec you can see
serverssection: - Copy
Server Urlto your noteshttps://d5dtglrhssdfgvms1i.apigw.yandexcloud.net(url example from spec)
- add name, in our case
-
Create
Yandex Cloud Functionin 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.pyfile (If is not created as template):- add temp code in
index.pyfile👇:
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 accountyou created earlier - set
timeout settingsto 5 sec - set variable
SLACK_BOT_TOKEN, with keyBot User OAuth Tokenfrom your notes - set variable
SLACK_SIGNING_SECRET, with keySigning Secretfrom your notes - click
Create version - copy
Function IDto your notes
- add name, in our case
-
Go back to
Yandex API-GatewaySettings in Yandex Cloud with namefor-slack-bot:- add
POSTspec 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 Urlfrom your API Gateway here - if it is
okyou will see messageVerified - below click
Subscribe to bot events - then click
Add Bot User Eventand 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 Functionin Yandex Cloud:- delete old temp code from
index.pyfile - copy and paste code from
index.pyto fileindex.pyinYandex Cloud FunctionEditor - create file
requirements.txtinYandex Cloud FunctionEditor - copy and paste data from
requirements.txtto filerequirements.txtinYandex Cloud FunctionEditor - click
Create Version
- delete old temp code from
-
Reopen Your Slack APP on your Windows\Linux OS
-
Everything should work 🙂