vntrungld/zalo-bot-php-sdk 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

vntrungld/zalo-bot-php-sdk

Composer 安装命令:

composer require vntrungld/zalo-bot-php-sdk

包简介

PHP SDK for the Zalo Bot Platform API (https://bot.zapps.me/docs/)

README 文档

README

A typed PHP client for the Zalo Bot Platform API.

  • Full coverage of the documented endpoints (getMe, getUpdates, webhooks, and all send* methods).
  • Typed model objects for every response — full IDE autocompletion.
  • A webhook helper that verifies the secret token and parses payloads into typed updates.
  • Built on Guzzle; PHP 8.1+.

Installation

composer require vntrungld/zalo-bot-php-sdk

Quick start

use ZaloBot\Client;

$bot = new Client('123456789:your-bot-token');

$me = $bot->getMe();
echo $me->accountName; // "bot.VDKyGxQvc"

$sent = $bot->sendMessage('chat_id_here', 'Xin chào 👋');
echo $sent->messageId;

The bot token is issued when you create a bot — see Create a bot.

Sending messages

use ZaloBot\Enum\ParseMode;
use ZaloBot\Enum\ChatAction;
use ZaloBot\Model\TextStyle;

// Plain text
$bot->sendMessage($chatId, 'Hello!');

// Markdown / HTML
$bot->sendMessage($chatId, '**bold**', ParseMode::Markdown);

// Inline styles: bold + colour over the first 7 characters
$bot->sendMessage($chatId, 'Xin chào bạn', null, [
    new TextStyle(start: 0, len: 7, styles: ['b', 'c_db342e']),
]);

// Photo with a caption
$bot->sendPhoto($chatId, 'https://example.com/image.jpg', 'A caption');

// Sticker (IDs from https://stickers.zaloapp.com/)
$bot->sendSticker($chatId, 'sticker_id');

// Voice — 1-on-1 only, must be a public .aac URL
$bot->sendVoice($userId, 'https://example.com/audio.aac');

// Typing indicator
$bot->sendChatAction($chatId, ChatAction::Typing);

Receiving updates

Long polling (development)

foreach ($bot->getUpdates(timeout: 30) as $update) {
    if ($update->message->text !== null) {
        $bot->sendMessage($update->message->chat->id, 'You said: ' . $update->message->text);
    }
}

getUpdates does not work while a webhook is registered.

Webhooks (production)

Register your endpoint once:

$bot->setWebhook('https://your-app.com/webhook', 'your-8-to-256-char-secret');

Then handle incoming deliveries. WebhookHandler verifies the X-Bot-Api-Secret-Token header (constant-time) and parses the body:

use ZaloBot\Webhook\WebhookHandler;
use ZaloBot\Enum\EventName;
use ZaloBot\Exception\WebhookException;

$handler = new WebhookHandler('your-8-to-256-char-secret');

try {
    $update = $handler->handle(
        file_get_contents('php://input'),
        $_SERVER['HTTP_X_BOT_API_SECRET_TOKEN'] ?? null,
    );
} catch (WebhookException $e) {
    http_response_code(403);
    exit;
}

if ($update->event() === EventName::TextReceived) {
    // ... react to $update->message
}

// Acknowledge the delivery
header('Content-Type: application/json');
echo json_encode(['ok' => true]);

Other webhook helpers: parse($rawBody) (parse without verifying) and verifySignature($headerValue) (verify only).

Error handling

Exception Raised when
ZaloBot\Exception\ApiException The API returns {"ok": false}. Exposes getErrorCode() and the description via getMessage().
ZaloBot\Exception\NetworkException A transport-level failure (connection, TLS, timeout).
ZaloBot\Exception\WebhookException An incoming webhook fails verification or parsing.

All three extend ZaloBot\Exception\ZaloBotException.

use ZaloBot\Exception\ApiException;

try {
    $bot->sendMessage($chatId, 'hi');
} catch (ApiException $e) {
    // e.g. 401 invalid token, 429 quota exceeded
    error_log("Zalo API error {$e->getErrorCode()}: {$e->getMessage()}");
}

See the error code reference.

API reference

Method Returns
getMe() Model\BotInfo
getUpdates(int $timeout = 30) list<Model\Update>
setWebhook(string $url, string $secretToken) Model\WebhookInfo
deleteWebhook() Model\WebhookInfo
getWebhookInfo() Model\WebhookInfo
sendMessage(string $chatId, string $text, ?ParseMode $parseMode = null, ?array $textStyles = null) Model\SentMessage
sendPhoto(string $chatId, string $photo, ?string $caption = null) Model\SentMessage
sendSticker(string $chatId, string $sticker) Model\SentMessage
sendVoice(string $chatId, string $voiceUrl) Model\SentMessage
sendChatAction(string $chatId, ChatAction $action) bool

Configuration

You can inject a preconfigured Guzzle client (e.g. to set proxies, timeouts, or middleware) or override the base URL:

use GuzzleHttp\Client as GuzzleClient;

$bot = new Client(
    token: '123:abc',
    http: new GuzzleClient(['timeout' => 10]),
);

Examples

Runnable scripts live in examples/:

  • send_message.php — send text, a photo, and a typing indicator.
  • long_polling.php — a minimal long-polling echo bot.
  • webhook.php — a standalone HTTPS webhook endpoint.

Development

composer install
composer test      # PHPUnit
composer analyse   # PHPStan (max level)
composer cs        # PHP-CS-Fixer (dry-run); use `composer cs:fix` to apply
composer check     # all of the above

License

MIT

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-07-07

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固