Categorygithub.com/mattn/go-mastodon
modulepackage
0.0.9
Repository: https://github.com/mattn/go-mastodon.git
Documentation: pkg.go.dev

# README

go-mastodon

Build Status Codecov Go Reference Go Report Card

Usage

Application

package main

import (
	"context"
	"fmt"
	"log"

	"github.com/mattn/go-mastodon"
)

func main() {
	app, err := mastodon.RegisterApp(context.Background(), &mastodon.AppConfig{
		Server:     "https://mstdn.jp",
		ClientName: "client-name",
		Scopes:     "read write follow",
		Website:    "https://github.com/mattn/go-mastodon",
	})
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("client-id    : %s\n", app.ClientID)
	fmt.Printf("client-secret: %s\n", app.ClientSecret)
}

Client

package main

import (
	"context"
	"fmt"
	"log"

	"github.com/mattn/go-mastodon"
)

func main() {
	c := mastodon.NewClient(&mastodon.Config{
		Server:       "https://mstdn.jp",
		ClientID:     "client-id",
		ClientSecret: "client-secret",
	})
	err := c.Authenticate(context.Background(), "your-email", "your-password")
	if err != nil {
		log.Fatal(err)
	}
	timeline, err := c.GetTimelineHome(context.Background(), nil)
	if err != nil {
		log.Fatal(err)
	}
	for i := len(timeline) - 1; i >= 0; i-- {
		fmt.Println(timeline[i])
	}
}

Client with Token

This option lets the user avoid storing login credentials in the application. Instead, the user's Mastodon server provides an access token which is used to authenticate. This token can be stored in the application, but should be guarded.

package main

import (
	"context"
	"fmt"
	"log"
	"net/url"

	"github.com/mattn/go-mastodon"
)

func main() {
	appConfig := &mastodon.AppConfig{
		Server:       "https://stranger.social",
		ClientName:   "client-name",
		Scopes:       "read write follow",
		Website:      "https://github.com/mattn/go-mastodon",
		RedirectURIs: "urn:ietf:wg:oauth:2.0:oob",
	}
	app, err := mastodon.RegisterApp(context.Background(), appConfig)
	if err != nil {
		log.Fatal(err)
	}

	// Have the user manually get the token and send it back to us
	u, err := url.Parse(app.AuthURI)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("Open your browser to \n%s\n and copy/paste the given token\n", u)
	var token string
	fmt.Print("Paste the token here:")
	fmt.Scanln(&token)
	config := &mastodon.Config{
		Server:       "https://stranger.social",
		ClientID:     app.ClientID,
		ClientSecret: app.ClientSecret,
		AccessToken:  token,
	}

	c := mastodon.NewClient(config)

	// Token will be at c.Config.AccessToken
	// and will need to be persisted.
	// Otherwise you'll need to register and authenticate token again.
	err = c.AuthenticateToken(context.Background(), token, "urn:ietf:wg:oauth:2.0:oob")
	if err != nil {
		log.Fatal(err)
	}

	acct, err := c.GetAccountCurrentUser(context.Background())
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("Account is %v\n", acct)

	finalText := "This is the content of my new post!"
	visibility := "public"

	// Post a toot
	toot := mastodon.Toot{
		Status:     finalText,
		Visibility: visibility,
	}
	post, err := c.PostStatus(context.Background(), &toot)

	if err != nil {
		log.Fatalf("%#v\n", err)
	}

	fmt.Printf("My new post is %v\n", post)
}

