How to Get Server and App SMS Alerts Using Twilio and CloudwaysBot

by Noor Ali  November 21, 2016

In this article, I will highlight how to integrate Laravel and Twilio.

Twilio and CloudwaysBot

First, a bit about Twilio.

Twilio is a cloud-based service that builds extremely strong communication channels between web applications and mobile devices. Businesses often use this service to fix minor gaps in their communication infrastructure. The best thing about Twilio is that the trial account offers extensive features that allows you to test-drive the platform. In order to use the trail account, you need to get a phone number verified and receive credit that could be used to test SMS alerts.

The service can be very useful for business. In particular, Twilio could be used as an alternative to HipChat and Slack in locations where internet access is expensive. By using this SMS-based service, a team could maintain communication at very low costs.

To demonstrate the capabilities of Twilio, I will construct a simple connection that would check for unread messages of CloudwaysBot using Cloudways API and send these alerts to your device via SMS. This simple connection could be easily extended to add further more advanced capabilities and options.

Before starting with the actual code, you must make sure that you have the following prerequisites fulfilled.

You have a launched Cloudways server.
Next, you have a launched Laravel app.

Verify Twilio Account

The first step is to get a Twilio account.

After sign up, you will get a number from Twilio that would be different from the number you submitted for verification. I received +1 (491) 072-1777. Remember to save this number as this will be used in the process.

Install Twilio Client for Laravel

The second step is the installation of the Twilio client for the Laravel app. For this launch the SSH terminal and navigate to the public_html folder. Once there enter the following commands:

Setup the Environment File

The next step is the setting up of the environment file. At the SSH terminal, enter the following command:

Paste the following code at the end of the file:

Create the Routes

I will now create routes that will act as an end point for the app. In Laravel 5.3, use the following command to open the file:

Add the following line to it:

This route activates the function makeMessage that is located inside the Server controller.

Create the Controller

The next step is the creation of the Server controller.

Go to the directory app/Http/Controller/ and make a new file in it. Name it Server.php and paste the following code in it:

Next, I will add several functions to this file. Two functions will access CloudwaysBot API and one will access Twilio. Rest of the functions are simply helper functions that chop the required data out of the code.

Add all the functions inside the Server class.

Unlock Your Number’s Region

An important requirement at this point is to unblock your verified number’s region for receive a confirmation message from Twilio. Click here and enable your country to get the SMS alert.

Adding a New Channel on Cloudways Bot

Now go to your platform and had to your channels and click on add new channel.

Now from the drop down select API

Now, copy your application URL. In the case of a Cloudways Laravel app, it would look like

http://php-xxxxxxxxxx.cloudways.com/makemessage.

And paste it in the URL box. Now name your channel and select the events you want to receive via SMS. Now click on Save & Test channel.

You will receive a message similar to the following message I received on my phone.

Conclusion

In this tutorial, I described how to link a Twilio account with CloudwaysBot so that you could receive notifications of the unread alerts via SMS. The complete code of the application is available here. If you face an issue in this code 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 Noor Ali

Noor Ali is an Associate Software Engineer at Cloudways. He loves to solve technical problems through programming and mathematics.

Stay Connected:

You Might Also Like...