X
    Categories: API Tutorials and Use Cases

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

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

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:

$ composer require twilio/sdk
$ composer update

Setup the Environment File

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

$vim .env

Paste the following code at the end of the file:

TWILIO_ACCOUNT_SID=YOU CAN FIND IT AT TWILIO DASHBOARD

TWILIO_AUTH_TOKEN=YOU CAN FIND IT AT TWILIO DASHBOARD

TWILIO_NUMBER="NUMBER WE RECEIVED AT STEP-2"  

API_KEY="YOUR CLOUDWAYS API KEY HERE"

API_EMAIL="YOUR EMAIL ADDRESS"

BASE_URL="https://api.cloudways.com/api/v1"

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:

$ vim routes/web.php

Add the following line to it:

Route::post('/makemessage',"Server@makeMessage");

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:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Illuminate\Foundation\Bus\DispatchesJobs;

use Illuminate\Routing\Controller as BaseController;

use Illuminate\Foundation\Validation\ValidatesRequests;

use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

use Twilio\Rest\Client;

class Server extends BaseController

{

  

}

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.

public function makeMessage(Request $request){
    //Getting response coming from webhook.
    $response = json_decode($request->getContent());
    $accountSid = env('TWILIO_ACCOUNT_SID');
    $authToken = env('TWILIO_AUTH_TOKEN');
    $twilioNumber = env('TWILIO_NUMBER');
    $client = new Client($accountSid, $authToken);
    $to = "+ENTER YOUR VERIFIED NUMBER HERE";
    $client->messages->create(
        $to,
        [
            "body" => $response->text,
            "from" => $twilioNumber
            //   On US phone numbers, you could send an image as well!
            //  'mediaUrl' => $imageUrl
        ]
    );
}

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.

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