forked from BenLubar/it_was_inevitable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbluesky.go
58 lines (52 loc) · 1.55 KB
/
bluesky.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package main
import (
"context"
"log"
"time"
atp "github.com/bluesky-social/indigo/api/atproto"
bsky "github.com/bluesky-social/indigo/api/bsky"
lexutil "github.com/bluesky-social/indigo/lex/util"
xrpc "github.com/bluesky-social/indigo/xrpc"
"github.com/urfave/cli/v2"
)
type Config struct {
BlueskyURL string `env:"BLUESKY_URL,required"`
BlueskyUsername string `env:"BLUESKY_USERNAME,required"`
BlueskyPassword string `env:"BLUESKY_PASSWORD,required"`
}
func postToBluesky(cctx *cli.Context, xrpcc *xrpc.Client, message string, cfg *Config) {
for attempts := 0; attempts < 5; attempts++ {
post := &bsky.FeedPost{
CreatedAt: time.Now().Local().Format(time.RFC3339),
Text: message,
}
resp, err := atp.RepoCreateRecord(context.TODO(), xrpcc, &atp.RepoCreateRecord_Input{
Collection: "app.bsky.feed.post",
Repo: xrpcc.Auth.Did,
Record: &lexutil.LexiconTypeDecoder{
Val: post,
},
})
if err != nil {
log.Fatalf("Error posting to Bluesky: %v\n, Response: %v\n", err, resp)
log.Println("Attempt", attempts+1, "of 5.")
}
log.Println("Giving up on post:", message)
}
}
func authenticateSession(xrpcc *xrpc.Client, cfg *Config) error {
ses, err := atp.ServerCreateSession(context.TODO(), xrpcc, &atp.ServerCreateSession_Input{
Identifier: cfg.BlueskyUsername,
Password: cfg.BlueskyPassword,
})
xrpcc.Auth = &xrpc.AuthInfo{
AccessJwt: ses.AccessJwt,
RefreshJwt: ses.RefreshJwt,
Handle: ses.Handle,
Did: ses.Did,
}
if err != nil {
log.Fatal("Error creating session: ", err)
}
return nil
}