From f67964b5fd67f8787a0cd74711f76cabee354872 Mon Sep 17 00:00:00 2001 From: astravexton Date: Wed, 18 Jun 2025 08:49:02 +0100 Subject: [PATCH 01/39] change to switch statement --- main.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index b45363f..36e4d8a 100644 --- a/main.go +++ b/main.go @@ -145,12 +145,12 @@ func (h *handler) HandleEvent(ctx context.Context, event *models.Event) error { return nil } - if event.Commit.Operation == models.CommitOperationCreate || - event.Commit.Operation == models.CommitOperationUpdate { + switch event.Commit.Operation { + case models.CommitOperationCreate, models.CommitOperationUpdate: h.bsky.Bluesky.Cfg.Cursor = event.TimeUS + 1 // +1 to not show same post bsky.PersistAuthSession(h.bsky.Bluesky.Cfg) h.ProcessPost(event) - } else if event.Commit.Operation == models.CommitOperationDelete { + case models.CommitOperationDelete: h.bsky.Bluesky.Cfg.Cursor = event.TimeUS + 1 // +1 to not show same post bsky.PersistAuthSession(h.bsky.Bluesky.Cfg) r, e := h.bsky.Bluesky.GetTelegramData(event.Commit.RKey) From 798f8134f4a5da64adc9b3a540783ee16b115ab0 Mon Sep 17 00:00:00 2001 From: astravexton Date: Thu, 19 Jun 2025 10:54:01 +0100 Subject: [PATCH 02/39] add CI build --- .forgejo/workflows/build.yml | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 .forgejo/workflows/build.yml diff --git a/.forgejo/workflows/build.yml b/.forgejo/workflows/build.yml new file mode 100644 index 0000000..35e0797 --- /dev/null +++ b/.forgejo/workflows/build.yml @@ -0,0 +1,22 @@ +on: + push: + branches: + - main +jobs: + build: + runs-on: ubuntu-latest + steps: + - name: Checkout repo + uses: actions/checkout@v4 + - name: Login to Docker Hub + uses: https://github.com/docker/login-action@v3 + with: + registry: git.zio.sh + username: ${{ secrets.REPO_USER }} + password: ${{ secrets.REPO_PASS }} + - name: Set up Docker Build Push Action + uses: https://github.com/docker/build-push-action@v2 + with: + tags: git.zio.sh/astra/bsky2tg:latest + push: true + load: false \ No newline at end of file From e0a63bd7d5c2a6d114c0c5d10d7fa49f4780c47c Mon Sep 17 00:00:00 2001 From: astravexton Date: Thu, 19 Jun 2025 11:41:32 +0100 Subject: [PATCH 03/39] add Dockerfile --- Dockerfile | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..2dadb1b --- /dev/null +++ b/Dockerfile @@ -0,0 +1,15 @@ +FROM golang:alpine AS builder + +WORKDIR /go/src/git.zio.sh/bsky2tg +COPY . . + +RUN apk update && \ + apk add --no-cache git bash && \ + go get -d -v ./... && \ + go install + +FROM alpine:latest + +COPY --from=builder /go/bin/bsky2tg /usr/local/bin/bsky2tg + +CMD ["bsky2tg"] \ No newline at end of file From 2bb394623713e422fff94dbc3f2ee7cd963a1c5d Mon Sep 17 00:00:00 2001 From: astravexton Date: Sun, 29 Jun 2025 18:35:01 +0100 Subject: [PATCH 04/39] add env var for endpoint --- main.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 36e4d8a..8f8e462 100644 --- a/main.go +++ b/main.go @@ -72,7 +72,11 @@ func main() { log.Fatalf("failed to create client: %v", err) } - bot, err := tgbotapi.NewBotAPIWithAPIEndpoint(os.Getenv("TG_TOKEN"), "https://bot.astra.blue/bot%s/%s") + endpoint := "https://api.telegram.org/bot%s/%s" + if os.Getenv("TG_API_ENDPOINT") != "" { + endpoint = os.Getenv("TG_API_ENDPOINT") + } + bot, err := tgbotapi.NewBotAPIWithAPIEndpoint(os.Getenv("TG_TOKEN"), endpoint) if err != nil { panic(err) } From bfa829d8c72be0cf4dd74d72777e4ef1fc784bf9 Mon Sep 17 00:00:00 2001 From: astravexton Date: Tue, 1 Jul 2025 07:08:46 +0100 Subject: [PATCH 05/39] add support for links as posts --- bsky/bluesky.go | 14 +++++ bsky/client.go | 22 ++++--- bsky/parse.go | 164 ++++++++++++++++++++++++++++++++++++++++++++++-- main.go | 73 +++++++++++++++------ 4 files changed, 240 insertions(+), 33 deletions(-) diff --git a/bsky/bluesky.go b/bsky/bluesky.go index ae8d5cf..38422f7 100644 --- a/bsky/bluesky.go +++ b/bsky/bluesky.go @@ -263,3 +263,17 @@ type Records struct { Cid string `json:"cid"` Value Value `json:"value"` } + +func (bluesky *Bluesky) FetchPost(did string, rkey string) FetchedPost { + resp := &struct { + Posts []FetchedPost `json:"posts"` + }{} + params := struct { + URIs string `url:"uris"` + }{ + URIs: fmt.Sprintf("at://%s/app.bsky.feed.post/%s", did, rkey), + } + bluesky.sling.New().Base("https://public.api.bsky.app"). + Get("/xrpc/app.bsky.feed.getPosts").QueryStruct(¶ms).Receive(resp, resp) + return resp.Posts[0] +} diff --git a/bsky/client.go b/bsky/client.go index 34047a4..c11661d 100644 --- a/bsky/client.go +++ b/bsky/client.go @@ -28,7 +28,7 @@ func NewBSky() *BSky { } } -func (b *BSky) getPDS() error { +func (b *BSky) ResolveHandle(handle string) (string, error) { httpClient := &http.Client{Timeout: 3 * time.Second} resp := new(BSkySessionResponse) errResp := &struct { @@ -38,23 +38,29 @@ func (b *BSky) getPDS() error { params := struct { Handle string `url:"handle"` }{ - Handle: b.Bluesky.Cfg.Handle, + Handle: handle, } sling.New().Base("https://public.api.bsky.app/").Client(httpClient). Get("/xrpc/com.atproto.identity.resolveHandle").QueryStruct(params). Receive(resp, errResp) if errResp.Error != "" { - return errors.New(errResp.Message) + return "", errors.New(errResp.Message) } + return resp.DID, nil +} + +func (b *BSky) getPDS() error { + did, _ := b.ResolveHandle(b.Bluesky.Cfg.Handle) + var didURL url.URL - if strings.HasPrefix(resp.DID, "did:web:") { - didURL.Host = "https://" + resp.DID[8:] + if strings.HasPrefix(did, "did:web:") { + didURL.Host = "https://" + did[8:] didURL.Path = "/.well-known/did.json" - } else if strings.HasPrefix(resp.DID, "did:plc:") { + } else if strings.HasPrefix(did, "did:plc:") { didURL.Host = "https://plc.directory" - didURL.Path = "/" + resp.DID + didURL.Path = "/" + did } else { return errors.New("DID is not supported") } @@ -104,7 +110,7 @@ func (b *BSky) Auth(authData []string) error { b.Bluesky.Cfg.AppPassword = authData[1] err = b.Bluesky.CreateSession(b.Bluesky.Cfg) if err != nil { - return errors.New(fmt.Sprintf("unable to auth: %s", err)) + return fmt.Errorf("unable to auth: %s", err) } b.Bluesky.Cfg.AppPassword = "" // we don't need to save this PersistAuthSession(b.Bluesky.Cfg) diff --git a/bsky/parse.go b/bsky/parse.go index 1560be2..c600af9 100644 --- a/bsky/parse.go +++ b/bsky/parse.go @@ -123,6 +123,160 @@ type ParsedEmbeds struct { Height int64 } +type FetchedPost struct { + URI string `json:"uri"` + Cid string `json:"cid"` + Author struct { + Did string `json:"did"` + Handle string `json:"handle"` + DisplayName string `json:"displayName"` + Avatar string `json:"avatar"` + Associated struct { + Chat struct { + AllowIncoming string `json:"allowIncoming"` + } `json:"chat"` + } `json:"associated"` + Labels []interface{} `json:"labels"` + CreatedAt time.Time `json:"createdAt"` + } `json:"author"` + Record *Post `json:"record"` + // Record struct { + // Type string `json:"$type"` + // CreatedAt time.Time `json:"createdAt"` + // Embed struct { + // Type string `json:"$type"` + // Media struct { + // Type string `json:"$type"` + // Images []struct { + // Alt string `json:"alt"` + // AspectRatio struct { + // Height int `json:"height"` + // Width int `json:"width"` + // } `json:"aspectRatio"` + // Image struct { + // Type string `json:"$type"` + // Ref struct { + // Link string `json:"$link"` + // } `json:"ref"` + // MimeType string `json:"mimeType"` + // Size int `json:"size"` + // } `json:"image"` + // } `json:"images"` + // } `json:"media"` + // Record struct { + // Type string `json:"$type"` + // Record struct { + // Cid string `json:"cid"` + // URI string `json:"uri"` + // } `json:"record"` + // } `json:"record"` + // } `json:"embed"` + // Labels struct { + // Type string `json:"$type"` + // Values []struct { + // Val string `json:"val"` + // } `json:"values"` + // } `json:"labels"` + // Langs []string `json:"langs"` + // Text string `json:"text"` + // } `json:"record"` + Embed struct { + Type string `json:"$type"` + Media struct { + Type string `json:"$type"` + Images []struct { + Thumb string `json:"thumb"` + Fullsize string `json:"fullsize"` + Alt string `json:"alt"` + AspectRatio struct { + Height int `json:"height"` + Width int `json:"width"` + } `json:"aspectRatio"` + } `json:"images"` + } `json:"media"` + Record struct { + Record struct { + Type string `json:"$type"` + URI string `json:"uri"` + Cid string `json:"cid"` + Author struct { + Did string `json:"did"` + Handle string `json:"handle"` + DisplayName string `json:"displayName"` + Avatar string `json:"avatar"` + Associated struct { + Chat struct { + AllowIncoming string `json:"allowIncoming"` + } `json:"chat"` + } `json:"associated"` + Labels []interface{} `json:"labels"` + CreatedAt time.Time `json:"createdAt"` + } `json:"author"` + Value struct { + Type string `json:"$type"` + CreatedAt time.Time `json:"createdAt"` + Embed struct { + Type string `json:"$type"` + AspectRatio struct { + Height int `json:"height"` + Width int `json:"width"` + } `json:"aspectRatio"` + Video struct { + Type string `json:"$type"` + Ref struct { + Link string `json:"$link"` + } `json:"ref"` + MimeType string `json:"mimeType"` + Size int `json:"size"` + } `json:"video"` + } `json:"embed"` + Facets []struct { + Type string `json:"$type"` + Features []struct { + Type string `json:"$type"` + Did string `json:"did"` + } `json:"features"` + Index struct { + ByteEnd int `json:"byteEnd"` + ByteStart int `json:"byteStart"` + } `json:"index"` + } `json:"facets"` + Langs []string `json:"langs"` + Text string `json:"text"` + } `json:"value"` + Labels []interface{} `json:"labels"` + LikeCount int `json:"likeCount"` + ReplyCount int `json:"replyCount"` + RepostCount int `json:"repostCount"` + QuoteCount int `json:"quoteCount"` + IndexedAt time.Time `json:"indexedAt"` + Embeds []struct { + Type string `json:"$type"` + Cid string `json:"cid"` + Playlist string `json:"playlist"` + Thumbnail string `json:"thumbnail"` + AspectRatio struct { + Height int `json:"height"` + Width int `json:"width"` + } `json:"aspectRatio"` + } `json:"embeds"` + } `json:"record"` + } `json:"record"` + } `json:"embed,omitempty"` + ReplyCount int `json:"replyCount"` + RepostCount int `json:"repostCount"` + LikeCount int `json:"likeCount"` + QuoteCount int `json:"quoteCount"` + IndexedAt time.Time `json:"indexedAt"` + Labels []struct { + Src string `json:"src"` + URI string `json:"uri"` + Cid string `json:"cid"` + Val string `json:"val"` + Cts time.Time `json:"cts"` + } `json:"labels"` +} + func (b *BSky) ParsePost(post []byte) (*Post, error) { var p = &Post{} err := json.Unmarshal(post, &p) @@ -160,12 +314,10 @@ func (post *Post) ProcessFacets(aliases []Records) string { switch feature.Type { case "app.bsky.richtext.facet#mention": link := fmt.Sprintf(`%s`, feature.Did, post.Text[start:end]) - if aliases != nil { - for _, alias := range aliases { - if alias.Value.Subject == feature.Did { - link = fmt.Sprintf(`%s`, - strings.SplitN(alias.Value.Target, "#", 2)[0], strings.SplitN(alias.Value.Target, "#", 2)[1]) - } + for _, alias := range aliases { + if alias.Value.Subject == feature.Did { + link = fmt.Sprintf(`%s`, + strings.SplitN(alias.Value.Target, "#", 2)[0], strings.SplitN(alias.Value.Target, "#", 2)[1]) } } result.WriteString(link) diff --git a/main.go b/main.go index 8f8e462..29c3282 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,8 @@ package main import ( "bytes" "context" + "encoding/json" + "flag" "fmt" "image/jpeg" "io" @@ -11,6 +13,7 @@ import ( "net/http" "net/url" "os" + "regexp" "strconv" "strings" "time" @@ -19,6 +22,7 @@ import ( tgbotapi "github.com/OvyFlash/telegram-bot-api" // apibsky "github.com/bluesky-social/indigo/api/bsky" + "github.com/bluesky-social/jetstream/pkg/client" "github.com/bluesky-social/jetstream/pkg/client/schedulers/sequential" "github.com/bluesky-social/jetstream/pkg/models" @@ -39,7 +43,13 @@ type handler struct { bsky *bsky.BSky } +var ( + post = flag.String("post", "", "URL to a BlueSky post") +) + func main() { + flag.Parse() + var handle = os.Getenv("BSKY_HANDLE") var password = os.Getenv("BSKY_PASSWORD") bskyClient := bsky.NewBSky() @@ -48,30 +58,11 @@ func main() { log.Fatal(err, ". please set BSKY_HANDLE and BSKY_PASSWORD env variables") } - ctx := context.Background() - slog.SetDefault(slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{ - Level: slog.LevelDebug.Level(), - }))) - - logger := slog.Default() - config := client.DefaultClientConfig() - config.WebsocketURL = serverAddr - config.WantedCollections = []string{"app.bsky.feed.post"} - config.WantedDids = []string{bskyClient.Bluesky.Cfg.DID} - config.Compress = true - h := &handler{ seenSeqs: make(map[int64]struct{}), bsky: bskyClient, } - scheduler := sequential.NewScheduler("jetstream_localdev", logger, h.HandleEvent) - - c, err := client.NewClient(config, logger, scheduler) - if err != nil { - log.Fatalf("failed to create client: %v", err) - } - endpoint := "https://api.telegram.org/bot%s/%s" if os.Getenv("TG_API_ENDPOINT") != "" { endpoint = os.Getenv("TG_API_ENDPOINT") @@ -86,6 +77,50 @@ func main() { log.Fatal("TG_CHANNEL_ID is not set") } + if *post != "" { + r := regexp.MustCompile(`^https:\/\/.*?\/profile\/(.*?)\/post\/(.*?)$`) + s := r.FindStringSubmatch(*post) + handle := s[1] + if s[1][0:4] != "did:" { + handle, _ = bskyClient.ResolveHandle(s[1]) + } + + postJSON := bskyClient.Bluesky.FetchPost(handle, s[2]) + p, _ := json.Marshal(postJSON.Record) + h.ProcessPost(&models.Event{ + Did: postJSON.Author.Did, + TimeUS: postJSON.Record.CreatedAt.Unix(), + Kind: "", + Commit: &models.Commit{ + CID: postJSON.Cid, + Operation: "create", + RKey: strings.Split(postJSON.URI, "/")[4], + Collection: "app.bsky.feed.post", + Record: p, + }, + }) + return + } + + ctx := context.Background() + slog.SetDefault(slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{ + Level: slog.LevelDebug.Level(), + }))) + + logger := slog.Default() + config := client.DefaultClientConfig() + config.WebsocketURL = serverAddr + config.WantedCollections = []string{"app.bsky.feed.post"} + config.WantedDids = []string{bskyClient.Bluesky.Cfg.DID} + config.Compress = true + + scheduler := sequential.NewScheduler("jetstream_localdev", logger, h.HandleEvent) + + c, err := client.NewClient(config, logger, scheduler) + if err != nil { + log.Fatalf("failed to create client: %v", err) + } + // ------------------------------------------------------------------------------ // file, err := os.Open("posts.json") // if err != nil { From f572ee3958c39c73230ca5f192dd8c33aa07a6e9 Mon Sep 17 00:00:00 2001 From: astravexton Date: Wed, 2 Jul 2025 16:40:20 +0100 Subject: [PATCH 06/39] add delete flag, fix deleteRecord --- main.go | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 29c3282..f59e825 100644 --- a/main.go +++ b/main.go @@ -44,7 +44,8 @@ type handler struct { } var ( - post = flag.String("post", "", "URL to a BlueSky post") + post = flag.String("post", "", "URL to a BlueSky post") + delete = flag.Bool("delete", false, "true/false to delete post") ) func main() { @@ -85,6 +86,19 @@ func main() { handle, _ = bskyClient.ResolveHandle(s[1]) } + if *delete { + r, e := h.bsky.Bluesky.GetTelegramData(s[2]) + if e == "" { + log.Printf("Found post %s in channel %d, deleting", s[2], r.ChannelID) + m := tgbotapi.NewDeleteMessage(r.ChannelID, r.MessageID) + h.tg.Send(m) + h.bsky.Bluesky.DeleteRecord([]string{s[2], s[1], "blue.zio.bsky2tg.post"}) + } else { + log.Printf("Unable to find post %s on PDS", s[2]) + } + return + } + postJSON := bskyClient.Bluesky.FetchPost(handle, s[2]) p, _ := json.Marshal(postJSON.Record) h.ProcessPost(&models.Event{ @@ -196,7 +210,7 @@ func (h *handler) HandleEvent(ctx context.Context, event *models.Event) error { if e == "" { m := tgbotapi.NewDeleteMessage(r.ChannelID, r.MessageID) h.tg.Send(m) - h.bsky.Bluesky.DeleteRecord([]string{event.Commit.RKey, event.Did, event.Commit.Collection}) + h.bsky.Bluesky.DeleteRecord([]string{event.Commit.RKey, event.Did, "blue.zio.bsky2tg.post"}) } } From 21722264d1940f282e7dc5bd29792cb9ba7ebcd1 Mon Sep 17 00:00:00 2001 From: astravexton Date: Thu, 3 Jul 2025 10:24:07 +0100 Subject: [PATCH 07/39] update readme for TG_API_ENDPOINT --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index b61cf15..8ee216b 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,12 @@ BSKY_HANDLE= BSKY_PASSWORD= ``` +If you use a different Telegram bot endpoint, you can set it with + +```properties +TG_API_ENDPOINT=https://api.domain.com/bot%s/%s +``` + To run: ```bash From 4f94ea647cad85894ef52b9d76d2905df8374d29 Mon Sep 17 00:00:00 2001 From: astra Date: Thu, 3 Jul 2025 15:43:05 +0200 Subject: [PATCH 08/39] Update .forgejo/workflows/build.yml --- .forgejo/workflows/build.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.forgejo/workflows/build.yml b/.forgejo/workflows/build.yml index 35e0797..7e3ba3d 100644 --- a/.forgejo/workflows/build.yml +++ b/.forgejo/workflows/build.yml @@ -17,6 +17,8 @@ jobs: - name: Set up Docker Build Push Action uses: https://github.com/docker/build-push-action@v2 with: - tags: git.zio.sh/astra/bsky2tg:latest + tags: + - git.zio.sh/astra/bsky2tg:latest + - git.zio.sh/astra/bsky2tg:${{ github.sha }} push: true load: false \ No newline at end of file From ce0709f72d4f131e70cf2e01c28365d7286b9cbd Mon Sep 17 00:00:00 2001 From: astra Date: Thu, 3 Jul 2025 15:45:10 +0200 Subject: [PATCH 09/39] Update .forgejo/workflows/build.yml --- .forgejo/workflows/build.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.forgejo/workflows/build.yml b/.forgejo/workflows/build.yml index 7e3ba3d..8b5eaef 100644 --- a/.forgejo/workflows/build.yml +++ b/.forgejo/workflows/build.yml @@ -17,8 +17,8 @@ jobs: - name: Set up Docker Build Push Action uses: https://github.com/docker/build-push-action@v2 with: - tags: - - git.zio.sh/astra/bsky2tg:latest - - git.zio.sh/astra/bsky2tg:${{ github.sha }} + tags: | + git.zio.sh/astra/bsky2tg:latest + git.zio.sh/astra/bsky2tg:${{ github.sha }} push: true load: false \ No newline at end of file From be8b787c52389f841c140e4cf82ad54b6f1beadf Mon Sep 17 00:00:00 2001 From: astravexton Date: Thu, 3 Jul 2025 16:10:36 +0100 Subject: [PATCH 10/39] add error message --- main.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/main.go b/main.go index f59e825..e6c6b5b 100644 --- a/main.go +++ b/main.go @@ -86,6 +86,10 @@ func main() { handle, _ = bskyClient.ResolveHandle(s[1]) } + if handle != bskyClient.Bluesky.Cfg.DID { + log.Fatal("Unable to send posts from other accounts") + } + if *delete { r, e := h.bsky.Bluesky.GetTelegramData(s[2]) if e == "" { From dbc89e5b95d39ed46c3f703329de5dcbcee2a5dc Mon Sep 17 00:00:00 2001 From: astravexton Date: Thu, 3 Jul 2025 18:10:35 +0100 Subject: [PATCH 11/39] remove test stuff --- main.go | 41 ----------------------------------------- 1 file changed, 41 deletions(-) diff --git a/main.go b/main.go index e6c6b5b..c96a5bd 100644 --- a/main.go +++ b/main.go @@ -139,47 +139,6 @@ func main() { log.Fatalf("failed to create client: %v", err) } - // ------------------------------------------------------------------------------ - // file, err := os.Open("posts.json") - // if err != nil { - // fmt.Printf("Error opening file: %v\n", err) - // return - // } - // defer file.Close() - // byteValue, err := io.ReadAll(file) - // if err != nil { - // fmt.Printf("Error reading file: %v\n", err) - // return - // } - - // var posts = struct { - // Records []struct { - // URI string `json:"uri"` - // CID string `json:"cid"` - // Value *bsky.Post `json:"value"` - // } `json:"records"` - // }{} - - // // 4. Unmarshal (decode) the JSON data into the struct - // err = json.Unmarshal(byteValue, &posts) - // if err != nil { - // fmt.Printf("Error unmarshaling JSON: %v\n", err) - // return - // } - // for _, post := range posts.Records { - // log.Printf("post: %s\n", post.Value.ProcessFacets(h.bsky.Bluesky.FetchAliases())) - // s, _ := json.Marshal(post.Value) - // h.ProcessPost(&models.Event{Did: bskyClient.Bluesky.Cfg.DID, Commit: &models.Commit{ - // Record: s, - // RKey: strings.Split(post.URI, "/")[4], - // CID: post.CID, - // Collection: "app.bsky.feed.post", - // }}) - // time.Sleep(time.Second * 2) - // } - // return - // ------------------------------------------------------------------------------ - cursor := time.Now().UnixMicro() restartCount := 0 loop: From bd8a437f43742e4b14cbc00432ff7de92b80494b Mon Sep 17 00:00:00 2001 From: astravexton Date: Thu, 10 Jul 2025 19:19:49 +0100 Subject: [PATCH 12/39] Change link media to be large --- main.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index c96a5bd..2e9a476 100644 --- a/main.go +++ b/main.go @@ -21,8 +21,6 @@ import ( "git.zio.sh/astra/bsky2tg/bsky" tgbotapi "github.com/OvyFlash/telegram-bot-api" - // apibsky "github.com/bluesky-social/indigo/api/bsky" - "github.com/bluesky-social/jetstream/pkg/client" "github.com/bluesky-social/jetstream/pkg/client/schedulers/sequential" "github.com/bluesky-social/jetstream/pkg/models" @@ -303,7 +301,8 @@ func (h *handler) ProcessPost(event *models.Event) error { URL: fmt.Sprintf("https://bsky.app/profile/%s/post/%s", strings.Split(ps.Embed.Record.URI, "/")[2], strings.Split(ps.Embed.Record.URI, "/")[4]), - PreferSmallMedia: true, + PreferSmallMedia: false, + PreferLargeMedia: true, ShowAboveText: true, } } else { From 1690279d5c39f140abc58e61cd1a266b4fbc4091 Mon Sep 17 00:00:00 2001 From: astravexton Date: Fri, 11 Jul 2025 14:15:09 +0100 Subject: [PATCH 13/39] add support for multiple message IDs --- bsky/bluesky.go | 2 +- main.go | 20 ++++++++++++++------ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/bsky/bluesky.go b/bsky/bluesky.go index 38422f7..1ca7bd5 100644 --- a/bsky/bluesky.go +++ b/bsky/bluesky.go @@ -127,7 +127,7 @@ func (bluesky *Bluesky) CheckSessionValid() { type TelegramRecord struct { ChannelID int64 `json:"channel_id"` - MessageID int `json:"message_id"` + MessageID []int `json:"message_id"` Link *Link `json:"link"` Error string `json:"error"` Message string `json:"message"` diff --git a/main.go b/main.go index 2e9a476..acbbc01 100644 --- a/main.go +++ b/main.go @@ -92,8 +92,10 @@ func main() { r, e := h.bsky.Bluesky.GetTelegramData(s[2]) if e == "" { log.Printf("Found post %s in channel %d, deleting", s[2], r.ChannelID) - m := tgbotapi.NewDeleteMessage(r.ChannelID, r.MessageID) - h.tg.Send(m) + for _, msgID := range r.MessageID { + m := tgbotapi.NewDeleteMessage(r.ChannelID, msgID) + h.tg.Send(m) + } h.bsky.Bluesky.DeleteRecord([]string{s[2], s[1], "blue.zio.bsky2tg.post"}) } else { log.Printf("Unable to find post %s on PDS", s[2]) @@ -169,8 +171,10 @@ func (h *handler) HandleEvent(ctx context.Context, event *models.Event) error { bsky.PersistAuthSession(h.bsky.Bluesky.Cfg) r, e := h.bsky.Bluesky.GetTelegramData(event.Commit.RKey) if e == "" { - m := tgbotapi.NewDeleteMessage(r.ChannelID, r.MessageID) - h.tg.Send(m) + for _, msgID := range r.MessageID { + m := tgbotapi.NewDeleteMessage(r.ChannelID, msgID) + h.tg.Send(m) + } h.bsky.Bluesky.DeleteRecord([]string{event.Commit.RKey, event.Did, "blue.zio.bsky2tg.post"}) } } @@ -278,9 +282,13 @@ func (h *handler) ProcessPost(event *models.Event) error { } else { resp, _ := h.tg.SendMediaGroup(tgbotapi.NewMediaGroup(cid, mediaGroup)) uri, cid := getLink(event) + var messageIDs []int + for _, msgID := range resp { + messageIDs = append(messageIDs, msgID.MessageID) + } h.bsky.Bluesky.CommitTelegramResponse(&bsky.TelegramRecord{ ChannelID: resp[0].Chat.ID, - MessageID: resp[0].MessageID, + MessageID: messageIDs, Link: &bsky.Link{ Cid: cid, URI: uri, @@ -312,7 +320,7 @@ func (h *handler) ProcessPost(event *models.Event) error { uri, cid := getLink(event) h.bsky.Bluesky.CommitTelegramResponse(&bsky.TelegramRecord{ ChannelID: resp.Chat.ID, - MessageID: resp.MessageID, + MessageID: []int{resp.MessageID}, Link: &bsky.Link{ Cid: cid, URI: uri, From aff13c04dd869a9d28c3396226ec0c940544572e Mon Sep 17 00:00:00 2001 From: astravexton Date: Fri, 11 Jul 2025 14:32:00 +0100 Subject: [PATCH 14/39] use deleteMessages instead --- main.go | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/main.go b/main.go index acbbc01..433fcd0 100644 --- a/main.go +++ b/main.go @@ -92,10 +92,8 @@ func main() { r, e := h.bsky.Bluesky.GetTelegramData(s[2]) if e == "" { log.Printf("Found post %s in channel %d, deleting", s[2], r.ChannelID) - for _, msgID := range r.MessageID { - m := tgbotapi.NewDeleteMessage(r.ChannelID, msgID) - h.tg.Send(m) - } + m := tgbotapi.NewDeleteMessages(r.ChannelID, r.MessageID) + h.tg.Send(m) h.bsky.Bluesky.DeleteRecord([]string{s[2], s[1], "blue.zio.bsky2tg.post"}) } else { log.Printf("Unable to find post %s on PDS", s[2]) @@ -171,10 +169,8 @@ func (h *handler) HandleEvent(ctx context.Context, event *models.Event) error { bsky.PersistAuthSession(h.bsky.Bluesky.Cfg) r, e := h.bsky.Bluesky.GetTelegramData(event.Commit.RKey) if e == "" { - for _, msgID := range r.MessageID { - m := tgbotapi.NewDeleteMessage(r.ChannelID, msgID) - h.tg.Send(m) - } + m := tgbotapi.NewDeleteMessages(r.ChannelID, r.MessageID) + h.tg.Send(m) h.bsky.Bluesky.DeleteRecord([]string{event.Commit.RKey, event.Did, "blue.zio.bsky2tg.post"}) } } From dc7382f16288e99bdffe2577638f129db6cc9f56 Mon Sep 17 00:00:00 2001 From: astravexton Date: Sat, 13 Sep 2025 17:45:28 +0100 Subject: [PATCH 15/39] Update parse.go --- bsky/parse.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/bsky/parse.go b/bsky/parse.go index c600af9..a06f131 100644 --- a/bsky/parse.go +++ b/bsky/parse.go @@ -18,42 +18,50 @@ type Post struct { Facets *[]Facets `json:"facets,omitempty"` CreatedAt time.Time `json:"createdAt"` } + type Ref struct { Link string `json:"$link,omitempty"` } + type Thumb struct { Type string `json:"$type,omitempty"` Ref *Ref `json:"ref,omitempty"` MimeType string `json:"mimeType,omitempty"` Size int `json:"size,omitempty"` } + type External struct { URI string `json:"uri,omitempty"` Thumb *Thumb `json:"thumb,omitempty"` Title string `json:"title,omitempty"` Description string `json:"description,omitempty"` } + type Video struct { Type string `json:"$type,omitempty"` Ref *Ref `json:"ref,omitempty"` MimeType string `json:"mimeType,omitempty"` Size int `json:"size,omitempty"` } + type Image struct { Type string `json:"$type,omitempty"` Ref *Ref `json:"ref,omitempty"` MimeType string `json:"mimeType,omitempty"` Size int `json:"size,omitempty"` } + type AspectRatio struct { Width int `json:"width,omitempty"` Height int `json:"height,omitempty"` } + type Images struct { Alt string `json:"alt,omitempty"` Image *Image `json:"image,omitempty"` AspectRatio *AspectRatio `json:"aspectRatio,omitempty"` } + type Media struct { Type string `json:"$type,omitempty"` External *External `json:"external,omitempty"` @@ -61,16 +69,19 @@ type Media struct { Images *[]Images `json:"images,omitempty"` AspectRatio *AspectRatio `json:"aspectRatio,omitempty"` } + type Record struct { Cid string `json:"cid,omitempty"` URI string `json:"uri,omitempty"` } + type PostRecord struct { Type string `json:"$type,omitempty"` Cid string `json:"cid,omitempty"` URI string `json:"uri,omitempty"` Record *Record `json:"record,omitempty"` } + type Embed struct { Type string `json:"$type,omitempty"` Media *Media `json:"media,omitempty"` @@ -79,35 +90,59 @@ type Embed struct { Record *PostRecord `json:"record,omitempty"` External *External `json:"external,omitempty"` } + type Values struct { Val string `json:"val,omitempty"` } + type Labels struct { Type string `json:"$type,omitempty"` Values *[]Values `json:"values,omitempty"` } + type Root struct { Cid string `json:"cid,omitempty"` URI string `json:"uri,omitempty"` } + +func (r *Root) GetDID() string { + return strings.Split(r.URI, "/")[2] +} + +func (r *Root) GetRKey() string { + return strings.Split(r.URI, "/")[4] +} + type Parent struct { Cid string `json:"cid,omitempty"` URI string `json:"uri,omitempty"` } + +func (p *Parent) GetDID() string { + return strings.Split(p.URI, "/")[2] +} + +func (p *Parent) GetRKey() string { + return strings.Split(p.URI, "/")[4] +} + type Reply struct { Root *Root `json:"root,omitempty"` Parent *Parent `json:"parent,omitempty"` } + type Index struct { ByteEnd int `json:"byteEnd,omitempty"` ByteStart int `json:"byteStart,omitempty"` } + type Features struct { Did string `json:"did,omitempty"` URI string `json:"uri,omitempty"` Tag string `json:"tag,omitempty"` Type string `json:"$type,omitempty"` } + type Facets struct { Type string `json:"$type"` Index *Index `json:"index,omitempty"` @@ -118,6 +153,7 @@ type ParsedEmbeds struct { Type string MimeType string Ref string + Cid string URI string Width int64 Height int64 From 5ff08f5acc7f5669e872551fe7407630f2b05677 Mon Sep 17 00:00:00 2001 From: astravexton Date: Sat, 13 Sep 2025 17:46:14 +0100 Subject: [PATCH 16/39] Change quote post to use deer.social for embeds --- main.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 433fcd0..f7ada17 100644 --- a/main.go +++ b/main.go @@ -32,7 +32,7 @@ const ( serverAddr = "wss://jetstream2.us-west.bsky.network/subscribe" // serverAddr = "wss://stream.zio.blue/subscribe" postFormat = "%s\n—\nšŸ¦‹ @%s" - quotePostFormat = "
%s
\nāž”ļø @%s\n—\nšŸ¦‹ @%s" + quotePostFormat = "
%s
\nāž”ļø @%s\n—\nšŸ¦‹ @%s" ) type handler struct { @@ -327,7 +327,7 @@ func (h *handler) ProcessPost(event *models.Event) error { } func buildBlobURL(server string, did string, cid string) string { - return server + "/xrpc/com.atproto.sync.getBlob?did=" + url.QueryEscape(did) + "&cid=" + url.QueryEscape(cid) + return server + "/xrpc/com.atproto.sync.getBlob?did=" + url.QueryEscape(did) + "&cid=" + cid } func getLink(event *models.Event) (string, string) { From acbdd41680a402e1c7bc00711694fa52713d34b6 Mon Sep 17 00:00:00 2001 From: astravexton Date: Sun, 14 Sep 2025 09:55:27 +0100 Subject: [PATCH 17/39] Fix video embed --- main.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index f7ada17..eef0dc9 100644 --- a/main.go +++ b/main.go @@ -32,7 +32,7 @@ const ( serverAddr = "wss://jetstream2.us-west.bsky.network/subscribe" // serverAddr = "wss://stream.zio.blue/subscribe" postFormat = "%s\n—\nšŸ¦‹ @%s" - quotePostFormat = "
%s
\nāž”ļø @%s\n—\nšŸ¦‹ @%s" + quotePostFormat = "
%s
\nāž”ļø @%s\n—\nšŸ¦‹ @%s" ) type handler struct { @@ -302,7 +302,7 @@ func (h *handler) ProcessPost(event *models.Event) error { if ps.IsQuotePost() { m.LinkPreviewOptions = tgbotapi.LinkPreviewOptions{ IsDisabled: false, - URL: fmt.Sprintf("https://bsky.app/profile/%s/post/%s", + URL: fmt.Sprintf("https://fxbsky.app/profile/%s/post/%s", strings.Split(ps.Embed.Record.URI, "/")[2], strings.Split(ps.Embed.Record.URI, "/")[4]), PreferSmallMedia: false, From c4d4e915484c027bfe6a556eb27c03d3a833c486 Mon Sep 17 00:00:00 2001 From: astravexton Date: Tue, 23 Sep 2025 17:51:12 +0100 Subject: [PATCH 18/39] check if post is already in channel --- main.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index eef0dc9..2f4ac7e 100644 --- a/main.go +++ b/main.go @@ -88,11 +88,11 @@ func main() { log.Fatal("Unable to send posts from other accounts") } + tgpost, tgposterr := h.bsky.Bluesky.GetTelegramData(s[2]) if *delete { - r, e := h.bsky.Bluesky.GetTelegramData(s[2]) - if e == "" { - log.Printf("Found post %s in channel %d, deleting", s[2], r.ChannelID) - m := tgbotapi.NewDeleteMessages(r.ChannelID, r.MessageID) + if tgposterr == "" { + log.Printf("Found post %s in channel %d, deleting", s[2], tgpost.ChannelID) + m := tgbotapi.NewDeleteMessages(tgpost.ChannelID, tgpost.MessageID) h.tg.Send(m) h.bsky.Bluesky.DeleteRecord([]string{s[2], s[1], "blue.zio.bsky2tg.post"}) } else { @@ -101,6 +101,11 @@ func main() { return } + if tgpost.ChannelID != 0 { + log.Printf("Post %s already sent to channel %d, exiting", s[2], tgpost.ChannelID) + return + } + postJSON := bskyClient.Bluesky.FetchPost(handle, s[2]) p, _ := json.Marshal(postJSON.Record) h.ProcessPost(&models.Event{ From 2afd9af2af35747cfa65070d89b0593d2ccdc542 Mon Sep 17 00:00:00 2001 From: astravexton Date: Fri, 26 Sep 2025 09:34:25 +0100 Subject: [PATCH 19/39] add embed URL --- main.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 2f4ac7e..87d00df 100644 --- a/main.go +++ b/main.go @@ -29,10 +29,11 @@ import ( ) const ( - serverAddr = "wss://jetstream2.us-west.bsky.network/subscribe" // serverAddr = "wss://stream.zio.blue/subscribe" + serverAddr = "wss://jetstream2.us-west.bsky.network/subscribe" postFormat = "%s\n—\nšŸ¦‹ @%s" quotePostFormat = "
%s
\nāž”ļø @%s\n—\nšŸ¦‹ @%s" + embedURL = "https://fxbsky.app/profile/%s/post/%s" ) type handler struct { @@ -307,7 +308,7 @@ func (h *handler) ProcessPost(event *models.Event) error { if ps.IsQuotePost() { m.LinkPreviewOptions = tgbotapi.LinkPreviewOptions{ IsDisabled: false, - URL: fmt.Sprintf("https://fxbsky.app/profile/%s/post/%s", + URL: fmt.Sprintf(embedURL, strings.Split(ps.Embed.Record.URI, "/")[2], strings.Split(ps.Embed.Record.URI, "/")[4]), PreferSmallMedia: false, From 69b715f4ee25707c5a70abd8e2bcb2b6afb4a1aa Mon Sep 17 00:00:00 2001 From: astravexton Date: Fri, 26 Sep 2025 09:55:52 +0100 Subject: [PATCH 20/39] fix auth --- bsky/client.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bsky/client.go b/bsky/client.go index c11661d..be5c795 100644 --- a/bsky/client.go +++ b/bsky/client.go @@ -106,7 +106,7 @@ func (b *BSky) Auth(authData []string) error { b.Bluesky.Cfg.Handle = authData[0] b.getPDS() auth, err := loadAuth() - if err != nil { // no auth session found + if auth.AccessJWT == "" { // no auth session found b.Bluesky.Cfg.AppPassword = authData[1] err = b.Bluesky.CreateSession(b.Bluesky.Cfg) if err != nil { From 958b6ccb7a70235d55e6753294038cf33f4b5802 Mon Sep 17 00:00:00 2001 From: astravexton Date: Mon, 29 Sep 2025 10:31:07 +0100 Subject: [PATCH 21/39] fix auth --- bsky/client.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bsky/client.go b/bsky/client.go index be5c795..aa6dc83 100644 --- a/bsky/client.go +++ b/bsky/client.go @@ -105,10 +105,10 @@ func (b *BSky) GetPDS(handle string) string { func (b *BSky) Auth(authData []string) error { b.Bluesky.Cfg.Handle = authData[0] b.getPDS() - auth, err := loadAuth() - if auth.AccessJWT == "" { // no auth session found + auth, _ := loadAuth() + if auth == nil || auth.AccessJWT == "" { // no auth session found b.Bluesky.Cfg.AppPassword = authData[1] - err = b.Bluesky.CreateSession(b.Bluesky.Cfg) + err := b.Bluesky.CreateSession(b.Bluesky.Cfg) if err != nil { return fmt.Errorf("unable to auth: %s", err) } From 55beb3be6873ee72ce8f0e0fec27a740fd230d79 Mon Sep 17 00:00:00 2001 From: astravexton Date: Tue, 30 Sep 2025 19:36:00 +0100 Subject: [PATCH 22/39] Update build.yml --- .forgejo/workflows/build.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.forgejo/workflows/build.yml b/.forgejo/workflows/build.yml index 8b5eaef..6eab080 100644 --- a/.forgejo/workflows/build.yml +++ b/.forgejo/workflows/build.yml @@ -1,5 +1,11 @@ on: push: + paths: + - '*.go' + - 'bsky/*.go' + - 'go.sum' + - 'go.mod' + - 'Dockerfile' branches: - main jobs: From aa8932ea38c973a906c0fe6d527b40ccfb35eff9 Mon Sep 17 00:00:00 2001 From: astravexton Date: Tue, 30 Sep 2025 19:36:07 +0100 Subject: [PATCH 23/39] Update README.md --- README.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 8ee216b..77f033f 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ If you use a different Telegram bot endpoint, you can set it with TG_API_ENDPOINT=https://api.domain.com/bot%s/%s ``` -To run: +# Podman ```bash podman run -it --name bsky2tg_ \ @@ -40,3 +40,11 @@ podman run -it --name bsky2tg_ \ --env BSKY_PASSWORD= \ git.zio.sh/astra/bsky2tg:latest ``` + + +## Bash + +```bash +source .env +./bsky2tg +``` \ No newline at end of file From e041d002263171953b39eab439453d908d6e9589 Mon Sep 17 00:00:00 2001 From: astravexton Date: Wed, 8 Oct 2025 14:30:32 +0100 Subject: [PATCH 24/39] Fix auth (again) --- bsky/bluesky.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/bsky/bluesky.go b/bsky/bluesky.go index 1ca7bd5..d369a0f 100644 --- a/bsky/bluesky.go +++ b/bsky/bluesky.go @@ -118,8 +118,14 @@ func (bluesky *Bluesky) RefreshSession() error { func (bluesky *Bluesky) CheckSessionValid() { resp := new(BSkySessionResponse) + params := struct { + Actor string `url:"actor"` + }{ + Actor: bluesky.Cfg.Handle, + } + bluesky.sling.New().Set("Authorization", fmt.Sprintf("Bearer %s", bluesky.Cfg.AccessJWT)). - Get("/xrpc/app.bsky.actor.getProfile").Receive(resp, resp) + Get("/xrpc/app.bsky.actor.getProfile").QueryStruct(params).Receive(resp, resp) if resp.Error == "ExpiredToken" { bluesky.RefreshSession() } From 70b30a9313655b52e7e55de5a09b86b5fc049dcf Mon Sep 17 00:00:00 2001 From: Astra Date: Wed, 29 Oct 2025 23:12:45 +0000 Subject: [PATCH 25/39] Fix post command --- bsky/bluesky.go | 2 +- main.go | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/bsky/bluesky.go b/bsky/bluesky.go index d369a0f..33aa06a 100644 --- a/bsky/bluesky.go +++ b/bsky/bluesky.go @@ -195,7 +195,7 @@ func (bluesky *Bluesky) GetTelegramData(rkey string) (*TelegramRecord, string) { } bluesky.sling.New().Get("/xrpc/com.atproto.repo.getRecord").QueryStruct(¶ms).Receive(resp, resp) - return resp.Value, resp.Message + return resp.Value, resp.Error } func (bluesky *Bluesky) GetPost(uri string) *Post { diff --git a/main.go b/main.go index 87d00df..fdc3019 100644 --- a/main.go +++ b/main.go @@ -90,6 +90,7 @@ func main() { } tgpost, tgposterr := h.bsky.Bluesky.GetTelegramData(s[2]) + if *delete { if tgposterr == "" { log.Printf("Found post %s in channel %d, deleting", s[2], tgpost.ChannelID) @@ -102,7 +103,7 @@ func main() { return } - if tgpost.ChannelID != 0 { + if tgposterr == "" { log.Printf("Post %s already sent to channel %d, exiting", s[2], tgpost.ChannelID) return } From 1d2347ab7250cba249e5f9453cb32b98b99735e2 Mon Sep 17 00:00:00 2001 From: Astra Date: Mon, 3 Nov 2025 10:08:22 +0000 Subject: [PATCH 26/39] Add option to ignore old posts, default 24 hours --- main.go | 50 +++++++++++++++++++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 15 deletions(-) diff --git a/main.go b/main.go index fdc3019..34d95bb 100644 --- a/main.go +++ b/main.go @@ -43,8 +43,9 @@ type handler struct { } var ( - post = flag.String("post", "", "URL to a BlueSky post") - delete = flag.Bool("delete", false, "true/false to delete post") + post = flag.String("post", "", "URL to a BlueSky post") + delete = flag.Bool("delete", false, "true/false to delete post") + oldPosts = flag.Float64("oldposttime", 24, "Ignore posts if createdAt more than this many hours ago") ) func main() { @@ -189,6 +190,20 @@ func (h *handler) ProcessPost(event *models.Event) error { ps, _ := h.bsky.ParsePost(event.Commit.Record) po := ps.GetEmbeds() cid, _ := strconv.ParseInt(os.Getenv("TG_CHANNEL_ID"), 10, 64) + isEditedPost := false + + now := time.Now() + createdAt := ps.CreatedAt + duration := now.Sub(createdAt) + if duration.Hours() > *oldPosts { + // don't post old posts + return nil + } + + telegramRecord, telegramRecordErr := h.bsky.Bluesky.GetTelegramData(event.Commit.RKey) + if telegramRecordErr == "" { + isEditedPost = true + } if ps.IsReply() { //|| ps.IsQuotePost() { // don't want to post replies to channel @@ -283,20 +298,25 @@ func (h *handler) ProcessPost(event *models.Event) error { if len(mediaGroup) == 0 { log.Print("No mediaGroup to send, see previous error") } else { - resp, _ := h.tg.SendMediaGroup(tgbotapi.NewMediaGroup(cid, mediaGroup)) - uri, cid := getLink(event) - var messageIDs []int - for _, msgID := range resp { - messageIDs = append(messageIDs, msgID.MessageID) + if isEditedPost { + resp, err := h.tg.Send(tgbotapi.NewEditMessageCaption(telegramRecord.ChannelID, telegramRecord.MessageID[0], captionText)) + fmt.Println(resp, err) + } else { + resp, _ := h.tg.SendMediaGroup(tgbotapi.NewMediaGroup(cid, mediaGroup)) + uri, cid := getLink(event) + var messageIDs []int + for _, msgID := range resp { + messageIDs = append(messageIDs, msgID.MessageID) + } + h.bsky.Bluesky.CommitTelegramResponse(&bsky.TelegramRecord{ + ChannelID: resp[0].Chat.ID, + MessageID: messageIDs, + Link: &bsky.Link{ + Cid: cid, + URI: uri, + }, + }, event.Commit.RKey) } - h.bsky.Bluesky.CommitTelegramResponse(&bsky.TelegramRecord{ - ChannelID: resp[0].Chat.ID, - MessageID: messageIDs, - Link: &bsky.Link{ - Cid: cid, - URI: uri, - }, - }, event.Commit.RKey) } } else { m := tgbotapi.MessageConfig{} From e89137224bceb96b810e6904aa98cc0febd4b8a6 Mon Sep 17 00:00:00 2001 From: Astra Date: Mon, 3 Nov 2025 10:17:18 +0000 Subject: [PATCH 27/39] Don't post if starting with @ --- main.go | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/main.go b/main.go index 34d95bb..3fcd1a2 100644 --- a/main.go +++ b/main.go @@ -195,8 +195,9 @@ func (h *handler) ProcessPost(event *models.Event) error { now := time.Now() createdAt := ps.CreatedAt duration := now.Sub(createdAt) - if duration.Hours() > *oldPosts { - // don't post old posts + if duration.Hours() > *oldPosts || + strings.HasPrefix(ps.Text, "@") || + ps.IsReply() { return nil } @@ -205,11 +206,6 @@ func (h *handler) ProcessPost(event *models.Event) error { isEditedPost = true } - if ps.IsReply() { //|| ps.IsQuotePost() { - // don't want to post replies to channel - return nil - } - var captionText string if ps.IsQuotePost() { if ps.Embed.Record.Type == "app.bsky.embed.record" { From a3f4f4f54c0596f1c5bf043aa7c6b10dcfabaee2 Mon Sep 17 00:00:00 2001 From: Astra Date: Thu, 6 Nov 2025 16:17:07 +0000 Subject: [PATCH 28/39] resolve own DID on posts --- main.go | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index 3fcd1a2..a85af72 100644 --- a/main.go +++ b/main.go @@ -45,7 +45,7 @@ type handler struct { var ( post = flag.String("post", "", "URL to a BlueSky post") delete = flag.Bool("delete", false, "true/false to delete post") - oldPosts = flag.Float64("oldposttime", 24, "Ignore posts if createdAt more than this many hours ago") + oldPosts = flag.Float64("oldposttime", 1, "Ignore posts if createdAt more than this many hours ago") ) func main() { @@ -208,6 +208,10 @@ func (h *handler) ProcessPost(event *models.Event) error { var captionText string if ps.IsQuotePost() { + ownHandle, handleErr := h.bsky.GetHandleFromDID(h.bsky.Bluesky.Cfg.DID) + if handleErr != nil { + ownHandle = h.bsky.Bluesky.Cfg.Handle + } if ps.Embed.Record.Type == "app.bsky.embed.record" { handle, _ := h.bsky.GetHandleFromDID(strings.Split(ps.Embed.Record.Record.URI, "/")[2]) captionText = fmt.Sprintf( @@ -218,7 +222,7 @@ func (h *handler) ProcessPost(event *models.Event) error { handle, event.Did, event.Commit.RKey, - h.bsky.Bluesky.Cfg.Handle) + ownHandle) } else { handle, _ := h.bsky.GetHandleFromDID(strings.Split(ps.Embed.Record.URI, "/")[2]) captionText = fmt.Sprintf( @@ -229,15 +233,19 @@ func (h *handler) ProcessPost(event *models.Event) error { handle, event.Did, event.Commit.RKey, - h.bsky.Bluesky.Cfg.Handle) + ownHandle) } } if captionText == "" { + ownHandle, handleErr := h.bsky.GetHandleFromDID(h.bsky.Bluesky.Cfg.DID) + if handleErr != nil { + ownHandle = h.bsky.Bluesky.Cfg.Handle + } if ps.ProcessFacets(h.bsky.Bluesky.FetchAliases()) != "" { - captionText = fmt.Sprintf(postFormat, ps.ProcessFacets(h.bsky.Bluesky.FetchAliases()), h.bsky.Bluesky.Cfg.DID, event.Commit.RKey, h.bsky.Bluesky.Cfg.Handle) + captionText = fmt.Sprintf(postFormat, ps.ProcessFacets(h.bsky.Bluesky.FetchAliases()), h.bsky.Bluesky.Cfg.DID, event.Commit.RKey, ownHandle) } else { - captionText = fmt.Sprintf("šŸ¦‹ @%s", h.bsky.Bluesky.Cfg.DID, event.Commit.RKey, h.bsky.Bluesky.Cfg.Handle) + captionText = fmt.Sprintf("šŸ¦‹ @%s", h.bsky.Bluesky.Cfg.DID, event.Commit.RKey, ownHandle) } } From 64a24f828d214271a438661af96df41a141e9ec5 Mon Sep 17 00:00:00 2001 From: Astra Date: Wed, 12 Nov 2025 07:55:14 +0000 Subject: [PATCH 29/39] PreferLargeMedia = true --- main.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index a85af72..2231dce 100644 --- a/main.go +++ b/main.go @@ -336,8 +336,8 @@ func (h *handler) ProcessPost(event *models.Event) error { URL: fmt.Sprintf(embedURL, strings.Split(ps.Embed.Record.URI, "/")[2], strings.Split(ps.Embed.Record.URI, "/")[4]), - PreferSmallMedia: false, - PreferLargeMedia: true, + PreferSmallMedia: true, + PreferLargeMedia: false, ShowAboveText: true, } } else { From 1540e8de8e3264d8ce7e3698cb6e1bb25914b86b Mon Sep 17 00:00:00 2001 From: Astra Date: Wed, 26 Nov 2025 14:11:23 +0000 Subject: [PATCH 30/39] Add error message output for media --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index 2231dce..d40afb3 100644 --- a/main.go +++ b/main.go @@ -278,7 +278,7 @@ func (h *handler) ProcessPost(event *models.Event) error { mediaAdd := tgbotapi.NewInputMediaVideo(tgbotapi.FileReader{Name: "video.mp4", Reader: f}) metadata, err := getVideoMetadata(f.Name()) if err != nil { - log.Printf("Unable to read video metadata: %s\n", buildBlobURL(h.bsky.Bluesky.Cfg.PDSURL, h.bsky.Bluesky.Cfg.DID, media.URI)) + log.Printf("Unable to read video metadata: %s - URL: %s\n", err, buildBlobURL(h.bsky.Bluesky.Cfg.PDSURL, h.bsky.Bluesky.Cfg.DID, media.URI)) break } mediaAdd.SupportsStreaming = true From e807e36a1f10bd1fdd2a30276e7e9246d11cb521 Mon Sep 17 00:00:00 2001 From: Astra Date: Wed, 26 Nov 2025 14:14:34 +0000 Subject: [PATCH 31/39] Add ffmpeg to Dockerfile --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 2dadb1b..35b064d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,7 +4,7 @@ WORKDIR /go/src/git.zio.sh/bsky2tg COPY . . RUN apk update && \ - apk add --no-cache git bash && \ + apk add --no-cache git bash ffmpeg && \ go get -d -v ./... && \ go install From 04eec0113e348756993b368edc075941889f8128 Mon Sep 17 00:00:00 2001 From: Astra Date: Wed, 26 Nov 2025 14:15:53 +0000 Subject: [PATCH 32/39] Add ffmpeg to right place --- Dockerfile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 35b064d..7f1a81c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,12 +4,13 @@ WORKDIR /go/src/git.zio.sh/bsky2tg COPY . . RUN apk update && \ - apk add --no-cache git bash ffmpeg && \ + apk add --no-cache git bash && \ go get -d -v ./... && \ go install FROM alpine:latest COPY --from=builder /go/bin/bsky2tg /usr/local/bin/bsky2tg +RUN apk update && apk add --no-cache ffmpeg CMD ["bsky2tg"] \ No newline at end of file From 9616eee62f3d5f297ce27424303eb699f0ecfbf4 Mon Sep 17 00:00:00 2001 From: Astra Date: Tue, 2 Dec 2025 09:32:46 +0000 Subject: [PATCH 33/39] Change link for hashtag search --- bsky/parse.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bsky/parse.go b/bsky/parse.go index a06f131..97a915b 100644 --- a/bsky/parse.go +++ b/bsky/parse.go @@ -361,7 +361,7 @@ func (post *Post) ProcessFacets(aliases []Records) string { link := fmt.Sprintf(`%s`, feature.URI, post.Text[start:end]) result.WriteString(link) case "app.bsky.richtext.facet#tag": - link := fmt.Sprintf(`%s`, feature.Tag, post.Text[start:end]) + link := fmt.Sprintf(`%s`, feature.Tag, post.Text[start:end]) result.WriteString(link) default: result.WriteString(post.Text[start:end]) From 86720ce988a20ec3a07480de1fbc5df88ab0a478 Mon Sep 17 00:00:00 2001 From: Astra Date: Wed, 11 Mar 2026 12:05:21 +0000 Subject: [PATCH 34/39] Handle auth error --- bsky/client.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/bsky/client.go b/bsky/client.go index aa6dc83..8e0bc72 100644 --- a/bsky/client.go +++ b/bsky/client.go @@ -105,8 +105,8 @@ func (b *BSky) GetPDS(handle string) string { func (b *BSky) Auth(authData []string) error { b.Bluesky.Cfg.Handle = authData[0] b.getPDS() - auth, _ := loadAuth() - if auth == nil || auth.AccessJWT == "" { // no auth session found + auth, err := loadAuth() + if err != nil { // no auth session found b.Bluesky.Cfg.AppPassword = authData[1] err := b.Bluesky.CreateSession(b.Bluesky.Cfg) if err != nil { @@ -151,6 +151,9 @@ func loadAuth() (*BlueskyConfig, error) { } var auth *BlueskyConfig - json.Unmarshal(fBytes, &auth) + err = json.Unmarshal(fBytes, &auth) + if err != nil { + return nil, fmt.Errorf("failed to parse auth file: %w", err) + } return auth, nil } From 02cef125234d2ce025a223be59d00a5d99cba954 Mon Sep 17 00:00:00 2001 From: Astra Date: Wed, 11 Mar 2026 12:14:42 +0000 Subject: [PATCH 35/39] RefreshSession code changes --- bsky/bluesky.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/bsky/bluesky.go b/bsky/bluesky.go index 33aa06a..616a2f4 100644 --- a/bsky/bluesky.go +++ b/bsky/bluesky.go @@ -99,7 +99,7 @@ func (bluesky *Bluesky) CreateSession(cfg *BlueskyConfig) error { return errors.New("unable to authenticate, check handle/password") } -func (bluesky *Bluesky) RefreshSession() error { +func (bluesky *Bluesky) RefreshSession() { resp := new(BSkySessionResponse) bluesky.sling.New().Set("Authorization", fmt.Sprintf("Bearer %s", bluesky.Cfg.RefreshJWT)). @@ -109,10 +109,13 @@ func (bluesky *Bluesky) RefreshSession() error { bluesky.Cfg.RefreshJWT = resp.RefreshJWT PersistAuthSession(bluesky.Cfg) bluesky.sling.Set("Authorization", fmt.Sprintf("Bearer %s", bluesky.Cfg.AccessJWT)) - return nil + return + } + if resp.Error != "" { + log.Fatalf("RefreshSession error: %s", resp.Message) } - return bluesky.CreateSession(bluesky.Cfg) + bluesky.CreateSession(bluesky.Cfg) } func (bluesky *Bluesky) CheckSessionValid() { @@ -126,7 +129,7 @@ func (bluesky *Bluesky) CheckSessionValid() { bluesky.sling.New().Set("Authorization", fmt.Sprintf("Bearer %s", bluesky.Cfg.AccessJWT)). Get("/xrpc/app.bsky.actor.getProfile").QueryStruct(params).Receive(resp, resp) - if resp.Error == "ExpiredToken" { + if resp.Error != "" { bluesky.RefreshSession() } } @@ -279,7 +282,6 @@ func (bluesky *Bluesky) FetchPost(did string, rkey string) FetchedPost { }{ URIs: fmt.Sprintf("at://%s/app.bsky.feed.post/%s", did, rkey), } - bluesky.sling.New().Base("https://public.api.bsky.app"). - Get("/xrpc/app.bsky.feed.getPosts").QueryStruct(¶ms).Receive(resp, resp) + bluesky.sling.New().Get("/xrpc/app.bsky.feed.getPosts").QueryStruct(¶ms).Receive(resp, resp) return resp.Posts[0] } From 85fc508aa3f6d11a57941b3ff9f403190927f27a Mon Sep 17 00:00:00 2001 From: Astra Date: Wed, 11 Mar 2026 13:58:29 +0000 Subject: [PATCH 36/39] Update README.md, add LICENSE --- LICENSE | 21 ++++++ README.md | 190 ++++++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 185 insertions(+), 26 deletions(-) create mode 100644 LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..28ae973 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2026 astra.blue + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md index 77f033f..0786164 100644 --- a/README.md +++ b/README.md @@ -1,50 +1,188 @@ -bsky2tg -======= +# bsky2tg -**bsky2tg** will mirror posts from your Bluesky account to a Telegram channel through a bot. It supports creation and deletion of posts on Bluesky but not the other way. +A real-time bridge that forwards Bluesky posts to Telegram. Monitor your Bluesky account and automatically send posts to a Telegram channel with full media support, quote posts, and more. ---- +## Features -### Usage +- šŸ¦‹ **Real-time sync** - Posts appear on Telegram seconds after posting on Bluesky +- šŸ“ø **Full media support** - Images, videos, GIFs (from Tenor) +- šŸ’¬ **Quote posts** - Properly formatted with links to original posts +- āœļø **Edit support** - Updates Telegram message when you edit a Bluesky post +- šŸ—‘ļø **Delete sync** - Removes from Telegram when you delete from Bluesky +- šŸ”— **Rich links** - @mentions, hashtags, and custom aliases converted to clickable links +- ā° **Time filtering** - Ignore old posts and replies if desired +- šŸŽ¬ **Video metadata** - Includes duration, dimensions, and thumbnail -Create a `.env` file with the following: +## Setup -```properties -TG_TOKEN= -TG_CHANNEL_ID= -BSKY_HANDLE= -BSKY_PASSWORD= -``` +### Prerequisites -If you use a different Telegram bot endpoint, you can set it with +- Go 1.21+ +- A Bluesky account +- A Telegram bot and channel -```properties -TG_API_ENDPOINT=https://api.domain.com/bot%s/%s -``` +### Installation -# Podman +1. **Clone the repository** + ```bash + git clone https://git.zio.sh/astra/bsky2tg + cd bsky2tg + ``` +2. **Build the project** + ```bash + go build + ``` + +3. **Set environment variables** + ```bash + export BSKY_HANDLE="your.bsky.handle" + export BSKY_PASSWORD="your-app-password" # NOT your main password + export TG_TOKEN="your-telegram-bot-token" + export TG_CHANNEL_ID="your-channel-id" + ``` + + **Optional:** + ```bash + export TG_API_ENDPOINT="https://api.telegram.org/bot%s/%s" # Custom Telegram API endpoint + export OLDPOSTTIME="1" # Ignore posts older than this many hours (default: 1) + ``` + +4. **Run the daemon** + ```bash + ./bsky2tg + ``` + +## Running with Podman + +Run the bot in a container using Podman: + +### With `.env` file ```bash podman run -it --name bsky2tg_ \ --env-file /path/to/.env \ git.zio.sh/astra/bsky2tg:latest ``` -Or without `.env` file: - +### With environment variables ```bash podman run -it --name bsky2tg_ \ - --env TG_TOKEN= \ - --env TG_CHANNEL_ID= \ - --env BSKY_HANDLE= \ - --env BSKY_PASSWORD= \ + --env TG_TOKEN= \ + --env TG_CHANNEL_ID= \ + --env BSKY_HANDLE= \ + --env BSKY_PASSWORD= \ git.zio.sh/astra/bsky2tg:latest ``` +### Getting Your Credentials -## Bash +**Bluesky App Password:** +- Go to Settings → Privacy and Security → App Passwords +- Create a new app password (NOT your main Bluesky password) +**Telegram Bot Token:** +- Message [@BotFather](https://t.me/BotFather) on Telegram +- Create a new bot with `/newbot` +- Copy the token + +**Telegram Channel ID:** +- Create a channel (can also be private) +- Add your bot as an admin +- Use `@userinfobot` to get the channel ID + +## Usage + +### Daemon Mode +The bot runs continuously and syncs new posts in real-time: ```bash -source .env ./bsky2tg -``` \ No newline at end of file +``` + +### One-Shot Post Sync +Send a specific post to Telegram: +```bash +./bsky2tg -post "https://bsky.app/profile/user.bsky/post/abc123" +``` + +### Delete a Post +Remove a post from Telegram (delete from Bluesky first): +```bash +./bsky2tg -post "https://bsky.app/profile/user.bsky/post/abc123" -delete +``` + +### Ignore Old Posts +Ignore posts created more than 2 hours ago: +```bash +./bsky2tg -oldposttime 2 +``` + +## How It Works + +1. **Authentication** - Logs into Bluesky via ATProto and stores the session +2. **Jetstream Connection** - Subscribes to real-time post events from your account +3. **Post Processing** - Parses posts, extracts media, processes facets (links/mentions) +4. **Telegram Delivery** - Sends formatted messages with media to your channel +5. **Metadata Storage** - Records post mapping (Bluesky → Telegram) for edits/deletes + +## Configuration + +### Post Format + +Posts are sent with this format: +``` +[Post text with @mentions and #hashtags] +— +šŸ¦‹ @your.handle +``` + +Quote posts include the quoted post above in a blockquote. + +### Custom Aliases + +You can set up custom link replacements by creating entries in the `blue.zio.bsky2tg.alias` collection on your PDS. + +## Troubleshooting + +### Auth errors +- Verify `BSKY_HANDLE` and `BSKY_PASSWORD` are correct +- Use an app password, not your main Bluesky password +- Check `auth-session.json` file permissions + +### Posts not syncing +- Ensure the bot is admin in the channel +- Check `TG_CHANNEL_ID` is correct +- Verify Jetstream connection with logs + +### Video errors +- FFmpeg must be installed for video processing +- Check that video file can be read + +## Project Structure + +``` +. +ā”œā”€ā”€ main.go # Event handler, post processing, Telegram sender +ā”œā”€ā”€ bsky/ +│ ā”œā”€ā”€ client.go # Bluesky session management, handle resolution +│ ā”œā”€ā”€ bluesky.go # ATProto API calls (posts, records, sessions) +│ └── parse.go # Post parsing, facet processing +ā”œā”€ā”€ auth-session.json # Stored auth session (auto-created) +└── README.md # This file +``` + +## API Integration + +- **Bluesky ATProto** - Session creation, post fetching, record management +- **Jetstream** - Real-time firehose subscription +- **Telegram Bot API** - Message/media sending, editing, deleting + +## Notes + +- Auth sessions are persisted in `auth-session.json` +- Tokens are automatically refreshed when expired +- Posts are deduplicated to prevent duplicates on sync restart +- Media is fetched from your PDS via blob endpoints + +## License + +See LICENSE file From c9cc325ef7c9fda9865124f78640020e031ed1ef Mon Sep 17 00:00:00 2001 From: Astra Date: Wed, 11 Mar 2026 13:58:38 +0000 Subject: [PATCH 37/39] Code Refactor --- bsky/bluesky.go | 36 ++++++++++++++++-------- bsky/client.go | 75 ++++++++++++++++++++++++++++--------------------- main.go | 17 ++++++----- 3 files changed, 77 insertions(+), 51 deletions(-) diff --git a/bsky/bluesky.go b/bsky/bluesky.go index 616a2f4..4e69a78 100644 --- a/bsky/bluesky.go +++ b/bsky/bluesky.go @@ -10,6 +10,17 @@ import ( "github.com/dghubble/sling" ) +const ( + // URI parsing indices for at:// URIs split by "/" + uriRepoIndex = 2 + uriCollectionIndex = 3 + uriRkeyIndex = 4 + + // Custom collections + PostCollection = "blue.zio.bsky2tg.post" + AliasCollection = "blue.zio.bsky2tg.alias" +) + type BlueskyConfig struct { PDSURL string `json:"pds-url"` Repo string `json:"repo"` @@ -72,10 +83,11 @@ type Link struct { } type Bluesky struct { - Cfg *BlueskyConfig - HttpClient *http.Client - Logger *log.Logger - sling *sling.Sling + Cfg *BlueskyConfig + HttpClient *http.Client + Logger *log.Logger + sling *sling.Sling + publicSling *sling.Sling } func (bluesky *Bluesky) CreateSession(cfg *BlueskyConfig) error { @@ -88,14 +100,14 @@ func (bluesky *Bluesky) CreateSession(cfg *BlueskyConfig) error { } resp := new(BSkySessionResponse) - bluesky.sling.New().Post("/xrpc/com.atproto.server.createSession").BodyJSON(body).ReceiveSuccess(resp) + bluesky.sling.New().Client(bluesky.HttpClient). + Post("/xrpc/com.atproto.server.createSession").BodyJSON(body).ReceiveSuccess(resp) if resp.AccessJWT != "" { cfg.AccessJWT = resp.AccessJWT cfg.RefreshJWT = resp.RefreshJWT return nil } - bluesky.sling.New().Set("Authorization", fmt.Sprintf("Bearer %s", bluesky.Cfg.AccessJWT)) return errors.New("unable to authenticate, check handle/password") } @@ -161,7 +173,7 @@ func (bluesky *Bluesky) CommitTelegramResponse(data *TelegramRecord, rkey string Record TelegramRecord `json:"record"` }{ Repo: bluesky.Cfg.DID, - Collection: "blue.zio.bsky2tg.post", + Collection: PostCollection, RKey: rkey, Record: TelegramRecord{ ChannelID: data.ChannelID, @@ -193,7 +205,7 @@ func (bluesky *Bluesky) GetTelegramData(rkey string) (*TelegramRecord, string) { RKey string `url:"rkey"` }{ Repo: bluesky.Cfg.DID, - Collection: "blue.zio.bsky2tg.post", + Collection: PostCollection, RKey: rkey, } @@ -215,9 +227,9 @@ func (bluesky *Bluesky) GetPost(uri string) *Post { Repo string `url:"repo"` Collection string `url:"collection"` }{ - RKey: args[4], - Repo: args[2], - Collection: args[3], + RKey: args[uriRkeyIndex], + Repo: args[uriRepoIndex], + Collection: args[uriCollectionIndex], } bluesky.sling.New().Get("/xrpc/com.atproto.repo.getRecord").QueryStruct(params).ReceiveSuccess(&post) @@ -249,7 +261,7 @@ func (bluesky *Bluesky) FetchAliases() []Records { Collection string `url:"collection"` }{ Repo: bluesky.Cfg.DID, - Collection: "blue.zio.bsky2tg.alias", + Collection: AliasCollection, } bluesky.sling.New().Get("/xrpc/com.atproto.repo.listRecords").QueryStruct(¶ms).Receive(resp, resp) diff --git a/bsky/client.go b/bsky/client.go index 8e0bc72..6158d00 100644 --- a/bsky/client.go +++ b/bsky/client.go @@ -13,23 +13,28 @@ import ( "github.com/dghubble/sling" ) +const ( + didWebPrefixLen = len("did:web:") + atPrefixLen = len("at://") + httpClientTimeout = 3 * time.Second +) + type BSky struct { Bluesky *Bluesky - DID string } func NewBSky() *BSky { return &BSky{ Bluesky: &Bluesky{ - Cfg: &BlueskyConfig{}, - HttpClient: &http.Client{}, - sling: sling.New().Client(&http.Client{Timeout: time.Second * 3}), + Cfg: &BlueskyConfig{}, + HttpClient: &http.Client{}, + sling: sling.New().Client(&http.Client{Timeout: httpClientTimeout}), + publicSling: sling.New().Base("https://public.api.bsky.app/").Client(&http.Client{Timeout: httpClientTimeout}), }, } } func (b *BSky) ResolveHandle(handle string) (string, error) { - httpClient := &http.Client{Timeout: 3 * time.Second} resp := new(BSkySessionResponse) errResp := &struct { Message string `json:"message"` @@ -40,8 +45,7 @@ func (b *BSky) ResolveHandle(handle string) (string, error) { }{ Handle: handle, } - sling.New().Base("https://public.api.bsky.app/").Client(httpClient). - Get("/xrpc/com.atproto.identity.resolveHandle").QueryStruct(params). + b.Bluesky.publicSling.New().Get("/xrpc/com.atproto.identity.resolveHandle").QueryStruct(params). Receive(resp, errResp) if errResp.Error != "" { @@ -51,54 +55,62 @@ func (b *BSky) ResolveHandle(handle string) (string, error) { return resp.DID, nil } +func parseDIDURL(did string) (*url.URL, error) { + if strings.HasPrefix(did, "did:web:") { + return url.Parse("https://" + did[didWebPrefixLen:] + "/.well-known/did.json") + } else if strings.HasPrefix(did, "did:plc:") { + return url.Parse("https://plc.directory/" + did) + } + return nil, errors.New("DID is not supported") +} + func (b *BSky) getPDS() error { did, _ := b.ResolveHandle(b.Bluesky.Cfg.Handle) - var didURL url.URL - if strings.HasPrefix(did, "did:web:") { - didURL.Host = "https://" + did[8:] - didURL.Path = "/.well-known/did.json" - } else if strings.HasPrefix(did, "did:plc:") { - didURL.Host = "https://plc.directory" - didURL.Path = "/" + did - } else { - return errors.New("DID is not supported") + didURL, err := parseDIDURL(did) + if err != nil { + return err } didResp := new(DIDResponse) - sling.New().Base(didURL.Host).Get(didURL.Path).ReceiveSuccess(didResp) + baseURL := fmt.Sprintf("%s://%s", didURL.Scheme, didURL.Host) + sling.New().Base(baseURL).Get(didURL.Path).ReceiveSuccess(didResp) if didResp.ID == "" { return errors.New("unable to resolve DID") } b.Bluesky.Cfg.DID = didResp.ID - b.Bluesky.Cfg.PDSURL = didResp.Service[0].ServiceEndpoint - b.Bluesky.sling.Base(didResp.Service[0].ServiceEndpoint) + if len(didResp.Service) == 0 { + return errors.New("DID response has no services") + } + + pdsURL := didResp.Service[0].ServiceEndpoint + if pdsURL == "" { + return errors.New("service endpoint is empty") + } + + b.Bluesky.Cfg.PDSURL = pdsURL + b.Bluesky.sling.Base(pdsURL) return nil } func (b *BSky) GetHandleFromDID(did string) (handle string, err error) { - var didURL url.URL - if strings.HasPrefix(did, "did:web:") { - didURL.Host = "https://" + did[8:] - didURL.Path = "/.well-known/did.json" - } else if strings.HasPrefix(did, "did:plc:") { - didURL.Host = "https://plc.directory" - didURL.Path = "/" + did - } else { - return "", errors.New("DID is not supported") + didURL, err := parseDIDURL(did) + if err != nil { + return "", err } didResp := new(DIDResponse) - sling.New().Base(didURL.Host).Get(didURL.Path).ReceiveSuccess(didResp) + baseURL := fmt.Sprintf("%s://%s", didURL.Scheme, didURL.Host) + sling.New().Base(baseURL).Get(didURL.Path).ReceiveSuccess(didResp) if didResp.ID == "" { return "", errors.New("unable to resolve DID") } - return didResp.AlsoKnownAs[0][5:], nil + return didResp.AlsoKnownAs[0][atPrefixLen:], nil } -func (b *BSky) GetPDS(handle string) string { +func (b *BSky) GetPDS() string { return b.Bluesky.Cfg.PDSURL } @@ -118,7 +130,6 @@ func (b *BSky) Auth(authData []string) error { b.Bluesky.Cfg.Cursor = auth.Cursor b.Bluesky.Cfg.AccessJWT = auth.AccessJWT b.Bluesky.Cfg.RefreshJWT = auth.RefreshJWT - // b.RefreshSession() b.Bluesky.CheckSessionValid() } diff --git a/main.go b/main.go index d40afb3..34d2b0b 100644 --- a/main.go +++ b/main.go @@ -97,7 +97,7 @@ func main() { log.Printf("Found post %s in channel %d, deleting", s[2], tgpost.ChannelID) m := tgbotapi.NewDeleteMessages(tgpost.ChannelID, tgpost.MessageID) h.tg.Send(m) - h.bsky.Bluesky.DeleteRecord([]string{s[2], s[1], "blue.zio.bsky2tg.post"}) + h.bsky.Bluesky.DeleteRecord([]string{s[2], s[1], bsky.PostCollection}) } else { log.Printf("Unable to find post %s on PDS", s[2]) } @@ -179,7 +179,7 @@ func (h *handler) HandleEvent(ctx context.Context, event *models.Event) error { if e == "" { m := tgbotapi.NewDeleteMessages(r.ChannelID, r.MessageID) h.tg.Send(m) - h.bsky.Bluesky.DeleteRecord([]string{event.Commit.RKey, event.Did, "blue.zio.bsky2tg.post"}) + h.bsky.Bluesky.DeleteRecord([]string{event.Commit.RKey, event.Did, bsky.PostCollection}) } } @@ -206,6 +206,9 @@ func (h *handler) ProcessPost(event *models.Event) error { isEditedPost = true } + aliases := h.bsky.Bluesky.FetchAliases() + facets := ps.ProcessFacets(aliases) + var captionText string if ps.IsQuotePost() { ownHandle, handleErr := h.bsky.GetHandleFromDID(h.bsky.Bluesky.Cfg.DID) @@ -216,7 +219,7 @@ func (h *handler) ProcessPost(event *models.Event) error { handle, _ := h.bsky.GetHandleFromDID(strings.Split(ps.Embed.Record.Record.URI, "/")[2]) captionText = fmt.Sprintf( quotePostFormat, - ps.ProcessFacets(h.bsky.Bluesky.FetchAliases()), + facets, strings.Split(ps.Embed.Record.Record.URI, "/")[2], strings.Split(ps.Embed.Record.Record.URI, "/")[4], handle, @@ -227,7 +230,7 @@ func (h *handler) ProcessPost(event *models.Event) error { handle, _ := h.bsky.GetHandleFromDID(strings.Split(ps.Embed.Record.URI, "/")[2]) captionText = fmt.Sprintf( quotePostFormat, - ps.ProcessFacets(h.bsky.Bluesky.FetchAliases()), + facets, strings.Split(ps.Embed.Record.URI, "/")[2], strings.Split(ps.Embed.Record.URI, "/")[4], handle, @@ -242,8 +245,8 @@ func (h *handler) ProcessPost(event *models.Event) error { if handleErr != nil { ownHandle = h.bsky.Bluesky.Cfg.Handle } - if ps.ProcessFacets(h.bsky.Bluesky.FetchAliases()) != "" { - captionText = fmt.Sprintf(postFormat, ps.ProcessFacets(h.bsky.Bluesky.FetchAliases()), h.bsky.Bluesky.Cfg.DID, event.Commit.RKey, ownHandle) + if facets != "" { + captionText = fmt.Sprintf(postFormat, facets, h.bsky.Bluesky.Cfg.DID, event.Commit.RKey, ownHandle) } else { captionText = fmt.Sprintf("šŸ¦‹ @%s", h.bsky.Bluesky.Cfg.DID, event.Commit.RKey, ownHandle) } @@ -325,7 +328,7 @@ func (h *handler) ProcessPost(event *models.Event) error { } else { m := tgbotapi.MessageConfig{} if captionText == "" { - m = tgbotapi.NewMessage(cid, fmt.Sprintf(postFormat, ps.ProcessFacets(h.bsky.Bluesky.FetchAliases()), h.bsky.Bluesky.Cfg.DID, event.Commit.RKey, h.bsky.Bluesky.Cfg.Handle)) + m = tgbotapi.NewMessage(cid, fmt.Sprintf(postFormat, facets, h.bsky.Bluesky.Cfg.DID, event.Commit.RKey, h.bsky.Bluesky.Cfg.Handle)) } else { m = tgbotapi.NewMessage(cid, captionText) } From 2675ce1ea0892709d30624204c992aeaec0df364 Mon Sep 17 00:00:00 2001 From: Astra Date: Thu, 12 Mar 2026 08:46:04 +0000 Subject: [PATCH 38/39] escapeHTML --- main.go | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 34d2b0b..fd472a9 100644 --- a/main.go +++ b/main.go @@ -219,7 +219,7 @@ func (h *handler) ProcessPost(event *models.Event) error { handle, _ := h.bsky.GetHandleFromDID(strings.Split(ps.Embed.Record.Record.URI, "/")[2]) captionText = fmt.Sprintf( quotePostFormat, - facets, + escapeHTML(facets), strings.Split(ps.Embed.Record.Record.URI, "/")[2], strings.Split(ps.Embed.Record.Record.URI, "/")[4], handle, @@ -230,7 +230,7 @@ func (h *handler) ProcessPost(event *models.Event) error { handle, _ := h.bsky.GetHandleFromDID(strings.Split(ps.Embed.Record.URI, "/")[2]) captionText = fmt.Sprintf( quotePostFormat, - facets, + escapeHTML(facets), strings.Split(ps.Embed.Record.URI, "/")[2], strings.Split(ps.Embed.Record.URI, "/")[4], handle, @@ -246,7 +246,7 @@ func (h *handler) ProcessPost(event *models.Event) error { ownHandle = h.bsky.Bluesky.Cfg.Handle } if facets != "" { - captionText = fmt.Sprintf(postFormat, facets, h.bsky.Bluesky.Cfg.DID, event.Commit.RKey, ownHandle) + captionText = fmt.Sprintf(postFormat, escapeHTML(facets), h.bsky.Bluesky.Cfg.DID, event.Commit.RKey, ownHandle) } else { captionText = fmt.Sprintf("šŸ¦‹ @%s", h.bsky.Bluesky.Cfg.DID, event.Commit.RKey, ownHandle) } @@ -328,7 +328,7 @@ func (h *handler) ProcessPost(event *models.Event) error { } else { m := tgbotapi.MessageConfig{} if captionText == "" { - m = tgbotapi.NewMessage(cid, fmt.Sprintf(postFormat, facets, h.bsky.Bluesky.Cfg.DID, event.Commit.RKey, h.bsky.Bluesky.Cfg.Handle)) + m = tgbotapi.NewMessage(cid, fmt.Sprintf(postFormat, escapeHTML(facets), h.bsky.Bluesky.Cfg.DID, event.Commit.RKey, h.bsky.Bluesky.Cfg.Handle)) } else { m = tgbotapi.NewMessage(cid, captionText) } @@ -360,6 +360,16 @@ func (h *handler) ProcessPost(event *models.Event) error { return nil } +func escapeHTML(text string) string { + // Escape HTML special characters so they display literally + replacements := strings.NewReplacer( + "&", "&", + "<", "<", + ">", ">", + ) + return replacements.Replace(text) +} + func buildBlobURL(server string, did string, cid string) string { return server + "/xrpc/com.atproto.sync.getBlob?did=" + url.QueryEscape(did) + "&cid=" + cid } From e2faeaac75633f09fe395811829ea5666b371c19 Mon Sep 17 00:00:00 2001 From: Astra Date: Fri, 13 Mar 2026 14:31:54 +0000 Subject: [PATCH 39/39] Fix HTML escaping --- bsky/parse.go | 18 ++++++++++-------- main.go | 18 ++++-------------- 2 files changed, 14 insertions(+), 22 deletions(-) diff --git a/bsky/parse.go b/bsky/parse.go index 97a915b..99bab03 100644 --- a/bsky/parse.go +++ b/bsky/parse.go @@ -3,6 +3,7 @@ package bsky import ( "encoding/json" "fmt" + "html" "sort" "strings" "time" @@ -329,7 +330,7 @@ func (post *Post) ProcessFacets(aliases []Records) string { } if post.Facets == nil { - return post.Text + return html.EscapeString(post.Text) } sort.Slice((*post.Facets), func(i, j int) bool { @@ -338,18 +339,18 @@ func (post *Post) ProcessFacets(aliases []Records) string { var result strings.Builder lastIndex := 0 - // post.Text = html.EscapeString(post.Text) for _, facet := range *post.Facets { start := facet.Index.ByteStart end := facet.Index.ByteEnd - result.WriteString(post.Text[lastIndex:start]) + // Escape HTML in plain text portions + result.WriteString(html.EscapeString(post.Text[lastIndex:start])) for _, feature := range *facet.Features { switch feature.Type { case "app.bsky.richtext.facet#mention": - link := fmt.Sprintf(`%s`, feature.Did, post.Text[start:end]) + link := fmt.Sprintf(`%s`, feature.Did, html.EscapeString(post.Text[start:end])) for _, alias := range aliases { if alias.Value.Subject == feature.Did { link = fmt.Sprintf(`%s`, @@ -358,18 +359,19 @@ func (post *Post) ProcessFacets(aliases []Records) string { } result.WriteString(link) case "app.bsky.richtext.facet#link": - link := fmt.Sprintf(`%s`, feature.URI, post.Text[start:end]) + link := fmt.Sprintf(`%s`, feature.URI, html.EscapeString(post.Text[start:end])) result.WriteString(link) case "app.bsky.richtext.facet#tag": - link := fmt.Sprintf(`%s`, feature.Tag, post.Text[start:end]) + link := fmt.Sprintf(`%s`, feature.Tag, html.EscapeString(post.Text[start:end])) result.WriteString(link) default: - result.WriteString(post.Text[start:end]) + result.WriteString(html.EscapeString(post.Text[start:end])) } } lastIndex = end } - result.WriteString(post.Text[lastIndex:]) + // Escape HTML in the final plain text portion + result.WriteString(html.EscapeString(post.Text[lastIndex:])) return result.String() } diff --git a/main.go b/main.go index fd472a9..34d2b0b 100644 --- a/main.go +++ b/main.go @@ -219,7 +219,7 @@ func (h *handler) ProcessPost(event *models.Event) error { handle, _ := h.bsky.GetHandleFromDID(strings.Split(ps.Embed.Record.Record.URI, "/")[2]) captionText = fmt.Sprintf( quotePostFormat, - escapeHTML(facets), + facets, strings.Split(ps.Embed.Record.Record.URI, "/")[2], strings.Split(ps.Embed.Record.Record.URI, "/")[4], handle, @@ -230,7 +230,7 @@ func (h *handler) ProcessPost(event *models.Event) error { handle, _ := h.bsky.GetHandleFromDID(strings.Split(ps.Embed.Record.URI, "/")[2]) captionText = fmt.Sprintf( quotePostFormat, - escapeHTML(facets), + facets, strings.Split(ps.Embed.Record.URI, "/")[2], strings.Split(ps.Embed.Record.URI, "/")[4], handle, @@ -246,7 +246,7 @@ func (h *handler) ProcessPost(event *models.Event) error { ownHandle = h.bsky.Bluesky.Cfg.Handle } if facets != "" { - captionText = fmt.Sprintf(postFormat, escapeHTML(facets), h.bsky.Bluesky.Cfg.DID, event.Commit.RKey, ownHandle) + captionText = fmt.Sprintf(postFormat, facets, h.bsky.Bluesky.Cfg.DID, event.Commit.RKey, ownHandle) } else { captionText = fmt.Sprintf("šŸ¦‹ @%s", h.bsky.Bluesky.Cfg.DID, event.Commit.RKey, ownHandle) } @@ -328,7 +328,7 @@ func (h *handler) ProcessPost(event *models.Event) error { } else { m := tgbotapi.MessageConfig{} if captionText == "" { - m = tgbotapi.NewMessage(cid, fmt.Sprintf(postFormat, escapeHTML(facets), h.bsky.Bluesky.Cfg.DID, event.Commit.RKey, h.bsky.Bluesky.Cfg.Handle)) + m = tgbotapi.NewMessage(cid, fmt.Sprintf(postFormat, facets, h.bsky.Bluesky.Cfg.DID, event.Commit.RKey, h.bsky.Bluesky.Cfg.Handle)) } else { m = tgbotapi.NewMessage(cid, captionText) } @@ -360,16 +360,6 @@ func (h *handler) ProcessPost(event *models.Event) error { return nil } -func escapeHTML(text string) string { - // Escape HTML special characters so they display literally - replacements := strings.NewReplacer( - "&", "&", - "<", "<", - ">", ">", - ) - return replacements.Replace(text) -} - func buildBlobURL(server string, did string, cid string) string { return server + "/xrpc/com.atproto.sync.getBlob?did=" + url.QueryEscape(did) + "&cid=" + cid }