Message formatting, submit buttons via Slack API with PHP

I use webhooks to send messages through the Slack API

I need to send a button with a link to the report

I have successfully dealt with Python

But with PHP I am having problems with arrays of actions in attachments

code 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) conclusion:

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

                )

        )

)

Paylod is displayed, but the button is not

code 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) conclusion:

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

        )

)

Neither useful data nor buttons are displayed

Here is a sample documentation, it is very easy to send this using 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"
                }
            ]
        }
    ]
}

How can I create such a structure in PHP?

+4
source share
1 answer

Here is your "code 1" with the fix. The attachments property must be an array of application arrays. You had a simple array.

$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);

.

+1

Source: https://habr.com/ru/post/1695428/


All Articles