PHP code example of sejator / waba-sdk

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

    

sejator / waba-sdk example snippets




return [

    'meta' => [

        'app_id' => env(
            'META_APP_ID'
        ),

        'app_secret' => env(
            'META_APP_SECRET'
        ),

        'api_version' => env(
            'META_API_VERSION',
            'v25.0'
        ),

        'base_url' => sprintf(
            'https://graph.facebook.com/%s',
            env('META_API_VERSION', 'v25.0')
        ),

        'access_token' => env(
            'META_ACCESS_TOKEN'
        ),

        'system_user_token' => env(
            'META_SYSTEM_USER_TOKEN'
        ),

        'business_id' => env(
            'META_BUSINESS_ID'
        ),

    ],

    'embedded' => [

        'config_id' => env(
            'META_CONFIGURATION_ID'
        ),

        'redirect_uri' => env(
            'META_REDIRECT_URI'
        ),

        'oauth_version' => env(
            'META_OAUTH_VERSION',
            env('META_API_VERSION', 'v25.0')
        ),

        'version' => env(
            'META_EMBEDDED_VERSION',
            'v4'
        ),

        'auto_resolve_waba' => env(
            'META_AUTO_RESOLVE_WABA',
            true
        ),

        'auto_subscribe_webhook' => env(
            'META_AUTO_SUBSCRIBE_WEBHOOK',
            true
        ),

        'auto_fetch_phone_numbers' => env(
            'META_AUTO_FETCH_PHONES',
            true
        ),

        'auto_fetch_templates' => env(
            'META_AUTO_FETCH_TEMPLATES',
            true
        ),

        'state_ttl' => env(
            'META_STATE_TTL',
            300
        ),
    ],

    'http' => [
        'timeout' => env(
            'WABA_HTTP_TIMEOUT',
            30
        ),

        'retry' => [
            'times' => env(
                'WABA_HTTP_RETRY_TIMES',
                2
            ),

            'sleep' => env(
                'WABA_HTTP_RETRY_SLEEP',
                500
            ),
        ],
    ],
];


use Sejator\WabaSdk\Services\Client;
use Sejator\WabaSdk\Services\MessageService;

$client = app(Client::class)
    ->withToken($device->access_token);

$messageService = new MessageService(
    $client
);

$response = $messageService->text(
    phoneNumberId: $device->phone_number_id,
    to: '628xxxx',
    text: 'Hello World'
);

$response = $messageService->media(
    phoneNumberId: $device->phone_number_id,
    to: '628xxxx',
    type: 'image',
    url: 'https://example.com/image.jpg',
    caption: 'Image Caption'
);

$response = $messageService->media(
    phoneNumberId: $device->phone_number_id,
    to: '628xxxx',
    type: 'document',
    url: 'https://example.com/file.pdf',
    caption: 'Invoice',
    filename: 'invoice.pdf'
);

$response = $messageService->template(
    $device->phone_number_id,
    [
        'to' => '628xxxx',
        'template' => [
            'name' => 'hello_world',
            'language' => [
                'code' => 'id'
            ],
        ],
    ]
);

use Sejator\WabaSdk\Services\MediaService;

$media = new MediaService($client);

$response = $media->upload(
    $device->phone_number_id,
    storage_path('app/image.jpg')
);

$binary = $media->download($mediaUrl);

use Sejator\WabaSdk\Services\TemplateService;

$templates = $service->all(
    $device->waba_id
);

$response = $service->create(
    $device->waba_id,
    [
        'name' => 'promo_template',
        'language' => 'id',
        'category' => 'MARKETING',
        'components' => [
            [
                'type' => 'BODY',
                'text' => 'Halo {{1}}'
            ]
        ],
    ]
);

use Sejator\WabaSdk\Services\EmbeddedSignupService;
use Sejator\WabaSdk\Support\Embedded\OAuthCallbackHandler;

$handler = app(
    OAuthCallbackHandler::class
);

$session = $handler->createSession([
    'context' => [
        'tenant_id' => tenant()->id,
        'user_id' => auth()->id(),
    ],
]);

$embedded = app(
    EmbeddedSignupService::class
);

$signup = $embedded->signupUrl(
    $session->state
);

$url = $signup['url'];

$result = $handler->handle(
    code: request('code'),
    state: request('state')
);

$accessToken = $result->accessToken;

use Sejator\WabaSdk\Exceptions\GraphApiException;

try {

    // SDK Call

} catch (GraphApiException $e) {

    logger()->error(
        $e->toArray()
    );

}

$client = app(Client::class)
    ->withToken(
        $device->access_token
    );

$messageService = new MessageService(
    $client
);
bash
php artisan vendor:publish   --provider="Sejator\WabaSdk\WabaServiceProvider" --tag=waba-config