PHP code example of waotomatis / sdk

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

    

waotomatis / sdk example snippets


use Waotomatis\Client;

$wao = new Client(getenv('WAO_API_KEY'));

$msg = $wao->sessions('sess_123')->messages->sendText(
    '628123456789',
    'Halo dari WAOtomatis 👋',
);

echo $msg['id']; // msg_abc123

$wao = new Client('wao_live_...', [
    'baseUrl'    => 'https://api.waotomatis.com', // override the API base
    'timeout'    => 60.0,                         // per-request timeout (seconds)
    'maxRetries' => 2,                            // retries for 408/429/5xx/network
    'headers'    => ['X-Org-Id' => 'org_123'],    // sent on every request
]);

// Scope into one session.
$session = $wao->sessions('sess_123');

// List / get / disconnect.
$page    = $wao->sessions->list();   // ['data' => [...], 'hasMore' => bool, 'cursor' => ?string]
$one     = $wao->sessions->get('sess_123');
$wao->sessions->delete('sess_123');

// (equivalent fluent forms)
$one = $wao->sessions('sess_123')->get();
$wao->sessions('sess_123')->delete();

$messages = $wao->sessions('sess_123')->messages;

// Plain text, with a link preview
$messages->sendText('628123456789', 'Cek https://waotomatis.com', previewUrl: true);

// Image by uploaded media id (with caption + idempotency key)
$messages->sendMedia('628123456789', 'image', mediaId: 'media_abc', caption: 'Invoice', idempotencyKey: 'inv-4711');

// Document by public link
$messages->sendMedia('628123456789', 'document', link: 'https://example.com/invoice.pdf', fileName: 'invoice.pdf');

// Audio as a voice note
$messages->sendMedia('628123456789', 'audio', mediaId: 'media_xyz', voice: true);

$messages->sendTemplate('628123456789', 'order_update', 'en_US', [
    ['type' => 'body', 'parameters' => [['type' => 'text', 'text' => 'A1234']]],
]);

// Reply buttons (max 3)
$messages->sendInteractive('628123456789', 'button', bodyText: 'Konfirmasi pesananmu?', buttons: [
    ['id' => 'yes', 'title' => 'Ya'],
    ['id' => 'no',  'title' => 'Tidak'],
]);

// List
$messages->sendInteractive('628123456789', 'list', bodyText: 'Pilih menu', listButton: 'Lihat', sections: [
    ['title' => 'Makanan', 'rows' => [
        ['id' => 'nasi', 'title' => 'Nasi goreng', 'description' => 'Pedas'],
    ]],
]);

// Call-to-action URL button
$messages->sendInteractive('628123456789', 'cta_url',
    bodyText: 'Visit our store',
    ctaDisplayText: 'Shop now',
    ctaUrl: 'https://example.com/shop',
);

// WhatsApp Flow
$messages->sendInteractive('628123456789', 'flow', bodyText: 'Book an appointment', flow: [
    'flowCta'           => 'Book now',
    'flowId'            => '1234567890',             // optional
    'flowAction'        => 'navigate',              // 'navigate' | 'data_exchange'
    'flowActionPayload' => ['screen' => 'WELCOME'], // optional
    'mode'              => 'published',             // 'draft' | 'published'
]);

// Single catalog product
$messages->sendInteractive('628123456789', 'product',
    catalogId: 'cat_123',
    productRetailerId: 'SKU-1',
);

// Multi-section product list
$messages->sendInteractive('628123456789', 'product_list',
    bodyText: 'Browse our products',
    catalogId: 'cat_123',
    productSections: [
        ['title' => 'Shoes', 'productItems' => [
            ['productRetailerId' => 'SKU-1'],
            ['productRetailerId' => 'SKU-2'],
        ]],
    ],
);

$messages->sendReaction('628123456789', 'wamid.HBgL...', '👍');

// Clear it
$messages->sendReaction('628123456789', 'wamid.HBgL...', '');

$messages->sendLocation('628123456789', -6.2, 106.816666, name: 'Monas', address: 'Jakarta Pusat');

