Skip to content

Instantly share code, notes, and snippets.

@JonathanMH
Created December 2, 2022 09:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JonathanMH/698f8756216a7b202eb6a10e5640ca7c to your computer and use it in GitHub Desktop.
Save JonathanMH/698f8756216a7b202eb6a10e5640ca7c to your computer and use it in GitHub Desktop.
Go mastodon bot
text = "The official Twitter account for Fall Guys released a \"human for scale\" illustration featuring their playable characters. Fall Guys are all 183cm (6ft) by the way."
tags = ['FallGuys']
assets = ['fallguys-scale.jpeg', 'fallguys-eyes.png']
# alt texts for images
assetsAlts = ['A Fall Guy next to human', 'Fall Guy eyes and skull']
credits = ['https://www.artstation.com/tudormorris']
package main
import (
"fmt"
"os"
"github.com/joho/godotenv"
)
func readConfigFromENV() map[string]string {
var envs = make(map[string]string)
envs["MASTODON_SERVER"] = os.Getenv("MASTODON_SERVER")
envs["APP_CLIENT_ID"] = os.Getenv("APP_CLIENT_ID")
envs["APP_CLIENT_SECRET"] = os.Getenv("APP_CLIENT_SECRET")
envs["APP_USER"] = os.Getenv("APP_USER")
envs["APP_PASSWORD"] = os.Getenv("APP_PASSWORD")
return envs
}
func GetConfig() (map[string]string, error) {
if os.Getenv("APP_ENV") != "production" {
envs, error := godotenv.Read(".env")
if error != nil {
fmt.Println("Error loading .env file")
}
return envs, error
} else {
envs := readConfigFromENV()
return envs, nil
}
}
package main
import (
"context"
"fmt"
"log"
"os"
"github.com/mattn/go-mastodon"
)
var config map[string]string
func main() {
post, err := LoadPost()
if err != nil {
fmt.Printf("No post found %v\n", err)
return
}
envs, error := GetConfig()
if error != nil {
log.Fatalf("Error loading .env or ENV: %v", error)
}
c := mastodon.NewClient(&mastodon.Config{
Server: envs["MASTODON_SERVER"],
ClientID: envs["APP_CLIENT_ID"],
ClientSecret: envs["APP_CLIENT_SECRET"],
})
error = c.Authenticate(context.Background(), envs["APP_USER"], envs["APP_PASSWORD"])
if err != nil {
log.Fatalf("Error authenticating %v", err)
}
fmt.Printf("%v%v\n", post, err)
var media []mastodon.Media
for i := len(post.Assets) - 1; i >= 0; i-- {
file, aerr := os.Open("./assets/" + post.Assets[i])
if aerr != nil {
log.Fatalf("Error reading attachment %v", aerr)
}
fmt.Println(post.Assets[i])
if len(post.AssetsAlts) > 0 && len(post.AssetsAlts) >= i {
fmt.Println("got description")
// append with description
media = append(media, mastodon.Media{File: file, Description: post.AssetsAlts[i]})
} else {
// append without description
media = append(media, mastodon.Media{File: file})
}
}
var attachmentIDs []mastodon.ID
for i := len(media) - 1; i >= 0; i-- {
currentA, err := c.UploadMediaFromMedia(context.Background(), &media[i])
if err != nil {
log.Fatalf("Whoops attachment: %v", err)
}
fmt.Printf("%v", currentA)
attachmentIDs = append(attachmentIDs, currentA.ID)
}
toot := mastodon.Toot{
Status: EnrichPostText(post),
MediaIDs: attachmentIDs,
}
status, err := c.PostStatus(context.Background(), &toot)
fmt.Printf("%+v", status)
}
package main
import (
"fmt"
"os"
"time"
"github.com/BurntSushi/toml"
)
type Post struct {
Text string
Tags []string
Assets []string
AssetsAlts []string
Credits []string
}
func LoadPost() (Post, error) {
var post Post
currentTime := time.Now()
postDateName := fmt.Sprintf("date-posts/%s/%s.toml", currentTime.Format("2006"), currentTime.Format("2006-01-02"))
_, fileExistErr := os.Stat("./" + postDateName)
if fileExistErr == nil {
fmt.Printf("File exists\n")
} else {
fmt.Printf("File does not exist\n")
return post, fileExistErr
}
_, tomlError := toml.DecodeFile(postDateName, &post)
if tomlError == nil {
fmt.Printf("TOML file reading/decoding error: %v\n", tomlError)
}
return post, nil
}
func EnrichPostText(post Post) string {
tootWithTags := appendTags(post.Text, post.Tags)
tootWithCredits := appendCredits(tootWithTags, post.Credits)
return tootWithCredits
}
func appendTags(tootText string, tags []string) string {
tagsLine := "\n"
if len(tags) != 0 {
for i := 0; i < len(tags); i++ {
tagsLine = tagsLine + "#" + tags[i] + " "
}
}
return tootText + tagsLine
}
func appendCredits(tootText string, credits []string) string {
creditsLine := "\nCredits: "
if len(credits) != 0 {
for i := 0; i < len(credits); i++ {
creditsLine = creditsLine + credits[i] + " "
}
}
return tootText + creditsLine
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment