Last active
June 5, 2024 17:05
-
-
Save nadar/68a347d2d1de586e4393 to your computer and use it in GitHub Desktop.
Post a message to a slack channel with PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Send a Message to a Slack Channel. | |
* | |
* In order to get the API Token visit: | |
* | |
* 1.) Create an APP -> https://api.slack.com/apps/ | |
* 2.) See menu entry "Install App" | |
* 3.) Use the "Bot User OAuth Token" | |
* | |
* The token will look something like this `xoxb-2100000415-0000000000-0000000000-ab1ab1`. | |
* | |
* @param string $message The message to post into a channel. | |
* @param string $channel The name of the channel prefixed with #, example #foobar | |
* @return boolean | |
*/ | |
function slack($message, $channel) | |
{ | |
$ch = curl_init("https://slack.com/api/chat.postMessage"); | |
$data = http_build_query([ | |
"token" => "YOUR_API_TOKEN", | |
"channel" => $channel, //"#mychannel", | |
"text" => $message, //"Hello, Foo-Bar channel message.", | |
"username" => "MySlackBot", | |
]); | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | |
$result = curl_exec($ch); | |
curl_close($ch); | |
return $result; | |
} | |
// Example message will post "Hello world" into the random channel | |
slack('Hello world', '#random'); |
I did get invalid_payload
response. Issue was with $data
, I had to pass it as json:
function slack($message, $channel)
{
$url = getenv('SLACK_WEBHOOK_URL');
$ch = curl_init($url);
$data = [
'channel' => $channel,
'text' => $message,
'username' => 'SlackBot',
];
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data, JSON_THROW_ON_ERROR, 512));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
Legacy tokens creation disabled...
Yeah from:
https://api.slack.com/legacy/custom-integrations/legacy-tokens
Legacy token management
Tokens may no longer be created, but you can re-issue existing tokens here.
I needed a simple test so now have to look for another PHP example...
The code still works but you have to use Bot User OAuth Token
which requires to:
1.) Create an APP -> https://api.slack.com/apps/
2.) See menu entry "Install App"
3.) Use the "Bot User OAuth Token"
The file_get_contents()
version:
function slack($message, $channel)
{
$url = "https://slack.com/api/chat.postMessage";
$data = http_build_query([
"token" => getenv('SLACK_BOT_TOKEN'), // the "Bot User OAuth Token" value
"channel" => $channel,
"text" => $message,
"username" => "NotificationBot",
]);
$context = stream_context_create([
'http' => [
'method' => 'POST',
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'content' => $data,
'ignore_errors' => true // This is useful to get the content of the response even if the status code is not 200
]
]);
$result = file_get_contents($url, false, $context);
return $result;
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fix it using PHP_EOL. Thanks for this simple and working function that does not include a lot of other codes.