-
Notifications
You must be signed in to change notification settings - Fork 0
/
captain-cold.go
139 lines (101 loc) · 2.59 KB
/
captain-cold.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
"github.com/aws/aws-lambda-go/lambda"
"github.com/joho/godotenv"
)
const MIN_TEMP = 1
const NUM_HOURS = 11
type Hourly struct {
// we only want the first 32 results (8am the next day)
Temperature [32]float64 `json:"temperature_2m"`
}
type WeatherData struct {
Hourly `json:"hourly"`
}
type Temperatures *[NUM_HOURS]float64
func main() {
lambda.Start(HandleRequest)
}
func HandleRequest() {
defer log.Println("Finished running Captain Cold")
// we dont care if it fails, only used for dev
godotenv.Load()
weatherUrl := os.Getenv("WEATHER_BASE_URL")
if weatherUrl == "" {
log.Fatalln("WEATHER_BASE_URL not set")
}
lat := os.Getenv("WEATHER_LAT")
if lat == "" {
log.Fatalln("WEATHER_LAT not set")
}
lng := os.Getenv("WEATHER_LNG")
if lng == "" {
log.Fatalln("WEATHER_LNG not set")
}
temps := getTemps(weatherUrl, lat, lng)
lowerThanMin := isLowerThanMin(temps)
if !lowerThanMin {
log.Print("Min temperature is above 1c, not sending message")
return
}
log.Println("Attempting to send discord message")
webhook := os.Getenv("WEBHOOK_URL")
sendMessage(webhook)
}
func getTemps(baseUrl string, lat string, lng string) Temperatures {
v := url.Values{}
v.Set("latitude", lat)
v.Set("longitude", lng)
v.Set("hourly", "temperature_2m")
url := baseUrl + "?" + v.Encode()
log.Printf("Sending request to %s", url)
resp, err := http.Get(url)
if err != nil {
log.Fatalln(err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
log.Fatalln("Weather request failed with status code", resp.StatusCode)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
}
var data = new(WeatherData)
err = json.Unmarshal([]byte(body), &data)
if err != nil {
log.Fatalln(err)
}
// get the 15 times we care about 9pm - 8am
tempSlice := data.Hourly.Temperature[21:32]
// convert the slice to fixed array so we aren't returning a unknown sized slice
temps := (*[11]float64)(tempSlice)
log.Print("Temperatures returned from the API are", temps)
return temps
}
func isLowerThanMin(temps Temperatures) bool {
for i := 0; i < NUM_HOURS; i++ {
if temps[i] <= MIN_TEMP {
return true
}
}
return false
}
func sendMessage(webhook string) {
requestBody, err := json.Marshal(map[string]string{
"content": fmt.Sprintf("It's freezing tonight! It will drop lower than %dc before 8am tomorrow.", MIN_TEMP),
})
if err != nil {
log.Fatalln(err)
}
log.Printf("Going to send message to %s", webhook)
http.Post(webhook, "application/json", bytes.NewBuffer(requestBody))
}