PHP code example of hoangkhacphuc / zalobot-sdk

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

    

hoangkhacphuc / zalobot-sdk example snippets




aloBot\Sdk\ZaloBot;

// Default: Guzzle 7/8 as HTTP client, configured via environment variables.
$bot = ZaloBot::fromEnv([
    'ZALO_BOT_TOKEN' => $_ENV['ZALO_BOT_TOKEN'],
    'ZALO_BOT_SECRET' => $_ENV['ZALO_BOT_SECRET'] ?? null,
]);

$result = $bot->message->sendText('user_chat_id', 'Hello from PHP SDK!');

use ZaloBot\Sdk\ZaloBot;
use ZaloBot\Sdk\ZaloClient;

// Any PSR-18 ClientInterface implementation works.
$myClient = new MyCustomHttpClient();

$bot = new ZaloBot([
    'botToken'  => $_ENV['ZALO_BOT_TOKEN'],
    'secretKey' => $_ENV['ZALO_BOT_SECRET'] ?? null,
], $myClient);

// Or use ZaloClient directly for lower-level control:
$client = new ZaloClient($_ENV['ZALO_BOT_TOKEN'], httpClient: $myClient);
$result = $client->get('getMe');

// Text
$bot->message->sendText('chat_id', 'Hello!');

// Photo
$bot->message->sendPhoto('chat_id', 'https://example.com/photo.jpg', [
    'caption' => 'Nice photo!',
]);

// Sticker
$bot->message->sendSticker('chat_id', 'sticker-id-from-zalo');

// Voice (1:1 only)
$bot->message->sendVoice('user_id', 'https://example.com/voice.aac');

use ZaloBot\Sdk\Modules\WebhookModule;

$webhook = new WebhookModule(secretKey: $_ENV['ZALO_BOT_SECRET']);

// parseEvent() — backward-compatible plain array:
$raw    = json_decode(file_get_contents('php://input'), true);
$event  = $webhook->parseEvent($raw);

if ($event['event'] === 'user_text') {
    $bot->message->sendText($event['chatId'], 'You said: ' . $event['message']['text']);
}

// parseEventDto() — typed, immutable WebhookEvent value object:
$dto = $webhook->parseEventDto($raw);
if ($dto->isText()) {
    $bot->message->sendText($dto->chatId, 'DTO: ' . $dto->message['text']);
}
// $dto->event, $dto->userId, $dto->chatId, $dto->messageId, $dto->timestamp, $dto->raw
// Also implements ArrayAccess: $dto['event'] works and is immutable.

$user = $bot->user->getProfile('user_id');
echo $user['name'];

// In-memory LRU cache: 5-min TTL, 1000-entry cap, per PHP process.
$user = $bot->user->getProfileCached('user_id');

// Force refresh:
$bot->user->getProfileCached('user_id', forceRefresh: true);

// Clear cache:
$bot->user->clearCache('user_id');
$bot->user->clearCache(); // all users

$result = $bot->media->uploadImage('/path/to/photo.jpg');
$attachmentId = $result['attachment_id'];

$result = $bot->media->uploadFile('/path/to/document.pdf');

// Streams to a temp file, then atomically renames into place.
// Redirects are rejected — download URLs are validated against SSRF rules.
$bot->media->downloadMedia('attachment_id', '/tmp/save/image.png');

use ZaloBot\Sdk\Exceptions\RateLimitException;

try {
    $bot->message->sendText($chatId, $text);
} catch (RateLimitException $e) {
    $retryAfter = $e->getRetryAfter();
    sleep($retryAfter !== null ? (int) $retryAfter : 1);
    // retry
}

$client = new ZaloClient('token', retryMutations: true);