Twillio & Laravel
For one of my personal projects I’ve found myself in a need of sending text messages to some of the users that use the application.
I’ve looked some other options like: Viber but there were lots and lots of requirements and forms to fill and eventually I’ve dropped it.
After a while I’ve remembered that in the past I’ve worked on a project that used a service called Twillio which from what I’ve remembered was quite easy to use and had a lot offer.
What is Twillio:
Twilio is a cloud communications platform that enables developers to build, scale, and operate communication features like voice calls, SMS, video, and messaging directly into their applications. It provides APIs (Application Programming Interfaces) that simplify integrating communication capabilities without needing complex infrastructure or telecom expertise.
In order to start with it you should create an account on their website, which takes 4–5 mins tops.
So now on to the programming part, one of the great things is that Twillio provides is a robust and easy-to-use PHP library which have everything you might need to send text messages, let’s have a look:
Installation
In your Laravel project the easiest way would be using composer:
composer require twilio/sdkAnd that’s it, it pull up the SDK and you can use it,
Usage
After you have created account you need to grab the keys required for the authentication, you can find them in the Account Info section on the Dashboard.
After that place them in the .env file of your object:
TWILIO_SID="your-twillio-sid"
TWILIO_AUTH_TOKEN="your-twillio-auth-token"After that since your are going to sent SMS messages to a phone numbers, you should obtain a phone number from Twillio, you can do that navigating to the Phone Numbers menu that’s located on the top of the sidebar.
Add the number to your env file also you will need it later.
TWILIO_NUMBER="your-twillio-number"I personally prefer to have some type of structure to my custom made classes so I’ve created a new Services folder in the project and in this new folder I’ve added new File called Twiilio which will handle all the things related to the sending of text messages.
<?php
namespace App\Services;
use Twilio\Rest\Client;
class Twilio
{
public function sendMessage($message, $recipients): void
{
$account_sid = env('TWILIO_SID');
$auth_token = env('TWILIO_AUTH_TOKEN');
$twilio_number = env('TWILIO_NUMBER');
$client = new Client($account_sid, $auth_token);
$client->messages->create($recipients,
['from' => $twilio_number, 'body' => $message]
);
}
}It’s a simple Service but it suits my needs, it can be used as follows:
use App\Services\Twilio;
...
$twilio = new Twilio;
$twilio->sendMessage($message, $recipient);When logged into your Twillio account there is History page on which all the messages sent can be seen, including what’s there statuses, when where sent and to whom were they sent it’s perfect for debugging.
Pricing
One might wonder how much it cost to use system such as this and the answer is: As much you want to, in your account there is a balance that you can top-up and based on the price of the messages sent in your country you can put as much you need to send messages, the only thing is that the amount should be at least $20 and I must say is more than enough if you have a small application that doesn’t need to send that many messages.
Conclusion
There are different options to implement this in your Laravel project you choose which is the best suite for you, for me personally the using Twillio was a great experience, it's affordable, have a great documentation and support. It's situational it depends a lot of what you need for your particular project.
Share this post