run your own bot on bluesky
An account that wakes up when you tag it in a thread. No framework, one file, about 60 lines.
A bot on Bluesky is an ordinary account driven by a script. "On call" means the script is
watching the account's notifications and answers when someone @-mentions it. Your own
nathanbot is exactly this, plus whatever you do with the text (call an LLM, look something up, run a check).
1. Make the account
Sign up for a second Bluesky account (a custom-domain handle like nathanbot.yourdomain.com works too).
Then declare it: Settings → Account → Automation label (the thread's advice; the label tells people a bot runs it).
Check the current wording in the app, as the menu moves around.
2. Make an app password
Settings → Privacy and security → App passwords. Create one for the bot. It's a revocable stand-in for the real password, so the script never needs your real one. Keep it in an environment variable, never in a repo.
export BOT_HANDLE=nathanbot.bsky.social
export BOT_PASSWORD=xxxx-xxxx-xxxx-xxxx
3. The whole bot
Save as bot.mjs; run with node bot.mjs (Node 18+). It logs in, polls notifications every 30 seconds,
and replies to mentions. Swap the answer() function for whatever you want the bot to do.
const PDS = "https://bsky.social";
const { BOT_HANDLE, BOT_PASSWORD } = process.env;
async function api(path, { method = "GET", token, body, params } = {}) {
const url = new URL(`${PDS}/xrpc/${path}`);
for (const [k, v] of Object.entries(params || {})) url.searchParams.set(k, v);
const r = await fetch(url, {
method,
headers: {
...(token && { authorization: `Bearer ${token}` }),
...(body && { "content-type": "application/json" }),
},
body: body && JSON.stringify(body),
});
if (!r.ok) throw new Error(`${path} ${r.status} ${await r.text()}`);
return r.json();
}
const session = await api("com.atproto.server.createSession", {
method: "POST",
body: { identifier: BOT_HANDLE, password: BOT_PASSWORD },
});
const token = session.accessJwt;
// Replace me: take the mention's text, return the reply.
async function answer(text) {
return `You said: ${text.slice(0, 200)}`;
}
let seen = new Date().toISOString(); // only react to mentions from now on
while (true) {
const { notifications } = await api("app.bsky.notification.listNotifications", {
token, params: { limit: 50 },
});
const fresh = notifications
.filter((n) => (n.reason === "mention" || n.reason === "reply") && n.indexedAt > seen)
.reverse();
for (const n of fresh) {
seen = n.indexedAt;
// A reply is a reply to the bot only if you want it to be; mentions are the "call" signal.
if (n.reason !== "mention") continue;
const parent = { uri: n.uri, cid: n.cid };
const root = n.record.reply?.root ?? parent;
await api("com.atproto.repo.createRecord", {
method: "POST", token,
body: {
repo: session.did,
collection: "app.bsky.feed.post",
record: {
$type: "app.bsky.feed.post",
text: (await answer(n.record.text)).slice(0, 290),
createdAt: new Date().toISOString(),
reply: { root, parent },
},
},
});
}
await new Promise((r) => setTimeout(r, 30_000));
}
The reply field with root and parent is what makes your post land in the thread rather than float free.
The bot's session token is short-lived; for something that runs for days, call com.atproto.server.refreshSession
or just log in again when a request fails with 400 ExpiredToken.
4. Make it good manners
- Only answer when called. Don't auto-reply to strangers, and don't reply to every post that merely contains a keyword. Unprompted replies get accounts reported.
- Remember what you've answered (a file of URIs is fine), so a restart doesn't answer twice.
- Don't reply to bots that reply to you. Two bots can chat forever.
- Gate it. Decide who may call it (only you, only your follows) before it does anything costly.
- Posts are 300 characters. Split longer answers across a reply chain.
5. "Call when you see innumeracy"
That's a different shape: instead of waiting to be tagged, the bot watches something. Two honest routes:
poll app.bsky.feed.searchPosts for your terms, or subscribe to the
Jetstream firehose and filter locally.
Then don't reply to the author. Quote or like it from a bot account people opted into following, or add a
label, or post to a feed. Replying uninvited to people's posts is the thing that gets bots blocked.
Where to go next
- Bluesky developer docs, with the full API reference.
- How @buildthis works: a bot that builds sites from mentions. Same shape, plus a mutual-follow gate and a queue.
- buildthis.bisks.net/start