This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

📣 Try the fastest hosting platform with pay-as-you-go pricing & 24/7 expert support! MIGRATE NOW →

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

Updated on February 15, 2018

4 Min Read

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:

$ 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.

Share your opinion in the comment section. COMMENT NOW

Share This Article

Noor Ali

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

×

Get Our Newsletter
Be the first to get the latest updates and tutorials.

Thankyou for Subscribing Us!

×

Webinar: How to Get 100% Scores on Core Web Vitals

Join Joe Williams & Aleksandar Savkovic on 29th of March, 2021.

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Want to Experience the Cloudways Platform in Its Full Glory?

Take a FREE guided tour of Cloudways and see for yourself how easily you can manage your server & apps on the leading cloud-hosting platform.

Start my tour

CYBER WEEK SAVINGS

  • 0

    Days

  • 0

    Hours

  • 0

    Mints

  • 0

    Sec

GET OFFER

For 4 Months &
40 Free Migrations

For 4 Months &
40 Free Migrations

Upgrade Now