Post messages to Slack Channel

2024-02-08 PHP

find the channel id

There are many ways to find the channel id.
The simplest is, to use the Slack browser application and copy the channel id from the URL after you enter the channel.
The URL looks like https://app.slack.com/client/xxx/C0XXXXXXXX.
The Channel id is the last segment, starting with a 'C'.

Create api token

1. visit the info page

https://api.slack.com/tutorials/tracks/posting-messages-with-curl

2. click "Create app"

start page with button

3. follow the instructions

Select the workspace and make sure there are the right scopes: chat:write, chat:write.public

select a workspace

4. execute the "Install your app" step

install workspace

5. go back

At the end, there is an overlay on top of the page with the headline "Your're all set for now!".
Press the link "Posting messages using curl" and get back to the page from step 1.

page after creating the app

6. copy token.

You are back on the "Posting messages using curl".
The "Create app" is gone.
There is now a black box with your token.

a website with token

post a message

$channelId = "Cxxxxxxxxxx";
$token = "xoxb-xxxxxxx-xxxxxx-xxxxxx";

$ch = curl_init();

$message = [
    "channel" => $channelId,
    "blocks" => [
        [
            "type" => "section",
            "text" => [
                "type" => "mrkdwn",
                "text" => "This is a *test-message*"
            ]
        ]
    ]
];

curl_setopt_array($ch, [
    CURLOPT_URL => "https://slack.com/api/chat.postMessage",
    CURLOPT_HEADER => false,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST => true,
    CURLOPT_HTTPHEADER => [
        'Content-Type: application/json; charset=UTF-8',
        "Authorization: Bearer {$token}"
    ],
    CURLOPT_POSTFIELDS => json_encode($message)
]);

$content = curl_exec($ch);
$header  = curl_getinfo($ch);

alternative

Another way to post messages in a channel is incoming webhooks.

❰ back