Skip to content

Instantly share code, notes, and snippets.

@vhogemann
Created September 2, 2024 11:14
Show Gist options
  • Select an option

  • Save vhogemann/4908b0222c89bb661a77d7f7f40d184e to your computer and use it in GitHub Desktop.

Select an option

Save vhogemann/4908b0222c89bb661a77d7f7f40d184e to your computer and use it in GitHub Desktop.
Quick and dirty way to fetch your BlueSky notifications using F# fsi
open System.IO
let load () =
let envFile = Path.Combine(__SOURCE_DIRECTORY__, ".env")
if File.Exists(envFile) then
File.ReadAllLines(envFile)
|> Array.map (fun line -> line.Split('='))
|> Array.map (fun parts -> parts.[0], parts.[1])
|> Map
else
Map []
#r "nuget: FSharp.Data"
#load "Env.fsx"
open FSharp.Data
open Microsoft.DotNet.Interactive
open System.Text.Json
let API_URL = "https://bsky.social/xrpc"
let env = Env.load()
let identifier =
match (env |> Map.tryFind "USERNAME") with
| Some username -> username
| None -> Kernel.GetInputAsync("Enter your username:") |> Async.AwaitTask |> Async.RunSynchronously
let password =
match (env |> Map.tryFind "PASSWORD") with
| Some password -> password
| None -> Kernel.GetPasswordAsync("Enter your password:") |> Async.AwaitTask |> Async.RunSynchronously |> string
type AuthData = {
accessJwt: string;
did: string;
}
// Get the access token
let getAccessToken (identifier, password) = async {
let body = $"""
{{
"identifier" : "{identifier}",
"password" : "{password}"
}}
"""
let! response = Http.AsyncRequestString(
$"{API_URL}/com.atproto.server.createSession",
headers = [ "Content-Type", "application/json" ],
body = TextRequest(body))
let authData = JsonSerializer.Deserialize<AuthData>(response)
return authData
}
let getNotifications (authData) = async {
let! response = Http.AsyncRequestString(
$"{API_URL}/app.bsky.notification.listNotifications",
headers = [ "Authorization", $"Bearer {authData.accessJwt}" ]
)
return response
}
let authData = getAccessToken (identifier, password) |> Async.RunSynchronously
let notifications = getNotifications authData |> Async.RunSynchronously
notifications
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment