Something went wrong. Try again.
Bluesky bot for notifying on follow/unfollow.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171package main
import ( "context" "flag" "fmt" "log/slog" "net/http" "os"
"github.com/bluesky-social/indigo/atproto/auth/oauth")
type App struct { db *Database oauthClient *oauth.ClientApp}
const ( loginCmd = "login" runCmd = "run" testCmd = "test" dataSourceName = "data/data.db" callbackURI = "http://127.0.0.1:8080/callback")
func main() { if len(os.Args) < 2 { fmt.Println("Usage: go run main.go <login|run|test>") os.Exit(1) }
db, err := InitDB(dataSourceName) if err != nil { slog.Error("failed to init db", "err", err) os.Exit(1) }
scopes := []string{ "atproto", "rpc:app.bsky.graph.getFollows?aud=*", "rpc:app.bsky.graph.getFollowers?aud=*", "rpc:chat.bsky.convo.getConvoForMembers?aud=*", "rpc:chat.bsky.convo.sendMessage?aud=*", } config := oauth.NewLocalhostConfig(callbackURI, scopes) oauthClient := oauth.NewClientApp(&config, db)
app := &App{ db: db, oauthClient: oauthClient, }
switch os.Args[1] { case loginCmd: login(app) case runCmd: run(app) case testCmd: test(app) default: fmt.Println("Usage: go run main.go <login|run|test>") os.Exit(1) }}
func login(app *App) { fmt.Println("Starting local web server for login...")
// Bypass private IP restriction during auth flow app.oauthClient.Resolver.Client = http.DefaultClient
http.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) { if r.Method == "GET" { w.Header().Set("Content-Type", "text/html") w.Write([]byte(` <form method="POST" action="/login"> <label>Bluesky Handle:</label> <input type="text" name="handle" required placeholder="you.bsky.social"> <button type="submit">Login</button> </form> `)) return } handle := r.FormValue("handle") redirectURL, err := app.oauthClient.StartAuthFlow(r.Context(), handle) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } http.Redirect(w, r, redirectURL, http.StatusFound) })
http.HandleFunc("/callback", func(w http.ResponseWriter, r *http.Request) { sessionData, err := app.oauthClient.ProcessCallback(r.Context(), r.URL.Query()) if err != nil { http.Error(w, fmt.Sprintf("OAuth Callback failed: %v", err), http.StatusBadRequest) return } fmt.Printf("Login successful. Saved session for DID: %s\n", sessionData.AccountDID) w.Write([]byte("Login successful. You can close this window and stop the server.")) })
fmt.Println("Please open: https://127.0.0.1:8080/login") if err := http.ListenAndServe("0.0.0.0:8080", nil); err != nil { slog.Error("server error", "err", err) }}
func getActiveSession(ctx context.Context, app *App) (*oauth.ClientSession, error) { storedSession, err := app.db.GetActiveSession() if err != nil { return nil, fmt.Errorf("no active session found, please run 'login': %v", err) }
// Resume the session session, err := app.oauthClient.ResumeSession(ctx, storedSession.AccountDID, storedSession.SessionID) if err != nil { return nil, fmt.Errorf("failed to resume session: %v", err) }
// Try to refresh if needed (if it fails, it might mean the token is fully expired/revoked) _, err = session.RefreshTokens(ctx) if err != nil { slog.Warn("failed to proactively refresh tokens", "err", err) }
return session, nil}
func run(app *App) { ctx := context.Background() session, err := getActiveSession(ctx, app) if err != nil { slog.Error("Auth error", "err", err) os.Exit(1) }
err = SyncFollowers(ctx, session, app.db) if err != nil { slog.Error("Sync followers failed", "err", err) os.Exit(1) }}
func test(app *App) { fs := flag.NewFlagSet(testCmd, flag.ExitOnError) handle := fs.String("handle", "", "recipient handle (required)") message := fs.String("message", "This is a test message.", "message content") fs.Parse(os.Args[2:])
if *handle == "" { slog.Error("handle is required") fs.Usage() os.Exit(1) }
ctx := context.Background() session, err := getActiveSession(ctx, app) if err != nil { slog.Error("Auth error", "err", err) os.Exit(1) }
err = SendTestDM(ctx, session, *handle, *message) if err != nil { slog.Error("Test DM failed", "err", err) os.Exit(1) }}