Blog Detail

26

Aug
Send or Receive SMS with Laravel Twillio API Integration cover image

Send or Receive SMS with Laravel Twillio API Integration

This package provides the Laravel Twillio API Integration that helps you add robust messaging capabilities to your applications. Using this REST API, you can send and receive SMS messages, track the delivery of sent messages, and retrieve and modify message history. You can easily utilize Twilio Programmable Messaging in your web application by following the instructions given below.

Installation

You've to run these commands for the installation of this package.

composer require aloha/twilio

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

In your app.php config file, you've to add the following line to the aliases array if you want to utilize a Facade class name:

'Twilio' => 'Aloha\Twilio\Support\Laravel\Facade',

Next, you should publish the default config file to config/twilio.php with the terminal command:

php -r "copy('vendor/aloha/twilio/src/config/config.php', 'config/twilio.php');"

Facade

The facade has the same methods as the Aloha\Twilio\TwilioInterface. First, you've to add 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);

Twilio has another main feature, 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

After installation and setup next you've to create 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 (\Twilio\TwiML\VoiceResponse $message) {
    $message->say('Hello');
    $message->play('https://api.twilio.com/cowbell.mp3', ['loop' => 5]);
});

You can make a call with any Twiml description, you can pass along any Twiml object:

$message = new \Twilio\TwiML\VoiceResponse();
$message->say('Hello');
$message->play('https://api.twilio.com/cowbell.mp3', ['loop' => 5]);
$twilio->call('+18085551212', $message);

Access the configured Twilio\Rest\Client 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 form field is prepended from the configuration.

$twilio->message($to, $message, $mediaUrls, $params);
// passes all these params on.

The same is true for the call method.
$twilio->call($to, $message, $params);
// passes all these params on.

Use Dummy Class

If you want to use a dummy implementation of Twilio then it is available on TwilioInterface available Aloha\Twilio\Dummy. This class enables you to inject this instead of a working implementation in case you need to run quick integration tests.

There's a lot more content is available about Twilio on its official Website. If you want to dig more, you can view its complete documentation here.

Published at : 26-08-2021

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

web application development client
web application development client
web application development client
web application development client
web application development client

Launch project