Installation
composer require biruwon/whatsapi-bundle
Add to bundles.php (Symfony 2.x):
Biruwon\WhatsAPIBundle\BiruwonWhatsAPIBundle::class => ['all' => true],
Configuration
Edit config/packages/biruwon_whatsapi.yaml (or create if missing):
biruwon_whatsapi:
api_key: '%env(WHATSAPP_API_KEY)%'
phone_number: '+1234567890'
base_url: 'https://api.whatsapp.com'
First Use Case Inject the service and send a message:
use Biruwon\WhatsAPIBundle\Service\WhatsAPIService;
class SomeController
{
public function __construct(private WhatsAPIService $whatsapp)
{}
public function sendWelcomeMessage()
{
$this->whatsapp->sendMessage(
'+1987654321', // Recipient
'Hello from Symfony!' // Message
);
}
}
src/Service/WhatsAPIService.php (Core logic)Resources/config/services.yaml (Service definitions)Tests/ (Basic usage examples)Sending Messages
$this->whatsapp->sendMessage($phone, $message, [
'type' => 'text', // Optional: 'text', 'image', 'document'
'media_url' => 'https://example.com/image.jpg' // For media types
]);
Handling Responses Use Symfony’s event system to listen for message delivery status:
# config/packages/biruwon_whatsapi.yaml
biruwon_whatsapi:
listeners:
on_message_sent: App\EventListener\WhatsAPIListener
// src/EventListener/WhatsAPIListener.php
class WhatsAPIListener
{
public function onMessageSent(MessageSentEvent $event)
{
if (!$event->isSuccess()) {
// Log or retry failed messages
}
}
}
Media Handling Upload and send images/documents:
$this->whatsapp->sendMedia(
'+1987654321',
'https://example.com/receipt.pdf',
'Your invoice.pdf'
);
Template Messages (Advanced) Use WhatsApp’s template system (requires pre-approved templates):
$this->whatsapp->sendTemplate(
'+1987654321',
'template_name',
['param1' => 'value1'] // Template variables
);
$this->whatsapp->setLogger($logger); // Inject PSR-3 logger
WhatsAPIService in unit tests:
$this->createMock(WhatsAPIService::class)
->method('sendMessage')
->willReturn(true);
API Key Exposure
api_key in config. Use .env and restrict file permissions.WHATSAPP_API_KEY validation (e.g., Symfony’s env validator).Phone Number Format
+1234567890, not 1234567890).libphonenumber to normalize numbers:
use libphonenumber\PhoneNumberUtil;
$util = PhoneNumberUtil::getInstance();
$phone = $util->format($util->parse($rawNumber), PhoneNumberFormat::E164);
Media Size Limits
if ($file->getSize() > 5 * 1024 * 1024) {
throw new \RuntimeException('File too large');
}
Deprecated Methods
class ExtendedWhatsAPIService extends WhatsAPIService
{
public function sendMessage(string $phone, string $message, array $options = []): bool
{
// ...
}
}
Enable API Debugging Add this to config to log raw API responses:
biruwon_whatsapi:
debug: true
Common Errors
401 Unauthorized: Invalid API key or phone number.400 Bad Request: Malformed phone number or message.429 Too Many Requests: Hit rate limits (WhatsApp allows ~240 messages/minute for business accounts).Extension Points
$this->whatsapp->setHttpClient($customClient);
MessageSentEvent to add custom logic (e.g., analytics).base_url may not match WhatsApp’s current API endpoint. Verify with WhatsApp Business API docs.WHATSAPP_API_KEY exists. Add to .env.dist:
WHATSAPP_API_KEY=your_key_here
How can I help you explore Laravel packages today?