How to build a Bluesky bot using ATProto and OpenAI API

Amir Shevat
2 min readApr 12, 2023

--

In the last two days have been building CatGTP, a bluesky bot using the openAI API to generate funny cat posts and submitting them to the amazing new social app, using the ATProto API.

Wanted to share the code, so that you can do it too. I have not been a professional software engineer for the last five years, so please don't judge me:

import * as dotenv from 'dotenv'
import blue from '@atproto/api';
import ai from 'openai'

dotenv.config()
const { BskyAgent } = blue;
const { Configuration, OpenAIApi } = ai;
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});

const openai = new OpenAIApi(configuration);
const generateFunnyCatQuote = async () => {
// get the post from the openAI API
const completion = await openai.createChatCompletion({
model: "gpt-3.5-turbo",
messages: [{role: "user", content: "Generate a funny cat quote Tweet, omit the hashtags."}],
});
// terrible way to deal with the fact that ChatGPT sometimes puts the posts in ""
const postText = completion.data.choices[0].message.content.slice(1, completion.data.choices[0].message.content.lastIndexOf('"') )
console.log("Post:"+postText);

//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: process.env.BLUESKY_BOT_EMAIL, password: process.env.BLUESKY_BOT_PASSWORD})
const rt = new RichText({text: postText })
const postRecord = {
$type: 'app.bsky.feed.post',
text: rt.text,
facets: rt.facets,
createdAt: new Date().toISOString()
}
await agent.post(postRecord)
};

generateFunnyCatQuote();

That is it, assuming you know how to deal with .env files, npm, and have API access to openAI you are good to go.

Here is how it looks:

Share the love. and do not forget to follow CatGPT here.

--

--

Amir Shevat

Investor in early stage startups. Previously: Head of Product, Twitter Dev Platform, VP product at Twitch, Slack, Google, Microsoft. Author at O'Reilly.