-
Notifications
You must be signed in to change notification settings - Fork 0
/
check.go
156 lines (143 loc) · 3.07 KB
/
check.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package dayforit
import (
"fmt"
"math"
"net/http"
"os"
"github.com/pkg/errors"
"github.com/slack-go/slack"
owm "github.com/briandowns/openweathermap"
)
func Check(w http.ResponseWriter, r *http.Request) {
err := check()
if err != nil {
http.Error(
w,
errors.Wrap(err, "running check").Error(),
http.StatusInternalServerError,
)
return
}
w.WriteHeader(http.StatusOK)
w.Write([]byte("ok"))
}
func check() error {
wtr, err := owm.NewCurrent("C", "EN", os.Getenv("OWM_KEY"))
if err != nil {
return errors.Wrap(err, "getting owm new current")
}
wtr.CurrentByZip(2017, "AU")
if err != nil {
return errors.Wrap(err, "getting current by zip")
}
windSpeedKPH := int(math.Trunc(wtr.Wind.Speed * 60 * 60 / 1000))
windDirection := directionFromDegrees(wtr.Wind.Deg)
primaryWeather := wtr.Weather[0]
var title, subtitle string
switch true {
case primaryWeather.ID == 800:
title = "It is an absolute day for it! :sunny:"
subtitle = "The weather today is *perfect*. Good day to get on the bikes!"
case primaryWeather.ID > 800:
title = "No reason not to cycle. :cloud:"
subtitle = "The weather today is perfectly cycleable. Get on the bikes!"
case primaryWeather.ID >= 300 && primaryWeather.ID < 400:
title = "Worth a shot! :rain_cloud:"
subtitle = "It's a bit drizzly today, but gotta risk it for the biscuit."
default:
title = "Nope :no_good:"
subtitle = "Not today."
}
err = slack.PostWebhook(
os.Getenv("SLACK_WEBHOOK_URL"),
&slack.WebhookMessage{
Text: title,
Blocks: &slack.Blocks{
BlockSet: []slack.Block{
slack.NewHeaderBlock(
slack.NewTextBlockObject(
slack.PlainTextType,
title,
false,
false,
),
),
slack.NewSectionBlock(
slack.NewTextBlockObject(
slack.MarkdownType,
subtitle,
false,
false,
),
nil,
nil,
),
slack.NewDividerBlock(),
slack.NewSectionBlock(
nil,
[]*slack.TextBlockObject{
slack.NewTextBlockObject(
slack.MarkdownType,
fmt.Sprintf("*Currently*: %v°c", wtr.Main.Temp),
false,
false,
),
slack.NewTextBlockObject(
slack.MarkdownType,
fmt.Sprintf(
"*Range*: %v°c - %v°c",
wtr.Main.TempMin,
wtr.Main.TempMax,
),
false,
false,
),
slack.NewTextBlockObject(
slack.MarkdownType,
fmt.Sprintf("*Rain*: %vmm", wtr.Rain.ThreeH),
false,
false,
),
slack.NewTextBlockObject(
slack.MarkdownType,
fmt.Sprintf(
"*Windspeed*: %vkm/h %v",
windSpeedKPH,
windDirection,
),
false,
false,
),
},
nil,
),
},
},
},
)
if err != nil {
return errors.Wrap(err, "posting to slack")
}
return nil
}
func directionFromDegrees(deg float64) string {
idx := int(math.Round(math.Mod(deg, 360) / 22.5))
return []string{
"N",
"NNE",
"NE",
"ENE",
"E",
"ESE",
"SE",
"SSE",
"S",
"SSW",
"SW",
"WSW",
"W",
"WNW",
"NW",
"NNW",
}[idx]
}