PHP code example of ashararsi / laravel-whatsapp-cloud

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

    

ashararsi / laravel-whatsapp-cloud example snippets


use Vendor\LaravelWhatsAppCloud\Facades\WhatsApp;

// Send a text message (uses default account)
WhatsApp::send('923001234567', 'Hello World');

// Target a specific account
WhatsApp::account(1)->sendText('923001234567', 'Hello');
WhatsApp::using('marketing')->send('923001234567', 'Sale Started');

// Queue for background delivery
WhatsApp::queue()->send('923001234567', 'Queued message');

// Send a template with variables
WhatsApp::template('923001234567', 'order_confirmed', ['Ali', '#12345']);

WhatsApp::sendText($to, $message, $previewUrl = false);
WhatsApp::sendTemplate($to, 'hello_world', 'en_US', $components);
WhatsApp::template($to, 'order_confirmed', ['Ali', '#12345']);
WhatsApp::sendImage($to, 'https://example.com/image.jpg', $caption);
WhatsApp::sendDocument($to, 'https://example.com/doc.pdf', $filename, $caption);
WhatsApp::sendAudio($to, 'https://example.com/audio.mp3');
WhatsApp::sendVideo($to, 'https://example.com/video.mp4', $caption);
WhatsApp::sendLocation($to, 24.86, 67.00, 'Office', 'Address');

use Vendor\LaravelWhatsAppCloud\Models\WhatsAppAccount;

WhatsAppAccount::create([
    'name' => 'primary',
    'provider' => WhatsAppAccount::PROVIDER_META,
    'phone_number' => '923001234567',
    'phone_number_id' => 'YOUR_PHONE_NUMBER_ID',
    'access_token' => 'YOUR_ACCESS_TOKEN',
    'app_secret' => 'YOUR_META_APP_SECRET',
    'webhook_verify_token' => 'my-secret-token',
    'is_default' => true,
    'is_active' => true,
]);

WhatsAppAccount::create([
    'name' => 'twilio-support',
    'provider' => WhatsAppAccount::PROVIDER_TWILIO,
    'phone_number' => '923001234567',
    'twilio_sid' => 'ACxxxxxxxx',
    'twilio_token' => 'your_auth_token',
    'twilio_whatsapp_number' => '14155238886',
    'is_default' => false,
    'is_active' => true,
]);

WhatsApp::using('twilio-support')->sendText('923001234567', 'Hello from Twilio');

'providers' => [
    'meta' => \Vendor\LaravelWhatsAppCloud\Providers\MetaProvider::class,
    'twilio' => \Vendor\LaravelWhatsAppCloud\Providers\TwilioProvider::class,
],

use Vendor\LaravelWhatsAppCloud\Facades\WhatsApp;

// Sends order_confirmed template with body variables
WhatsApp::template('923001234567', 'order_confirmed', [
    'Ali',      // {{1}}
    '#12345',   // {{2}}
]);

// Specific account + language override
WhatsApp::using('marketing')->template('923001234567', 'order_confirmed', ['Ali', '#12345'], 'en_US');

WhatsApp::sendTemplate('923001234567', 'order_confirmed', 'en_US', [
    [
        'type' => 'body',
        'parameters' => [
            ['type' => 'text', 'text' => 'Ali'],
            ['type' => 'text', 'text' => '#12345'],
        ],
    ],
]);

use Vendor\LaravelWhatsAppCloud\Contracts\TenantResolverInterface;

class TenantResolver implements TenantResolverInterface
{
    public function resolve(): ?int
    {
        return auth()->user()?->tenant_id;
    }
}

$this->app->bind(
    \Vendor\LaravelWhatsAppCloud\Contracts\TenantResolverInterface::class,
    TenantResolver::class,
);

config(['whatsapp.tenant.resolver' => TenantResolver::class]);

app(\Vendor\LaravelWhatsAppCloud\Services\TenantContext::class)
    ->runForTenant($tenantId, fn () => WhatsApp::sendText('923001234567', 'Hello'));

// Programmatic access
app(\Vendor\LaravelWhatsAppCloud\Services\WhatsAppSettingsService::class)->get('queue.enabled');
app(\Vendor\LaravelWhatsAppCloud\Services\WhatsAppSettingsService::class)->updateMany([
    'webhook.

use Vendor\LaravelWhatsAppCloud\Notifications\WhatsAppChannel;

public function via($notifiable): array
{
    return [WhatsAppChannel::class];
}

public function toWhatsApp($notifiable): array
{
    return [
        'using' => 'twilio-support', // or account ID
        'text' => 'Your order shipped!',
        'queue' => true,
    ];
}

use Vendor\LaravelWhatsAppCloud\Events\MessageReceived;

Event::listen(MessageReceived::class, function (MessageReceived $event) {
    // Handle incoming message (Meta or Twilio)
});
bash
composer whatsapp:install
php artisan migrate
bash
php artisan whatsapp:install --single --migrate
php artisan whatsapp:install --tenant --migrate
bash
php artisan whatsapp:campaigns:run
bash
php artisan vendor:publish --tag=whatsapp-views

resources/views/vendor/whatsapp/
├── layouts/
│   └── admin.blade.php          # Master layout (sidebar, menu, alerts)
└── admin/
    ├── dashboard.blade.php
    ├── accounts/
    ├── contacts/
    └── conversations/
bash
php artisan vendor:publish --tag=whatsapp-views --force

resources/views/vendor/whatsapp/layouts/admin.blade.php
bash
php artisan vendor:publish --tag=whatsapp-views

resources/views/vendor/whatsapp/admin/accounts/_form.blade.php
bash
php artisan view:clear
bash
php artisan whatsapp:doctor