Developing Slack Bots with PHP: Part 1

Custom Slack Bots With PHP

I have done a lot of work with slack bots in the past both professionally and in my spare time. In the past I build a plugin for WHMCS called WHMCS Slack which was originally a paid addon but is now open source and available for free on Github. This post is a first part in a series of posts i would like to write about both Go and PHP and how we can use these programming languages to create useful bots for Slack.

So let’s get started with a simple example on how to create a bot and make it send a simple message to Slack via the API. First you will need to create a bot for your Slack workspace which I have covered in a previous post here.

Make sure you have your bot and that you have added this bot to a public channel. You will need your bot API token, the channel name as it is written with the hashtag, e.g. “#general”. Finally you will also need a message that you would like to send to your channel. With this information we are able to make a simple curl request using PHP that will post a message to Slack as you can see in the gist below.

In the above code we start by adding our endpoint url for the chat.postMessage method as defined in the docs. We then go on to define the three required parameters: token, channel and text. We then proceed to make a post request via curl and print the results. You can run this via the command line using ‘php slack.php’ if you have php installed locally. This will give you the following result.

If you check your channel you should now see the message.

Thank you for reading, don’t forget to leave a comment if you found this guide helpful or if you have any questions!

Author: Markus Tenghamn

I have been building websites and apps since I was 13. At 18 I was running my own web hosting company. Today I work on a bunch of different projects and hope that I can create something useful for others. See my projects on Github. I love writing, it is one of my favourite hobbies which lets me share things I find interesting with others. Don't forget to follow me on Twitter.

One thought on “Developing Slack Bots with PHP: Part 1”

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.