/
app.go
75 lines (69 loc) · 1.89 KB
/
app.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
package main
import (
"fmt"
"labix.org/v2/mgo"
"log"
"net/http"
"net/mail"
"strings"
"time"
)
type Signup struct {
Name string `bson:"name" json:"name"`
Email string `bson:"email" json:"email"`
Raw string `bson:"raw" json:"raw"`
Timestamp time.Time `bson:"timestamp" json:"timestamp"`
}
var mgoSession *mgo.Session
func SendSMS(w http.ResponseWriter, r *http.Request, message string) {
w.Header().Set("Content-Type", "text/xml")
s := fmt.Sprintf("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Response><Sms>%s</Sms></Response>", message)
fmt.Fprint(w, s)
}
func SMSHandler(w http.ResponseWriter, r *http.Request) {
sms := r.URL.Query().Get("Body")
sms = strings.Trim(sms, "8575-2838")
if sms == "" {
log.Println("Blank body.")
SendSMS(w, r, "Please send a text with: \"Name\" \"email@address\"")
return
}
info := strings.Fields(sms)
name := strings.Join(info[:len(info)-1], " ")
email := info[len(info)-1]
name = strings.Trim(name, "\"")
if name == "" {
log.Println("Blank name.")
SendSMS(w, r, "Please send a text with: \"Name\" \"email@address\"")
return
}
email = strings.Trim(email, "\"")
address := fmt.Sprintf("%s <%s>", name, email)
_, err := mail.ParseAddress(address)
if err != nil {
SendSMS(w, r, "Please try again with a valid email address.")
return
}
signup := Signup{Name: name, Email: email, Raw: sms, Timestamp: time.Now()}
log.Println(signup)
c := mgoSession.DB("sms_signup").C("signups")
err = c.Insert(signup)
if err != nil {
log.Println(err.Error())
}
SendSMS(w, r, fmt.Sprintf("Thanks for signing up, %s!", info[0]))
}
func main() {
var err error
mgoSession, err = mgo.Dial("localhost")
if err != nil {
log.Println(err.Error())
}
log.Println("Connected to mongodb.")
log.Println("Starting on :8080")
http.HandleFunc("/sms", SMSHandler)
err = http.ListenAndServe(":8080", nil)
if err != nil {
log.Println(err.Error())
}
}