Appearance
Evolution Handlers
Overview
Evolution Handlers are a key part of the WhatsApp module that processes webhook events from the Evolution API. They provide a structured way to respond to various events such as incoming messages, status updates, connection changes, and more. Each handler is responsible for processing a specific type of event and performing the appropriate actions.
The handler system is designed to be extensible, allowing creating custom handlers to meet specific requirements.
Creating a Custom Handler
To create a custom handler, you need to create a class that extends the BaseHandler
abstract class.
php
use Modules\Whatsapp\Support\Evolution\BaseHandler;
class YourCustomHandler extends BaseHandler
{
// Define which Evolution event this handler process
// Uses one of the values from the WebhookEvents enum
public ?string $evolutionEvent = 'remove.instance';
protected bool $toQueue = true; // Define if the handler should be queued
// Implement the handle method to process the event
public function handle(): void
{
Log::info('Removed instance', [
'instance' => $this->data['instance'] ?? 'unknown',
]);
}
}
Using the Artisan Command
You can quickly generate a new handler using the provided Artisan command:
bash
php artisan make:evo-handler HandlerName event.name
The command supports the following options and arguments:
name
: The name of the handler class (e.g.,MessageReceivedHandler
)event
: The Evolution event name (e.g.,message.received
)--module
: The module where the handler should be created (defaults to Whatsapp if not specified)
Interactive Mode
If you don't provide the required arguments, the command will prompt you for them:
bash
php artisan make:evo-handler
Examples
Create a handler in the Whatsapp module:
bash
php artisan make:evo-handler MessageReceivedHandler message.received
Create a handler in a different module:
bash
php artisan make:evo-handler StatusUpdatedHandler status.updated --module=CustomModule
Registering Handlers
In your service provider's boot()
method:
php
use Modules\Whatsapp\Facades\Handler;
use Modules\Whatsapp\Handlers\YourCustomHandler;
protected function registerEvolutionHandlers(): void
{
Handler::register(YourCustomHandler::class);
}