Slack Incoming Webhooks が Payload was not valid JSON を返し、ドキュメントがあまり意味を成していないようです。
public function index() {
$message = [
"pretext" => "There's been a sale on Example Cart",
"title" => "Sales Order #123456 on Example Cart",
"title_link" => "http://www.example.com/admin/index.php?route=sale/order/info&order_id=123456",
"text" => "There's been a new sale on the Example website. Click the order above to check it out.",
];
$send = self::slack($message);
dd($send); // die and dump curl result
}
public static function slack($message, $room = "test-messages", $color = "#ED7100", $icon = ":mailbox:") {
foreach ($message as $key => $value):
$message[$key] = str_replace(["<", ">", "&"], ["<", ">", "&"], $value);
endforeach;
$fallback = $message["pretext"] . " - " . $message["title"] . " - " . $message["title_link"];
$attachments = [
"fallback" => $fallback,
"pretext" => $message["pretext"],
"title" => $message["title"],
"title_link" => $message["title_link"],
"text" => $message["text"],
"color" => $color,
];
$payload = [
"channel" => "#{$room}",
"channel" => "@bossman",
"icon_emoji" => $icon,
"username" => "webhook-tests",
"attachments" => [$attachments]
];
$data = "payload=" . json_encode($payload);
$ch = curl_init("https://hooks.slack.com/services/endpoint");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
オンライン リンターのいずれかで JSON をリントすると、有効な JSON として返されます。
ドキュメントにない Slack が探しているものは何ですか?