【问题标题】:Message formatting, send buttons via Slack API with PHP消息格式化,通过 Slack API 和 PHP 发送按钮
【发布时间】:2018-03-27 19:01:03
【问题描述】:

我正在使用 webhook 通过 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 中创建这种结构?

【问题讨论】:

标签: php slack slack-api


【解决方案1】:

这是您的“代码 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 中工作。

【讨论】:

  • @Guilherme 如果此答案解决了您的问题(我相信确实如此),请考虑将其标记为解决方案。泰
  • 太棒了!我太专注于“动作”,以至于我没有意识到“附件”也是一个数组数组
猜你喜欢
  • 2022-01-12
  • 1970-01-01
  • 2019-09-28
  • 1970-01-01
  • 1970-01-01
  • 2017-07-12
  • 1970-01-01
  • 1970-01-01
  • 2022-12-05
相关资源
最近更新 更多