PHP code example of haimanresources / laravel-whatsapp

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

    

haimanresources / laravel-whatsapp example snippets


use HRC\WhatsApp\Facades\WhatsApp;

WhatsApp::sendText('+234 801 234 5678', 'Your order has shipped.');

use HRC\WhatsApp\Facades\WhatsApp;

WhatsApp::sendText($to, 'Free-form text');                  // inside the 24h window
WhatsApp::sendTemplate($to, 'order_update', 'en_US', [      // outside it
    ['type' => 'body', 'parameters' => [['type' => 'text', 'text' => 'ORD-1234']]],
]);
WhatsApp::sendMedia($to, 'https://example.com/receipt.pdf', 'document', 'Your receipt');
WhatsApp::markAsRead($wamid);

use HRC\WhatsApp\Events\MessageReceived;

class ReplyToCustomer
{
    public function handle(MessageReceived $event): void
    {
        $event->message->text;          // 'Where is my order?'
        $event->conversation->wa_id;    // '2348012345678'
        $event->conversation->owner;    // your model, if attached
    }
}

'webhook' => [
    'queue' => true,             // or WHATSAPP_WEBHOOK_QUEUE=true
    'queue_name' => 'default',
],

WhatsApp::sendText($to, 'Hello', owner: $client);

$client->id === $conversation->owner->id;

Event::listen(function (ConversationStarted $event) {
    $client = Client::where('phone', $event->conversation->wa_id)->first();
    $client && $event->conversation->update([
        'owner_type' => $client->getMorphClass(),
        'owner_id' => $client->getKey(),
    ]);
});

// config/whatsapp.php
'ui' => [
    'enabled' => true,
    'path' => 'whatsapp',
    'middleware' => ['web', 'auth', 'can:manage-whatsapp'],   // yours
],

'ui' => ['enabled' => false],
'api' => ['enabled' => true, 'path' => 'api/whatsapp', 'middleware' => ['api', 'auth:sanctum']],

use HRC\WhatsApp\Broadcaster;

$broadcast = app(Broadcaster::class)->create(
    name: 'September promo',
    recipients: ['2348011111111', '2348022222222'],
    bodyText: 'Our new prices are live.',   // reaches contacts inside the window
    template: 'promo_september',            // reaches everyone else
);

app(Broadcaster::class)->dispatch($broadcast);

use HRC\WhatsApp\Models\Conversation;

Conversation::with('messages')->latest('last_message_at')->paginate(30);
Conversation::sole()->messages()->inbound()->get();

'webhook' => ['enabled' => false],

Route::post('/custom/whatsapp', [HRC\WhatsApp\Http\Controllers\WebhookController::class, 'handle']);
Route::get('/custom/whatsapp', [HRC\WhatsApp\Http\Controllers\WebhookController::class, 'verify']);

Http::fake(['graph.facebook.com/*' => Http::response(['messages' => [['id' => 'wamid.test']]])]);

WhatsApp::sendText('2348012345678', 'Hello');

Http::assertSent(fn ($request) => $request['type'] === 'text');
bash
php artisan vendor:publish --tag=whatsapp-config
bash
php artisan vendor:publish --tag=whatsapp-migrations
bash
   php artisan whatsapp:subscribe
   
bash
   php artisan route:list --name=whatsapp   # both routes registered
   php artisan whatsapp:readiness           # Meta's side agrees
   
bash
php artisan vendor:publish --tag=whatsapp-views
bash
php artisan vendor:publish --tag=whatsapp-types   # -> resources/js/whatsapp/