mirror of
https://github.com/idanoo/go-mastodon-matrix-webhooks.git
synced 2024-11-23 08:55:21 +00:00
Lookups
This commit is contained in:
parent
2a898e3bd1
commit
9cc5fec58f
@ -2,4 +2,4 @@ MATRIX_WEBHOOK_URL=
|
|||||||
MATRIX_WEBHOOK_API_KEY=
|
MATRIX_WEBHOOK_API_KEY=
|
||||||
MATRIX_CHANNEL=
|
MATRIX_CHANNEL=
|
||||||
PORT=
|
PORT=
|
||||||
IP2LOCATION_API_KEY=
|
IP2LOCATION_FILE=
|
BIN
ip/IP2LOCATION-LITE-DB1.IPV6.BIN
Executable file
BIN
ip/IP2LOCATION-LITE-DB1.IPV6.BIN
Executable file
Binary file not shown.
74
main.go
74
main.go
@ -1,7 +1,6 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
@ -16,7 +15,7 @@ var MATRIX_WEBHOOK_URL string
|
|||||||
var MATRIX_WEBHOOK_API_KEY string
|
var MATRIX_WEBHOOK_API_KEY string
|
||||||
var MATRIX_CHANNEL string
|
var MATRIX_CHANNEL string
|
||||||
var PORT string
|
var PORT string
|
||||||
var IP2LOCATION_API_KEY string
|
var IP2LOCATION_FILE string
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
err := godotenv.Load()
|
err := godotenv.Load()
|
||||||
@ -44,7 +43,7 @@ func init() {
|
|||||||
log.Fatal("PORT empty or invalid")
|
log.Fatal("PORT empty or invalid")
|
||||||
}
|
}
|
||||||
|
|
||||||
IP2LOCATION_API_KEY = os.Getenv("IP2LOCATION_API_KEY")
|
IP2LOCATION_FILE = os.Getenv("IP2LOCATION_API_KEY")
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@ -72,7 +71,7 @@ func handler(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
} else if i.Event == "account.created" {
|
} else if i.Event == "account.created" {
|
||||||
country := ipLookup(i.Object.IP)
|
country := ipLookup(i.Object.IP)
|
||||||
err = sendWebhook(fmt.Sprintf("*New Signup* %s has joined from %s", i.Object.Username, country))
|
err = sendWebhook(fmt.Sprintf("*New Signup* %s has signed up.%s", i.Object.Username, country))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err.Error())
|
log.Println(err.Error())
|
||||||
return
|
return
|
||||||
@ -85,60 +84,49 @@ func handler(w http.ResponseWriter, r *http.Request) {
|
|||||||
func sendWebhook(msgText string) error {
|
func sendWebhook(msgText string) error {
|
||||||
log.Println(msgText)
|
log.Println(msgText)
|
||||||
|
|
||||||
data := MatrixWebhook{
|
// data := MatrixWebhook{
|
||||||
Key: MATRIX_WEBHOOK_API_KEY,
|
// Key: MATRIX_WEBHOOK_API_KEY,
|
||||||
}
|
// }
|
||||||
data.Body = msgText
|
// data.Body = msgText
|
||||||
b, err := json.Marshal(data)
|
// b, err := json.Marshal(data)
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
return err
|
// return err
|
||||||
}
|
// }
|
||||||
req, err := http.NewRequest("POST", MATRIX_WEBHOOK_URL+"/"+MATRIX_CHANNEL, bytes.NewBuffer(b))
|
// req, err := http.NewRequest("POST", MATRIX_WEBHOOK_URL+"/"+MATRIX_CHANNEL, bytes.NewBuffer(b))
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
return err
|
// return err
|
||||||
}
|
// }
|
||||||
|
|
||||||
req.Header.Set("X-Custom-Header", "myvalue")
|
// req.Header.Set("X-Custom-Header", "myvalue")
|
||||||
req.Header.Set("Content-Type", "application/json")
|
// req.Header.Set("Content-Type", "application/json")
|
||||||
|
|
||||||
client := &http.Client{}
|
// client := &http.Client{}
|
||||||
resp, err := client.Do(req)
|
// resp, err := client.Do(req)
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
panic(err)
|
// panic(err)
|
||||||
}
|
// }
|
||||||
defer resp.Body.Close()
|
// defer resp.Body.Close()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Lookup to country!
|
// Lookup to country!
|
||||||
func ipLookup(ip string) string {
|
func ipLookup(ip string) string {
|
||||||
if IP2LOCATION_API_KEY == "" {
|
if IP2LOCATION_FILE == "" {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
apipackage := "WS25"
|
db, err := ip2location.OpenDB(IP2LOCATION_FILE)
|
||||||
usessl := true
|
if err != nil {
|
||||||
addon := "continent,country,region,city,geotargeting,country_groupings,time_zone_info" // leave blank if no need
|
fmt.Print(err)
|
||||||
lang := "en" // leave blank if no need
|
return ""
|
||||||
|
}
|
||||||
ws, err := ip2location.OpenWS(IP2LOCATION_API_KEY, apipackage, usessl)
|
results, err := db.Get_all(ip)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Print(err)
|
fmt.Print(err)
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
res, err := ws.LookUp(ip, addon, lang)
|
return "(" + results.Country_long + ")"
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
fmt.Print(err)
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
if res.Response != "OK" {
|
|
||||||
fmt.Printf("Error: %s\n", res.Response)
|
|
||||||
}
|
|
||||||
|
|
||||||
return res.Country.Name
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user