我使用webhooks通过Slack API发送消息
我需要发送一个带有报告链接的按钮
我成功地用Python做到了
但是对于PHP,我在附件中的操作数组有问题
$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,但按钮没有
$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中创建这种结构?
答案 0 :(得分:2)
这是您的&#34;代码1&#34;纠正。您的附件属性需要是附件数组的数组。你只有一个简单的数组。
$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。
中工作