Laravel - Twilio

Wiki on web

WHAT IT IS?


INSTALLATION

Run this command from the Terminal:

    composer require lakshmajim/twilio
    composer dumpautoload
    composer update

LARAVEL INTEGRATION

you need to add the service provider. Open app/config/app.php, and add a new item to the providers array.

  Lakshmajim\Twilio\TwilioServiceProvider::class,

Then, add a Facade for more convenient usage. In app/config/app.php add the following line to the aliases array:

  'Twilio'    => Lakshmajim\Twilio\Facade\Twilio::class,

Again do composer update


METHOD, AVAILABLE PARAMETERS AND RESPONSES

Method
message(array, string, boolean, boolean, boolean)

    $message_array = array(
        'sender_id'     => 'TWILIO_AUTH_SID',
        'sender_secret' => 'TWILIO_AUTH_SECRET',
        'reciver_mobile' => 'MOBILE_NUMBER',
        'media_url' => 'MEDIA_URL',
        'otp'     =>'OTP',
        'sender' => 'TWILIO_SOURCE_NUMBER'
    );
The message_array parameters
PARAMETER DESCRIPTION
sender_id This is the key defined in ".env" file for auth_sid
sender_secret This is the key defined in ".env" file for auth_secret
sender This is the key defined in .env file for sender mobile number
reciver_mobile This is the receivers mobile number
media_url This is the "uri" for mutimedia
otp This key values associates with the otp
Responses
CODE DESCRIPTION
16000 Error due to all flags are set to false or no flag was set
16001 Error due to all flags were set to true
16002 No sms type was set witin the avialbel list of flag parameters
16003 Un handled error

MISCELLANEOUS

To send SMS
  Twilio::message($message_array,$op="only msg", true,  false, false ); // sms
To send MMS
  Twilio::message($message_array,$op="only MMS", false, false, true  ); // media
To send OTP
  Twilio::message($message_array,$op="only verfication code", false, true,  false ); // otp
To send both SMS and MMS
  Twilio::message($message_array,$op="This is combaination both SMS and MMS", true,  false, true  ); // sms , media 
INVALID METHOD CALLS
Twilio::message($message_array,$op="All set to true sms,mms,otp", true,  true,  true  ); // sms , otp , media
Twilio::message($message_array,$op="all set to false", false, false, false );            // none defined
Twilio::message($message_array,$op="all considered to be false");                        // none defined
Twilio::message($message_array); 

SENDING SMS


<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Twilio; 


/**
 * Twilio - Package usage Example
 *
 * @access  public
 * @since   1.2.0
 * @author  lakshmaji <lakshmajee88@gmail.com>
 */
class TwilioTest extends Controller
{
  public function testMesssage()
  {

    // initialize message array 
    $message_array = array(
        'sender_id'     => 'TWILIO_AUTH_ID',
        'sender_secret' => 'TWILIO_AUTH_SECRET',
        'reciver_mobile' => '999999999',
        'media_url' => 'http://goo.gl/F9igRq',
        'otp'     =>'325565',
        'sender' => 'TWILIO_SOURCE_NUMBER'
    );


    // This will send message only
    $sms_response = Twilio::message($message_array,$op="only msg", true,  false, false ); 

    return response()->json($sms_response,200);
  }

}
// end of class TwilioTest
// end of file TwilioTest.php

Example code for Laravel along with sample .env file

.env file

APP_ENV=local
APP_DEBUG=true
APP_KEY=BPfhzoGJ7RJB8D3qoyP6KZ2MjX2MAzcN

DB_HOST=127.0.0.1
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null


TWILIO_SOURCE_MOBILE_NUMBER=+447481338931
TWILIO_USER_ID=AC5f0d5a51944ddbf821ea00c2bfd8a04e
TWILIO_USER_PASSWORD=a0fb1706748dc12ccbb9501b5b904a74

The code to use above ".env" file is given below


<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Twilio; 


/**
 * Twilio - Package usage Example
 *
 * @access  public
 * @since   1.2.0
 * @author  lakshmaji <lakshmajee88@gmail.com>
 */
class TwilioTest extends Controller
{
  public function testMesssage()
  {

    // initialize message array 
    $message_array = array(
        'sender_id'     => 'TWILIO_USER_ID',
        'sender_secret' => 'TWILIO_USER_PASSWORD',
        'reciver_mobile' => '99999999999',
        'media_url' => 'http://goo.gl/F9igRq',
        'otp'     =>'325456',
        'sender' => 'TWILIO_SOURCE_MOBILE_NUMBER'
    );

    // This will send OTP only
    $sms_response = Twilio::message($message_array,$op="otp only", false, true,  false ); // otp

    return response()->json($sms_response,200);
  }

}
// end of class TwilioTest
// end of file TwilioTest.php


Handling Exceptions

<?php

namespace App\Exceptions;

use Exception;
use Lakshmajim\Twilio\Exception\TwilioException;


/**
 * Twilio - A Simple Exception handler class to Catch
 * Exceptions thrown by TwilioException class 
 *
 * @author   lakshmaji <lakshmajee88@gmail.com>
 */
class Handler extends ExceptionHandler
{

    //....
    //.................
    //....

     /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Exception  $e
     * @return \Illuminate\Http\Response
     */
    public function render($request, Exception $e)
    {
        if($e instanceof TwilioException)
        {
            return response()->json(array('message'=>$e->getMessage(),'status' =>$e->getStatusCode()),500);
        }
        return parent::render($request, $e);
    }
}

In laravel we can easily handle the errors by using Handler.php (You can use custom Exception Handlr too)


TWILIO TRAIL ACCOUNT USAGE:


Licence

MIT License (MIT)

@ MUTYALA ANANTHA LAKSHMAJI