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

Laravel PHP控制器中如何使用Slack API实现列表样式消息?

在Laravel中使用Slack API发送原生格式化列表消息

我目前能从Laravel控制器向Slack频道发送简单字符串消息,代码如下:

Http::post(
    config('slack_webhook'),
    [
        "type" => "rich_text",
        "text" => "• User: $user_name
        \n • Email: $user_email
        \n etc
        "
    ]
);

但我想使用Slack原生的列表样式,而非用Unicode符号和换行模拟,尝试转成PHP格式时一直失败,每次都会报错:

local.ERROR: ErrorException: foreach() argument must be of type array|object, null given

官方的JSON示例结构是这样的:

{
    "type": "rich_text",
    "elements": [
        {
            "type": "rich_text_list",
            "style": "bullet",
            "elements": [
                {
                    "type": "rich_text_section",
                    "elements": [
                        {
                            "type": "text",
                            "text": "Fill out your W-2"
                        }
                    ]
                },
                {
                    "type": "rich_text_section",
                    "elements": [
                        {
                            "type": "text",
                            "text": "Enroll in "
                        },
                        {
                            "type": "link",
                            "text": "benefits",
                            "url": "https://salesforcebenefits.com"
                        }
                    ]
                },
                {
                    "type": "rich_text_section",
                    "elements": [
                        {
                            "type": "text",
                            "text": "Fill out your Slack profile, including:"
                        }
                    ]
                }
            ]
        }
    ]
}

我尝试转成的PHP错误版本:
版本1:

$data = [
    'type' => "rich_text_list",
    'text' => "Report1",
    'elements' => [
        'type' => 'rich_text_section',
        'elements' =>
            [
                'type' => 'text',
                'text' => 'test section 1'
            ],
            [
                'type' => 'text',
                'text' => 'test section 1'
            ],

    ]
];

版本2:

$data = [
    'type' => "rich_text_list",
    'text' => "Report1",
    'elements' => [
        'type' => 'rich_text_section',
        'elements' =>
         [
            [
                'type' => 'text',
                'text' => 'test section 1'
            ],
            [
                'type' => 'text',
                'text' => 'test section 1'
            ],
         ]
    ]
];

我也试过创建JSON对象并用json_decode处理,还是报错。请问怎么在PHP里正确构造Slack的格式化消息?


解决方案

核心问题是没有严格遵循Slack Rich Text的层级结构,正确的PHP代码需要按照以下嵌套逻辑构造:

  1. 最外层是rich_text类型,包含elements数组
  2. rich_text的elements数组中放入rich_text_list对象
  3. rich_text_list必须指定style(如bullet),其elements数组的每一项都是rich_text_section对象
  4. 每个rich_text_section的elements数组里放具体的text或link元素

以下是可直接使用的正确代码:

Http::post(
    config('slack_webhook'),
    [
        'type' => 'rich_text',
        'elements' => [
            [
                'type' => 'rich_text_list',
                'style' => 'bullet',
                'elements' => [
                    // 列表项1:纯文本
                    [
                        'type' => 'rich_text_section',
                        'elements' => [
                            ['type' => 'text', 'text' => "User: $user_name"]
                        ]
                    ],
                    // 列表项2:纯文本
                    [
                        'type' => 'rich_text_section',
                        'elements' => [
                            ['type' => 'text', 'text' => "Email: $user_email"]
                        ]
                    ],
                    // 列表项3:带链接的文本
                    [
                        'type' => 'rich_text_section',
                        'elements' => [
                            ['type' => 'text', 'text' => '查看详情:'],
                            ['type' => 'link', 'text' => '用户资料', 'url' => 'https://your-app.com/user/'.$user_id]
                        ]
                    ]
                ]
            ]
        ]
    ]
);

关键注意点:

  • 所有层级的数组必须正确嵌套:每个对象用PHP关联数组表示,多个对象的集合必须用索引数组包裹
  • 不能遗漏rich_text_list的style字段,这是Slack识别列表类型的必填项
  • 检查语法错误:比如数组嵌套时的逗号、括号配对问题,这类错误会导致字段值为null,触发foreach()报错

如果仍有问题,可以先用json_encode($data)把PHP数组转成JSON字符串,对比官方示例检查结构是否一致。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 15:23:10