dota_patch_bot/main.go

141 lines
3.3 KiB
Go
Raw Normal View History

2021-09-16 17:16:05 -04:00
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"time"
)
2021-12-22 13:37:15 -05:00
type patchList struct {
Patches []patch `json:"patches"`
2021-09-16 17:16:05 -04:00
Success bool `json:"success"`
}
2021-12-22 13:37:15 -05:00
type patch struct {
2021-09-16 17:16:05 -04:00
PatchNumber string `json:"patch_number"`
PatchName string `json:"patch_name"`
PatchTimestamp int `json:"patch_timestamp"`
PatchWebsite string `json:"patch_website,omitempty"`
PatchWebsiteAnchor string `json:"patch_website_anchor,omitempty"`
}
2021-12-22 13:37:15 -05:00
func loadPatches(pl *patchList, url string) {
2021-09-16 17:16:05 -04:00
res, err := http.Get(url)
if err != nil {
2021-10-20 19:59:12 -04:00
log.Printf("Got %v, retrying in 5s", err)
time.Sleep(5 * time.Second)
res, err = http.Get(url)
if err != nil {
log.Printf("Got %v, not trying again", err)
return
}
2021-09-16 17:16:05 -04:00
}
2021-10-26 13:33:58 -04:00
defer res.Body.Close()
2021-09-16 17:16:05 -04:00
body, readErr := ioutil.ReadAll(res.Body)
if readErr != nil {
log.Fatal(readErr)
}
err = json.Unmarshal(body, pl)
if err != nil {
log.Fatal("error: ", err)
}
}
2021-12-22 13:37:15 -05:00
func notify(ptch patch, url string) {
2021-09-16 17:16:05 -04:00
type Payload struct {
Text string `json:"text"`
Format string `json:"format"`
DisplayName string `json:"displayName"`
AvatarURL string `json:"avatar_url"`
}
var msg string
2021-12-22 13:37:15 -05:00
if ptch.PatchNumber == "" {
2021-09-16 17:16:05 -04:00
msg = "DotaPatchBot online"
} else {
2021-12-22 13:37:15 -05:00
log.Printf("Patch %v found, notifying\n", ptch.PatchNumber)
2021-09-16 17:16:05 -04:00
2021-12-22 13:37:15 -05:00
if ptch.PatchWebsite != "" {
msg = fmt.Sprintf("Patch %v released, see website:\nhttps://www.dota2.com/%v", ptch.PatchNumber, ptch.PatchWebsite)
2021-09-16 17:16:05 -04:00
} else {
2021-12-22 13:37:15 -05:00
msg = fmt.Sprintf("Patch %v Released", ptch.PatchNumber)
2021-09-16 17:16:05 -04:00
}
}
if url != "" {
data := struct {
Text string `json:"text"`
Format string `json:"format"`
DisplayName string `json:"displayName"`
AvatarURL string `json:"avatar_url"`
}{
msg,
"plain",
"DotaPatchBot",
"https://i.pinimg.com/originals/8a/8b/50/8a8b50da2bc4afa933718061fe291520.jpg",
}
payloadBytes, err := json.Marshal(data)
if err != nil {
// handle err
}
body := bytes.NewReader(payloadBytes)
req, err := http.NewRequest("POST", url, body)
if err != nil {
// handle err
}
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
// handle err
}
defer resp.Body.Close()
}
}
func main() {
hookPtr := flag.String("hook", "", "the webhook to notify")
sourcePtr := flag.String("url", "https://www.dota2.com/datafeed/patchnoteslist", "url to fetch patchnotes from")
2021-12-22 13:37:15 -05:00
patchlist := patchList{}
var currentPatch patch
2021-09-16 17:21:34 -04:00
flag.Parse()
2021-12-22 13:37:15 -05:00
loadPatches(&patchlist, *sourcePtr)
2021-09-16 17:16:05 -04:00
2021-12-22 13:37:15 -05:00
if len(patchlist.Patches) <= 0 {
2021-10-20 19:59:12 -04:00
log.Fatal("error getting initial patch list")
}
2021-12-22 13:37:15 -05:00
currentPatch = patchlist.Patches[len(patchlist.Patches)-1]
2021-09-16 17:16:05 -04:00
log.Println("Started bot, loaded patch list")
2021-12-22 13:37:15 -05:00
log.Printf("Starting on patch %v", currentPatch.PatchNumber)
2021-09-16 17:16:05 -04:00
if *hookPtr == "" {
log.Println("No webhook set, not notifying")
2021-09-16 17:21:34 -04:00
} else {
log.Printf("Notifying %v", *hookPtr)
2021-09-16 17:16:05 -04:00
}
2021-12-22 13:37:15 -05:00
notify(patch{}, *hookPtr)
2021-09-16 17:16:05 -04:00
for {
time.Sleep(30 * time.Second)
2021-12-22 13:37:15 -05:00
loadPatches(&patchlist, *sourcePtr)
if len(patchlist.Patches) > 0 {
newestPatch := patchlist.Patches[len(patchlist.Patches)-1]
if newestPatch.PatchNumber != currentPatch.PatchNumber {
currentPatch = newestPatch
2021-10-20 19:59:12 -04:00
if *hookPtr != "" {
2021-12-22 13:37:15 -05:00
notify(currentPatch, *hookPtr)
2021-10-20 19:59:12 -04:00
} else {
2021-12-22 13:37:15 -05:00
log.Printf("Patch %v found, no webhook\n", currentPatch.PatchNumber)
2021-10-20 19:59:12 -04:00
}
2021-09-16 17:16:05 -04:00
}
}
}
}