Skip to main content

SMS Integration using laravel 5.3 (Using Twilio trial account)

Installation

Begin by installing this package through Composer. Run this command from the Terminal:
composer require aloha/twilio

Laravel integration

To wire this up in your Laravel project, whether it's built in Laravel 4 or 5, you need to add the service provider. Open app.php, and add a new item to the providers array.
'Aloha\Twilio\Support\Laravel\ServiceProvider',
This will register two new artisan commands for you:
  • twilio:sms
  • twilio:call
And make these objects resolvable from the IoC container:
  • Aloha\Twilio\Manager (aliased as twilio)
  • Aloha\Twilio\TwilioInterface (resolves a Twilio object, the default connection object created by the Manager).
There's a Facade class available for you, if you like. In your app.php config file add the following line to the aliases array if you want to use a short class name:
'Twilio' => 'Aloha\Twilio\Support\Laravel\Facade',
In Laravel 4 you can publish the default config file to app/config/packages/aloha/twilio/config.php with the artisan command config:publish aloha/twilio.
In Laravel 5 you can publish the default config file to config/twilio.php with the artisan command vendor:publish.

Facade

The facade has the exact same methods as the Aloha\Twilio\TwilioInterface. First, include the Facade class at the top of your file:
use Twilio;
To send a message using the default entry from your twilio config file:
Twilio::message($user->phone, $message);
One extra feature is that you can define which settings (and which sender phone number) to use:
Twilio::from('call_center')->message($user->phone, $message);
Twilio::from('board_room')->message($boss->phone, 'Hi there boss!');
Define multiple entries in your twilio config file to make use of this feature.

Usage

Creating a Twilio object. This object implements the Aloha\Twilio\TwilioInterface.
$twilio = new Aloha\Twilio\Twilio($accountId, $token, $fromNumber);
Sending a text message:
$twilio->message('+18085551212', 'Pink Elephants and Happy Rainbows');
Creating a call:
$twilio->call('+18085551212', 'http://foo.com/call.xml');
Generating a call and building the message in one go:
$twilio->call('+18085551212', function ($message) {
    $message->say('Hello');
    $message->play('https://api.twilio.com/cowbell.mp3', ['loop' => 5]);
});
Access the configured \Services_Twilio object:
$sdk = $twilio->getTwilio();
You can also access this via the Facade as well:
$sdk = Twilio::getTwilio();
Pass as many optional parameters as you want
If you want to pass on extra optional parameters to the messages->sendMessage(...) method from the Twilio SDK, you can do so by adding to the message method. All arguments are passed on, and the from field is prepended from configuration.
$twilio->message($to, $message, $mediaUrls, $params);
// passes all these arguments on.
The same is true for the call method.
$twilio->call($to, $message, $params);
// passes all these arguments on.

Dummy class

There is a dummy implementation of the TwilioInterface available: Aloha\Twilio\Dummy. This class allows you to inject this instead of a working implementation in case you need to run quick integration tests.

Logging decorator

There is one more class available for you: the Aloha\Twilio\LoggingDecorator. This class wraps any TwilioInterface object and logs whatever Twilio will do for you. It also takes a Psr\Log\LoggerInterface object (like Monolog) for logging, you know.
By default the service providers don't wrap objects with the LoggingDecorator, but it is at your disposal in case you want it. A possible use case is to construct a TwilioInterface object that logs what will happen, but doesn't actually call Twilio (using the Dummy class):
if (getenv('APP_ENV') === 'production') {
    $twilio = $container->make(\Aloha\Twilio\Manager::class);
} else {
    $psrLogger = $container->make(\Psr\Log\LoggerInterface::class);
    $twilio = new LoggingDecorator($psrLogger, new \Aloha\Twilio\Dummy());
}

// Inject it wherever you want.
$notifier = new Notifier($twilio);
 
 

Reference Link: 

https://github.com/aloha/laravel-twilio

 

Twilio SMS Setting in Trial Account:

  1. Signup a twilio trial account settings
  2. In twilio account go to Account->Account Settings to get the Account SID, Auth Token (Testing purpose you just new a response, just go with Test credential. Testing purpose also you need sms, you just go with Live Credential)
  3. If you just a need a response in testing purpose
    • From Number : +15017250604
    • To Number      : Verified twilio number (you have used phone number for signup purpose)
    • Credential       : Get Test credentials in account settings page
  4. Testing purpose all you need to send sms without credit means do this step
    • From Number : Click left side # (Phone number) in dashboard to chose different number to get the from number
    • To Number      : https://www.twilio.com/console/phone-numbers/verified
      To click Verified Caller Ids to add a number to receive a sms (That is  a twilio verified phone number)
    • Credential       : Get Live credentials in account settings page
  5. Just hit a send sms script you will get sms as well as response.

Comments

Popular posts from this blog

The requested URL /phpmyadmin was not found on this server. Apache/2.4.18 (Ubuntu) Server at localhost Port 80

Hi dudes, I'm getting Not Found error, when i hit a  http://localhost/phpmyadmin/ url, but i my system php, mysql & apache2 is already installed still i'm getting this error When i do this following step, the error get fixed. Do the following steps & EnjoyπŸ˜ƒπŸ˜„πŸ˜…πŸ˜„πŸ˜„πŸ˜ƒ You need to configure your apache2.conf to make phpMyAdmin work. For more detail Please read  this section  in Ubuntu wiki. open a termianl and run gksu gedit /etc/apache2/apache2.conf Then add the following line to the end of the file. Include /etc/phpmyadmin/apache.conf Then restart apache /etc/init.d/apache2 restart

Install a node & npm specific versions using nvm (Node Version Manager)

Hi buddies, When i tried to install a node & npm in my system, i will be facing some problem with the version. I want to install a recent LTS (Long Term Support) Don't Do (If you want to install a specific version):  I don't know, how its gonna installed, but its not a recent Node LTS version sudo apt-get install nodejs-legacy sudo apt-get install npm Do (Using a NVM- Node Version Manager to install a specific version): Use the following command to install a NVM into your system $ curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash Reload the system environment using this command $ source ~/.profile $ source ~/.bashrc List all the nodejs version using this command $ nvm ls-remote Install a nodejs version, what ever you want (If you want to install multiple node version you can install using this command) $ nvm install 6.11.1 $ nvm install 8.2.1 List a node version using this command

PHP 7 with Laravel 5.3 installation problem ( Your requirements could not be resolved to an installable set of packages )

I'm Facing this problem, while installation using laravel 5.3 with PHP 7 Your requirements could not be resolved to an installable set of packages.   Problem 1     - phpunit/phpunit 5.5.5 requires ext-dom * -> the requested PHP extension dom is missing from your system.     - phpunit/phpunit 5.5.4 requires ext-dom * -> the requested PHP extension dom is missing from your system.     - phpunit/phpunit 5.5.3 requires ext-dom * -> the requested PHP extension dom is missing from your system.     - phpunit/phpunit 5.5.2 requires ext-dom * -> the requested PHP extension dom is missing from your system.     - phpunit/phpunit 5.5.1 requires ext-dom * -> the requested PHP extension dom is missing from your system.     - phpunit/phpunit 5.5.0 requires ext-dom * -> the requested PHP extension dom is missing from your system.     - phpunit/phpunit 5.4.8 requires ext-dom * -> the requested PHP extension dom is missing from your system.     - phpunit/phpunit 5.4.7 requir