Zalo Bot SDK for Laravel - A comprehensive SDK for building Zalo Bots
nhanchaukp/zalo-bot-sdk is a Laravel package for zalo bot sdk for laravel - a comprehensive sdk for building zalo bots.
It currently has 11 GitHub stars and 328 downloads on Packagist (latest version v1.0.2).
Install it with composer require nhanchaukp/zalo-bot-sdk.
Discover more Laravel packages by nhanchaukp
or browse all Laravel packages to compare alternatives.
Last updated
Một SDK mạnh mẽ và dễ sử dụng để xây dựng Zalo Bot trên Laravel. Package này được thiết kế với cấu trúc tương tự như Telegram Bot SDK, giúp việc phát triển và bảo trì trở nên đơn giản.
Bạn có thể cài đặt package qua Composer:
composer require nhanchaukp/zalo-bot-sdk
Publish file config để tùy chỉnh:
php artisan vendor:publish --tag="zalo-bot-config"
Hoặc:
php artisan vendor:publish --provider="NhanChauKP\ZaloBotSdk\ZaloBotServiceProvider" --tag="config"
Thêm các biến sau vào file .env:
ZALO_BOT_TOKEN=your-zalo-bot-token
ZALO_WEBHOOK_URL=https://yourapp.com/webhook/zalo
ZALO_BASE_BOT_URL=https://bot-api.zapps.me/bot
ZALO_ASYNC_REQUESTS=false
Trong file config/zalo-bot.php:
'bots' => [
'default' => [
'token' => env('ZALO_BOT_TOKEN'),
'webhook_url' => env('ZALO_WEBHOOK_URL'),
'commands' => [
// App\ZaloBots\Commands\StartCommand::class
],
],
'support_bot' => [
'token' => env('ZALO_SUPPORT_BOT_TOKEN'),
'webhook_url' => env('ZALO_SUPPORT_WEBHOOK_URL'),
'commands' => [
// App\ZaloBots\Commands\SupportCommand::class
],
],
],
'default' => 'default', // Bot mặc định
use NhanChauKP\ZaloBotSdk\Facades\ZaloBot;
// Sử dụng bot mặc định
ZaloBot::sendMessage('chat_id', 'Xin chào!');
// Sử dụng bot cụ thể
ZaloBot::bot('support_bot')->sendMessage('chat_id', 'Chúng tôi có thể giúp gì?');
ZaloBot::sendPhoto('chat_id', 'https://example.com/image.jpg', 'Caption cho hình ảnh');
// Lấy sticker từ https://stickers.zaloapp.com/
ZaloBot::sendSticker('chat_id', 'sticker_id_from_stickers_zaloapp_com');
Tham khảo tài liệu: https://bot.zapps.me/docs/apis/sendChatAction/
use NhanChauKP\ZaloBotSdk\Enums\ChatAction;
// Đang soạn tin nhắn
ZaloBot::sendChatAction('chat_id', ChatAction::Typing);
// Đang tải ảnh (sắp ra mắt)
ZaloBot::sendChatAction('chat_id', ChatAction::UploadPhoto);
// Set webhook
ZaloBot::setWebhook('https://yourapp.com/webhook/zalo');
// Xóa webhook
ZaloBot::deleteWebhook();
// Lấy thông tin webhook
$webhookInfo = ZaloBot::getWebhookInfo();
<?php
namespace App\ZaloBots\Commands;
use NhanChauKP\ZaloBotSdk\Commands\Command;
use NhanChauKP\ZaloBotSdk\ZaloBot;
class StartCommand extends Command
{
protected string $name = 'start';
protected string $description = 'Bắt đầu cuộc trò chuyện';
protected array $aliases = ['begin'];
public function handle(array $update, ZaloBot $bot): mixed
{
return $this->reply($update, $bot, 'Xin chào! Chào mừng bạn đến với bot của chúng tôi.');
}
}
Trong config file:
'commands' => [
App\ZaloBots\Commands\StartCommand::class,
App\ZaloBots\Commands\HelpCommand::class,
],
Hoặc đăng ký động:
ZaloBot::addCommand(App\ZaloBots\Commands\StartCommand::class);
ZaloBot::addCommands([
App\ZaloBots\Commands\StartCommand::class,
App\ZaloBots\Commands\HelpCommand::class,
]);
'command_groups' => [
'admin' => [
App\ZaloBots\Commands\AdminStartCommand::class,
App\ZaloBots\Commands\AdminStatsCommand::class,
],
'user' => [
App\ZaloBots\Commands\UserProfileCommand::class,
App\ZaloBots\Commands\UserSettingsCommand::class,
],
],
// Sử dụng groups trong bot
'bots' => [
'admin_bot' => [
'token' => env('ZALO_ADMIN_BOT_TOKEN'),
'commands' => [
'admin', // Sử dụng command group
],
],
],
'shared_commands' => [
'help' => App\ZaloBots\Commands\HelpCommand::class,
'start' => App\ZaloBots\Commands\StartCommand::class,
],
// Sử dụng trong bot
'bots' => [
'default' => [
'commands' => [
'help', // Shared command
'start', // Shared command
],
],
],
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use NhanChauKP\ZaloBotSdk\Facades\ZaloBot;
class ZaloWebhookController extends Controller
{
public function handle(Request $request)
{
$update = $request->all();
try {
$result = ZaloBot::processUpdate($update);
return response()->json(['status' => 'ok']);
} catch (\Exception $e) {
\Log::error('Zalo webhook error: ' . $e->getMessage());
return response()->json(['status' => 'error'], 500);
}
}
}
// routes/web.php hoặc routes/api.php
Route::post('/webhook/zalo', [ZaloWebhookController::class, 'handle']);
class WeatherCommand extends Command
{
protected string $name = 'weather';
protected string $description = 'Xem thời tiết của thành phố';
protected array $arguments = [
'city' => 'Tên thành phố (bắt buộc)',
];
public function handle(array $update, ZaloBot $bot): mixed
{
$text = $this->getMessageText($update);
$args = $this->getCommandArguments($text);
if (empty($args)) {
return $this->reply($update, $bot, 'Vui lòng nhập tên thành phố. Ví dụ: /weather Hà Nội');
}
$city = implode(' ', $args);
// Logic lấy thời tiết
$weather = $this->getWeather($city);
return $this->reply($update, $bot, "Thời tiết tại {$city}: {$weather}");
}
private function getWeather(string $city): string
{
// Implementation logic
return "Nhiều mây, 25°C";
}
}
// Trong config
'http_client_handler' => new CustomHttpClient(),
// Hoặc bind trong Service Provider
$this->app->bind('zalo-bot.http_client', function() {
return new CustomHttpClient();
});
'commands_paths' => [
base_path('app/ZaloBots/Commands'),
base_path('app/CustomCommands'),
],
composer test
Vui lòng xem CHANGELOG để biết thêm thông tin về những thay đổi gần đây.
Vui lòng xem CONTRIBUTING để biết chi tiết.
Nếu bạn phát hiện lỗ hổng bảo mật, vui lòng gửi email đến [email protected].
MIT License. Vui lòng xem License File để biết thêm thông tin.