lakshmaji / twilio by lakshmaji

SMS sending using Twilio
742
31
4
Package Data
Maintainer Username: lakshmaji
Package Create Date: 2016-02-17
Package Last Update: 2023-08-12
Home Page: http://lakshmaji.github.io/twilio/
Language: PHP
License: MIT
Last Refreshed: 2024-12-15 15:26:35
Package Statistics
Total Downloads: 742
Monthly Downloads: 0
Daily Downloads: 0
Total Stars: 31
Total Watchers: 4
Total Forks: 6
Total Open Issues: 0

Laravel - Twilio

Wiki on web

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

INDEX

|Index|Description| |---|:---| |What it is|- Introduction| |Installation |- Installing Twilio package | |Laravel Integration |- Integrating this package with Laravel application| |Docs|- Description of methods available and parameters etc Method Responses| |Miscellaneous |- Miscellaneous content regarding method calls Invalid method calls|- Invalid arguments (Not supported)| |Sending SMS |- A simple Examp to illustarte the using this package| |Example with Laravel|- Sample code in Laravel| |Exception Handling |- An Exception Handling mechanism to catch errors| |Twilio |- How to Get registered on Twilio to use free trail account| |License |- License|

##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

| 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=+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 <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 Lakshmaji\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:

  • 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