PHP code example of aghfatehi / laravel-whatsapp-api

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

    

aghfatehi / laravel-whatsapp-api example snippets


use Aghfatehi\WhatsApp\Facades\WhatsApp;

// Send a text message
WhatsApp::messages()->sendText('966555555555', 'Hello!');

// Send a media message (image, document, audio, video)
WhatsApp::messages()->sendMedia('966555555555', 'image', 'https://example.com/photo.jpg', 'Caption');

// Send a template message
WhatsApp::messages()->sendTemplate('966555555555', 'hello_world', 'en');

// Send a location
WhatsApp::messages()->sendLocation('966555555555', 24.7136, 46.6753, 'Riyadh', 'Saudi Arabia');

// Send an interactive (button/list)
WhatsApp::messages()->sendInteractive('966555555555', [
    'type' => 'button',
    'body' => ['text' => 'Choose:'],
    'action' => [
        'buttons' => [
            ['type' => 'reply', 'reply' => ['id' => '1', 'title' => 'Yes']],
            ['type' => 'reply', 'reply' => ['id' => '2', 'title' => 'No']],
        ],
    ],
]);

// Send reaction
WhatsApp::messages()->sendReaction('966555555555', 'wamid.xxx', '👍');

// Mark message as read
WhatsApp::messages()->markAsRead('wamid.xxx');

WhatsApp::messages()->sendText($to, $body, $previewUrl);
WhatsApp::messages()->sendMedia($to, $type, $url, $caption, $filename);
WhatsApp::messages()->sendMediaById($to, $type, $mediaId, $caption, $filename);
WhatsApp::messages()->sendTemplate($to, $name, $lang, $components);
WhatsApp::messages()->sendInteractive($to, $data);
WhatsApp::messages()->sendLocation($to, $lat, $lng, $name, $address);
WhatsApp::messages()->sendContacts($to, $contacts);
WhatsApp::messages()->sendReaction($to, $messageId, $emoji);
WhatsApp::messages()->markAsRead($messageId);

WhatsApp::media()->upload($filePath, $type);     // Upload to WhatsApp servers
WhatsApp::media()->getUrl($mediaId);               // Get download URL
WhatsApp::media()->download($mediaId);             // Download raw content
WhatsApp::media()->delete($mediaId);               // Delete from WhatsApp

WhatsApp::phoneNumbers()->list();                  // All phone numbers
WhatsApp::phoneNumbers()->get($phoneNumberId);      // Single number details
WhatsApp::phoneNumbers()->requestPin($phoneNumberId);
WhatsApp::phoneNumbers()->verifyPin($phoneNumberId, $pin);
WhatsApp::phoneNumbers()->deregister($phoneNumberId);

WhatsApp::businessProfile()->get($phoneNumberId);
WhatsApp::businessProfile()->update($phoneNumberId, $data);

WhatsApp::templates()->list($status, $limit);
WhatsApp::templates()->get($templateId);
WhatsApp::templates()->create($data);
WhatsApp::templates()->update($templateId, $data);
WhatsApp::templates()->delete($templateId);

WhatsApp::qrCodes()->create($prefilledMessage);
WhatsApp::qrCodes()->list();
WhatsApp::qrCodes()->get($qrCodeId);
WhatsApp::qrCodes()->update($qrCodeId, $data);
WhatsApp::qrCodes()->delete($qrCodeId);

WhatsApp::twoStepVerification()->setPin($phoneNumberId, $pin);
WhatsApp::twoStepVerification()->deletePin($phoneNumberId);

WhatsApp::conversations()->list($phoneNumberId, $filters);
WhatsApp::conversations()->getAnalytics($phoneNumberId, $dimensions);
WhatsApp::conversations()->getPricing($phoneNumberId);

WhatsApp::webhook()->verifyToken($mode, $token, $challenge);
WhatsApp::webhook()->verifySignature($body, $signature);
WhatsApp::webhook()->parsePayload($payload);
WhatsApp::webhook()->handle($payload);

WhatsApp::connection('default')->messages()->sendText(...);
WhatsApp::connection('secondary')->messages()->sendText(...);

'connections' => [
    'default' => [
        'api_token' => env('WHATSAPP_API_TOKEN'),
        'phone_number_id' => env('WHATSAPP_PHONE_NUMBER_ID'),
        'waba_id' => env('WHATSAPP_WABA_ID'),
    ],
    'secondary' => [
        'api_token' => env('WHATSAPP_API_TOKEN_2'),
        'phone_number_id' => env('WHATSAPP_PHONE_NUMBER_ID_2'),
        'waba_id' => env('WHATSAPP_WABA_ID_2'),
    ],
],
bash
php artisan whatsapp:setup              # Validate configuration
php artisan whatsapp:phone-numbers      # List connected phone numbers
php artisan whatsapp:register-webhook   # Register webhook URL