mirror of
https://github.com/idanoo/go-mastodon-stats
synced 2025-07-02 14:22:19 +00:00
Test mastodon posting
This commit is contained in:
parent
aec56e7352
commit
3aeff25285
8 changed files with 78 additions and 2 deletions
|
@ -14,6 +14,10 @@ var (
|
|||
MATRIX_WEBHOOK_API_KEY string
|
||||
MATRIX_WEBHOOK_CHANNEL string
|
||||
|
||||
MASTODON_INSTANCE_URL string
|
||||
MASTODON_USERNAME string
|
||||
MASTODON_PASSWORD string
|
||||
|
||||
// UserCount metric name
|
||||
METRICNAME_USERCOUNT = "userCount"
|
||||
|
||||
|
|
|
@ -15,4 +15,6 @@ func Run() {
|
|||
updatedMetrics := persistMetrics(metrics)
|
||||
|
||||
sendToMatrix(updatedMetrics)
|
||||
|
||||
postToMastodon(updatedMetrics)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,6 +4,8 @@ import (
|
|||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"golang.org/x/text/cases"
|
||||
)
|
||||
|
||||
// Stores out metric/row data
|
||||
|
@ -114,5 +116,5 @@ func getUserCounts() ([]metric, error) {
|
|||
}
|
||||
|
||||
func getPrintableString(m metric) string {
|
||||
return fmt.Sprintf("%s: %d", m.Service, m.MetricValue)
|
||||
return fmt.Sprintf("%s: %d", cases.Title(m.Service), m.MetricValue)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue