August 9, 2016 3:51 am

Creating a simple Slack chat bot with PHP

To create the bot, we are going to use Slack’s Outgoing Webhooks integration. With it, we can create any page and whenever a message is sent into a specific Slack channel or in any Slack channel your page will be called with information about the sender of the message, the message itself and other useful metadata. When a request is made to your page, you can return JSON with the property text in it and the bot will send back a response directly in the channel in which the message occurred.Creating a simple Slack chat bot with PHP

 

Setting up Slack's Outgoing Webhooks

Whenever you initialize Outgoing Webhooks for a team you will be given a few settings. The bot can listen for messages on a specific channel, or in all channels. If the bot listens for requests on all channels, you need to provide a trigger word mandatorily. The message in the channel needs to start with the trigger word for your page/script to be called by Slack. You can also customize the bot by providing it with a name and a picture.  You have to enter the URL which will be called when a message is sent in the channel and which matches the trigger word(s), if any.

Now, let us create a file responding to the given URL in the Outgoing Webhooks settings.

Let us make a simple help function which returns a list of what the bot is capable of. We can actually provide Markdown in the bot’s response text. To do this, we simply enter the Markdown itself in the message and when returning a response, we simply provide another property called mrkdwn.

function help() {
  global $botName;        
  $out = "*$botName: kittens* => Returns a cat gif\r\n";
  echo json_encode(array("text" => $out, "mrkdwn" => true));
}

We then check if the parameter text is sent with the request – and if so, we remove everything from the command up to the first : sign. We do this because we expect a text such as botName: command

if (isset($_REQUEST['text'])) {
$text = trim(implode(":", array_slice(explode(":", $_REQUEST['text']), 1)));

 

Then, if what is left of the text is equal to help – we know that the person wants to see the help command.

if ($text === "help") {
help();

}

 

The same goes with the kittens command.

 

function kittens() {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://thecatapi.com/api/images/get?format=src&type=gif");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
$url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
echo json_encode(array("text" => $url));
}

 

We send a request to the Cat API to get an actual image which would be previewable in Slack and simply send the URL back into the channel. We then simply check if the command is kittens and call the kittens function:

else if ($text === "kittens") {
kittens();

}

So, we end up with a simple bot which can be refactored and customized in great details to serve any purpose.

Author Ivan Dimov

Ivan is a student of IT, a freelance web designer/developer and a tech writer. He deals with both front-end and back-end stuff. Whenever he is not in front of an Internet-enabled device he is probably reading a book or traveling. You can find more about him at: http://www.dimoff.biz. facebook, twitter


Tutorial Categories:

Leave a Reply

Your email address will not be published. Required fields are marked *