Skip to content

Instantly share code, notes, and snippets.

@c-neto
Last active January 4, 2022 00:50
Show Gist options
  • Save c-neto/358cbb9450c2913f376f052518763d0d to your computer and use it in GitHub Desktop.
Save c-neto/358cbb9450c2913f376f052518763d0d to your computer and use it in GitHub Desktop.
# simple example to echo button message content in slack interactive messages
# requires:
# slack_sdk
# flask
from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError
from slack_sdk import WebClient
from flask import Flask, request
import json
import os
# slack token 'xoxb-2896551911366-2903247644338-xptfasdfasdfasdfto'
app = Flask(__name__)
client = WebClient(token=os.getenv('SLACK_APP_TOKEN'))
@app.route('/', methods=['POST'])
def index():
payload = json.loads(request.form['payload'])
button = payload['actions'][0]["text"]["text"]
print(json.dumps(payload, indent=True))
client.chat_postMessage(channel='#devops', text=f"echo reply: {button}")
return 'ok'
app.run('0.0.0.0', port="80")
# payload print:
# {
# "type": "block_actions",
# "user": {
# "id": "U02S4J4GG9M",
# "username": "augusto.liks",
# "name": "augusto.liks",
# "team_id": "T02SCG7STAS"
# },
# "api_app_id": "A02SG96PDS9",
# "token": "Xdcj0dJN3uT83ToGocQezrEP",
# "container": {
# "type": "message",
# "message_ts": "1641252971.000600",
# "channel_id": "C02SXS5MZPB",
# "is_ephemeral": false
# },
# "trigger_id": "2905773424228.2896551911366.14fcb4c7611c545e0dd3e4cec4f12655",
# "team": {
# "id": "T02SCG7STAS",
# "domain": "liksworkspace"
# },
# "enterprise": null,
# "is_enterprise_install": false,
# "channel": {
# "id": "C02SXS5MZPB",
# "name": "privategroup"
# },
# "message": {
# "bot_id": "B02SG9LCQUD",
# "type": "message",
# "text": "Este conte\u00fado n\u00e3o pode ser exibido.",
# "user": "U02SK79JY9Y",
# "ts": "1641252971.000600",
# "team": "T02SCG7STAS",
# "blocks": [
# {
# "type": "header",
# "block_id": "+=L",
# "text": {
# "type": "plain_text",
# "text": "New request",
# "emoji": true
# }
# },
# {
# "type": "section",
# "block_id": "A==",
# "fields": [
# {
# "type": "mrkdwn",
# "text": "*Type:*\nPaid Time Off",
# "verbatim": false
# },
# {
# "type": "mrkdwn",
# "text": "*Created by:*\n<example.com|Fred Enriquez>",
# "verbatim": false
# }
# ]
# },
# {
# "type": "section",
# "block_id": "UMeO",
# "fields": [
# {
# "type": "mrkdwn",
# "text": "*When:*\nAug 10 - Aug 13",
# "verbatim": false
# }
# ]
# },
# {
# "type": "actions",
# "block_id": "OvF1m",
# "elements": [
# {
# "type": "button",
# "action_id": "amm",
# "text": {
# "type": "plain_text",
# "text": "Approve",
# "emoji": true
# },
# "style": "primary",
# "value": "click_me_123"
# },
# {
# "type": "button",
# "action_id": "g5=L",
# "text": {
# "type": "plain_text",
# "text": "Reject",
# "emoji": true
# },
# "style": "danger",
# "value": "click_me_123"
# }
# ]
# }
# ]
# },
# "state": {
# "values": {}
# },
# "response_url": "https://hooks.slack.com/actions/T02SCG7STAS/2916125947937/Ex6U2bIyl4jQVTRRUL0Db5Wi",
# "actions": [
# {
# "action_id": "amm",
# "block_id": "OvF1m",
# "text": {
# "type": "plain_text",
# "text": "Approve",
# "emoji": true
# },
# "value": "click_me_123",
# "style": "primary",
# "type": "button",
# "action_ts": "1641256773.989121"
# }
# ]
# }
#
#
# echo reply: Approve
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment