Python

How to automate article post notifications in Bluesky using Python

Package Install

By installing “atproto”, You can automate article post notifications in Bluesky.

Before installing, You should create a virtual environment.

How to create a virtual environment in pythonIn this article, introduce how to create a virtual environment in Py...
pip install atproto

Source Code

By using the following code, You can automate article post notifications.

As needed, Assign parameters such as username, password, etc.

import datetime
from atproto import models, Client as BlueskyClient

def bluesky_post(username, password, link_card_title, link_card_description, link_card_url, post_text):
    """
    Authenticate with Bluesky, build an external link embed, and send a post.

    Parameters:
    - username (str): Bluesky account username.
    - password (str): Bluesky account password.
    - link_card_title (str): Title to display on the embedded link card.
    - link_card_description (str): Description to display on the embedded link card.
    - link_card_url (str): URL to embed in the link card.
    - post_text (str): Main text content of the post.

    Returns:
    - result: Response from the Bluesky API if successful.
    - None: If an error occurred during posting.
    """
    try:
        bluesky_client = BlueskyClient(base_url='https://bsky.social')
        bluesky_client.login(login=username, password=password)

        embed_external = models.AppBskyEmbedExternal.Main(
            external=models.AppBskyEmbedExternal.External(
                title=link_card_title,
                description=link_card_description,
                uri=link_card_url
            )
        )

        result = bluesky_client.send_post(post_text, embed=embed_external)
        return result

    except Exception as e:
        print(
            f"BlueSky: Post failed - "
            f"Execution datetime: {datetime.datetime.now()} - "
            f"Error: {e}"
        )
        return None


# Assign parameters to variables
username = "username.bsky.social"
password = "PASSWORD"
link_card_title = "TEST_TITLE"
link_card_description = "TEST_DESCRIPTION"
link_card_url = "https://tech-learning-blog.com/what-is-a-data-lakehouse/"
post_text = "TEST_TEXT"

# Call the function using those variables
result = bluesky_post(
    username,
    password,
    link_card_title,
    link_card_description,
    link_card_url,
    post_text
)

# Optionally, inspect or print the result
print(result)