You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Slack Block Kit、Bolt和Python为/Command返回自定义Block消息

解决方案
  • 你的问题核心是Block Kit结构与接口参数传参不匹配,同时参数来源取值有误
  • 你提到的client.chat_postMessage写法本身是可行的,只需要修正两处问题:
    1. 参数结构匹配:你现有的Block Kit是包裹在attachments字段下的,不能直接传给blocks参数。可以选择两种适配方式:要么提取attachments[0]["blocks"]的数组传给blocks参数,要么保留完整结构传给attachments参数
    2. 频道ID取值错误:不要从event对象取频道ID,Bolt框架的斜杠命令触发时,频道ID、用户ID都可以直接从command对象获取,避免取值失败

修正后的代码示例

@app.command("/q")
def q_command(client, ack, command):
    # 3秒内必须先ack,避免Slack侧超时报错
    ack("Gotcha! Let me take a look...")
    user_id = command["user_id"]
    # 此处保留你原有的变量赋值逻辑:question、answer、author、booktitle、page_num、content等
    client.chat_postMessage(
        channel=command["channel_id"],
        attachments=[
            {
                "blocks": [
                    {
                        "type": "context",
                        "elements": [
                            {
                                "type": "plain_text",
                                "text": user_id + ", this was your question",
                                "emoji": True
                            }
                        ]
                    },
                    {
                        "type": "section",
                        "text": {
                            "type": "plain_text",
                            "text": question,
                            "emoji": True
                        }
                    },
                    {
                        "type": "context",
                        "elements": [
                            {
                                "type": "plain_text",
                                "text": "Here's what I found",
                                "emoji": True
                            }
                        ]
                    },
                    {
                        "type": "section",
                        "text": {
                            "type": "mrkdwn",
                            "text": answer
                        }
                    },
                    {
                        "type": "divider"
                    },
                    {
                        "type": "section",
                        "text": {
                            "type": "plain_text",
                            "text": "Info",
                            "emoji": True
                        }
                    },
                    {
                        "type": "context",
                        "elements": [
                            {
                                "type": "mrkdwn",
                                "text": "*Author:* " + author
                            }
                        ]
                    },
                    {
                        "type": "context",
                        "elements": [
                            {
                                "type": "mrkdwn",
                                "text": "*Book:* " + booktitle
                            }
                        ]
                    },
                    {
                        "type": "context",
                        "elements": [
                            {
                                "type": "mrkdwn",
                                "text": "*Page:* " + str(page_num)
                            }
                        ]
                    },
                    {
                        "type": "section",
                        "text": {
                            "type": "mrkdwn",
                            "text": content
                        }
                    },
                    {
                        "type": "divider"
                    },
                    {
                        "type": "section",
                        "text": {
                            "type": "mrkdwn",
                            "text": "Was this helpful?"
                        },
                        "accessory": {
                            "type": "static_select",
                            "placeholder": {
                                "type": "plain_text",
                                "text": "please select an answer from the drop-down",
                                "emoji": True
                            },
                            "options": [
                                {
                                    "text": {
                                        "type": "plain_text",
                                        "text": "*This was spot on*",
                                        "emoji": True
                                    },
                                    "value": "1"
                                },
                                {
                                    "text": {
                                        "type": "plain_text",
                                        "text": "*This was somewhat helpful*",
                                        "emoji": True
                                    },
                                    "value": "0"
                                },
                                {
                                    "text": {
                                        "type": "plain_text",
                                        "text": "*This was not helpful*",
                                        "emoji": True
                                    },
                                    "value": "-1"
                                }
                            ],
                            "action_id": "static_select-action"
                        }
                    }
                ]
            }
        ]
    )

额外注意事项

确认你的Slack应用已经开启了chat:write和chat:write.public权限,否则会触发权限报错导致无输出。

内容的提问来源于stack exchange,提问作者DirkLX

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 22:12:03