i-rocky / laravel-twilio
Twilio Fax, SMS, MMS, Voice Call recording, receiving, sending support for laravel
Installs: 44
Dependents: 0
Suggesters: 0
Security: 0
Stars: 4
Watchers: 2
Forks: 0
Open Issues: 0
pkg:composer/i-rocky/laravel-twilio
Requires
- twilio/sdk: ^5.38
Requires (Dev)
- orchestra/testbench: ^4.0
- phpunit/phpunit: ^8.4
This package is auto-updated.
Last update: 2025-10-29 03:10:26 UTC
README
Installation
Run the following commands
composer require i-rocky/laravel-twilio
php artisan laravel-twilio:install
This should publish the following files
project
└───config
| laravel-twilio.php
|
└───resources
└───assets
└───js
└───vendor
└───laravel-twilio
└───mappers
| ResponseMapper.js //maps the response into Response instance
|
└───models
| Response.js //response model
|
└───services
HttpService.js //proxy for axios requests
TwilioService.js //wrapper for twilio client
To use
TwilioService.jsrunyarn add axios twilio-client
You can add/update/remove/move the files and use as your wish
Setup
Update config/services.php
... 'twilio' => [ 'account_sid' => env('TWILIO_ACCOUNT_SID'), 'auth_token' => env('TWILIO_AUTH_TOKEN'), 'caller_id' => env('TWILIO_NUMBER'), 'username' => env('TWILIO_USERNAME'), 'password' => env('TWILIO_PASSWORD'), 'app_sid' => env('TWIML_APP_SID'), ], ...
Update .env
TWILIO_ACCOUNT_SID= TWILIO_AUTH_TOKEN= TWILIO_NUMBER= TWIML_APP_SID= LARAVEL_TWILIO_BASE_URL=laravel-twilio LARAVEL_TWILIO_ENABLE_CALL=true LARAVEL_TWILIO_RECORD_CALL=true LARAVEL_TWILIO_REJECT_CALL_MESSAGE="Thank you for calling us" LARAVEL_TWILIO_REPLY_MESSAGE=null MIX_LARAVEL_TWILIO_BASE_URL="${LARAVEL_TWILIO_BASE_URL}"
TWIML_APP_SID- you need to create a TwiML app for calling from browserLARAVEL_TWILIO_BASE_URL- URL prefix for laravel-twilioLARAVEL_TWILIO_REJECT_CALL_MESSAGE- reject incoming calls with this message when calling is disabledLARAVEL_TWILIO_REPLY_MESSAGE- reply to incoming messages, null for no reply
Now you have to set the Webhook URL in Twilio console.
Replace
laravel-twilioin the Webhook URL with the base URL you've set forLARAVEL_TWILIO_BASE_URLin.env
Incoming Calls
Go to your phone number configuration from Active Numbers then click on the desired number.
- Under
Voice & FaxforAccept IncomingselectVoice Calls - Under
Configure WithselectWebhooks, TwiML Bins, Functions, Studio, or Proxy - Under
A Call Comes InselectWebhookand set the value tohttps://your-domain.tld/api/laravel-twilio/voice/incoming
Incoming Faxes
Go to your phone number configuration from Active Numbers then click on the desired number.
- Under
Voice & FaxforAccept IncomingselectFaxes - Under
Configure WithselectWebhooks, TwiML Bins, Functions, Studio, or Proxy - Under
A Fax Comes InselectWebhookand set the value tohttps://your-domain.tld/api/laravel-twilio/fax/incoming
Incoming Messages
Go to your phone number configuration from Active Numbers then click on the desired number.
- Under
Configure WithselectWebhooks, TwiML Bins, Functions, Studio, or Proxy - Under
A Message Comes InselectWebhookand set the value tohttps://your-domain.tld/api/laravel-twilio/message/incoming
Outgoing Calls
Go to TwiML Apps list and select desired app or create a new app
- Under
Voiceset theREQUEST URItohttps://your-domain.tld/api/laravel-twilio/voice
Usage
Implement Notifiable
/** * @property string username * @property string phone * @property string phone_number */ clas User extends Authenticable { use Notifiable; ... public function routeNotificationForTwilio() { return "+{$this->phone}"; } public function laravelTwilioIdentity() { return Str::snake($this->first_name); } }
Implement notification
use Rocky\LaravelTwilio\Foundation\TwilioMessage; use Rocky\LaravelTwilio\Message\TwilioSMSMessage; use Rocky\LaravelTwilio\Message\TwilioMMSMessage; use Rocky\LaravelTwilio\Message\TwilioFaxMessage; use Rocky\LaravelTwilio\Message\TwilioCallMessage; use Rocky\LaravelTwilio\TwilioChannel; class TwilioTestNotification extends Notification { ... public function via($notifiable) { return [TwilioChannel::class]; } ... public function toTwilio($notifiable) { // SMS return (new TwilioSMSMessage()) ->to('+receiver') // optional ->from('+sender') // optional ->text('Your message'); // required // MMS (only works for Canada and US number) return (new TwilioMMSMessage()) ->to('+receiver') // optional ->from('+sender') // optional ->text('Your message') // optional ->mediaUrl('publicly accessible media url'); // required // Call return (new TwilioCallMessage()) ->to('+receiver') // optional ->from('+sender') // optional ->mediaUrl('publicly accessible media'); // required // Fax return (new TwilioSMSMessage()) ->to('+receiver') // optional ->from('+sender') // optional ->mediaUrl('publicly accessible media url'); // required } }
If you don't use the
to('+number')method in your message construction, you must havephone,phone_numberproperty orrouteNotificationForTwilio()method implemented in yourNotifiableimplementation. The number must start with+followed by country code.
If you don't have
usernameproperty definition in your Auth provider model, you must implementlaravelTwilioIdentity()method to give your agents an identity for calling.
Events
Namespace Rocky\LaravelTwilio\Events
LaravelTwilioIncomingMessage::class[gives access toIncomingMessageat$event->getMessage()]LaravelTwilioIncomingFax::class[gives access toIncomingFaxat$event->getFax()]LaravelTwilioMessageSent::class[gives access toInstanceResourceat$event->getMessage()and$notifiableat$event->getNotifiable()]LaravelTwilioMessageSendingFailed::class[gives access toExceptionat$event->getException(),Notificationat$event->getNotification(),$notifiableat$event->getNotifiable()]LaravelTwilioMessageDeliveryReport::class[gives access toMessageDeliveryReportat$event->getReport()]LaravelTwilioFaxDeliveryReport::class[gives access toFaxDeliveryReportat$event->getReport()]LaravelTwilioInboundCall::class[gives access toInboundCallat$event->getCall()]LaravelTwilioInboundCallRejected::class[gives access toInboundCallat$event->getCall()]LaravelTwilioOutboundCall::class[gives access toOutboundCallat$event->getCall()]LaravelTwilioCallStatusUpdate::class[gives access toCallStatusat$event->getStatus()]LaravelTwilioCallRecord::class[gives access toCallRecordat$event->getRecord()]
All the parameters sent by Twilio are available in the instance passed through Event. Some of the frequently used properties are added for autocomplete support.
Example:
$call = $event->getCall(); $sid = $call->CallSid; $sid = $call->callSid; $sid = $call->call_sid; $from = $call->From; $from = $call->from; $allParams = $call->all();
###The incoming fax implementation is not tested.
Look into the source code for a clearer understanding.