go-mastodon-matrix-webhooks/main.go

157 lines
3.2 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 (
2022-11-20 05:12:08 +00:00
"bytes"
2022-11-20 04:44:58 +00:00
"encoding/json"
2022-11-20 05:11:08 +00:00
"fmt"
2022-11-20 04:44:58 +00:00
"log"
"net/http"
"os"
2022-11-20 05:11:08 +00:00
"github.com/ip2location/ip2location-go/v9"
2022-11-20 04:44:58 +00:00
"github.com/joho/godotenv"
)
var MATRIX_WEBHOOK_URL string
var MATRIX_WEBHOOK_API_KEY string
var MATRIX_CHANNEL string
var PORT string
2022-11-20 05:11:08 +00:00
var IP2LOCATION_API_KEY string
2022-11-20 04:44:58 +00:00
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 05:11:08 +00:00
IP2LOCATION_API_KEY = os.Getenv("IP2LOCATION_API_KEY")
2022-11-20 04:44:58 +00:00
}
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)
}
}
2022-11-20 05:11:08 +00:00
// Handle requests
2022-11-20 04:44:58 +00:00
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" {
2022-11-20 05:11:08 +00:00
var report MastodonReportEvent
err := json.NewDecoder(r.Body).Decode(&report)
if err != nil {
log.Println(err.Error())
return
}
2022-11-20 05:12:08 +00:00
err = sendWebhook("New report!")
if err != nil {
log.Println(err.Error())
return
}
2022-11-20 04:52:34 +00:00
} else if i.Event == "account.created" {
2022-11-20 05:11:08 +00:00
var account MastodonSignUpEvent
err := json.NewDecoder(r.Body).Decode(&account)
if err != nil {
log.Println(err.Error())
return
}
country := ipLookup(account.Object.IP)
2022-11-20 05:12:08 +00:00
err = sendWebhook(fmt.Sprintf("*New Signup* %s has joined from %s", account.Object.Username, country))
if err != nil {
log.Println(err.Error())
return
}
2022-11-20 04:52:34 +00:00
}
2022-11-20 04:44:58 +00:00
}
2022-11-20 04:36:31 +00:00
}
2022-11-20 05:11:08 +00:00
// sendWebhook - takes msg, sends to matrix
func sendWebhook(msgText string) error {
log.Println(msgText)
data := MatrixWebhook{
Key: MATRIX_WEBHOOK_API_KEY,
}
data.Body = msgText
b, err := json.Marshal(data)
if err != nil {
return err
}
2022-11-20 05:12:08 +00:00
req, err := http.NewRequest("POST", MATRIX_WEBHOOK_URL+"/"+MATRIX_CHANNEL, bytes.NewBuffer(b))
if err != nil {
return err
}
2022-11-20 05:11:08 +00:00
req.Header.Set("X-Custom-Header", "myvalue")
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
return nil
}
// Lookup to country!
func ipLookup(ip string) string {
if IP2LOCATION_API_KEY == "" {
return ""
}
apipackage := "WS25"
usessl := true
addon := "continent,country,region,city,geotargeting,country_groupings,time_zone_info" // leave blank if no need
lang := "en" // leave blank if no need
ws, err := ip2location.OpenWS(IP2LOCATION_API_KEY, apipackage, usessl)
if err != nil {
fmt.Print(err)
return ""
}
res, err := ws.LookUp(ip, addon, lang)
if err != nil {
fmt.Print(err)
return ""
}
if res.Response != "OK" {
fmt.Printf("Error: %s\n", res.Response)
}
return res.Country.Name
}