Lifecycle Hooks
Hooks let botmux run your own command at key moments: a new topic arrives, a session starts or exits, something needs a human, a turn finishes… Use them to send notifications, keep audit logs, auto-update skills, or run a custom check before a message reaches the CLI.
There are two kinds of hooks:
5-minute start: log events to a local file
-
Write a minimal hook script that appends the incoming JSON to a log:
-
Write the config file
~/.botmux/data/hooks.json(prefer an absolute path forcommand: the hook gets the daemon'sPATH, which may differ from your terminal's): -
Send a message in Feishu, then watch the log:
Editing hooks.json does not require a daemon restart: botmux checks the file whenever an event fires and reloads it when it changes.
The source repo's
examples/hooks/has ready-made examples:echo-to-log.sh(log),osascript-notify.sh(macOS Notification Center),http-webhook.sh(forward to an HTTP endpoint),prompt-gate.sh(sync gate). The npm package does not include this directory; copy them from the GitHub repo if you need them.
Where the config lives
From highest to lowest precedence; only the first one found is used:
- Env var
BOTMUX_HOOKS_JSON: a JSON array inline - Env var
BOTMUX_HOOKS_FILE: path to a JSON file - Default file
~/.botmux/data/hooks.json(orhooks.jsonunderSESSION_DATA_DIRif that is set)
The content is an array; each item is one hook. You can attach several hooks to the same event. If the JSON fails to parse, none of it takes effect and the log shows [hooks] Failed to load hook config.
Config fields
All conditions in filter must match together; an array inside one condition means "any of these". If the event lacks the field, that condition does not match.
Events
Fields on every event
- The payload is written as JSON to the command's stdin, and the env var
BOTMUX_HOOK_EVENT=<event name>is set. - Every payload has
eventandemittedAt(ISO time). - Most events also carry
larkAppIdandchatId; session events (session.*) also carrysessionId,chatType,scope,anchor,title,cliId,workingDir,hasHistory,spawnedAt,lastMessageAt.
Long text is truncated
content, message, description, finalOutput, and lastScreenContent are truncated past 600 characters. These fields always come with xxxLength (original length) and xxxTruncated (whether it was cut). For full text, list the event in this hook's redact.fullContentEvents. Sync gates are the exception and always get full text (see below).
Event details
session.exit: reason is graceful (exit code 0), exit_code_<N>, or dashboard_close (the session was closed on purpose, e.g. from the Dashboard; there is no code field). Not emitted when the daemon itself shuts down.
session.idle: fires at most once per session per state within 10 seconds, so flapping doesn't flood you.
schedule.fired: status matches the task's "last status":
A skip doesn't use up a repeat and doesn't delete the task. A skipped one-shot task stays enabled and is rechecked after at least 30 seconds; recurring tasks continue on their normal schedule. If your script only handles ok / error, add a skipped branch and don't treat it as success.
turn.finished:
statusis the simplified result,successorerror;terminalStatusis the raw result,completed,failed,cancelled, orambiguous(use it to tell cancel from failure).senderOpenIdis who started this turn; if unknown, the field is omitted rather than filled with the session owner.- Each turn (per dispatch attempt) fires once per daemon process; after a daemon restart it may fire again, so dedupe on
sessionId+turnId+dispatchAttempt. A retried turn has a differentdispatchAttempt, and each attempt fires. - If the session process exits without finishing a turn, no
turn.finishedis sent for it (the exit goes throughsession.exit). - It only says "this turn ended" and carries no duration or usage. For those, use the
turn.completedoutbound event in the Bot'sfeedbackWebhooksconfig. - The same notification also appears on the daemon's local
GET /api/eventsstream.
Meeting (VC) receiver sessions never trigger session.* or turn.finished hooks, because they process external meeting content.
reason values for session.requires_attention
New reason values may be added later; give unknown values a default branch in your script.
Outbound events can't block anything
outbound.send / outbound.reply fire after Feishu has accepted the message, so it's already in the chat. They don't support sync and are notifications only.
Sync gate: run your check before a message reaches the CLI
Add a "mode": "sync" hook on prompt.submit and every message headed for the CLI asks it first: allow or deny.
examples/hooks/prompt-gate.sh in the source repo is a good starting point.
How to answer "allow / deny"
Either way works; JSON on stdout wins:
stdout must be entirely one JSON object to count. A few log lines on stdout fall back to the exit code.
When denied, the user gets "🚫 This message was blocked by a pre-submit check and was not sent to the CLI" in the topic, followed by the reason.
When it runs
The gate runs in the daemon, before the CLI has seen this message (for a new topic the CLI hasn't even started). A denied message never reaches the CLI at all.
Note: there is no "text already typed in the CLI input box, before Enter" moment. botmux types the text and presses Enter in one step, with nowhere to insert a check.
Rules at a glance
Notify and gate on the same event
prompt.submit can also be a plain notification (no mode). Both can coexist:
- Notifications are sent before the verdict, so they see denied messages too, which is exactly what an audit of "whose message got blocked" needs.
- Notification hooks get text truncated to 600 chars as usual; only the gate gets full text.
- A notification hook's exit code and output never affect the verdict.
Try it
Send the Bot a message in Feishu. You should get the "blocked by a pre-submit check" reply, and /tmp/gate-payload.json will hold the payload. Restore hooks.json when you're done.
Join gate: decide whether to auto-start after being added to a group
If the Bot has join auto-start on (autoStartOnGroupJoin, see bots.json), you can add a sync hook on chat.bot_added to decide per group. The hook sees the group name chatName, description chatDescription, group type chatMode, and who added the Bot, operatorOpenId.
Key points:
- It decides only join auto-start. Async
chat.bot_addednotifications andgroupJoinCommand(run a command on join) keep running regardless. - Scope it with
filter.larkAppId/filter.chatId. Don't usefilter.senderOpenId: join events have no sender, so it never matches. To decide by inviter, readoperatorOpenIdin your script. - For "do nothing at all unless it's a target group": set
autoStartOnGroupJoinGateRequired: trueon the Bot and give the gate"onError": "deny". Then a missing, unmatched, timed-out, or crashed gate means no auto-start, instead of auto-starting in every new group. - With
autoStartOnGroupJoinGateRequired: true, the first-turn prompt also includes a group-join block and the inviter; without it, the first-turn prompt is exactly as before. - This join info only shows "the Bot was added to this group". It does not prove the first-turn caller is an authenticated human, so the first turn doesn't get human-only privileges (such as auto-continuation or creating scheduled tasks directly). If the inviter is an app or bot, it isn't recorded as the session owner either.
- Manual force-start entry points skip this gate.
To give one Bot its own join script (runs directly, no model), groupJoinCommand in bots.json is simpler; edit it in the Dashboard under Bot Config → Proactive Start. Its timeout is 10 minutes.
Execution environment
Before writing your own hook, know how it runs:
- Any executable works: bash, Python, Node, a Go binary, an internal CLI, an HTTP forwarder.
- No shell:
commandis just split on spaces and quotes into program + arguments. Pipes|, redirects>,&&, and~expansion don't work. Use a script, orbash -c '…'. - Minimal env vars: only basics such as
PATH,HOME,TMPDIR,SHELL,USER,LANG, plusBOTMUX_HOOK_EVENT. App secrets and API keys are not passed.exportanything else you need inside your script. - Payload on stdin: one JSON object. It's fine not to read it.
- Timeouts kill the whole process group, including child processes your script started.
- Nobody reads notification output: non-zero exit, timeout, or command not found only go to the botmux log and never affect messaging, scheduled tasks, or sessions.
Recipe: auto-update Skills when a session starts
botmux has built-in skill management (botmux skills install … to install, botmux skills update <name> to update, botmux skills list to list installed ones). With session.start you can check for updates every time a new session starts, similar to Claude Code's SessionStart hook.
Update one skill:
Update all installed skills: botmux skills update takes a single name (no *), so loop in a script:
Call agentbuddy directly to update user-global skills: hooks run without a shell and with a trimmed environment, so use a wrapper script:
Notes:
- Raise
timeoutMs: the 5 s default is rarely enough for a network update; use 60 s or more. - Doesn't block the session: updates run in the background and the current session doesn't wait; results take effect in the next new session.
- Narrow the scope: use
filter.chatId/filter.larkAppIdto run only for certain chats or Bots. - Prefer
botmux skills update: it updates the botmux-managed skill version, stays in step with botmux's skill lifecycle, and for skills installed from agentbuddy it strips the embedded usage telemetry.
