Integrate Twilio Into Laravel Like a Senior Dev

Integrate Twilio Into Laravel Like a Senior Dev

ยท

3 min read

In many tutorials around the web, authors use and define services like Twilio in the controller, which flies in the face of the principles of MVC frameworks like Laravel. It also doesn't heed DRY principles as defined in the book, The Pragmatic Programmer. In this tutorial, we'll discuss how you can implement third-party services like Twilio into your Laravel application, the right way.

Preliminaries:

  • Download the Twilio SDK by opening your terminal and typing composer require twilio/sdk in your project.
  • Ensure that you're all set up with Twilio and that you have your Twilio number, Account SID and Auth Token ready.
  • Place your SID, token, and number as separate values in your .env file. For this tutorial, I'll just name these variables TWILIO_SID, TWILIO_TOKEN, TWILIO_NUMBER.

First Steps:

  • In your project's config folder, go to services.php. This is where we will define our new third-party Twilio service.
<?php

return [
    ...
    'twilio' => [
        'sid' => env('TWILIO_SID'),
        'token' => env('TWILIO_TOKEN'),
        'phone' => env('TWILIO_NUMBER')
    ]
]

The Work Begins:

  • In your app folder, create a Services folder. It is there that you'll create a file called Twilio.php.
  • Our service will only need to authenticate the client and send text messages, so we'll only be using Twilio\Rest\Client. We'll also need to set the namespace for our file so that we can access it elsewhere.
<?php

namespace App\Services;

use Twilio\Rest\Client;
  • The first thing that we'll do is define some properties so that we can plug in the values that are in our .env file.
    • After that, we'll use the constructor to pass the values to our properties.
<?php

namespace App\Services;

use Twilio\Rest\Client;

class Twilio {
    protected $sid;
    protected $token;
    protected $phone;

    function __construct(){
        $this->sid = config('services.twilio.sid');
        $this->token = config('services.twilio.token');
        $this->phone = config('services.twilio.phone');
    }
}

The Work Continues:

  • Twilio demands that every request be authenticated with the use of our SID and Auth Token. So instead of calling a separate method to authenticate for every request that we make, we'll just house our client authentication code in our constructor. We'll instantiate the client, and pass it to a property so that we can use it later.
...
protected $client;

function __construct(){
    $this->sid = config('services.twilio.sid');
    $this->token = config('services.twilio.token');
    $this->phone = config('services.twilio.phone');

    $this->client = new Client($this->sid, $this->token);
}
  • Finally, we'll just define one method to send our text messages.
...
function __construct(){
    $this->sid = config('services.twilio.sid');
    $this->token = config('services.twilio.token');
    $this->phone = config('services.twilio.phone');

    $this->client = new Client($this->sid, $this->token);
}

public function send($recipient_phone, $message){
    $sms = $this->client->messages->create($recipient_phone, [
        'from' => $this->phone,
        'body' => $message
    ]);

    return $sms;
}

The End:

  • Now, when you want to use this service, like in an Event Listener, all you'll need to do is import the service, instantiate the service, and use the method!
<?php

namespace App\Listeners;

use App\Events\SomeEvent;
use App\Services\Twilio;

class DoThisAfterSomeEvent
{
    ...
    public function handle(SomeEvent $event){
        ...
        $twilio = new Twilio();
        $twilio->send($recipient_phone, $message);
    }
}
ย