Bots
Bots are accounts on the network that post automatically. Popular ones include bots that post the magnitude of recent earthquakes, photos from an archive on a regular schedule, etc.
You can skip the below tutorial and find the full starter script here.
Starter Template
Here's a TypeScript script that creates a bot that posts a smiley emoji on an automated schedule once every three hours.
Getting Started
Install TypeScript and Node.
npm i -g typescript
npm i -g ts-node
Then, save your bot's username and password to a .env file.
BLUESKY_USERNAME=
BLUESKY_PASSWORD=
Create a Script
The below script will post 🙂 every three hours.
import { BskyAgent } from '@atproto/api';
import * as dotenv from 'dotenv';
import { CronJob } from 'cron';
import * as process from 'process';
dotenv.config();
// Create a Bluesky Agent
const agent = new BskyAgent({
service: 'https://bsky.social',
})
async function main() {
await agent.login({ identifier: process.env.BLUESKY_USERNAME!, password: process.env.BLUESKY_PASSWORD!})
await agent.post({
text: "🙂"
});
console.log("Just posted!");
}
main();
Deploying Your Bot
You can deploy a simple bot for no or low cost on a variety of platforms, such as Heroku or Fly.io.
Rate Limits & Respecting Other Users
Keep in mind that bots should respect the network's rate limits. Automated bots that post to an account on an regular interval are welcome. If your bot interacts with other users, please only interact (like, repost, reply, etc.) if the user has tagged the bot account. It must be an opt-in interaction, or else your bot may be taken for spam.
Session Management
Rate limits for logging in are much lower than for write operations like posting. Calling agent.login() multiple times in a short period may trigger rate limits. You only need to call agent.login() once per session to authenticate your agent, and can call agent.post() multiple times after that.
You may want to persist your session to disk after logging in to avoid hitting login rate limits. You could do that by using helper functions like readSessionFromDisk and writeSessionToDisk with agent.resumeSession():
const session = readSessionFromDisk()
if (session) {
agent.resumeSession(session)
} else {
await agent.login({ identifier: process.env.BLUESKY_USERNAME!, password: process.env.BLUESKY_PASSWORD!})
writeSessionToDisk(agent.session)
}
await agent.post({
text: "🙂"
});
For more information about session management, see API Hosts and Auth.