Last active
November 8, 2024 12:19
-
-
Save elithrar/87d4e3e79a1b53385776657b09ed1090 to your computer and use it in GitHub Desktop.
Bluesky API + Cloudflare Workers example: https://docs.bsky.app/docs/get-started + https://developers.cloudflare.com/workers/ = π
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
import { BskyAgent } from "@atproto/api"; | |
type Env = { | |
BLUESKY_APP_PASSWORD: string; | |
POST_TOKEN: string; | |
}; | |
export default { | |
async fetch(request, env, ctx): Promise<Response> { | |
let url = new URL(request.url); | |
if (url.pathname.startsWith("/favicon")) { | |
return Response.json({}, { status: 404 }); | |
} | |
if (!env.POST_TOKEN) { | |
console.log("POST_TOKEN must not be empty."); | |
return Response.json({}); | |
} | |
// Require a shared secret (POST_TOKEN) to post, so we don't end up exposing our account to anyone | |
if (url.pathname === "/post" && url.searchParams.get("POST_TOKEN") === env.POST_TOKEN) { | |
const agent = new BskyAgent({ | |
service: "https://bsky.social", | |
}); | |
await agent.login({ | |
identifier: "elithrar.bsky.social", | |
// Stored in a Secret | |
password: env.BLUESKY_APP_PASSWORD, | |
}); | |
// Future: capture the content ID (cid) and cache it in order to avoid | |
// publishing duplicate content. | |
let { uri } = await agent.post({ | |
text: "This post is from a Cloudflare Worker via the Bluesky API :-)", | |
createdAt: new Date().toISOString(), | |
}); | |
return Response.json({ posted: true, uri: uri }); | |
} | |
return Response.json({ hello: "world" }); | |
}, | |
} satisfies ExportedHandler<Env>; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment