消息格式,通过 Slack API 和 PHP 发送按钮
Message formatting, send buttons via Slack API with PHP
我正在使用网络钩子通过 Slack 发送消息 API
我需要将带有 link 的按钮发送到报告
我用 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 中创建这种结构?
这是您的 "code 1" 更正。您的附件 属性 需要是附件数组的数组。你只有一个简单的数组。
$message = "Hello Whosebug";
$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 中工作。
我正在使用网络钩子通过 Slack 发送消息 API
我需要将带有 link 的按钮发送到报告
我用 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 中创建这种结构?
这是您的 "code 1" 更正。您的附件 属性 需要是附件数组的数组。你只有一个简单的数组。
$message = "Hello Whosebug";
$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 中工作。