Test mastodon posting

This commit is contained in:
idanoo 2023-01-30 11:54:25 +13:00
parent aec56e7352
commit 3aeff25285
Signed by: idanoo
GPG key ID: 387387CDBC02F132
8 changed files with 78 additions and 2 deletions

View file

@ -1 +1,43 @@
package gomastodonstats
import (
"context"
"fmt"
"log"
"github.com/mattn/go-mastodon"
)
func postToMastodon(metrics []metric) {
if MASTODON_INSTANCE_URL == "" {
log.Printf("Skipping posting to mastodon. Missing configuration")
return
}
c := mastodon.NewClient(&mastodon.Config{
Server: fmt.Sprintf("https://%s", MASTODON_INSTANCE_URL),
ClientID: "client-id",
ClientSecret: "client-secret",
})
err := c.Authenticate(context.Background(), MASTODON_USERNAME, MASTODON_PASSWORD)
if err != nil {
log.Println("Invalid mastodon credentials")
return
}
// Build status
startOfDay := getStartofDay()
msg := fmt.Sprintf("Stats for %s\n\n", startOfDay.String())
for _, m := range metrics {
msg = msg + getPrintableString(m) + "\n"
}
toot := mastodon.Toot{
Status: msg,
}
_, err = c.PostStatus(context.Background(), &toot)
if err != nil {
log.Print(err)
}
}