I’m trying to send notification messages from my Laravel project’s PHP controller to a slack channel. I am able to get it to send with a simple string, like so:
Http::post(
config('slack_webhook'),
[
"type" => "rich_text",
"text" => "• User: $user_name
n • Email: $user_email
n etc
"
]
);
However I’ve been trying to use Slack’s documentation to send formatted messages and I can’t get it to work. Specifically, I want to use Slack’s actual “list” styling as opposed to duping it using unicode characters and new lines like I did above. So, something like this from the docs:
{
"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:"
}
]
}
]
}
]
},
So I’ve tried to format it for PHP but I think I’m doing something wrong.
$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'
],
]
];
I also tried
$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'
],
]
]
];
I tried the above, and I’ve also tried creating a json object and using json_decode
, and every time I get the following error:
local.ERROR: ErrorException: foreach() argument must be of type array|object, null given
How can I use Slack API formatting in PHP?
1