Created
March 5, 2025 23:17
-
-
Save egyjs/d5ff4c239a79daaa90f6ee72829d71e6 to your computer and use it in GitHub Desktop.
Twilio Whatsapp Channel notifications channel for Laravel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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