1. Go to this page and download the library: Download beziworld/laravel-twilio library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
beziworld / laravel-twilio example snippets
use Beziworld\Twilio\Facades\Twilio;
use Beziworld\Twilio\Messages\TwilioMessage;
// Shortcut
$message = Twilio::sendSms('+48500100200', 'Your code is 1234');
// Builder
$message = Twilio::send(
TwilioMessage::create('Your order #1001 has shipped')
->to('+48500100200')
->from('Shop') // alphanumeric sender where supported
->media('https://example.com/label.pdf') // MMS
->statusCallback(route('twilio.webhooks.status'))
->validityPeriod(3600)
);
$message->sid; // SM...
$message->status; // MessageStatus::Queued
$message->numSegments;
Twilio::message('SM...'); // fetch
Twilio::messages(['To' => '+48500100200'], pageSize: 50); // first page (MessagePage)
Twilio::allMessages(['DateSent>' => now()->subWeek()]) // LazyCollection over every page
->each(fn ($message) => ...);
Twilio::redact('SM...'); // remove the body, keep metadata
Twilio::deleteMessage('SM...');
$message = Twilio::send(
TwilioMessage::create('Reminder: your visit is tomorrow at 10:00')
->to('+48500100200')
->messagingService('MG...') // or TWILIO_MESSAGING_SERVICE_SID
->sendAt($visit->starts_at->subDay())
);
Twilio::cancelScheduled($message->sid);
// Free-form message (inside the 24 hour customer service window)
Twilio::sendWhatsApp('+48500100200', 'Your table is ready');
// Approved content template (send(TwilioMessage::whatsapp('Your invoice')->to('+48500100200')->media('https://example.com/invoice.pdf'));
use Beziworld\Twilio\Enums\VerificationChannel;
Twilio::startVerification('+48500100200'); // SMS
Twilio::startVerification('+48500100200', VerificationChannel::WhatsApp, ['Locale' => 'pl']);
Twilio::startVerification('[email protected]', 'email');
if (Twilio::verifyCode('+48500100200', $request->input('code'))) {
// approved
}
$check = Twilio::checkVerification('123456', to: '+48500100200'); // full Verification object
$check->status; // VerificationStatus::Approved / Pending
$info = Twilio::lookup('+48500100200', ['line_type_intelligence']);
$info->valid; // bool
$info->phoneNumber; // +48500100200
$info->isMobile(); // LineType::Mobile
$info->lineTypeIntelligence?->carrierName;
$info->package('sms_pumping_risk'); // raw data of any other requested package
Twilio::lookup('500100200', countryCode: 'PL'); // national format
use Beziworld\Twilio\Messages\TwilioMessage;
use Illuminate\Notifications\Notification;
class OrderShipped extends Notification
{
public function via(object $notifiable): array
{
return ['twilio'];
}
public function toTwilio(object $notifiable): TwilioMessage|string
{
return TwilioMessage::whatsapp()
->template('HX...', ['1' => $this->order->number]);
}
}
public function routeNotificationForTwilio(): ?string
{
return $this->phone; // E.164, e.g. +48500100200
}