Instantly share code, notes, and snippets.

Embed
What would you like to do?
Post a simple slack message using a bot. See my blog post at https://gophp.io/developing-slack-bots-with-php-part-1 for a guide on how this works.
<?php
/**
* Created by Markus Tenghamn @ GoPHP.io
* Date: 2018-12-29
* Time: 12:47
*
* Post a simple slack message using a bot. You can run this in a console with 'php slack.php'
*
* See my blog post at https://gophp.io/developing-slack-bots-with-php-part-1 for a guide on how this works
*/
// Slack endpoint for chat.postMessage
$url = "https://slack.com/api/chat.postMessage";
// Required fields taken from docs at https://api.slack.com/methods/chat.postMessage
$fields = [
'token' => "your-token",
'channel' => "#general",
'text' => 'This is a test message from GoPHP.io'
];
$fields_string = http_build_query($fields);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
print_r($result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment