-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
201 lines (166 loc) · 4.33 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package main
import (
"log"
"regexp"
"strings"
"fmt"
"net/http"
"os"
"context"
"errors"
"encoding/json"
"github.com/gorilla/schema"
"github.com/sfreiberg/gotwilio"
"googlemaps.github.io/maps"
)
var (
twilioID string
twilioAuthToken string
mapsAPIKey string
port = os.Getenv("PORT")
)
type query struct {
Query string `json:"query"`
}
type message struct {
NumSegments int
FromState string
ApiVersion string
ToState string
SmsMessageSid string
FromCity string
Body string
To string
MessageSid string
ToCountry string
FromCountry string
From string
NumMedia int
ToZip int
ToCity string
FromZip string
SmsSid string
SmsStatus string
AccountSid string
}
func removeBold(str string) string {
inBold := false
newStr := ""
for i, r := range str {
if inBold {
newStr += strings.ToUpper(string(r))
} else {
newStr += string(r)
}
if string(r) == ">" && string(str[i-1]) == "b" && string(str[i-2]) == "<" {
inBold = true
}
if string(r) == ">" && string(str[i-1]) == "b" && string(str[i-2]) == "/" && string(str[i-3]) == "<" {
inBold = false
}
}
re := regexp.MustCompile(`<b>|<\/B>`)
newStr = re.ReplaceAllString(newStr, "")
re = regexp.MustCompile(`<div style="font-size:0.9em">`)
newStr = re.ReplaceAllString(newStr, " (")
re = regexp.MustCompile(`<\/div>`)
newStr = re.ReplaceAllString(newStr, ") ")
return newStr
}
func main() {
http.HandleFunc("/sms", receiveTextsHandler)
http.HandleFunc("/directions", directionsHandler)
log.Println("Magic happening on port " + "8080")
twilioID = os.Getenv("TWILIO_ID")
if twilioID == "" {
log.Fatal("TWILIO_ID environment variable not set")
}
twilioAuthToken = os.Getenv("TWILIO_AUTH_TOKEN")
if twilioAuthToken == "" {
log.Fatal("TWILIO_AUTH_TOKEN environment variable not set")
}
mapsAPIKey = os.Getenv("MAP_API_KEY")
if mapsAPIKey == "" {
log.Fatal("MAP_API_KEY environment variable not set")
}
log.Fatal(http.ListenAndServe(":"+port, nil))
}
func directionsHandler(w http.ResponseWriter, r *http.Request) {
q := query{}
err := json.NewDecoder(r.Body).Decode(&q)
if err != nil {
fmt.Fprint(w, "problem decoding json", err)
return
}
d, err := getDirections(q.Query)
if err != nil {
fmt.Fprint(w, "problem getting directions: ", err)
return
}
fmt.Fprint(w, d)
}
func receiveTextsHandler(w http.ResponseWriter, r *http.Request) {
fmt.Println("message recieved!")
err := r.ParseForm()
if err != nil {
fmt.Println("problem parsing form")
}
m := message{}
decoder := schema.NewDecoder()
// r.PostForm is a map of our POST form values
err = decoder.Decode(&m, r.PostForm)
if err != nil {
fmt.Println("problem decoding form", err)
}
fmt.Printf("incoming message: %+v", m)
directions, err := getDirections(m.Body)
cl := gotwilio.NewTwilioClient(twilioID, twilioAuthToken)
send(cl, "+13123131234", m.From, directions)
}
func getDirections(m string) (string, error) {
fmt.Println("getting directions for: ", m)
m = strings.ToLower(m)
var directions string
var from string
var to string
fromAndTo := strings.Split(m, " to ")
if len(fromAndTo) > 1 {
from = fromAndTo[0]
to = fromAndTo[1]
} else {
return directions, errors.New("directions malformed")
}
fmt.Println("1")
c, err := maps.NewClient(maps.WithAPIKey("AIzaSyBYsgGoxFUK8cDI2cm177AJb9jbthfpNsY"))
if err != nil {
fmt.Println("problem setting up google maps client: ", err)
return directions, err
}
r := &maps.DirectionsRequest{
Origin: from,
Destination: to,
}
resp, _, err := c.Directions(context.Background(), r)
if err != nil {
fmt.Println("problem getting directions: ", err)
return directions, err
}
fmt.Println("3")
for _, route := range resp {
legs := route.Legs
for _, leg := range legs {
steps := leg.Steps
for _, step := range steps {
directions += removeBold(step.HTMLInstructions) + " (" + step.Distance.HumanReadable + ")" + "\n\n"
}
}
}
fmt.Println("4")
fmt.Println("directions, error : ", directions, err)
return directions, err
}
func send(cl *gotwilio.Twilio, from string, to string, body string) {
fmt.Println("about to send message!")
res, exeption, err := cl.SendSMS(from, to, body, "", "") // todo: should not ignore those returns
fmt.Println("res, exeption, err", res, exeption, err)
}