PHP code example of nachoaguirre / wassenger-bundle

1. Go to this page and download the library: Download nachoaguirre/wassenger-bundle 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/ */

    

nachoaguirre / wassenger-bundle example snippets


return [
    // ...
    Nachoaguirre\WassengerBundle\NachoaguirreWassengerBundle::class => ['all' => true],
];

namespace App\Controller;

use Nachoaguirre\WassengerBundle\Provider\WassengerProvider;
use Nachoaguirre\WassengerBundle\Registry\RecipientRegistry;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class NotificationController
{
    #[Route('/notify', name: 'app_notify')]
    public function notify(WassengerProvider $whatsapp, RecipientRegistry $registry): Response 
    {
        // Using an alias defined in YAML
        $support = $registry->getActive('support_team');
        if ($support) {
            $whatsapp->sendMessage($support->identifier, "๐Ÿšจ System alert: CPU usage is high!");
        }

        // Or sending directly to a dynamic phone number 
        // (The bundle automatically normalizes E164 formats, stripping spaces/dashes)
        $sent = $whatsapp->sendMessage('+56 9 1234 5678', "Your order has been shipped.");

        // Every send returns a SentMessage DTO with the Wassenger message ID,
        // useful to correlate delivery-status webhooks later on.
        // $sent->id, $sent->status, $sent->deliverAt, $sent->raw

        return new Response('Notifications sent. Message ID: ' . $sent->id);
    }
}

$whatsapp->sendMedia(
    '+56912345678',
    'https://example.com/invoices/inv-2026-001.pdf',
    caption: 'Here is your invoice ๐Ÿงพ'
);

$whatsapp->scheduleMessage(
    '+56912345678',
    '¡Feliz Navidad! ๐ŸŽ„',
    new \DateTimeImmutable('2026-12-24 20:00:00', new \DateTimeZone('America/Santiago'))
);

$whatsapp->sendMessage('[email protected]', 'Deploy finished successfully โœ…');
$whatsapp->sendMedia('[email protected]', 'https://example.com/report.pdf', 'Weekly report');

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Nachoaguirre\WassengerBundle\Contract\WhatsappRecipientInterface;

#[ORM\Entity]
class User implements WhatsappRecipientInterface
{
    #[ORM\Column(length: 20)]
    private ?string $phone = null;

    public function getWhatsappIdentifier(): string
    {
        return $this->phone; 
    }
}

$validation = $whatsapp->validateNumber('+56912345678');

if ($validation->exists) {
    echo "This is a valid WhatsApp account in " . $validation->countryData['name']; 
} else {
    echo "Invalid number: " . $validation->errorMessage;
}

namespace App\EventSubscriber;

use Nachoaguirre\WassengerBundle\Event\WebhookEvent;
use Nachoaguirre\WassengerBundle\Provider\WassengerProvider;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class ChatBotSubscriber implements EventSubscriberInterface
{
    public function __construct(
        private readonly WassengerProvider $whatsapp
    ) {}

    public static function getSubscribedEvents(): array
    {
        return [
            WebhookEvent::NAME => 'onMessageReceived',
        ];
    }

    public function onMessageReceived(WebhookEvent $event): void
    {
        if ($event->getEventType() !== 'message:in') {
            return; // Ignore read receipts, delivery statuses, etc.
        }

        $payload = $event->getPayload();
        $text = strtolower(trim($payload['data']['body'] ?? ''));
        $sender = $payload['data']['from'];

        match ($text) {
            'status' => $this->whatsapp->sendMessage($sender, "All systems operational! ๐ŸŸข"),
            'help' => $this->whatsapp->sendMessage($sender, "Commands available: status, help"),
            default => null
        };
    }
}