Slack-API - 如何接收来自区块的按钮点击?收到错误“此应用响应状态代码 404”

Slack-API - How to receive button click from a block? Getting error "This App responded with status Code 404"

提问人:NNP 提问时间:11/7/2023 最后编辑:NNP 更新时间:11/8/2023 访问量:46

问:

我正在创建一个松弛机器人,它有一个处理一些数据的斜杠命令。slash 命令以确认块进行响应。斜杠命令处理按预期工作,我正在接收用户的数据并且可以响应它,但是当我单击是/否按钮时,我得到“此应用程序响应状态代码为 404”。

from flask import Flask, Response, jsonify
from flask import request
from slackeventsapi import SlackEventAdapter
import os
from threading import Thread
from slack import WebClient
import json



# This `app` represents your existing Flask app
app = Flask(__name__)

SLACK_SIGNING_SECRET = os.environ['SLACK_SIGNING_SECRET']
slack_token = os.environ['SLACK_BOT_TOKEN']
VERIFICATION_TOKEN = os.environ['VERIFICATION_TOKEN']

# instantiating slack client
slack_client = WebClient(slack_token)


@app.route("/")
def event_hook(request):
    json_dict = json.loads(request.body.decode("utf-8"))
    print(f"event hook: json_dict {json_dict}")
    if json_dict["token"] != VERIFICATION_TOKEN:
        return {"status": 403}

    if "type" in json_dict:
        if json_dict["type"] == "url_verification":
            response_dict = {"challenge": json_dict["challenge"]}
            return response_dict
    return {"status": 500}


slack_events_adapter = SlackEventAdapter(
    SLACK_SIGNING_SECRET, "/slack/events", app
)




def get_confirmation_block(confirmation_text: str, command_id: str):
    return [
        {
            "type": "header",
                    "text": {
                        "type": "plain_text",
                        "text": "New request",
                                "emoji": True
                    }
        },
        {
            "type": "section",
                    "fields": [
                        {
                            "type": "mrkdwn",
                            "text": "*Type:*\nPaid Time Off"
                        },
                        {
                            "type": "mrkdwn",
                            "text": "*Created by:*\n<example.com|Fred Enriquez>"
                        }
                    ]
        },
        {
            "type": "section",
                    "fields": [
                        {
                            "type": "mrkdwn",
                            "text": "*When:*\nAug 10 - Aug 13"
                        }
                    ]
        },
        {
            "type": "actions",
                    "elements": [
                        {
                            "type": "button",
                            "text": {
                                    "type": "plain_text",
                                "emoji": True,
                                "text": "Approve"
                            },
                            "style": "primary",
                            "value": "click_me_123"
                        },
                        {
                            "type": "button",
                            "text": {
                                    "type": "plain_text",
                                "emoji": True,
                                "text": "Reject"
                            },
                            "style": "danger",
                            "value": "click_me_123"
                        }
                    ]
        }
    ]




@app.route("/slack/events", methods=["POST"])
def message_actions():
    # Parse the request payload
    message_action = json.loads(request.form["payload"])
    user_id = message_action["user"]["id"]
    print(message_action)
    print("HERE") # I don't receive this
    return {"", 200}


@app.route("/slack/add_data", methods=["POST"])
def handle_slash_command():
    data = request.form
    print(data)
    channel_id = data["channel_id"]
    text = data['text'].strip()

    # Interactive message with confirmation buttons
    confirmation_text = f'Received: {text}\nDo you want to add this?'
    confirmation_block = get_confirmation_block(confirmation_text,
                                                "add_smth_confirmation")

    slack_client.chat_postMessage(channel=channel_id,
                                  blocks=confirmation_block,
                                  )

    return Response(status=200)


# Start the server on port 3000
if __name__ == "__main__":
    app.run(port=3000)

这就是我在频道中看到的 - 404 错误

我找不到类似的错误,我阅读了 slack-api 文档。/slack/events 在交互性设置中设置为请求 URL。

python 烧瓶 slack-api slack-block-kit

评论


答: 暂无答案