add bluesky post
This commit is contained in:
64
output/bluesky.go
Normal file
64
output/bluesky.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package output
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/bluesky-social/indigo/api/atproto"
|
||||
"github.com/bluesky-social/indigo/api/bsky"
|
||||
lexutil "github.com/bluesky-social/indigo/lex/util"
|
||||
"github.com/bluesky-social/indigo/util"
|
||||
"github.com/bluesky-social/indigo/xrpc"
|
||||
)
|
||||
|
||||
type blueskyoutput struct {
|
||||
identifier string
|
||||
password string
|
||||
client *xrpc.Client
|
||||
}
|
||||
|
||||
func BlueskyOutput(identifier string, password string) *blueskyoutput {
|
||||
blueskyoutput := &blueskyoutput{
|
||||
identifier: identifier,
|
||||
password: password,
|
||||
}
|
||||
|
||||
cli := &xrpc.Client{
|
||||
Host: "https://bsky.social",
|
||||
}
|
||||
|
||||
input := &atproto.ServerCreateSession_Input{
|
||||
Identifier: blueskyoutput.identifier,
|
||||
Password: blueskyoutput.password,
|
||||
}
|
||||
|
||||
output, err := atproto.ServerCreateSession(context.TODO(), cli, input)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
cli.Auth = &xrpc.AuthInfo{
|
||||
AccessJwt: output.AccessJwt,
|
||||
RefreshJwt: output.RefreshJwt,
|
||||
Handle: output.Handle,
|
||||
Did: output.Did,
|
||||
}
|
||||
|
||||
blueskyoutput.client = cli
|
||||
|
||||
return blueskyoutput
|
||||
}
|
||||
|
||||
func (bo *blueskyoutput) Write(str string) error {
|
||||
Recordinput := &atproto.RepoCreateRecord_Input{
|
||||
Collection: "app.bsky.feed.post",
|
||||
Repo: bo.client.Auth.Did, // "matope.bsky.social" のDID
|
||||
Record: &lexutil.LexiconTypeDecoder{&bsky.FeedPost{
|
||||
Text: str,
|
||||
CreatedAt: time.Now().Format(util.ISO8601),
|
||||
Langs: []string{"ja"},
|
||||
}},
|
||||
}
|
||||
_, recerr := atproto.RepoCreateRecord(context.TODO(), bo.client, Recordinput)
|
||||
return recerr
|
||||
}
|
||||
Reference in New Issue
Block a user