Skip to content

Commit

Permalink
add example to quote a post with an image (#484)
Browse files Browse the repository at this point in the history
  • Loading branch information
zzstoatzz authored Dec 11, 2024
1 parent f062df4 commit f8de46f
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions examples/advanced_usage/send_embed.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import httpx
from atproto import Client, models

DEFAULT_IMAGE_URL = 'https://cdn.bsky.app/img/avatar/plain/did:plc:kvwvcn5iqfooopmyzvb4qzba/bafkreicwqcugdgubtawr6xv6jjifoju67eigwx2xgz4zs73nkzzn36oucy@jpeg'

def main() -> None:

def main(handle: str, password: str) -> None:
client = Client()
client.login('my-handle', 'my-password')
client.login(handle, password)

# Example 1: Link card embed
text = 'Example post with embed external resource (link card)'
# AppBskyEmbedExternal is the same as "link card" in the app
embed_external = models.AppBskyEmbedExternal.Main(
Expand All @@ -14,15 +18,33 @@ def main() -> None:
uri='https://google.com',
)
)

post_with_link_card = client.send_post(text=text, embed=embed_external)

# Example 2: Simple quote post
text_quote = 'Example post with embed post and quote (quote post)'
# AppBskyEmbedRecord is the same as "quote post" in the app
embed_post = models.AppBskyEmbedRecord.Main(record=models.create_strong_ref(post_with_link_card))

client.send_post(text=text_quote, embed=embed_post)

# Example 3: Quote post with image
img_data = httpx.get(DEFAULT_IMAGE_URL).content
uploaded_blob = client.upload_blob(img_data).blob

embed_post_with_image = models.AppBskyEmbedRecordWithMedia.Main(
record=models.AppBskyEmbedRecord.Main(record=models.create_strong_ref(post_with_link_card)),
media=models.AppBskyEmbedImages.Main(
images=[
models.AppBskyEmbedImages.Image(
image=uploaded_blob,
alt='Example image with quote',
aspect_ratio=models.AppBskyEmbedDefs.AspectRatio(width=1, height=1),
)
]
),
)

client.send_post(text='Example post with both quote and image', embed=embed_post_with_image)


if __name__ == '__main__':
main()
main(handle='my-handle', password='my-password') # noqa: S106

0 comments on commit f8de46f

Please sign in to comment.