CommandHandler
This page describes CommandHandler in zalo-bot-js, a handler used to capture commands with syntax like /start.
CommandHandler is of the handler-based style and is often used with Application.
How it works
This Handler:
- read
update.message?.text - separate the first word by space
- compare with
/${command} - if matched, call callback with
CallbackContext
For example
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
In the callback, ctx.args contains the parameters after command.
Example with text:
text
/start abcthen ctx.args would be:
ts
["abc"]When should you use it?
- you want to catch the command explicitly with the syntax
/name - you are using
Applicationinstead ofbot.onText()
Next
- See MessageHandler for processing by filter.
- See CallbackContext to understand received callback data.
Last updated: April 5, 2026