1

Slack Incoming Webhooks 返回 Payload 不是有效的 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(["<", ">", "&"], ["&lt;", "&gt;", "&amp;"], $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;
}

当我在任何在线 linters 中对 JSON 进行 lint 时,它会作为有效的 JSON 返回。

Slack 在寻找什么不在文档中?

4

1 回答 1

1

看起来您的 POST 数据是payload=在前面发送的,而不是作为密钥发送的。

这个要点像这样发送数据:

$data = json_encode($payload);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('payload' => $data));
于 2015-12-06T10:08:51.460 回答