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!
One thought on “Developing Slack Bots with PHP: Part 1”