-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
155 lines (120 loc) · 3.3 KB
/
main.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"time"
"github.com/joho/godotenv"
"github.com/twilio/twilio-go"
openapi "github.com/twilio/twilio-go/rest/api/v2010"
)
type SubCondition struct {
MinHeight float32
MaxHeight float32
Rating string
ForecastDay string
TimeOfDay string
}
type Condition struct {
Am SubCondition
Pm SubCondition
ForecastDay string
}
type Conditions struct {
Conditions []Condition
}
type DataResponse struct {
Data Conditions
}
func getConditionsResponse() *DataResponse {
httpClient := http.Client{
Timeout: time.Second * 10,
}
req, err := http.NewRequest(http.MethodGet, "https://services.surfline.com/kbyg/regions/forecasts/conditions?subregionId=58581a836630e24c4487915a&accesstoken=5f79115fe609028d579946049802a8b91861c034", nil)
if err != nil {
log.Fatal(err)
}
// TODO: insert Auth header
// req.Header.Set("User-Agent", "spacecount-tutorial")
resp, getErr := httpClient.Do(req)
if getErr != nil {
log.Fatal(getErr)
}
if resp.Body != nil {
defer resp.Body.Close()
}
body, readErr := ioutil.ReadAll(resp.Body)
if readErr != nil {
log.Fatal(readErr)
}
conditions := new(DataResponse)
json.Unmarshal([]byte(body), conditions)
fmt.Println(conditions.Data.Conditions[0].Pm.MaxHeight)
return conditions
}
func filterWaveHeight(conditions []Condition, minHeight float32) []SubCondition {
filtered := []SubCondition{}
for i := range conditions {
if conditions[i].Am.MinHeight > minHeight {
newVal := conditions[i].Am
newVal.ForecastDay = conditions[i].ForecastDay
newVal.TimeOfDay = "AM"
filtered = append(filtered, newVal)
}
if conditions[i].Pm.MinHeight > minHeight {
newVal := conditions[i].Pm
newVal.ForecastDay = conditions[i].ForecastDay
newVal.TimeOfDay = "PM"
filtered = append(filtered, newVal)
}
}
return filtered
}
func filterRating(conditions *[]Condition, minRating string) {
}
func formatMessage(filtered []SubCondition) string {
//https://www.twilio.com/docs/glossary/what-sms-character-limit
stringBase := "Go Surf!"
for i := range filtered {
stringBase = fmt.Sprint(stringBase, "\n", filtered[i].ForecastDay, " ", filtered[i].TimeOfDay, ": ", filtered[i].MinHeight, "-", filtered[i].MaxHeight, " ", filtered[i].Rating)
}
return stringBase
}
func sendMessage(client *twilio.RestClient, message string) {
from := os.Getenv("TWILIO_FROM_PHONE_NUMBER")
to := os.Getenv("TWILIO_TO_PHONE_NUMBER")
params := &openapi.CreateMessageParams{}
params.SetTo(to)
params.SetFrom(from)
params.SetBody(message)
resp, err := client.ApiV2010.CreateMessage(params)
if err != nil {
fmt.Println(err.Error())
} else {
response, _ := json.Marshal(*resp)
fmt.Println("Response: " + string(response))
}
}
func loadEnvVars() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
}
func main() {
loadEnvVars()
client := twilio.NewRestClient()
rsp := getConditionsResponse()
filtered := filterWaveHeight(rsp.Data.Conditions, 2)
if len(filtered) > 0 {
message := formatMessage(filtered)
fmt.Printf("message", message);
sendMessage(client, message)
}
}
// make httpService (getJSON)
// make dataService (filterConditions)
// make textService (notify)