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. Openapp.php, and add a new item to the providers array.'Aloha\Twilio\Support\Laravel\ServiceProvider',
twilio:smstwilio:call
Aloha\Twilio\Manager(aliased astwilio)Aloha\Twilio\TwilioInterface(resolves aTwilioobject, the default connection object created by theManager).
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',
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 theAloha\Twilio\TwilioInterface. First, include the Facade class at the top of your file:use Twilio;
twilio config file:Twilio::message($user->phone, $message);
Twilio::from('call_center')->message($user->phone, $message);
Twilio::from('board_room')->message($boss->phone, 'Hi there boss!');
twilio config file to make use of this feature.Usage
Creating a Twilio object. This object implements theAloha\Twilio\TwilioInterface.$twilio = new Aloha\Twilio\Twilio($accountId, $token, $fromNumber);
$twilio->message('+18085551212', 'Pink Elephants and Happy Rainbows');
$twilio->call('+18085551212', 'http://foo.com/call.xml');
$twilio->call('+18085551212', function ($message) {
$message->say('Hello');
$message->play('https://api.twilio.com/cowbell.mp3', ['loop' => 5]);
});
\Services_Twilio object:$sdk = $twilio->getTwilio();
$sdk = Twilio::getTwilio();
Pass as many optional parameters as you want
If you want to pass on extra optional parameters to themessages->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.
$twilio->call($to, $message, $params);
// passes all these arguments on.
Dummy class
There is a dummy implementation of theTwilioInterface 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: theAloha\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:
- Signup a twilio trial account settings
- 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)
- 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
- From Number :
- 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
- From Number : Click left side # (Phone number) in dashboard to chose different number to get the from number
- Just hit a send sms script you will get sms as well as response.
Comments
Post a Comment