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

Error: ENOSPC: System limit for number of file watchers reached, watch

 Hi, Dependency version details. node: v15.8.0 nvm: 0.33.2 nodejs: v4.2.6 When I run an "npm start" command I'm getting this error. Error: ENOSPC: System limit for number of file watchers reached, watch '/var/www/html/project/simple-mern-project/frontend/public ' Error message: I found the solution from this StackOverflow URL:  https://stackoverflow.com/questions/55763428/react-native-error-enospc-system-limit-for-number-of-file-watchers-reached This is the command, I run to solve the issue:  # insert the new value into the system config echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p # check that the new value was applied cat /proc/sys/fs/inotify/max_user_watches # config variable name (not runnable) fs.inotify.max_user_watches=524288

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

Inapp Purchase Server side validation for Android as well as IOS using Laravel

Hi friends,                 I'm very happy to share today experience In-app purchase server-side validation you. This link is very use full for me to implement this server side validation. Please refer this link, https://github.com/aporat/store-receipt-validator I have faced some problems with Android server side validation. I will share problematic experience with you.......... https://developers.google.com/android-publisher/authorization  -> go through this URL and flow this steps If you follow the steps, you will get a client id and client secret. While doing this process it will ask you for redirect URI, you just any of your project URL for testing purpose to get the token http://localhost/project/public/refresh-token I Just gave a refresh token function as return URL. In this function, $request->code you can retrieve the refresh token Using this token you can do this process  Exchange this code for an...