Skip to content

Instantly share code, notes, and snippets.

@egyjs
Created March 5, 2025 23:17
Show Gist options
  • Save egyjs/d5ff4c239a79daaa90f6ee72829d71e6 to your computer and use it in GitHub Desktop.
Save egyjs/d5ff4c239a79daaa90f6ee72829d71e6 to your computer and use it in GitHub Desktop.
Twilio Whatsapp Channel notifications channel for Laravel
<?php
// app/Broadcasting/WhatsappChannel.php
namespace App\Broadcasting;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Facades\Log;
use Twilio\Rest\Client;
class WhatsappChannel
{
/**
* Create a new channel instance.
*/
public function __construct()
{
//
}
/**
* Authenticate the user's access to the channel.
*/
public function send($notifiable, Notification $notification)
{
// $notification = new OtpNotification($user);
$data = $notification->toWhatsapp($notifiable);
$twilioSid = config('services.twilio.id');
$twilioToken = config('services.twilio.token');
$twilioWhatsAppNumber = config('services.twilio.number');
$recipientNumber = 'whatsapp:+'.$data['country_code'].$data['phone']; // rakm ely hab3tluu
$twilio = new Client($twilioSid, $twilioToken);
$msgData = [
'from' => $twilioWhatsAppNumber,
'ContentSid' => $data['sid'],
];
if (! empty($data['vars'])) {
$msgData['ContentVariables'] = json_encode(array_map('strval', $data['vars']));
}
try {
$twilio->messages->create(
$recipientNumber,
$msgData
);
} catch (\Exception $e) {
Log::error($e->getMessage(), [$data]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment