How to Create a Slack Chatbot and Connect It With Cloudways API

by Ahmed Khan  November 7, 2016

In the series of Cloudways API related use cases, several important uses of the API (such as purging Varnish cache, adding and deleting Application, and managing Github repos) have been discussed.

connecting-cloudwaysapi-with-slackbot-banner

In today’s article, I will create a Slack chatBot and connect it with Cloudways API. This bot will perform the following server operations:

  • Get servers from Cloudways.
  • Get server application from Cloudways.
  • Start, stop, restart, and delete a server.
  • Restart server services (Apache, Nginx, PHP-FPM, etc.).
  • Manage Varnish cache.

In the future iterations of this chatbot, I will integrate more operations and functionalities.

Complete code can be found at CloudChatbot repository.

Create a Slackbot

Log in to your Slack group and click on the gear icon. Click on Add an app or integration.

integration slack

Now, on the apps page, click on Build button located in the top right corner.

build slack chat

Now on the Build page, click on Make a Custom Integration.

create custom integration

On the next page, click on Bots.

create chat bot

Name the Bot and click Add bot integration.

name your bot

Once the process finishes, the bot has been created. This bot could now be added to Channels on the Slack group.

For now, copy the API Token of the bot and copy it some place safe.

slack token key

Next, start working on chatbot script. But before that, I will add several functions in class CloudwaysAPIClient. This class was earlier created in a previous article on Cloudways API.

Updating CloudwaysAPIClient Class

Now open CloudwaysAPIClient.php and add the following functions in it to manage a server and its services:

Let’s understand the above functions:

  • Function get_services gets the status of all services including Apache, Varnish, Nginx, PHP-FPM running on the server.
  • Function manage_services manages different services of the server and helps restart services.
  • Functions start_server ,restart_server,stop_server,delete_server carry out various server management functions.

Next is the creation of the function to manage Varnish services for the servers. Paste the following code inside the class CloudwaysAPIClient in the CloudwaysAPIClient.php file:

The function service_varnish will enable, disable and purge Varnish cache. All the required functions in class CloudwaysAPIClient have been now created.

Creating Slackbot Script

Create a new folder in public_html and name it Slackbot.

I will first install a PHP SDK to work with Slack API using Composer. For this, create a new file composer.json and paste the following line in it:

Now run composer install. Once the process finishes, copy and paste the updated CloudwaysAPIClient.php in this folder. Then, create a new file mybot.php and paste the following code in it:

Let’s understand the above functions:

  • Function configure() sets the command name for the chatbot.
  • Function getArgs() breaks the message sent from the Slack channel into an array to perform the required action.
  • The message from the Slack channel to Slackbot will be executed by the function execute(). In this function, I have created cases to perform valid actions sent to the chatbot. To begin, I have created a single case. Thus, whenever a user sends “cloud hello, the bot will return all the chat commands to the user.

Let’s give it a try. Create a new file index.php in the same folder and paste the following code in it.

In above code, I have created an object of $bot using PHP SDK, bound the token and the newly created command class to it, and then run it.

Now, go to the Slack channel and send a direct message to your bot. You will notice that the bot is offline.

offline bot

Let’s take the bot online. Now run php index.php through the command line. The result is the following message:

index.php execution

Now, go back to the Slack channel, and you will notice that the bot is online.

online bot

Type “cloud hello” and press Enter. You will get the following response:

cloud hello

If you get the above message, the bot has been configured correctly!

It is now time to add cases for getting servers and the available applications. Now open mybot.php and add the following code inside switch and then save the file:

Let’s understand the above code:

  • When the user types cloud getserver, case getserver will be activated and the chatbot will list all the servers along with status and the total number of applications.
  • There are two scenarios for getapplications. If the user wants to get all the applications on all the servers, use cloud getapplications all. If the user requires applications for a specific server, use cloud getapplications <serverid>. The placeholder <serverid> should be replaced with your server id.

Let’s test this functionality.

First, stop all instances of index.php using Ctrl+C. Launch the script again. Send these new messages to your bot and see the magic.

In the following image, you can see how many apps are running inside the Server 59727.

cloud getservers

Now, you can see the names of all applications inside your Cloudways account.

cloud getapplications all

In the following image, the chatbot informed me about all the application inside the Server 59727.

cloud getapplications with serverid

Next, create cases to get server services and then manage these services.

Paste the following case inside switch and save the file:

Let’s understand the above code.

  • When the user types cloud getservices <serverid> (where <serverid> is to be replaced by the server id) case getservices will be activated and send all the services of the server to the
  • When the user types cloud restartservice <servicename> <serverid> (where <serverid> is to be replaced by the server id and <servicename> is to be replaced by apache2, mysql, nginx, etc.) case restartservice will restart the required server service.

Like before, stop the script and rerun it. Carry the following chat with the bot.

cloud get services

cloud restartservice mysql

Now, create cases for starting, stopping, restarting, and deleting servers. Paste the following code inside switch and then save the file:

When user type cloud server <service> <serverid> where <service> is to be replaced with start|stop|restart|delete and <serverid> is to be replaced with the server id.

Before performing these actions, you need to make sure that you are not running the bot on the same server for which you are performing the action. Otherwise, the bot will go offline once the operation finishes.

Now, I will create cases to manage Varnish cache. Paste the following code inside switch and save the file:

When the user types cloud varnish <action> <serverid> where <action> is to be replaced with enable|disable|purge and <serverid> is to be replaced with the actual server id or with “all” in order to disable for all servers. The bot will perform the case varnish, and the required action will be performed on server.

Stop index.php and rerun it. Send Varnish and you will see:

cloud varnish disable

You can run cloud getservices <serverid> to verify whether the varnish is disabled.

varnish disable services

Here is the complete code for the class Mybot:

The chatbot is fully operational and it responds to all the predetermined commands. However, you might notice that  once you close the terminal, the bot goes offline. Thus, in order to keep the bot running, make sure that index.php runs continuously. In order to run the script continuously, type the following command in the SSH terminal:

The above command adds a process in the background of the server. Now, even when the terminal is closed, the bot will continue to be online.

Conclusion

In this tutorial, I discussed how to create a Slack chatbot and then connect it with Cloudways API to perform server operation directly from the Slack chat. I hope that you have been able to follow the code without any issues. If you have a question or would like to add to the discussion, please leave a comment below.

Start Creating Web Apps on Managed Cloud Servers Now!

Easy Web App Deployment for Agencies, Developers and E-Commerce Industry

About Ahmed Khan

Ahmed was a PHP community expert at Cloudways - A Managed PHP Hosting Cloud Platform. He is a software engineer with extensive knowledge in PHP and SEO. He loves watching Game of Thrones is his free time. Follow Ahmed on Twitter to stay updated with his works. You can email him at ahmed.khan@cloudways.com

Stay Connected:

You Might Also Like...