Logo

dev-resources.site

for different kinds of informations.

So, you want to publish in Bluesky with a python program?

Published at
11/2/2023
Categories
python
bluesky
atprotocol
posse
Author
fernand0
Categories
4 categories in total
python
open
bluesky
open
atprotocol
open
posse
open
Author
8 person written this
fernand0
open
So, you want to publish in Bluesky with a python program?

Bluesky is a microblogging social platform that has been available for aproximately a year. Currently, access is by invitation only, but invitations are becoming more readily available. If you don't have one yet, you should receive one soon.

In my quest to experiment with the platform, I aimed to publish posts programmatically. I stumbled upon a helpful resource, Posting via the Bluesky API, which guided me in getting started.

Bluesky utilizes the AT protocol so I conducted a quick search to find a Python implementation of it, available at atproto. You can install it on your system using the usual method:

pip install -U atproto
Enter fullscreen mode Exit fullscreen mode

Once installed, you can start posting using the appropriate code. For instance, you can post a text message with a line like this:

You can post a text message with some line such as:

client.send_post(text='Hello World!')
Enter fullscreen mode Exit fullscreen mode

provided, of course, that you are authenticated, and so on.

However, I wanted to publish text with links, and simply writing a URL in your text won't automatically create a link:

Example of publication in Bluesky, with link not interpreted and then, as a reply, with the link interpreted

To achieve this, I needed to do some research. You can see the difference in the reply between having a link and not having one. You can include additional information when you write by using what they call 'facets.' For adding a link, you can use the AppBskyRichtextFacet, which allows you to:

  • Add a uri
  • Specify the start and end indices of the URI in your text

For example, if you want to insert some text before your URI, you can do something like this:

models.AppBskyRichtextFacet.Main(
            features=[models.AppBskyRichtextFacet.Link(uri=uri)],
            index=models.AppBskyRichtextFacet.ByteSlice(byte_start=len(text)+1,
                                                        byte_end=len(text)+len(uri)+1),
            )
Enter fullscreen mode Exit fullscreen mode

Note that you are specifying the position where your URI begins and ends. You can append it to your list of facets and send it as text to your Bluesky account.

You can see the complete program at:

from atproto import Client, models


def main():
    client = Client()
    password = input("Password? ")
    profile = client.login('fernand0.bsky.social', password)
    print('Welcome,', profile.display_name)

    facets =  []

    text = 'Mi GitHub:'
    uri = 'http://github.com/fernand0'
    facets.append(
            models.AppBskyRichtextFacet.Main(
                features=[models.AppBskyRichtextFacet.Link(uri=uri)],
                index=models.AppBskyRichtextFacet.ByteSlice(byte_start=len(text)+1,
                                                            byte_end=len(text)+len(uri)+1),
                )
            )

    text = f"{text} {uri}"
    response = client.com.atproto.repo.create_record(
        models.ComAtprotoRepoCreateRecord.Data(
            repo=client.me.did,
            collection=models.ids.AppBskyFeedPost,
            record=models.AppBskyFeedPost.Main(created_at=client.get_current_time_iso(), text=text, facets=facets),
        )
    )
    print(f"Response: {response}")


if __name__ == '__main__':
    main()
Enter fullscreen mode Exit fullscreen mode

You can download the code in this publish in bluesky gist.

bluesky Article's
30 articles in total
Favicon
How I Built a Profitable Bluesky Tool Directory in 7 Days
Favicon
I feel like Elon killed Twitter and I plan to be more "developerly active " on BlueSky! https://bsky.app/profile/mahanaz.bsky.social But I want to get back to posting, sharing, and connecting, so that's why I'm here! :)
Favicon
How to add comment from BlueSky to static/vue/nuxt project
Favicon
How to Build a GenAI Bluesky Bot with Langflow, TypeScript, and Node.js
Favicon
✍️ Cross-Posting Astro Blog Posts to BlueSky Using GPT-4 🧠
Favicon
How decentralized is Bluesky really?
Favicon
Thoughts on bluesky Vs X for technical discussions?
Favicon
How to use GitHub to be verified on Bluesky
Favicon
How to post a link with embed card on Bluesky with JavaScript
Favicon
Fetching Liked Posts Using the Bluesky API
Favicon
Free Insights for any Bluesky 🦋 profile
Favicon
Custom Bluesky Handle on AWS with Terraform/OpenTofu
Favicon
How to Build a BlueSky RSS-like Bot with AWS Lambda and Terraform
Favicon
Skymood - Watch Bluesky's heartbeat through emojis in real-time 🌟
Favicon
The Journey of CDK.dev: From Static Site to Bluesky
Favicon
AT Protocol services
Favicon
Building my own Zero Dawn platform
Favicon
Creating a Bot for Bluesky Social
Favicon
How Web5 and Bluesky are Building the Next Layer of the Web - A Comparative Analysis
Favicon
So, you want to publish in Bluesky with a python program?
Favicon
Posting on Bluesky Social using Python in 1 minute
Favicon
How to Create Bluesky BOT using Dart and Firehose
Favicon
How to Update Your Profile from Dart/Flutter App using Bluesky API
Favicon
A nova rede social Bluesky
Favicon
Let's Post to Bluesky Social easily with Dart and Flutter
Favicon
Easily use Firehose API on Bluesky Social with Dart and Flutter
Favicon
Bringing custom domains back to Glitch and bots to Bluesky 🤖🌤️
Favicon
Build bots on Bluesky with Node.js and GitHub Actions
Favicon
What is Bluesky Social Network? And why are developers excited about it?
Favicon
We're on Bluesky ☁️

Featured ones: