PHP code example of axolotesource / laravel-whatsapp-api

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

    

axolotesource / laravel-whatsapp-api example snippets


WhatsAppMessages::templete('521234567890')
    ->language('es_MX')
    ->addComponents([
        BodyComponent::create([
            Params::text('This is a test message')
        ]),
        ButtonComponent::create([
            Params::button('yes')
        ]),
        ButtonComponent::create([
            Params::button('no')
        ]),
        ButtonComponent::create(
            [
                Params::button('history/login?folio=123ABC')
            ],
            ButtonComponent::SUB_TYPE_URL
        ),
        ButtonComponent::create([
            Params::button('never')
        ])
    ]);

Params::text(string $text, ?string $parameterName = null)
Params::button(string $payload)
Params::imageFromUrl(string $url)

use Axolotesource\LaravelWhatsappApi\WhatsAppMessages\WhatsAppMessages;

WhatsAppMessages::text('521234567890')
    ->body('Hello, this is a text message')
    ->send();

// You can also disable URL previews
WhatsAppMessages::text('521234567890', false)
    ->body('Message without URL preview')
    ->send();

WhatsAppMessages::textMessage('521234567890')
    ->body('Hello world')
    ->send();

WhatsAppMessages::interactiveButtons('521234567890')
    ->setHeaderText('Optional header')
    ->body('Message body')
    ->footer('Optional footer')
    ->addButton('Option 1', 1)
    ->addButton('Option 2', 2)
    ->addButton('Option 3', 3)
    ->send();

WhatsAppMessages::interactiveButtons('521234567890')
    ->setHeaderImage('https://example.com/image.jpg')
    ->body('Message body')
    ->addButton('Yes', 'yes')
    ->addButton('No', 'no')
    ->send();

WhatsAppMessages::interactiveList('521234567890')
    ->body('Choose an option:')
    ->button('View options')
    ->addSection('Section 1', [
        Row::create('Row title 1', 'id-1', 'Optional description'),
        Row::create('Row title 2', 'id-2'),
    ])
    ->addSection('Section 2', [
        Row::create('Row title 3', 'id-3'),
    ])
    ->send();

$media = WhatsAppMedia::image('/local/path/image.jpg')->upload();

WhatsAppMessages::image('521234567890', $media)
    ->send();

WhatsAppMessages::imageByUrl('521234567890', 'https://example.com/image.jpg')
    ->send();

WhatsAppMessages::videoByUrl('521234567890', 'https://example.com/video.mp4')
    ->send();

use Axolotesource\LaravelWhatsappApi\WhatsAppMessages\WhatsAppMedia;

// Upload image
$media = WhatsAppMedia::image('/path/image.jpg')->upload();

// Upload video
$media = WhatsAppMedia::video('/path/video.mp4')->upload();

// Retrieve media metadata by ID
$media = WhatsAppMedia::retrieve('MEDIA_ID')->get();

WhatsAppMessages::raw([
    'type' => 'text',
    'text' => [
        'body' => 'Hello {{name}}'
    ],
], '521234567890', ['name' => 'John'])
    ->send();

use Axolotesource\LaravelWhatsappApi\WhatsAppMessages\WhatsAppTemplate;

$templates = WhatsAppTemplate::all(); // Collection of TemplateDTO

// By status
WhatsAppTemplate::where('status', 'APPROVED')->get();

// By category
WhatsAppTemplate::where('category', 'UTILITY')->get();

// By name
WhatsAppTemplate::where('name', 'my_template')->get();

// By language
WhatsAppTemplate::where('language', 'es_MX')->get();

// Multiple values
WhatsAppTemplate::whereIn('status', ['APPROVED', 'PENDING'])->get();

WhatsAppTemplate::select(['name', 'status', 'category', 'language', 'components'])->get();

WhatsAppTemplate::limit(10)->get();

$list = WhatsAppTemplate::list(); // TemplateList

foreach ($list->data() as $template) {
    echo $template->name . ' - ' . $template->status;
}

if ($list->hasNextPage()) {
    $nextPage = $list->nextPage();
}

WhatsAppMessages::fake();
WhatsAppTemplate::all(); // returns simulated data

// Activate to send all messages to the test number
WhatsAppMessages::fake();

$payload = WhatsAppMessages::text('521234567890')
    ->body('Hello')
    ->toArray();
bash
php artisan vendor:publish --provider="Axolotesource\LaravelWhatsappApi\LaravelWhatsappApiServiceProvider"