PHP code example of bahricanli / whatsapp-bridge

1. Go to this page and download the library: Download bahricanli/whatsapp-bridge 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/ */

    

bahricanli / whatsapp-bridge example snippets


use NotificationChannels\WhatsAppBridge\WhatsAppFacade as WhatsApp;
use NotificationChannels\WhatsAppBridge\WhatsAppMessage;

// Shorthand
WhatsApp::sendMessage('905551234567', 'Doğrulama kodunuz: 4821');

// Fluent message
WhatsApp::sendMessage(
    WhatsAppMessage::create('Doğrulama kodunuz: 4821')->to('905551234567')
);

use Illuminate\Notifications\Notification;
use NotificationChannels\WhatsAppBridge\WhatsAppChannel;
use NotificationChannels\WhatsAppBridge\WhatsAppMessage;

class PhoneVerificationNotification extends Notification
{
    public function __construct(private string $code) {}

    public function via($notifiable): array
    {
        return [WhatsAppChannel::class];
    }

    public function toWhatsApp($notifiable): WhatsAppMessage
    {
        return WhatsAppMessage::create("Doğrulama kodunuz: {$this->code}");
        // Recipient is pulled from routeNotificationForWhatsAppBridge()
    }
}

public function routeNotificationForWhatsAppBridge(): string
{
    return $this->phone_number; // e.g. 905551234567
}

public function via($notifiable): array
{
    return [
        \NotificationChannels\Netgsm\NetgsmChannel::class,
        \NotificationChannels\WhatsAppBridge\WhatsAppChannel::class,
    ];
}

public function toNetgsm($notifiable): string
{
    return "Doğrulama kodunuz: {$this->code}";
}

public function toWhatsApp($notifiable): WhatsAppMessage
{
    return WhatsAppMessage::create("Doğrulama kodunuz: {$this->code}");
}

use NotificationChannels\WhatsAppBridge\Events\MessageWasSent;

Event::listen(MessageWasSent::class, function (MessageWasSent $event) {
    Log::info('WhatsApp sent', [
        'to'      => $event->message->to,
        'content' => $event->message->content,
    ]);
});

use NotificationChannels\WhatsAppBridge\WhatsAppFacade as WhatsApp;

$status = WhatsApp::status();
// ['ok' => true, 'connected' => true, 'sessions' => 3]
bash
php artisan vendor:publish --tag=whatsapp-bridge-config