patelg10 / green-api-laravel
A clean Laravel wrapper for the Green API WhatsApp service.
Requires
- php: ^8.1
- guzzlehttp/guzzle: ^7.0
- illuminate/support: ^9.0|^10.0|^11.0
README
A professional, expressive Laravel wrapper for the Green API. This package provides a seamless way to integrate WhatsApp messaging into your Laravel applications using clean OOP principles, Facades, and an event-driven webhook system.
๐ Features
- โ Quick Setup โ Automatic package discovery and configuration publishing.
- โ
Fluent API โ Simple
GreenApifacade for sending text and media. - โ Webhook Ready โ Built-in endpoint and event dispatcher for incoming messages.
- โ Developer Friendly โ Full IDE auto-completion support via DocBlocks.
- โ Clean Architecture โ Follows Laravel best practices.
๐ฆ Installation
Install the package via Composer:
composer require patelg10/green-api-laravel
Publish the configuration file:
php artisan vendor:publish --tag="greenapi-config"
โ๏ธ Configuration
Add your Green API credentials to your .env file. You can find these in your Green API console.
GREEN_API_ID_INSTANCE=your_id_instance_here
GREEN_API_TOKEN_INSTANCE=your_api_token_here
GREEN_API_HOST=https://api.green-api.com
๐ Usage
Sending a Text Message
The package uses a Facade to make sending messages simple:
use YourName\GreenApi\Facades\GreenApi;
// Send message to a phone number (e.g., 1234567890@c.us)
GreenApi::sendMessage(
'1234567890@c.us',
'Hello! This is a test message from Laravel.'
);
Sending a File (via URL)
Send documents, images, or videos by providing a public URL:
use YourName\GreenApi\Facades\GreenApi;
GreenApi::sendFileByUrl(
'1234567890@c.us',
'https://example.com/invoice.pdf',
'invoice.pdf',
'Please find your invoice attached.'
);
๐ Handling Webhooks (Incoming Messages)
This package automatically registers a POST route at:
/greenapi/webhook
This endpoint listens for events from Green API and dispatches a Laravel Event.
Step 1: Create a Listener
Generate a listener in your application:
php artisan make:listener HandleWhatsAppWebhook
Step 2: Register the Listener
In your App\Providers\EventServiceProvider.php:
use YourName\GreenApi\Events\WebhookReceived;
use App\Listeners\HandleWhatsAppWebhook;
protected $listen = [
WebhookReceived::class => [
HandleWhatsAppWebhook::class,
],
];
Step 3: Implement the Logic
In HandleWhatsAppWebhook.php:
namespace App\Listeners;
use YourName\GreenApi\Events\WebhookReceived;
class HandleWhatsAppWebhook
{
public function handle(WebhookReceived $event)
{
$payload = $event->payload;
// Check if the webhook is an incoming message
if ($payload['typeWebhook'] === 'incomingMessageReceived') {
$sender = $payload['senderData']['sender'];
$message = $payload['messageData']['textMessageData']['textMessage'] ?? '';
// Your business logic here (Save to DB, reply, etc.)
\Log::info("New WhatsApp from {$sender}: {$message}");
}
}
}
๐งช Testing
If you are contributing to the package, you can run tests using:
composer test
๐ก Security
If you discover any security-related issues, please email, Instead of using the issue tracker.
patelg10@gmail.com
๐ License
This package is open-sourced software licensed under the MIT License.