Geek De Mic

How to create a Bluesky bot in 5 minutes in Python

First you’ll need a Bluesky account (doh..).
Momentarily the registration is invitation based but you can register your email address and you`ll receive an invitation after a period.
In your account go to Settings -> App Passwords.

Click “Add App Password” -> give a name to your app, in my case “First Bot”

Click “Create App Password” and a password will be generated.

Copy this password and save it into a text file because we will use it in our script to authenticate as a Bluesky user. Remember that if you lose this password you can not recover it an you`ll have to generate another on for the same app.

Now go to your Python enviroment and install atproto package.
In my case is PyCharm.
You can also use “pip install atproto”.

After this create a new python file and start coding 🙂

We will be using “my-handle” as your username on Bluesky. Don`t forget to add bsky.social after your username. Eg: username.bsky.social
And “my-password” is the password you generated earlier.

Simple script to send a text message with the bot on Bluesky social network:

from atproto import Client
def main():
 client = Client()
 client.login('my-handle', 'my-password')
 client.send_post(text='First test!')
if __name__ == '__main__':
main()

Another simple script to send a message with an image attached on Bluesky:

from atproto import Client
def main():
client = Client()
client.login('my-handle', 'my-password')
# replace the path to your image file
with open('cat.jpg', 'rb') as f:
img_data = f.read()
client.send_image(text='Post with image from Python', image=img_data, image_alt='Text version of the image (ALT)')
if __name__ == '__main__':
main()

All the example script are on the Github page of the package: https://github.com/MarshalX/atproto/tree/main/examples

And from here only the (Blue) sky is the limit. 🙂
You can set a cron job to run your script on a schedule and post automatically anything you want on Bluesky.

I used this all this scripts to create “Turist În Țara Mea” bot that posts on Bluesky photos from my trips.
All the information is extracted from a database that I populated and update regularly from a php script.
Everything is running on a Raspberry Pi 3.

Leave a Comment

Your email address will not be published. Required fields are marked *