Status of implementations

  • GET /api/v1/accounts/:id
  • GET /api/v1/accounts/verify_credentials
  • PATCH /api/v1/accounts/update_credentials
  • GET /api/v1/accounts/:id/followers
  • GET /api/v1/accounts/:id/following
  • GET /api/v1/accounts/:id/statuses
  • POST /api/v1/accounts/:id/follow
  • POST /api/v1/accounts/:id/unfollow
  • GET /api/v1/accounts/:id/block
  • GET /api/v1/accounts/:id/unblock
  • GET /api/v1/accounts/:id/mute
  • GET /api/v1/accounts/:id/unmute
  • GET /api/v1/accounts/:id/lists
  • GET /api/v1/accounts/relationships
  • GET /api/v1/accounts/search
  • GET /api/v1/apps/verify_credentials
  • GET /api/v1/bookmarks
  • POST /api/v1/apps
  • GET /api/v1/blocks
  • GET /api/v1/conversations
  • DELETE /api/v1/conversations/:id
  • POST /api/v1/conversations/:id/read
  • GET /api/v1/favourites
  • GET /api/v1/filters
  • POST /api/v1/filters
  • GET /api/v1/filters/:id
  • PUT /api/v1/filters/:id
  • DELETE /api/v1/filters/:id
  • GET /api/v1/follow_requests
  • POST /api/v1/follow_requests/:id/authorize
  • POST /api/v1/follow_requests/:id/reject
  • GET /api/v1/followed_tags
  • POST /api/v1/follows
  • GET /api/v1/instance
  • GET /api/v1/instance/activity
  • GET /api/v1/instance/peers
  • GET /api/v1/lists
  • GET /api/v1/lists/:id/accounts
  • GET /api/v1/lists/:id
  • POST /api/v1/lists
  • PUT /api/v1/lists/:id
  • DELETE /api/v1/lists/:id
  • POST /api/v1/lists/:id/accounts
  • DELETE /api/v1/lists/:id/accounts
  • POST /api/v1/media
  • GET /api/v1/mutes
  • GET /api/v1/notifications
  • GET /api/v1/notifications/:id
  • POST /api/v1/notifications/:id/dismiss
  • POST /api/v1/notifications/clear
  • POST /api/v1/push/subscription
  • GET /api/v1/push/subscription
  • PUT /api/v1/push/subscription
  • DELETE /api/v1/push/subscription
  • GET /api/v1/reports
  • POST /api/v1/reports
  • GET /api/v2/search
  • GET /api/v1/statuses/:id
  • GET /api/v1/statuses/:id/context
  • GET /api/v1/statuses/:id/card
  • GET /api/v1/statuses/:id/history
  • GET /api/v1/statuses/:id/reblogged_by
  • GET /api/v1/statuses/:id/source
  • GET /api/v1/statuses/:id/favourited_by
  • POST /api/v1/statuses
  • PUT /api/v1/statuses/:id
  • DELETE /api/v1/statuses/:id
  • POST /api/v1/statuses/:id/reblog
  • POST /api/v1/statuses/:id/unreblog
  • POST /api/v1/statuses/:id/favourite
  • POST /api/v1/statuses/:id/unfavourite
  • POST /api/v1/statuses/:id/bookmark
  • POST /api/v1/statuses/:id/unbookmark
  • GET /api/v1/timelines/home
  • GET /api/v1/timelines/public
  • GET /api/v1/timelines/tag/:hashtag
  • GET /api/v1/timelines/list/:id
  • GET /api/v1/streaming/user
  • GET /api/v1/streaming/public
  • GET /api/v1/streaming/hashtag?tag=:hashtag
  • GET /api/v1/streaming/hashtag/local?tag=:hashtag
  • GET /api/v1/streaming/list?list=:list_id
  • GET /api/v1/streaming/direct
  • GET /api/v1/endorsements

Installation

go install github.com/mattn/go-mastodon@latest

License

MIT

Author

Yasuhiro Matsumoto (a.k.a. mattn)

# Functions

Base64Encode returns the base64 data URI format string of the file.
Base64EncodeFileName returns the base64 data URI format string of the file with the file name.
NewClient returns a new mastodon API client.
RegisterApp returns the mastodon application.
String is a helper function to get the pointer value of a string.

# Constants

Convenience constants for Toot.Visibility.
Convenience constants for Toot.Visibility.
Convenience constants for Toot.Visibility.
Convenience constants for Toot.Visibility.

# Structs

Account holds information for a mastodon account.
AccountSource is a Mastodon account profile field.
No description provided by the author
AppConfig is a setting for registering applications.
Application is a mastodon application.
ApplicationVerification is mastodon application.
Attachment hold information for attachment.
AttachmentMeta holds information for attachment metadata.
AttachmentSize holds information for attatchment size.
Card holds information for a mastodon card.
Client is a API client for mastodon.
Config is a setting for access mastodon APIs.
Context holds information for a mastodon context.
Conversation holds information for a mastodon conversation.
DeleteEvent is a struct for passing deletion event to app.
Emoji hold information for CustomEmoji.
ErrorEvent is a struct for passing errors to app.
Field is a Mastodon account profile field.
Filter is metadata for a filter of users.
FollowedTag is a Hash Tag followed by the user.
History is the history of a followed tag.
History hold information for history.
Instance holds information for a mastodon instance.
InstanceConfig holds configuration accessible for clients.
InstanceStats holds information for mastodon instance stats.
List is metadata for a list of users.
Media is struct to hold media.
Mention hold information for mention.
Notification holds information for a mastodon notification.
NotificationEvent is a struct for passing notification event to app.
Pagination is a struct for specifying the get range.
Poll holds information for mastodon polls.
Poll holds information for a mastodon poll option.
Profile is a struct for updating profiles.
No description provided by the author
No description provided by the author
Relationship holds information for relationship to the account.
Report holds information for a mastodon report.
Results hold information for search result.
ScheduledStatus holds information returned when ScheduledAt is set on a status.
Source holds source properties so a status can be edited.
Status is struct to hold status.
StatusHistory is a struct to hold status history data.
Stream is a struct of data that flows in streaming.
Tag hold information for tag.
Toot is a struct to post status.
TootPoll holds information for creating a poll in Toot.
UnixTimeString represents a time in a Unix Epoch string.
UpdateEditEvent is a struct for passing status edit event to app.
UpdateEvent is a struct for passing status event to app.
WeeklyActivity holds information for mastodon weekly activity.
WSClient is a WebSocket client.

# Interfaces

Event is an interface passing events to app.
No description provided by the author

# Type aliases

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author