commit cd868e44b1b8517c1f595e61d53a15a16e91f5b1 Author: fade Date: Mon Aug 15 12:24:32 2022 +0300 init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..540ce7b --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +go.mod +go.sum +mastodon-group-bot \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..6ffd52c --- /dev/null +++ b/README.md @@ -0,0 +1,35 @@ +# Mastodon group bot + +This is a bot which implements group functionality in Mastodon. + +# Configuration + +The bot is configured in a JSON file that looks like this: + +``` +{ + "Server": "https://example.com", + "ClientID": "0000000000000000000000000000000000000000000", + "ClientSecret": "0000000000000000000000000000000000000000000", + "AccessToken": "0000000000000000000000000000000000000000000", + "WelcomeMessage": "We have a new member in our group. Please love and favor" +} +``` + +# Building + +``` +go mod init mastodon-group-bot + +go mod tidy + +go build +``` + +# Usage + +``` +Usage of mastodon-group-bot: + -config string + Path to config (default "config.json") +``` \ No newline at end of file diff --git a/config.json b/config.json new file mode 100644 index 0000000..9b21f67 --- /dev/null +++ b/config.json @@ -0,0 +1,7 @@ +{ + "Server": "https://example.com", + "ClientID": "0000000000000000000000000000000000000000000", + "ClientSecret": "0000000000000000000000000000000000000000000", + "AccessToken": "0000000000000000000000000000000000000000000", + "WelcomeMessage": "We have a new member in our group. Please love and favor" +} \ No newline at end of file diff --git a/main.go b/main.go new file mode 100644 index 0000000..b31ed46 --- /dev/null +++ b/main.go @@ -0,0 +1,97 @@ +package main + +import ( + "context" + "encoding/json" + "flag" + "fmt" + "log" + "os" + + "github.com/mattn/go-mastodon" +) + +func main() { + // Parse args + ConfPath := flag.String("config", "config.json", "Path to config") + flag.Parse() + + // Reading config + type Config struct { + Server string `json:"Server"` + ClientID string `json:"ClientID"` + ClientSecret string `json:"ClientSecret"` + AccessToken string `json:"AccessToken"` + WelcomeMessage string `json:"WelcomeMessage"` + } + + data, err := os.ReadFile(*ConfPath) + if err != nil { + log.Fatal(err) + } + + var Conf Config + json.Unmarshal(data, &Conf) + + c := mastodon.NewClient(&mastodon.Config{ + Server: Conf.Server, + ClientID: Conf.ClientID, + ClientSecret: Conf.ClientSecret, + AccessToken: Conf.AccessToken, + }) + + ctx := context.Background() + events, err := c.StreamingUser(ctx) + if err != nil { + log.Fatal(err) + } + + my_account, _ := c.GetAccountCurrentUser(ctx) + followers, _ := c.GetAccountFollowers(ctx, my_account.ID, &mastodon.Pagination{Limit: 60}) + signed := false + + // Run bot + for { + notifEvent, ok := (<-events).(*mastodon.NotificationEvent) + if !ok { + continue + } + + notif := notifEvent.Notification + + // Posting function + postToot := func(toot string, vis string) error { + conToot := mastodon.Toot{ + Status: toot, + Visibility: vis, + } + _, err := c.PostStatus(ctx, &conToot) + return err + } + + // New subscriber + if notif.Type == "follow" { + var message = fmt.Sprintf("%s @%s", Conf.WelcomeMessage, notif.Account.Acct) + postToot(message, "public") + } + + // Reblog toot + if notif.Type == "mention" { + sender := notif.Status.Account.Acct + + // Subscription check + for i := 0; i < len(followers); i++ { + if sender == string(followers[i].Acct) { + signed = true + } + } + + if signed { + c.Reblog(ctx, notif.Status.ID) + } else { + var message = fmt.Sprintf("@%s%s", notif.Account.Acct, ", you are not subscribed!") + postToot(message, "direct") + } + } + } +}