World of Slack Programming

Slack has been medium of communication between teams and most often customers as well, it provides a rich set of APIs for you to create Web hooks – which makes it easy to create channels , add users to channel , send rich text content to users or channels – this article should get you started in to the wonderful world of slack programming

Login to Slack.com create an account or login if you already have an account.

No alt text provided for this image

Create a workspace , example i have created a workspace by name oscatech , so my workspace url will be https://oscatech.slack.com/ , you can easily create channels

Add Incoming Webhooks , Click on Add Apps + and add Incoming Webhooks

No alt text provided for this image

Most important thing is to make a note of Webhook URL

No alt text provided for this image
 Request
curl -X POST --data-urlencode
"payload={\"channel\": \"#general\",
\"username\": \"webhookbot\", \"text\":
\"This is posted to #general and comes from a bot named webhookbot.\",
\"icon_emoji\": \":ghost:\"}"
https://hooks.slack.com/services/AA/BB/CC
No alt text provided for this image

You can create a payload in JSON file and send the same , this looks more tidy

 vi message.json
{
"username":"Madhu",
"channel" : "#devops",
"icon_emoji": ":ghost:",
"text": "Cricket match"
}
Request
curl -XPUT
"https://hooks.slack.com/services/AAA/BBB"
--data-binary "@message.json"
-H "Content-type:application/json"
No alt text provided for this image

Sending slightly complex JSON message to Slack

Request
{
"text": "Now back in stock!:tada:",
"username":"Madhu",
"channel" : "#devops",
"icon_emoji": ":ghost:",
"attachments": [
{
"title": "The Further Adventures of Slackbot",
"fields": [
{
"title": "Volume",
"value": "1",
"short": true
},
{
"title": "Issue",
"value": "3",
"short": true
}
],
"author_name": "Stanford S. Strickland",
"author_icon": "http://a.slack-edge.com/7f18https://a.slack-edge.com/a8304/img/api/homepage_custom_integrations-2x.png",
"image_url": "http://i.imgur.com/OJkaVOI.jpg?1"
},
{
"title": "Synopsis",
"text": "After @episod pushed exciting changes to a devious new branch back in Issue 1, Slackbot notifies @don about an unexpected deploy…"
},
{
"fallback": "Would you recommend it to customers?",
"title": "Would you recommend it to customers?",
"callback_id": "comic_1234_xyz",
"color": "#3AA3E3",
"attachment_type": "default",
"actions": [
{
"name": "recommend",
"text": "Recommend",
"type": "button",
"value": "recommend"
},
{
"name": "no",
"text": "No",
"type": "button",
"value": "bad"
}
]
}
]
}

Send Request

curl -XPUT "https://hooks.slack.com/services/AAAA/BBB/CCC" 
--data-binary "@message.json" -H "Content-type:application/json"
No alt text provided for this image

One more example of JSON sending few links and text as attachment

{
"username":"Madhu",
"channel" : "#devops",
"icon_emoji": ":ghost:",
"text": "User Interaction with Slack ! - ",
"attachments": [
{
"event": "star_added",
"title": "Star a Message",
"title_link": "https://get.slack.help/hc/en-us/articles/201331016-Using-stars",
"text": "Stars are a way to mark an item in Slack as important. You can star channels or direct messages to move them to the top of your left sidebar, or star messages so you can easily come back to them later."
},
{
"event": "reaction_added",
"title": "Respond With a Reaction Emoji",
"title_link": "https://get.slack.help/hc/en-us/articles/206870317-Emoji-reactions",
"text": "You can quickly respond to any message on Slack with an emoji reaction. Reactions can be used for any purpose: voting, checking off to-do items, showing excitement."
},
{
"event": "pin_added",
"title": "Pin a Message",
"title_link": "https://get.slack.help/hc/en-us/articles/205239997-Pinning-messages-and-files",
"text": "Important messages and files can be pinned to the details pane in any channel or direct message, including group messages, for easy reference."
}
]
}




No alt text provided for this image

Sending Slack messages from PHP

Download the Slack Repo from following GitHub https://github.com/SimonBackx/Slack-PHP-Webhook

Your PHP file should be

 ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
// slack.php is the one that we downloaded from Github
require 'slackphp/slack.php';
// Use the url you got earlier
$slack = new Slack('https://hooks.slack.com/services/AAA/BB/CC');
// Create a new message
$message = new SlackMessage($slack);
$message->setText("Hello My world!");
// Send it!
if ($message->send()) {
echo "Hurray Message Sent 😄";
} else {
echo "Message Failed 😢";
}

Sending Slack Message to a channel & User from PHP

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
require 'slackphp/slack.php';
// Use the url you got earlier
$slack = new Slack('https://hooks.slack.com/services/A/B/C');
// Create a new message
$message = new SlackMessage($slack);
$message->setText("Hello DevOps world!")->setChannel("#devops");
// Send it to channel!
$message->send();
// Create a new message
$message = new SlackMessage($slack);
$message->setText("Hello Madhu How are you!")->setChannel("@madhu");
// Send it to user!
$message->send();

Sending Rich Text to Slack channel along with Images and Links

 ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
require 'slackphp/slack.php';
// Use the url you got earlier
$slack = new Slack('https://hooks.slack.com/services/A/B/C');
$slack->setDefaultUsername('Cloud-Flix');
// Create a new message
$message = new SlackMessage($slack);
$attachment = new SlackAttachment("Required plain-text summary of the attachment.");
$attachment->setColor("#36a64f");
$attachment->setText("*Blogs on OCI & OCI-C");
$attachment->setPretext("Hands on Technical content");
$attachment->setAuthor(
"Madhusudhan Rao", "http://cloud-blogs.com/", //Optional author link "url/2014/10/Aishu22-1.jpg" // Optional author icon );
$attachment->setTitle("Cloud-Blogs", "Best Blog website on cloud 9");
$attachment->setImage("url/img/portfolio-1.jpg");
/*
Slack messages may be formatted using a simple markup language similar to Markdown. Supported
formatting includes: pre, code, italic, bold, and even ~strike~.; full details are
available on the Slack help site.
By default bot message text will be formatted, but attachments are not. To enable formatting on
attachment fields, you can use enableMarkdownFor
*/
//$attachment->enableMarkdownFor("text");
//$attachment->enableMarkdownFor("pretext");
//$attachment->enableMarkdownFor("fields");
// Add fields, last parameter stand for short (smaller field) and is optional
//$attachment->addField("Title", "Value");
//$attachment->addField("Title2", "Value2", true);
//$attachment->addField("Title", "Value", false);
// Add a footer
$attachment->setFooterText('By Madhu');
$attachment->setFooterIcon('url/img/portfolio-4.jpg');
$attachment->setTimestamp(time());
// Add it to your message
$message->addAttachment($attachment);
// Send
$message->send();

The output of the above code will be as shown below

No alt text provided for this image

Thank you, press like if you really liked the article 🙂

Blog Author @Madhusudhan , Cloud-Architect @ Oracle