go-mastodon-matrix-webhooks/main.go

67 lines
1.3 KiB
Go
Raw Normal View History

2022-11-20 04:36:31 +00:00
package main
2022-11-20 04:44:58 +00:00
import (
"encoding/json"
"log"
"net/http"
"os"
"github.com/joho/godotenv"
)
var MATRIX_WEBHOOK_URL string
var MATRIX_WEBHOOK_API_KEY string
var MATRIX_CHANNEL string
var PORT string
func init() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
MATRIX_WEBHOOK_URL = os.Getenv("MATRIX_WEBHOOK_URL")
if MATRIX_WEBHOOK_URL == "" {
log.Fatal("MATRIX_WEBHOOK_URL empty or invalid")
}
MATRIX_WEBHOOK_API_KEY = os.Getenv("MATRIX_WEBHOOK_API_KEY")
if MATRIX_WEBHOOK_API_KEY == "" {
log.Fatal("MATRIX_WEBHOOK_API_KEY empty or invalid")
}
MATRIX_CHANNEL = os.Getenv("MATRIX_CHANNEL")
if MATRIX_CHANNEL == "" {
log.Fatal("MATRIX_CHANNEL empty or invalid")
}
PORT = os.Getenv("PORT")
if PORT == "" {
log.Fatal("PORT empty or invalid")
}
}
2022-11-20 04:36:31 +00:00
func main() {
2022-11-20 04:44:58 +00:00
http.HandleFunc("/", handler)
if err := http.ListenAndServe(":"+PORT, nil); err != nil {
log.Fatal(err)
}
}
func handler(w http.ResponseWriter, r *http.Request) {
if r.Body != nil {
2022-11-20 04:52:34 +00:00
var i IdentifyingRequest
2022-11-20 04:55:05 +00:00
err := json.NewDecoder(r.Body).Decode(&i)
2022-11-20 04:52:34 +00:00
if err != nil {
2022-11-20 04:56:08 +00:00
log.Println(err.Error())
2022-11-20 04:52:34 +00:00
return
}
if i.Event == "report.created" {
log.Println("New report!")
} else if i.Event == "account.created" {
log.Println("New account!")
}
2022-11-20 04:44:58 +00:00
}
2022-11-20 04:36:31 +00:00
}