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

使用slack-go Actions无法显示按钮的问题求助

问题

我按照一篇Go Slack机器人教程尝试在临时消息中显示“No thanks!”和“Yes, please!”两个按钮,但消息仅显示Text字段内容,按钮并未出现。

相关代码如下:

func handleIsArticleGood() (interface{}, error) {
        attachment := slack.Attachment{
        Text:       "Would you like to see the most recent scores?",
        CallbackID: "ask_us",
        Color:      "#666666",
        Actions: []slack.AttachmentAction{
            slack.AttachmentAction{
                Name:  "action",
                Text:  "No thanks!",
                Type:  slack.ActionType("button"),
                Value: "no",
            },
            slack.AttachmentAction{
                Name:  "action",
                Text:  "Yes, please!",
                Type:  slack.ActionType("button"),
                Value: "yes",
            },
        },
    }
    return attachment, nil
}

从日志中可以看到响应里包含actions,但按钮不显示:

socketmode: 2024/02/29 12:00:27 socket_mode_managed_conn.go:348: Sending Socket Mode response with envelope ID "$envID": &{d02eddad-fe96-4a46-8aa0-ccd6a7e4bd09 {#666666  ask_us 0         Would you like to see the most recent scores?       [] [{action No thanks!  button no  0 [] [] [] <nil> } {action Yes, please!  button yes  0 [] [] [] <nil> }] [] {[]}   }}

代码检查工具(linter)还给出警告:

"redundant type from array, slice or map composite literal"

另外我尝试过将Type的值设为字符串"button"和slack.ActionType类型,都没能解决问题。

解决方案
  • 适配Slack API最新规范
    你参考的教程是2017年的,当前Slack已推荐使用Block Kit替代旧的Attachment结构来构建交互式消息,旧结构在Socket Mode下可能无法正确渲染按钮。

  • 修复冗余类型声明警告
    linter提示的问题是因为切片声明时可以省略显式类型,同时建议使用SDK内置常量避免手动类型转换:

    Actions: []slack.AttachmentAction{
        {
            Name:  "action",
            Text:  "No thanks!",
            Type:  slack.ActionTypeButton, // 使用SDK内置常量
            Value: "no",
        },
        {
            Name:  "action",
            Text:  "Yes, please!",
            Type:  slack.ActionTypeButton,
            Value: "yes",
        },
    },
    
  • 调整响应结构格式
    直接返回Attachment可能不符合Socket Mode的响应要求,改用Block Kit的示例代码如下:

    func handleIsArticleGood() (interface{}, error) {
        blocks := []slack.Block{
            slack.NewSectionBlock(
                slack.NewTextBlockObject("mrkdwn", "Would you like to see the most recent scores?", false, false),
                nil,
                nil,
            ),
            slack.NewActionBlock(
                "ask_us",
                slack.NewButtonBlockElement("action", "no", slack.NewTextBlockObject("plain_text", "No thanks!", false, false)),
                slack.NewButtonBlockElement("action", "yes", slack.NewTextBlockObject("plain_text", "Yes, please!", false, false)),
            ),
        }
        return slack.MsgOptionBlocks(blocks...), nil
    }
    
  • 验证应用权限
    确认你的Slack应用已启用Socket Mode,并且拥有chat:write等必要权限,缺少权限会导致交互式组件无法正常渲染。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 01:07:52