Building a Bot For Bluesky Social
After Elon Musk took over Twitter and changed the entire API infrastructure of the company, many solo developers have slowly began to leave the platform. Then Bluesky Social, a decentralized platform with Jack Dorsey himself on the board, started to make noise in the market as a better alternative to Twitter and all.
At the time of writing, Bluesky Social is an invite-only platform. Basically, you either join the waitlist to be invited into the platform or you can get an invite code from users who already got it. Fortunately, a very good friend of mine gave me an invite code and I got in. My username is divyaswor.bluesky.social.
Bluesky Social is based on the AT Protocol. This protocol was created by Bluesky themselves to power the next generation of social applications. (Yes, I took it from their homepage). After some digging around in their documentations, I decided to create a Bluesky Social bot that would constantly post random jokes. I will not reveal my source for the jokes but let us assume I have successfully json files for my jokes.
Let me break down the steps how we will create our bot.
1. Create a Bluesky Social Account
Yes for this, you either had to be a really early joiner or have good enough friends in your circle who would invite you to their platform.
2. Get App Password
This step is really easy.
First you’ll need to go your settings. You can slide the menu from the left side of the screen.
Then you go to Settings. You should see this screen.
Then you proceed to App Passwords.
When you Add App Password, you just give a name for your bot.
You will now get a password you will need for your bot to work.
3. Start coding
Let the post json be this.
{
"body": "I am not a bot. Please trust me. This is a joke."
}
We only need a single file to post any post.
import blue from "@atproto/api";
import fs from "node:fs";
const { BskyAgent } = blue;
const BLUESKY_BOT_USERNAME = "<username>";
const BLUESKY_BOT_PASSWORD = "<bot-password>";
const fileName = "./post.json";
const generateFunnyCatQuote = async () => {
const file = fs.readFileSync(fileName);
const fileContent = JSON.parse(file);
console.log(fileContent);
//now that we have the post, lets post it to bluesky
const { RichText } = blue;
const agent = new BskyAgent({ service: "https://bsky.social/" });
await agent.login({
identifier: BLUESKY_BOT_USERNAME,
password: BLUESKY_BOT_PASSWORD,
});
const rt = new RichText({ text: fileContent.body });
const postRecord = {
$type: "app.bsky.feed.post",
text: rt.text,
facets: rt.facets,
createdAt: new Date().toISOString(),
};
await agent.post(postRecord);
};
generateFunnyCatQuote();
You can find the link to the repo here.
https://github.com/divyaswormakai/bluesky-social-bot
Thank you for taking the time to read my article. I hope it was enjoyable for you. If you liked it, be sure to follow me for more great content!