attach image

This commit is contained in:
2026-07-03 10:18:39 +09:00
parent 62cff6355b
commit c579ae6980
6 changed files with 197 additions and 14 deletions

View File

@@ -1,8 +1,9 @@
package output
import (
"bytes"
"context"
"log"
"fmt"
"time"
"github.com/bluesky-social/indigo/api/atproto"
@@ -26,7 +27,7 @@ func BlueskyOutput(identifier string, password string) *blueskyoutput {
return blueskyoutput
}
func (bo *blueskyoutput) Write(str string) error {
func (bo *blueskyoutput) Write(str string, images []Image) error {
cli := &xrpc.Client{
Host: "https://bsky.social",
}
@@ -38,7 +39,7 @@ func (bo *blueskyoutput) Write(str string) error {
output, err := atproto.ServerCreateSession(context.TODO(), cli, input)
if err != nil {
log.Fatal(err)
return err
}
cli.Auth = &xrpc.AuthInfo{
AccessJwt: output.AccessJwt,
@@ -47,14 +48,35 @@ func (bo *blueskyoutput) Write(str string) error {
Did: output.Did,
}
post := &bsky.FeedPost{
Text: str,
CreatedAt: time.Now().Format(util.ISO8601),
Langs: []string{"ja"},
}
if len(images) > 0 {
embedimages := make([]*bsky.EmbedImages_Image, 0, len(images))
for _, img := range images {
blob, err := atproto.RepoUploadBlob(context.TODO(), cli, bytes.NewReader(img.Data))
if err != nil {
return fmt.Errorf("uploading %s: %w", img.Filename, err)
}
embedimages = append(embedimages, &bsky.EmbedImages_Image{
Alt: img.Filename,
Image: blob.Blob,
})
}
post.Embed = &bsky.FeedPost_Embed{
EmbedImages: &bsky.EmbedImages{
Images: embedimages,
},
}
}
Recordinput := &atproto.RepoCreateRecord_Input{
Collection: "app.bsky.feed.post",
Repo: cli.Auth.Did, // "matope.bsky.social" のDID
Record: &lexutil.LexiconTypeDecoder{&bsky.FeedPost{
Text: str,
CreatedAt: time.Now().Format(util.ISO8601),
Langs: []string{"ja"},
}},
Record: &lexutil.LexiconTypeDecoder{Val: post},
}
_, recerr := atproto.RepoCreateRecord(context.TODO(), cli, Recordinput)