php - 消息格式化,使用 PHP 通过 Slack API 发送按钮

标签 php slack slack-api

我正在使用 webhooks 通过 Slack API 发送消息

我需要发送一个带有报告链接的按钮

我用Python成功了

但是对于 PHP,我在处理附件中的操作数组时遇到了问题

代码1

$data = array(
        "text" => $message
    );

    $actions =
        [
            'type' => "button",
            'text' => "Report1",
            'url' => "https://url.report1"
        ];

    $data += [
        "attachments" =>
            [
                "fallback" => "More details...", //I only get the message and this text in the slack
                'actions' => [$actions] // or array($actions) 

            ]
    ];

    $payload = json_encode($data);

print_r($data) 输出:

Array
(
    [text] => teste
    [attachments] => Array
        (
            [fallback] => More details... 
            [actions] => Array
                (
                    [0] => Array
                        (
                            [type] => button
                            [text] => Report1
                            [url] => https://url.report1
                        )

                )

        )

)

显示了Paylod,但没有显示按钮

代码2

$data = array(
        "text" => $message
    );

    $actions =
        [
            'type' => "button",
            'text' => "Report1",
            'url' => "https://url.report1"
        ];

    $data += [
        "attachments" =>
            [
                "fallback" => "More details...",
                'actions' => $actions

            ]
    ];

    $payload = json_encode($data);

print_r($data) 输出:

Array
(
    [text] => teste
    [attachments] => Array
        (
            [fallback] => More details...
            [actions] => Array
                (
                    [type] => button
                    [text] => Report1
                    [url] => https://url.report1
                )

        )

)

有效负载或按钮均未显示

这是文档示例,使用 python 发送它非常容易

{
    "text": "<@W1A2BC3DD> approved your travel request. Book any airline you like by continuing below.",
    "attachments": [
        {
            "fallback": "Book your flights at https://flights.example.com/book/r123456",
            "actions": [
                {
                    "type": "button",
                    "text": "Book flights 🛫",
                    "url": "https://flights.example.com/book/r123456"
                }
            ]
        }
    ]
}

如何在 PHP 中创建这种结构?

最佳答案

这是经过更正的“代码 1”。您的附件属性需要是附件数组的数组。你只有一个简单的数组。

$message = "Hello Stackoverflow";

$data = array(
        "text" => $message
    );

    $actions =
        [
            'type' => "button",
            'text' => "Report1",
            'url' => "https://url.report1"
        ];

    $data += [
        "attachments" =>
            [
                [
                    "fallback" => "More details...", //I only get the message and this text in the slack
                    'actions' => [$actions] // or array($actions) 

                ]
            ]
    ];

    $payload = json_encode($data);

    print_r($payload);

生成的负载在 message builder 中工作.

关于php - 消息格式化,使用 PHP 通过 Slack API 发送按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49520570/

相关文章:

php - 如何用php将json数据转成html?

php - 在已售订阅计数期间计算续订

javascript - 来自递归获取请求的数组未正确返回

google-apps-script - 为 Slack 斜杠命令配置 Google Apps 脚本回调

curl - Slack提供的cURL命令无法发布内嵌json

php - 如何为一个站点使用多个网站图标

javascript - 在滚动和单击时更改导航文本颜色

api - Slack 为斜杠命令创建确认

amazon-web-services - 我可以自定义通过 Slack 的 AWS 聊天机器人集成接收的消息吗?

python - slack API users.identity 不起作用