-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
153 lines (125 loc) · 3.44 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
package main
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"strings"
"time"
)
type ErgastResponse struct {
MRData MRData `json:"MRData"`
}
type MRData struct {
Series string `json:"series,omitempty"`
Total int `json:"total,omitempty"`
RaceTable RaceTable `json:"RaceTable"`
}
type RaceTable struct {
Season string `json:"season,omitempty"`
Races []Race `json:"Races"`
}
type Race struct {
Season int `json:"season,omitempty"`
Round int `json:"round,omitempty"`
Url string `json:"url,omitempty"`
RaceName string `json:"raceName,omitempty"`
CircuitName string `json:"circuitName,omitempty"`
Date string `json:"date,omitempty"`
Time string `json:"time,omitempty"`
DateTime time.Time `json:"omitempty"`
}
type DbResponse struct {
Users []User `json:"users"`
}
type User struct {
UserId string `json:"userId,omitempty"`
Phone string `json:"phone,omitempty"`
Email string `json:"email,omitempty"`
Timezone string `json:"timezone,omitempty"`
}
func main() {
for _, race := range GetSchedule().RaceTable.Races {
t, _ := time.Parse(time.RFC3339, race.Date+"T"+race.Time)
if t.After(time.Now()) {
race.DateTime = t
SendNotifications(race)
break
}
}
}
func GetSchedule() MRData {
resp, err := http.Get("http://ergast.com/api/f1/current.json")
if err != nil {
log.Fatalln(err)
}
//We Read the response body on the line below.
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
}
var response ErgastResponse
json.Unmarshal(body, &response)
return response.MRData
}
func GetUsers() DbResponse {
url, _ := url.ParseRequestURI("https://phrhyp7dx2.execute-api.eu-west-1.amazonaws.com/Production")
req, err := http.NewRequest("GET", url.String(), nil)
//Handle Error
if err != nil {
log.Fatalf("An Error Occured %v", err)
}
req.Header.Set("Accept", "application/json")
cli := &http.Client{}
resp, err := cli.Do(req)
//Handle Error
if err != nil {
log.Fatalf("An Error Occured %v", err)
}
defer resp.Body.Close()
//Read the response body
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
}
var response DbResponse
json.Unmarshal(body, &response)
return response
}
func SendNotifications(race Race) {
for _, user := range GetUsers().Users {
SendSMS(user, race)
}
}
func SendSMS(user User, race Race) {
data := url.Values{}
data.Set("To", user.Phone)
data.Set("MessagingServiceSid", os.Getenv("MessagingServiceSid"))
loc, _ := time.LoadLocation(user.Timezone)
data.Set("Body", "Next Race: "+race.RaceName+" at: "+race.DateTime.In(loc).Format(time.RFC1123)+". https://www.formula1.com/")
url, _ := url.ParseRequestURI("https://api.twilio.com/2010-04-01/Accounts/" + os.Getenv("TWILIO_ACCOUNT_SID") + "/Messages.json")
req, err := http.NewRequest("POST", url.String(), strings.NewReader(data.Encode()))
//Handle Error
if err != nil {
log.Fatalf("An Error Occured %v", err)
}
req.Header.Set("Accept", "application/json")
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.SetBasicAuth(os.Getenv("TWILIO_ACCOUNT_SID"), os.Getenv("TWILIO_TOKEN"))
cli := &http.Client{}
resp, err := cli.Do(req)
//Handle Error
if err != nil {
log.Fatalf("An Error Occured %v", err)
}
defer resp.Body.Close()
//Read the response body
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
}
sb := string(body)
log.Printf(sb)
}