【发布时间】: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 中创建这种结构?
【问题讨论】:
-
解决方法是“手动”编写 json
-
我认为您的附件不正确。它必须是一个数组数组。你只有一个简单的数组。