PHP code example of 42dx / whatsapp-laravel-sdk

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

    

42dx / whatsapp-laravel-sdk example snippets


return [
    // [...]
    The42dx\Whatsapp\RouteServiceProvider::class,
    The42dx\Whatsapp\WhatsappServiceProvider::class,
];

$message = WhatsappMessage::first();
$firstPayloadItem = $message->payload[0] ?? null;

// Get only context payload items for reply messages
$replyContext = $message->getPayloadType(ContextType::REPLY);

// Get all payload items except reaction data
$withoutReactions = $message->getPayloadWithoutType(MessageType::REACTION);

// config/whatsapp.php
'database' => [
    'messageable_phone_column' => 'phone',       // column name for the phone number
    'skip_migrations'          => false,          // set true to disable package-loaded migrations
    'table_name'               => 'whatsapp_messages',
    'users_table'              => 'users',        // table to alter
    'users_table_pk'           => 'id',            // primary key of the users table
    'messageable_id_column'    => 'user_id',       // FK column on whatsapp_messages
    'user_model'               => App\Models\User::class, // model used for lookups
],

use The42dx\Whatsapp\Enums\{MessageComponent, MessageType};
use The42dx\Whatsapp\Models\Traits\CanSendWhatsappMsg;

class User extends Authenticatable
{
    use CanSendWhatsappMsg;
}

$phoneColumn = config('whatsapp.database.messageable_phone_column', 'phone');
$user = User::where($phoneColumn, '+1234567890')->first();

// Send a text message
$user->sendWhatsappMsg(MessageType::TEXT, 'Hello from the SDK!');

// Send a template message
$user->sendWhatsappMsg(MessageType::TEMPLATE, [
    'name' => 'hello_world',
    'lang' => 'en_US',
]);

// React to a message
$user->sendWhatsappMsg(MessageType::REACTION, '👍', $originalMessage);

use The42dx\Whatsapp\Factories\WhatsappApiMessage;
use The42dx\Whatsapp\Services\WhatsappService;

public function __construct(
    protected WhatsappService $whatsapp,
) {}

public function send(): void
{
    $message = WhatsappApiMessage::compose('+1234567890')
        ->with(text: 'Hello from the SDK!');

    $response = $this->whatsapp->send($message);
}

$whatsapp = app(WhatsappService::class);

$message = WhatsappApiMessage::make('+1234567890')
    ->withText('Hello from the SDK!');

$response = $whatsapp->send($message, $user);

$user->sendWhatsappMsg(MessageType::TEMPLATE, [
    'name' => 'order_update',
    'lang' => 'en_US',
    'components' => [
        [
            'type' => MessageComponent::BODY,
            'parameters' => [
                ['name' => 'customer_name', 'text' => 'Rafael'],
                ['name' => 'order_code', 'text' => 'DX-123'],
            ],
        ],
        [
            'type' => MessageComponent::BUTTON,
            'subType' => MessageComponent::COPY_CODE,
            'index' => 0,
            'parameters' => [
                [
                    'type' => MessageComponent::COUPON_CODE,
                    'couponCode' => 'SAVE10',
                ],
            ],
        ],
    ],
]);

$message = WhatsappApiMessage::compose('+1234567890')
    ->usingTemplate('order_update', 'en_US')
    ->withComponent(MessageComponent::BODY, [
        ['name' => 'customer_name', 'text' => 'Rafael'],
        ['name' => 'order_code', 'text' => 'DX-123'],
    ]);

$response = app(WhatsappService::class)->send($message, $user);

use The42dx\Whatsapp\Services\WhatsappService;

$templates = app(WhatsappService::class)->getMessageTemplates();
bash
php artisan vendor:publish --tag=whatsapp-business-api-config
bash
php artisan vendor:publish --tag=whatsapp-business-api-migrations
bash
php artisan migrate