$messages->sendContacts('628123456789', [
    [
        'name'   => [
            'formatted_name' => 'Budi Santoso',
            'first_name'     => 'Budi',
            'last_name'      => 'Santoso',
        ],
        'phones' => [
            ['phone' => '+628123456789', 'type' => 'WORK', 'wa_id' => '628123456789'],
        ],
        'emails' => [
            ['email' => '[email protected]', 'type' => 'WORK'],
        ],
        'org'    => ['company' => 'WAOtomatis', 'title' => 'Engineer'],
    ],
]);

$messages->sendCarousel('628123456789', 'summer_sale', 'en_US', [
    [
        'headerImageId' => 'media_abc',          // or 'headerImageLink' => 'https://...'
        'bodyParams'    => ['Shoes', '30%'],
        'buttons'       => [
            ['subType' => 'quick_reply', 'index' => 0, 'payload' => 'BUY_SHOES'],
            ['subType' => 'url', 'index' => 1, 'urlParam' => 'shoes'],
        ],
    ],
    [
        'headerVideoLink' => 'https://example.com/promo.mp4',
        'bodyParams'      => ['Bags', '20%'],
        'buttons'         => [
            ['subType' => 'quick_reply', 'index' => 0, 'payload' => 'BUY_BAGS'],
        ],
    ],
], bodyParams: ['Budi']); // bodyParams fills the message bubble body

$messages->sendText('628123456789', 'Halo', idempotencyKey: 'order-4711');

$wao->sessions('sess_123')->messages->markRead($providerMessageId);

$session = $wao->sessions('sess_123');

// By URL
$m = $session->media->uploadFromUrl('https://example.com/photo.jpg', 'image/jpeg');

// By raw bytes
$m = $session->media->upload($bytes, 'photo.jpg', 'image/jpeg');

// By local file path
$m = $session->media->uploadFile('/path/to/photo.jpg');

echo $m['mediaId']; // pass this to messages->sendMedia($to, 'image', mediaId: $m['mediaId'])

// Download inbound media bytes
$dl = $session->media->download('media_abc');
file_put_contents('out.bin', $dl['data']); // $dl['mimeType'] holds the content type

$templates = $wao->sessions('sess_123')->templates;

// List (all filters optional: limit 1–100, after cursor, name, language, status, category)
$page = $templates->list(['category' => 'MARKETING', 'limit' => 20]);
foreach ($page['data'] as $tpl) {
    echo "{$tpl['name']} [{$tpl['language']}] — {$tpl['status']}\n";
}
$next = $page['paging']['cursors']['after'] ?? null; // pass back as ['after' => $next]

// Get every language version registered under a name
$one = $templates->get('order_update'); // ['data' => [...], 'paging' => ...]

// Create — submit for Meta approval. `components` is Meta's component array.
$res = $templates->create('order_update', 'en_US', 'UTILITY', [
    ['type' => 'BODY', 'text' => 'Your order {{1}} has shipped.'],
    [
        'type'    => 'BUTTONS',
        'buttons' => [
            ['type' => 'URL', 'text' => 'Track', 'url' => 'https://example.com/track/{{1}}'],
        ],
    ],
], allowCategoryChange: true);
echo $res['id'] . ' ' . $res['status']; // e.g. "123456 PENDING"

// Delete by name (removes all language versions)
$templates->delete('order_update'); // ['success' => true]

use Waotomatis\Webhook;

$raw = file_get_contents('php://input');
$sig = $_SERVER['HTTP_X_WAO_SIGNATURE'] ?? null;
$secret = getenv('WAO_WEBHOOK_SECRET');

if (!Webhook::verify($raw, $sig, $secret)) {
    http_response_code(401);
    exit;
}

$event = Webhook::constructEvent($raw, $sig, $secret); // verifies + decodes
if ($event['event'] === 'message.received') {
    $text = $event['data']['text'] ?? null;
}

use Waotomatis\Exception\RateLimitError;
use Waotomatis\Exception\WaotomatisException;

try {
    $wao->sessions('sess_123')->messages->sendText('628123456789', 'Halo');
} catch (RateLimitError $e) {
    sleep($e->getRetryAfter() ?? 1);
} catch (WaotomatisException $e) {
    error_log("{$e->getCodeName()} ({$e->getStatus()}) req={$e->getRequestId()}: {$e->getMessage()}");
}