You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Slack消息中发送点击后调用API的链接/按钮?

Hey there! The issue with your current code is that Slack doesn’t render raw HTML <a> tags like that in messages—you need to use Slack’s native message formatting or their Block Kit for interactive buttons/links. Let’s break down two solutions for you:

If you just want a clickable text link instead of a button, Slack uses a special syntax for links: <URL|Display Text>. This will render properly in Slack messages without needing any complex structures.

Update your message variable like this:

$msg = "hey! click to approve <http://api2342.in/API/api.php|Approve>";
self::sendSlackMessage($slackId, $msg);

This will show up as a clickable "Approve" link in the Slack user's message, and clicking it will open your API URL directly in their browser (triggering the API call).

Solution 2: Use Slack Block Kit for an Interactive Button (More Polished)

If you want a proper button instead of a text link, Slack’s Block Kit is the way to go. You’ll need to send a JSON payload instead of plain text to sendSlackMessage. Assuming your sendSlackMessage function can accept a JSON string (or an array that gets converted to JSON), here’s how to structure it:

// Build the Block Kit payload
$blockKitPayload = [
    "text" => "hey! click to approve",
    "blocks" => [
        [
            "type" => "section",
            "text" => [
                "type" => "mrkdwn",
                "text" => "hey! click to approve"
            ],
            "accessory" => [
                "type" => "button",
                "text" => [
                    "type" => "plain_text",
                    "text" => "Approve"
                ],
                "url" => "http://api2342.in/API/api.php"
            ]
        ]
    ]
];

// Convert to JSON if your function expects a string
$msg = json_encode($blockKitPayload);
self::sendSlackMessage($slackId, $msg);
  • The url field in the button points directly to your API endpoint.
  • Slack will render this as a clean button, and clicking it will open the API URL (triggering the call).
  • Make sure your sendSlackMessage function is set up to send JSON payloads to Slack’s API—most Slack message libraries support this out of the box.

Quick Notes

  • If your API requires a POST request or needs authentication, a direct link (GET request) might not be enough. In that case, you’d need to set up a middleware endpoint (like a simple PHP script) that receives the Slack button click, handles authentication/POST, and then calls your actual API. But for a simple GET API call, the above solutions work perfectly.
  • Double-check that your Slack app has the chat:write permission to send messages with blocks/links.

内容的提问来源于stack exchange,提问作者fahad_ansari

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:35:00