CommandHandler
Trang này mô tả CommandHandler trong zalo-bot-js, handler dùng để bắt command theo cú pháp như /start.
CommandHandler thuộc phong cách handler-based và thường dùng cùng Application.
Cách hoạt động
Handler này:
- đọc
update.message?.text - tách từ đầu tiên theo khoảng trắng
- so sánh với
/${command} - nếu khớp, gọi callback với
CallbackContext
Ví dụ
ts
import "dotenv/config";
import { ApplicationBuilder, CommandHandler } from "zalo-bot-js";
const app = new ApplicationBuilder()
.token(process.env.ZALO_BOT_TOKEN!)
.build();
app.addHandler(new CommandHandler("start", async (update, ctx) => {
await update.message?.replyText(`Xin chào ${ctx.args[0] ?? "bạn"}!`);
}));
void app.runPolling();CallbackContext
Trong callback, ctx.args chứa các tham số sau command.
Ví dụ với text:
text
/start abcthì ctx.args sẽ là:
ts
["abc"]Khi nào nên dùng
- bạn muốn bắt command rõ ràng theo cú pháp
/name - bạn đang dùng
Applicationthay vìbot.onText()
Kế tiếp
- Xem MessageHandler để xử lý theo filter.
- Xem CallbackContext để hiểu dữ liệu callback nhận được.
Cập nhật lần cuối: 05/04/2026