Last active
May 3, 2023 11:14
-
-
Save halka/c0a58a4723e0553f004f804478c3092d to your computer and use it in GitHub Desktop.
Hello Bluesky From MacOS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Original: https://gist.github.com/pojntfx/72403066a96593c1ba8fd5df2b531f2d | |
# This script resolves a DID, retrieves an API key, fetches a user's feed, | |
# and posts a "Hello, world" message to the user's feed. | |
# INSTALL coreutils for using gdate. MacOS's `date` is not support nano seconds as `%3N` | |
# brew install coreutils | |
# Resolve DID for handle | |
HANDLE='your_handle_name' | |
DID_URL="https://bsky.social/xrpc/com.atproto.identity.resolveHandle" | |
export DID=$(curl -G \ | |
--data-urlencode "handle=$HANDLE" \ | |
"$DID_URL" | jq -r .did) | |
# Get an app password from here: https://staging.bsky.app/settings/app-passwords | |
export APP_PASSWORD= | |
# Get API key with the app password | |
API_KEY_URL='https://bsky.social/xrpc/com.atproto.server.createSession' | |
POST_DATA="{ \"identifier\": \"${DID}\", \"password\": \"${APP_PASSWORD}\" }" | |
export API_KEY=$(curl -X POST \ | |
-H 'Content-Type: application/json' \ | |
-d "$POST_DATA" \ | |
"$API_KEY_URL" | jq -r .accessJwt) | |
# Get a user's feed | |
ACTOR='why.bsky.team' | |
LIMIT=4 | |
FEED_URL="https://bsky.social/xrpc/app.bsky.feed.getAuthorFeed" | |
curl -G \ | |
-H "Authorization: Bearer ${API_KEY}" \ | |
--data-urlencode "actor=$ACTOR" \ | |
--data-urlencode "limit=$LIMIT" \ | |
"$FEED_URL" | jq -r .feed # Or if you want to return only a user's own posts: jq '.feed | .[] | select((.post.record."$type" == "app.bsky.feed.post") and (.post.record.reply.parent? | not) and (.reason? | not)) | {text: .post.record.text, createdAt: .post.record.createdAt, replyCount: .post.replyCount, repostCount: .post.repostCount, likeCount: .post.likeCount, author: {handle: .post.author.handle, displayName: .post.author.displayName, avatar: .post.author.avatar}}' | |
# Post "Hello, world" to your feed | |
POST_FEED_URL='https://bsky.social/xrpc/com.atproto.repo.createRecord' | |
POST_DATETIME=$(gdate -u +%Y-%m-%dT%H:%M:%S.%3N)Z | |
POST_RECORD="{ \"collection\": \"app.bsky.feed.post\", \"repo\": \"${DID}\", \"record\": { \"text\": \"Hello, world\", \"createdAt\": \"$POST_DATETIME\", \"\$type\": \"app.bsky.feed.post\" } }" | |
curl -X POST \ | |
-H "Authorization: Bearer ${API_KEY}" \ | |
-H 'Content-Type: application/json' \ | |
-d "$POST_RECORD" \ | |
"$POST_FEED_URL" | jq -r |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment