bskybot

package
v1.11.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 19, 2025 License: CC0-1.0 Imports: 17 Imported by: 0

README

gobot-bsky

Gobot-bsky - a simple GO lib to write Bluesky bots

Usage example

Has to provide:

  • a handle - example bluesky handle: "example.bsky.social"
  • an apikey - is used for authetication and the retrieval of the access token and refresh token. To create a new one: Settings --> App Passwords
  • the server (PDS) - the Bluesky's "PDS Service" is bsky.social.
import 	gobot "github.com/danrusei/gobot-bsky"

func main() {

	godotenv.Load()
	handle := os.Getenv("HANDLE")
	apikey := os.Getenv("APIKEY")
	server := "https://bsky.social"

	ctx := context.Background()

	agent := gobot.NewAgent(ctx, server, handle, apikey)
	agent.Connect(ctx)

	// Facets Section
	// =======================================
	// Facet_type coulf be Facet_Link, Facet_Mention or Facet_Tag
	// based on the selected type it expect the second argument to be URI, DID, or TAG
	// the last function argument is the text, part of the original text that is modifiend in Richtext

	post1, err := gobot.NewPostBuilder("Hello to Bluesky, the coolest open social network").
		WithFacet(gobot.Facet_Link, "https://docs.bsky.app/", "Bluesky").
		WithFacet(gobot.Facet_Tag, "bsky", "open social").
		Build()
	if err != nil {
		fmt.Printf("Got error: %v", err)
	}

	cid1, uri1, err := agent.PostToFeed(ctx, post1)
	if err != nil {
		fmt.Printf("Got error: %v", err)
	} else {
		fmt.Printf("Succes: Cid = %v , Uri = %v", cid1, uri1)
	}

	// Embed Links section
	// =======================================

	u, err := url.Parse("https://go.dev/")
	if err != nil {
		log.Fatalf("Parse error, %v", err)
	}
	post2, err := gobot.NewPostBuilder("Hello to Go on Bluesky").
		WithExternalLink("Go Programming Language", *u, "Build simple, secure, scalable systems with Go").
		Build()
	if err != nil {
		fmt.Printf("Got error: %v", err)
	}

	cid2, uri2, err := agent.PostToFeed(ctx, post2)
	if err != nil {
		fmt.Printf("Got error: %v", err)
	} else {
		fmt.Printf("Succes: Cid = %v , Uri = %v", cid2, uri2)
	}

	// Embed Images section
	// =======================================
	images := []gobot.Image{}

	url1, err := url.Parse("https://www.freecodecamp.org/news/content/images/2021/10/golang.png")
	if err != nil {
		log.Fatalf("Parse error, %v", err)
	}
	images = append(images, gobot.Image{
		Title: "Golang",
		Uri:   *url1,
	})

	blobs, err := agent.UploadImages(ctx, images...)
	if err != nil {
		log.Fatalf("Parse error, %v", err)
	}

	post3, err := gobot.NewPostBuilder("Gobot-bsky - a simple golang lib to write Bluesky bots").
		WithImages(blobs, images).
		Build()
	if err != nil {
		fmt.Printf("Got error: %v", err)
	}

	cid3, uri3, err := agent.PostToFeed(ctx, post3)
	if err != nil {
		fmt.Printf("Got error: %v", err)
	} else {
		fmt.Printf("Succes: Cid = %v , Uri = %v", cid3, uri3)
	}

}

The results of running the above code

Content generated with gobot-bsky

Documentation

Index

Constants

This section is empty.

Variables

View Source
var FeedPost_Embed appbsky.FeedPost_Embed

Functions

This section is empty.

Types

type BskyAgent

type BskyAgent struct {
	// contains filtered or unexported fields
}

Wrapper over the atproto xrpc transport

func NewAgent

func NewAgent(ctx context.Context, server string, handle string, apikey string) BskyAgent

Creates new BlueSky Agent

func (*BskyAgent) Client

func (c *BskyAgent) Client() *xrpc.Client

func (*BskyAgent) Connect

func (c *BskyAgent) Connect(ctx context.Context) error

Connect and Authenticate to the provided Personal Data Server, default is Bluesky PDS No need to refresh the access token if the bot script will be executed based on the cron job

func (*BskyAgent) PostToFeed

func (c *BskyAgent) PostToFeed(ctx context.Context, post appbsky.FeedPost) (string, string, error)

Post to social app

func (*BskyAgent) UploadImages

func (c *BskyAgent) UploadImages(ctx context.Context, images ...Image) ([]lexutil.LexBlob, error)

type Embed

type Embed struct {
	Link           Link
	Images         []Image
	UploadedImages []lexutil.LexBlob
}

type Facet

type Facet struct {
	Ftype   Facet_Type
	Value   string
	T_facet string
}

type Facet_Type

type Facet_Type int
const (
	Facet_Link Facet_Type = iota + 1
	Facet_Mention
	Facet_Tag
)

func (Facet_Type) String

func (f Facet_Type) String() string

type Image

type Image struct {
	Title string
	Uri   url.URL
}
type Link struct {
	Title       string
	Uri         url.URL
	Description string
}

type PostBuilder

type PostBuilder struct {
	Text  string
	Facet []Facet
	Embed Embed
	Time  time.Time
	Reply *appbsky.FeedPost_ReplyRef
}

construct the post

func NewPostBuilder

func NewPostBuilder(text string) PostBuilder

Create a simple post with text

func (PostBuilder) AtTime

func (pb PostBuilder) AtTime(t time.Time) PostBuilder

func (PostBuilder) Build

func (pb PostBuilder) Build() (appbsky.FeedPost, error)

Build the request

func (PostBuilder) InReplyTo

func (pb PostBuilder) InReplyTo(post appbsky.FeedPost, actorID, cid, rkey string) PostBuilder

Create a post in reply to another post

func (pb PostBuilder) WithExternalLink(title string, link url.URL, description string) PostBuilder

Create a Post with external links

func (PostBuilder) WithFacet

func (pb PostBuilder) WithFacet(ftype Facet_Type, value string, text string) PostBuilder

Create a Richtext Post with facests

func (PostBuilder) WithImages

func (pb PostBuilder) WithImages(blobs []lexutil.LexBlob, images []Image) PostBuilder

Create a Post with images

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL