dota_patch_bot/main.go

138 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"
2023-08-08 18:07:05 -04:00
"io"
2021-09-16 17:16:05 -04:00
"log"
"net/http"
2022-08-10 21:19:34 -04:00
"os"
2021-09-16 17:16:05 -04:00
"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()
2023-08-08 18:07:05 -04:00
body, readErr := io.ReadAll(res.Body)
2021-09-16 17:16:05 -04:00
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"`
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 != "" {
2023-10-05 21:53:25 -04:00
data := Payload{
2021-09-16 17:16:05 -04:00
msg,
"DotaPatchBot",
"https://i.pinimg.com/originals/8a/8b/50/8a8b50da2bc4afa933718061fe291520.jpg",
}
payloadBytes, err := json.Marshal(data)
if err != nil {
2022-06-02 13:34:30 -04:00
log.Fatalf("Can't create webhook notice; failing since that means drastic data format change")
2021-09-16 17:16:05 -04:00
}
body := bytes.NewReader(payloadBytes)
2023-10-05 21:53:25 -04:00
resp, err := http.Post(url, "application/json", body)
2021-09-16 17:16:05 -04:00
if err != nil {
2022-06-02 13:34:30 -04:00
log.Println("error posting to webhook")
2023-10-05 21:53:25 -04:00
return
2021-09-16 17:16:05 -04:00
}
2023-10-05 21:53:25 -04:00
defer resp.Body.Close()
2021-09-16 17:16:05 -04:00
}
}
2023-08-08 18:07:05 -04:00
2021-09-16 17:16:05 -04:00
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()
2022-08-10 21:19:34 -04:00
if *hookPtr == "" {
if val, ok := os.LookupEnv("DOTA_WEBHOOK"); ok {
*hookPtr = val
}
}
if *sourcePtr == "" {
if val, ok := os.LookupEnv("DOTA_PATCH_SOURCE"); ok {
*sourcePtr = val
}
}
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
}
}
}
}