Multiple Telegram Users
I am committing this early draft so that I have something to work with when I demonstrate how to add prose editing tools using Friday. I would not normally commit un-edited work; but adding this chapter at the same time as the prose editing functionality would get messy.
Friday was a single-player game. One Claude session, one conversation, one user at a time. If Liz sent a message while I was mid-turn, it queued behind my work and the response went to whoever's chat the bridge happened to be polling. Making Friday a shared assistant meant taking turns, and nobody wanted to take turns.
One Session instance, created at startup. One TelegramBridge instance wrapping it. All Telegram users shared the same conversation, the same prompt queue, the same message buffer, the same polling cursor, and the same response message tracker. The allowedChatIds config determined who could talk to Friday, but it didn't give them separate conversations.
The consequences are immediate. I send a coding question. While Claude is streaming the response, Liz sends a recipe question. Her message enters the same queue. Claude finishes the coding response and picks up the recipe question – but the context is still the coding conversation. The response mentions variables from three messages ago. /clear resets everyone's session. /project switches everyone's working directory. /cost shows everyone's combined token usage.
Background tasks had already solved this problem in miniature. Each task creates its own Session instance with a scoped state key (background::${taskId}), giving it isolated conversation context, session ID persistence, and independent cwd. The pattern existed. It just hadn't been applied at the user level.
Shared Bot, Per-User Bridge
Telegram's Bot API enforces one long-polling connection per token. You can't open two concurrent connections from the same bot – the API returns a 409 Conflict and drops one of them. So the grammY bot stays shared: it receives all messages from all users on a single connection.
The bridge is where per-user isolation lives. We give each user their own TelegramBridge instance with its own session, its own polling cursor, and its own response message tracking. A new BridgeRouter sits between the shared bot and the per-user bridges. When a message arrives, the router looks up the sender's chat ID and delegates to the right bridge.
The SessionRegistry handles the session side. We give each user a Session scoped to user::${userName} – the same key-prefix pattern that background tasks use with background::${taskId}:
for (const user of users) {
const scope = `user::${user.name}`;
const startupCwd = Session.resolveStartupCwd(
state,
fallbackCwd,
scope
);
const session = new Session(state, {
cwd: scope,
executionCwd: startupCwd,
userName: user.name,
model: defaults.model,
effort: defaults.effort,
permissionMode: defaults.permissionMode,
});
this.#sessions.set(user.name, session);
}
This is from
src/acp/session-registry.ts
Each user gets independent acp_session_id, acp_compact_boundary_id, and active_cwd entries in the state store. We don't need new tables – the existing key-value store handles it through the scoped key prefix.
The BridgeRouter creates the per-user bridges and holds a Map<number, TelegramBridge> keyed by chat ID:
for (const user of users) {
const session = registry.get(user.name);
const bridge = new TelegramBridge(
bot,
session,
aggregator,
token,
transcriber,
state,
registry,
runner
);
this.#bridges.set(user.id, bridge);
}
This is from
src/telegram/bridge-router.ts
Routing is a one-liner: look up the bridge by chat ID and delegate. If the chat ID isn't in the map, the message is silently dropped. The old allowedChatIds guard is gone – map membership does the same job without the explicit check.
The Handler Refactor
The most structurally significant change wasn't creating multiple sessions – it was untangling how bot handlers were registered.
The original TelegramBridge registered global bot handlers in its constructor: this.#bot.command('clear', ...), this.#bot.on('message:text', ...). Each handler captured this, meaning the single bridge instance. That worked fine when there was only one bridge. With multiple bridges, the bot's global handlers can't know which bridge to delegate to – they all captured this at construction time, pointing to whichever bridge was created last.
The fix was to move handler registration out of the bridge and into the router. The router registers handlers on the bot once, during its own construction. Each handler resolves the correct bridge by chat ID at the moment a message arrives, then calls into the bridge:
for (const [command, handler] of Object.entries(COMMANDS)) {
bot.command(command, async (ctx) => {
const bridge = route(ctx.chat.id);
if (bridge) {
await handler(bridge.cmd)(ctx);
}
});
}
bot.on('message:text', async (ctx) => {
const bridge = route(ctx.chat.id);
if (bridge) {
await bridge.handleText(ctx);
}
});
This is from
src/telegram/bridge-router.ts
The bridge no longer touches bot.command() or bot.on() at all. Instead, it exposes a cmd property (a CommandDependencies object) and handler methods like handleText, handlePhoto, and handleVoice. The router calls those. The bridge handles them.
Without this refactor, every bridge would fight over the same global bot handlers. It's what made everything else possible.
User-Scoped Data
Sessions weren't the only things that needed a user. Scheduled tasks, background tasks, and notes all needed to know who they belonged to – so that queries could filter by owner, and so that notifications could route to the right person.
Each table gets a user_name TEXT NOT NULL DEFAULT 'chris' column. SQLite's ALTER TABLE ADD COLUMN with a default value backfills existing rows automatically, so the migration is a no-op for the data that already exists. Every row that predates the column becomes Chris's.
New rows carry their user forward through the call chain. A background task created from Liz's session passes userName: 'liz' when it calls the store:
const result = db
.prepare(
'INSERT INTO background_tasks (prompt, project_context, created_at, post_to, hud_entry_type, model, effort, user_name) VALUES (?, ?, ?, ?, ?, ?, ?, ?)'
)
.run(
prompt,
projectContext,
now,
postTo,
opts.hudEntryType ?? null,
opts.model ?? null,
opts.effort ?? null,
opts.userName ?? 'chris'
);
This is from
src/data/stores/background-tasks/create.ts
Scheduled task queries filter by user_name, so each user can only see their own. Notes track their creator but stay visible to everyone – no privacy enforcement needed, since all sessions share the same database.
The heartbeat needed rethinking too. It previously took a Session instance to send recovery notifications when interrupted scheduled tasks were re-queued. With per-user sessions, there's no obvious session to use. So we remove the Session dependency entirely. Recovery notifications go through the messenger instead, using the user_name on each interrupted task to route to the right channel:
for (const [user, descriptions] of byUser) {
this.#messenger.sendSync(`telegram:${user}`, {
text: `⚠️ The following scheduled tasks were interrupted before completing and have been re-queued:\n${descriptions.join('\n')}`,
});
}
This is from
src/scheduler/heartbeat.ts
The heartbeat gets simpler by gaining multi-user support. It turns out the Session dependency was the wrong abstraction all along.
Surprises
Command isolation came for free. The implementation plan included a phase specifically for verifying command isolation – checking that /clear only resets the issuing user's session, that /project only changes the issuing user's cwd, and so on. That phase turned out to be verification-only with no code changes. Because the router dispatches by chat ID to the correct bridge, and each bridge holds a reference to its own #session, every command that touches this.#session is automatically per-user. The architecture handled it by construction.
The /approve and /deny split required explicit thought. Each user can only see and resolve pending permission requests on their own session – Liz can't approve my tool-use confirmations, and vice versa. But privileged command approvals (getLatestPendingToken()) are system-level infrastructure – any user can approve them, because they're not session-scoped. The code distinguishes these two cases: session permission gates are per-bridge, system privilege gates are global. We had to be deliberate about what "per-user" means in each context.
/fresh and /reboot stay global. Both call process.exit(0). Systemd restarts the process, which recreates all sessions. There's no way to restart "just one user's" Claude SDK session without a process-level reset. /clear is the per-user equivalent – it resets the session without a process restart. This distinction is intentional, but it's worth being explicit about: if Liz sends /fresh, everyone's session restarts.
Liz's chat ID shipped as a placeholder. The feature was architecturally complete – config, sessions, bridges, routing, scoped data – with one active user. Adding the second user required exactly one number change in config. That's the right shape for a multi-user architecture: the hard part is the plumbing, not the count.
Conclusion
I work on a coding problem. Liz asks about dinner recipes. Both conversations happen simultaneously, in their own Claude sessions, with their own context, cwd, model, and cost tracking. Neither interferes with the other.
Commands are per-user by construction. My /clear doesn't touch Liz's session. Her /project doesn't change my working directory. Each user's /cost shows their own token usage. The only global commands are /fresh and /reboot, which restart the entire process – and that's intentional.
The HTTP API and scripting interface require explicit user targeting. Every session-related endpoint now requires a ?user=<name> parameter. No default. Missing or unknown user returns 400. No more ambiguity about which session receives a programmatic message.
The surprising part was how much of the work was already done. Background tasks had proven the per-session pattern. The messenger had proven the per-channel pattern. The user_name column additions were the least interesting code and the most important data change – every query that previously returned "all tasks" now returns "this user's tasks", and every notification that previously went to "everyone" now goes to "the owner."
The handler refactor was the only genuinely new architectural problem. Extracting bot handlers from a constructor that assumed it was the only instance turned out to be straightforward once the shape was clear. But it's the piece that unlocked everything else. Without it, multiple bridges would've been impossible. With it, adding a third user is a config change.