laraditz/twilio

Twilio SDk wrapper for Laravel

1.0.1 2022-10-08 20:28 UTC

This package is auto-updated.

Last update: 2024-04-08 23:50:28 UTC


README

Latest Version on Packagist Total Downloads GitHub Actions

Twilio SDk wrapper for Laravel. Includes event for receiving messages and status updates.

Installation

You can install the package via composer:

composer require laraditz/twilio

Before Start

Configure your variables in your .env (recommended) or you can publish the config file and change it there.

TWILIO_ACCOUNT_SID=<your_account_sid>
TWILIO_AUTH_TOKEN=<your_auth_token>
TWILIO_FROM=<the_sender_number>

(Optional) You can publish the config file via this command:

php artisan vendor:publish --provider="Laraditz\Twilio\TwilioServiceProvider" --tag="config"

Run the migration command to create the necessary database table.

php artisan migrate

Add routeNotificationForTwilio method to your Notifiable model.

public function routeNotificationForTwilio($notification)
{
    return $this->mobile_no;
}

Usage

Now you can use the channel in your via() method inside the notification:

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Laraditz\Twilio\TwilioChannel;
use Laraditz\Twilio\TwilioWhatsappMessage;

class TestNotification extends Notification
{
    use Queueable;  
  
    public function via($notifiable)
    {
        return [TwilioChannel::class];
    }

    public function toTwilio($notifiable)
    {
        return (new TwilioWhatsappMessage())
            ->content("Test Whatsapp message!")
            ->mediaUrl("https://news.tokunation.com/wp-content/uploads/sites/5/2022/07/Kamen-Rider-Geats-Teaser.jpeg");
    }
}

You can also send SMS:

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Laraditz\Twilio\TwilioChannel;
use Laraditz\Twilio\TwilioSmsMessage;

class TestNotification extends Notification
{
    use Queueable;  
  
    public function via($notifiable)
    {
        return [TwilioChannel::class];
    }

    public function toTwilio($notifiable)
    {
        return (new TwilioSmsMessage())
            ->content("Test SMS message!");
    }
}

Event

This package also provide an event to allow your application to listen for Twilio message receive and status callback. You can create your listener and register it under event below.

Event Description
Laraditz\Twilio\Events\MessageReceived When a message comes in.
Laraditz\Twilio\Events\StatusCallback Receive status update.

Webhook URL

You may setup the URLs below on Twilio dashboard so that Twilio will push new messages and status update to it and it will then trigger the MessageReceived and StatusCallback events above.

https://your-app-url/twilio/webhooks/receive //for message receive
https://your-app-url/twilio/webhooks/status //for status update

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email raditzfarhan@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.