lakshmaji/twilio

SMS sending using Twilio

v1.2.3 2017-04-22 18:14 UTC

This package is auto-updated.

Last update: 2024-12-12 10:56:46 UTC


README

Wiki on web

Latest Stable Version Total Downloads Latest Unstable Version License Monthly Downloads Daily Downloads composer.lock

INDEX

##WHAT IT IS?

  • This package is used to send sms to any mobile number.
  • This uses Twilio API.
  • It requires AccountSID and AuthToken, they can be generated by registrting @at Twilio
    • after registration click on Account ,there you will be able to see authsid and authtoken.
    • You have assigned a sender mobile number which can be found at Twilio,which is used to send Text Messages or MMS and ShortCodes etc.

##INSTALLATION

  • Download package form https://github.com/lakshmaji/twilio .
  • OR YOU CAN RUN FOLLOWING COMMAND FROM TERMINAL
  • With composer you can run this line composer require lakshmaji/twilio

Run this command from the Terminal:

    composer require lakshmaji/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.

  Lakshmaji\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'    => Lakshmaji\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
Responses

##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 
 */
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=+44778338721
TWILIO_USER_ID=ACef0d5a519rwetbf821ea07c2fdbfd8204e
TWILIO_USER_PASSWORD=a0fb23srfdsf4825cbb9501df25b906a74

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 
 */
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 Lakshmaji\Twilio\Exception\TwilioException;


/**
 * Twilio - A Simple Exception handler class to Catch
 * Exceptions thrown by TwilioException class 
 *
 * @author   lakshmaji 
 */
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:

  • If You are trying to implement SMS functionality with Twilio the you need to verify the list of destination mobile numbers at Twilio VERIFIED NUMBERS
  • Before sending MESSAGE make sure that you have enabled GEO-PERMISSIONS at Twilio GEO PERMISSIONS

##Licence

MIT License (MIT)

@ MUTYALA ANANTHA LAKSHMAJI