├── .gitignore ├── README.md ├── composer.json └── src ├── Exception └── TwilioException.php ├── Facade └── Twilio.php ├── Twilio.php └── TwilioServiceProvider.php /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | composer.lock -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel - Twilio 2 | [Wiki on web](http://lakshmaji.github.io/plivo/) 3 | 4 | [![Latest Stable Version](https://poser.pugx.org/lakshmajim/twilio/v/stable)](https://packagist.org/packages/lakshmajim/twilio) 5 | [![Total Downloads](https://poser.pugx.org/lakshmajim/twilio/downloads)](https://packagist.org/packages/lakshmajim/twilio) 6 | [![Latest Unstable Version](https://poser.pugx.org/lakshmajim/twilio/v/unstable)](https://packagist.org/packages/lakshmajim/twilio) 7 | [![License](https://poser.pugx.org/lakshmajim/twilio/license)](https://packagist.org/packages/lakshmajim/twilio) 8 | [![Monthly Downloads](https://poser.pugx.org/lakshmajim/twilio/d/monthly)](https://packagist.org/packages/lakshmajim/twilio) 9 | [![Daily Downloads](https://poser.pugx.org/lakshmajim/twilio/d/daily)](https://packagist.org/packages/lakshmajim/twilio) 10 | [![composer.lock](https://poser.pugx.org/lakshmajim/twilio/composerlock)](https://packagist.org/packages/lakshmajim/twilio) 11 | 12 | ## INDEX 13 | 14 | |Index|Description| 15 | |---|:---| 16 | |[What it is](https://github.com/lakshmaji/twilio/blob/master/README.md#what-it-is)|- Introduction| 17 | |[Installation](https://github.com/lakshmaji/twilio/blob/master/README.md#installation) |- Installing Twilio package | 18 | |[Laravel Integration](https://github.com/lakshmaji/twilio/blob/master/README.md#laravel-integration) |- Integrating this package with Laravel application| 19 | |[Docs](https://github.com/lakshmaji/twilio/blob/master/README.md#method-available-parameters-and-responses)|- Description of methods available and parameters etc [Method](https://github.com/lakshmaji/twilio/blob/master/README.md#method) [Responses](https://github.com/lakshmaji/twilio/blob/master/README.md#responses)| 20 | |[Miscellaneous](https://github.com/lakshmaji/twilio/blob/master/README.md#miscellaneous) |- Miscellaneous content regarding method calls 21 | [Invalid method calls](https://github.com/lakshmaji/twilio/blob/master/README.md#invalid-method-calls)|- Invalid arguments (Not supported)| 22 | |[Sending SMS](https://github.com/lakshmaji/twilio/blob/master/README.md#sending-sms) |- A simple Examp to illustarte the using this package| 23 | |[Example with Laravel](https://github.com/lakshmaji/twilio/blob/master/README.md#example-code-for-laravel-along-with-sample-env-file)|- Sample code in Laravel| 24 | |[Exception Handling](https://github.com/lakshmaji/twilio/blob/master/README.md#handling-exceptions) |- An Exception Handling mechanism to catch errors| 25 | |[Twilio](https://github.com/lakshmaji/twilio/blob/master/README.md#twilio-trail-account-usage) |- How to Get registered on Twilio to use free trail account| 26 | |[License](https://github.com/lakshmaji/twilio/blob/master/README.md#licence) |- License| 27 | 28 | 29 | 30 | ##WHAT IT IS? 31 | 32 | 33 | 34 | - This package is used to send sms to any mobile number. 35 | - This uses [Twilio](https://www.twilio.com/) API. 36 | - It requires *AccountSID* and *AuthToken*, they can be generated by registrting @at [Twilio](https://www.twilio.com/try-twilio) 37 | - after registration click on Account ,there you will be able to see authsid and authtoken. 38 | - You have assigned a sender mobile number which can be found at [Twilio](https://www.twilio.com/user/account/phone-numbers/incoming),which is used to send Text Messages or MMS and ShortCodes etc. 39 | 40 | 41 | 42 | --- 43 | 44 | 45 | 46 | ##INSTALLATION 47 | 48 | 49 | - Download package form https://github.com/lakshmaji/twilio . 50 | - OR YOU CAN RUN FOLLOWING COMMAND FROM TERMINAL 51 | - With composer you can run this line **composer require lakshmaji/twilio** 52 | 53 | Run this command from the Terminal: 54 | 55 | ```bash 56 | composer require lakshmaji/twilio 57 | composer dumpautoload 58 | composer update 59 | ``` 60 | 61 | *** 62 | 63 | ##LARAVEL INTEGRATION 64 | 65 | you need to add the service provider. Open `app/config/app.php`, and add a new item to the providers array. 66 | ```php 67 | Lakshmaji\Twilio\TwilioServiceProvider::class, 68 | ``` 69 | Then, add a Facade for more convenient usage. In `app/config/app.php` add the following line to the `aliases` array: 70 | ```php 71 | 'Twilio' => Lakshmaji\Twilio\Facade\Twilio::class, 72 | ``` 73 | Again do composer update 74 | 75 | *** 76 | 77 | 78 | ## METHOD, AVAILABLE PARAMETERS AND RESPONSES 79 | 80 | #####Method 81 | ```php 82 | message(array, string, boolean, boolean, boolean) 83 | ``` 84 | 85 | ```php 86 | 87 | $message_array = array( 88 | 'sender_id' => 'TWILIO_AUTH_SID', 89 | 'sender_secret' => 'TWILIO_AUTH_SECRET', 90 | 'reciver_mobile' => 'MOBILE_NUMBER', 91 | 'media_url' => 'MEDIA_URL', 92 | 'otp' =>'OTP', 93 | 'sender' => 'TWILIO_SOURCE_NUMBER' 94 | ); 95 | 96 | ``` 97 | 98 | ##### The message_array parameters 99 | 100 | | PARAMETER | DESCRIPTION | 101 | |:-------------- |:----------------------------------------| 102 | |sender_id | This is the key defined in ".env" file for auth_sid | 103 | |sender_secret| This is the key defined in ".env" file for auth_secret | 104 | | sender | This is the key defined in .env file for sender mobile number | 105 | |reciver_mobile| This is the receivers mobile number| 106 | |media_url|This is the "uri" for mutimedia| 107 | |otp|This key values associates with the otp| 108 | 109 | 110 | 111 | ##### Responses 112 | 113 | | CODE | DESCRIPTION | 114 | |:-------------- |:----------------------------------------| 115 | |16000 | Error due to all flags are set to false or no flag was set| 116 | |16001|Error due to all flags were set to true | 117 | |16002|No sms type was set witin the avialbel list of flag parameters| 118 | |16003|Un handled error| 119 | 120 | 121 | 122 | *** 123 | 124 | 125 | ##MISCELLANEOUS 126 | 127 | #####To send SMS 128 | ```php 129 | Twilio::message($message_array,$op="only msg", true, false, false ); // sms 130 | ``` 131 | 132 | #####To send MMS 133 | ```php 134 | Twilio::message($message_array,$op="only MMS", false, false, true ); // media 135 | ``` 136 | #####To send OTP 137 | ```php 138 | Twilio::message($message_array,$op="only verfication code", false, true, false ); // otp 139 | ``` 140 | #####To send both SMS and MMS 141 | ```php 142 | Twilio::message($message_array,$op="This is combaination both SMS and MMS", true, false, true ); // sms , media 143 | ``` 144 | 145 | 146 | 147 | #####INVALID METHOD CALLS 148 | 149 | ```php 150 | Twilio::message($message_array,$op="All set to true sms,mms,otp", true, true, true ); // sms , otp , media 151 | Twilio::message($message_array,$op="all set to false", false, false, false ); // none defined 152 | Twilio::message($message_array,$op="all considered to be false"); // none defined 153 | Twilio::message($message_array); 154 | 155 | ``` 156 | 157 | 158 | *** 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | ##SENDING SMS 170 | 171 | ```php 172 | 173 | 'TWILIO_AUTH_ID', 198 | 'sender_secret' => 'TWILIO_AUTH_SECRET', 199 | 'reciver_mobile' => '999999999', 200 | 'media_url' => 'http://goo.gl/F9igRq', 201 | 'otp' =>'325565', 202 | 'sender' => 'TWILIO_SOURCE_NUMBER' 203 | ); 204 | 205 | 206 | // This will send message only 207 | $sms_response = Twilio::message($message_array,$op="only msg", true, false, false ); 208 | 209 | return response()->json($sms_response,200); 210 | } 211 | 212 | } 213 | // end of class TwilioTest 214 | // end of file TwilioTest.php 215 | 216 | ``` 217 | 218 | 219 | 220 | 221 | 222 | 223 | ##Example code for Laravel along with sample .env file 224 | 225 | **.env file** 226 | ```erlang 227 | 228 | APP_ENV=local 229 | APP_DEBUG=true 230 | APP_KEY=BPfhzoGJ7RJB8D3qoyP6KZ2MjX2MAzcN 231 | 232 | DB_HOST=127.0.0.1 233 | DB_DATABASE=homestead 234 | DB_USERNAME=homestead 235 | DB_PASSWORD=secret 236 | 237 | CACHE_DRIVER=file 238 | SESSION_DRIVER=file 239 | QUEUE_DRIVER=sync 240 | 241 | REDIS_HOST=127.0.0.1 242 | REDIS_PASSWORD=null 243 | REDIS_PORT=6379 244 | 245 | MAIL_DRIVER=smtp 246 | MAIL_HOST=mailtrap.io 247 | MAIL_PORT=2525 248 | MAIL_USERNAME=null 249 | MAIL_PASSWORD=null 250 | MAIL_ENCRYPTION=null 251 | 252 | 253 | TWILIO_SOURCE_MOBILE_NUMBER=+44778338721 254 | TWILIO_USER_ID=ACef0d5a519rwetbf821ea07c2fdbfd8204e 255 | TWILIO_USER_PASSWORD=a0fb23srfdsf4825cbb9501df25b906a74 256 | 257 | ``` 258 | 259 | 260 | 261 | The code to use above ".env" file is given below 262 | ```php 263 | 264 | 'TWILIO_USER_ID', 289 | 'sender_secret' => 'TWILIO_USER_PASSWORD', 290 | 'reciver_mobile' => '99999999999', 291 | 'media_url' => 'http://goo.gl/F9igRq', 292 | 'otp' =>'325456', 293 | 'sender' => 'TWILIO_SOURCE_MOBILE_NUMBER' 294 | ); 295 | 296 | // This will send OTP only 297 | $sms_response = Twilio::message($message_array,$op="otp only", false, true, false ); // otp 298 | 299 | return response()->json($sms_response,200); 300 | } 301 | 302 | } 303 | // end of class TwilioTest 304 | // end of file TwilioTest.php 305 | 306 | 307 | ``` 308 | 309 | *** 310 | 311 | ## Handling Exceptions 312 | 313 | ```php 314 | json(array('message'=>$e->getMessage(),'status' =>$e->getStatusCode()),500); 347 | } 348 | return parent::render($request, $e); 349 | } 350 | } 351 | 352 | ``` 353 | In laravel we can easily handle the errors by using Handler.php (You can use custom Exception Handlr too) 354 | 355 | 356 | 357 | 358 | *** 359 | 360 | ## TWILIO TRAIL ACCOUNT USAGE: 361 | 362 | - If You are trying to implement SMS functionality with Twilio the you need to verify the list of destination mobile numbers at [Twilio](https://www.twilio.com/user/account/phone-numbers/verified) 363 | ![VERIFIED NUMBERS](https://raw.githubusercontent.com/lakshmaji/images/master/verified_numbers.png) 364 | - Before sending MESSAGE make sure that you have enabled **GEO-PERMISSIONS** at [Twilio](https://www.twilio.com/user/account/settings/international) 365 | ![GEO PERMISSIONS](https://raw.githubusercontent.com/lakshmaji/images/master/geo_permissions.png) 366 | 367 | *** 368 | 369 | ##Licence 370 | 371 | [*MIT License (MIT)*](https://opensource.org/licenses/MIT) 372 | 373 | @ MUTYALA ANANTHA LAKSHMAJI 374 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "lakshmaji/twilio", 3 | "description": "SMS sending using Twilio", 4 | "keywords": [ 5 | "Twilio","Laravel","composer", "php" 6 | ], 7 | "license": "MIT", 8 | "authors": [ 9 | { 10 | "name": "lakshmaji", 11 | "email": "" 12 | } 13 | ], 14 | "require": { 15 | "php": ">=5.3.0", 16 | "twilio/sdk": "^4.10" 17 | }, 18 | "autoload": { 19 | "psr-4": { 20 | "Lakshmaji\\Twilio\\": "src/" 21 | } 22 | }, 23 | "minimum-stability": "dev" 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/Exception/TwilioException.php: -------------------------------------------------------------------------------- 1 | setStatusCode($statusCode); 20 | } 21 | } 22 | 23 | /** 24 | * @param int $statusCode 25 | */ 26 | public function setStatusCode($statusCode) 27 | { 28 | $this->statusCode = $statusCode; 29 | } 30 | 31 | /** 32 | * @return int the status code 33 | */ 34 | public function getStatusCode() 35 | { 36 | return $this->statusCode; 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/Facade/Twilio.php: -------------------------------------------------------------------------------- 1 | account->messages->create($message_twilio); 127 | $message_status = $message->status; 128 | 129 | $message_possibilities = array('queued', 'sending', 'sent', 'queued', 'accepted', 'delivered'); 130 | 131 | if(in_array($message_status, $message_possibilities)) 132 | { 133 | return true; 134 | } 135 | else 136 | { 137 | throw new TwilioException('Error while sending message due to package',16003); 138 | } 139 | } 140 | catch(Services_Twilio_RestException $te) 141 | { 142 | throw new TwilioException($te->getMessage(), $te->getCode()); 143 | } 144 | } 145 | 146 | //------------------------------------------------------------------------- 147 | } 148 | //end of Twilio class 149 | // end of file Twilio.php -------------------------------------------------------------------------------- /src/TwilioServiceProvider.php: -------------------------------------------------------------------------------- 1 | app->singleton('twilio', function($app) { 38 | return new Twilio; 39 | }); 40 | } else { 41 | $this->app['twilio'] = $this->app->share(function($app) { 42 | return new Twilio; 43 | }); 44 | } 45 | // $this->app['twilio'] = $this->app->share(function($app) { 46 | // return new Twilio; 47 | // }); 48 | } 49 | } 50 | // end of TwilioServiceProvider class 51 | // end of file TwilioServiceProvider 52 | 53 | --------------------------------------------------------------------------------