消息格式化,通过Slack API用PHP发送按钮
作者:互联网
我正在使用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 标签:php,slack-api,slack
来源: https://codeday.me/bug/20190701/1347383.html