-
Notifications
You must be signed in to change notification settings - Fork 0
/
sms.go
102 lines (86 loc) · 3.17 KB
/
sms.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
package teamvite
// func (s *Server) SMS() http.Handler {
// return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// // TODO: check basic auth
// // TODO: validate request signature
// // https://www.twilio.com/docs/usage/security#http-authentication
// // message := make(map[string]string)
// // TODO: use SmsMessageSid to get unique message id
// // https://support.twilio.com/hc/en-us/articles/223134387-What-is-a-Message-SID-
// // https://www.twilio.com/docs/glossary/what-is-a-sid
// err := r.ParseForm()
// checkErr(err, "Parsing form data")
// rawTel := r.PostForm.Get("From")
// if len(rawTel) < 10 {
// response := "ERROR: Unknown number"
// w.Header().Set("Content-Type", "text/plain")
// w.Write([]byte(response))
// return
// }
// tel := UnTelify(rawTel[2:])
// reg, _ := regexp.Compile("[^a-zA-Z]+")
// // get clean message (Yes should fuzzy-match y/Yes/YES/yes)
// msg := strings.ToUpper(reg.ReplaceAllString(r.PostForm.Get("Body"), ""))
// log.Printf("Raw tel: %s Parsed tel: %d", r.PostForm.Get("From"), tel)
// log.Println("Message: ", msg)
// // get user from phone number
// p := playerByPhone(s.DB, tel)
// if p.ID == 0 {
// log.Printf("[ERROR] Unable to find player with tel: %d", tel)
// response := "ERROR: Unknown number"
// w.Header().Set("Content-Type", "text/plain")
// w.Write([]byte(response))
// return
// } else {
// log.Println("Player: ", p)
// }
// // Note: this has limitations if a player is playing on multiple
// // teams and we send multiple alerts to them.
// // If needed, could get multiple numbers from Twilio to handle
// // multiple teams for a single player
// teams := p.Teams(s.DB)
// // find most recently reminded game
// var nextGame game
// for _, t := range teams {
// g, ok := t.NextGame(s.DB)
// if ok {
// nextGame = g
// }
// }
// var response string
// switch msg[0:1] {
// case "Y":
// SetStatus(s.DB, msg[0:1], p.ID, nextGame.ID)
// response = "See you at the game"
// case "N":
// SetStatus(s.DB, msg[0:1], p.ID, nextGame.ID)
// response = "Sorry you can't make it"
// case "S":
// _, err := s.DB.Exec(
// "update players_teams set remind_sms = false where player_id = ? and team_id = ?", p.ID, nextGame.TeamID)
// checkErr(err, "stop reminders")
// response = "Stopping future reminders"
// default:
// response = "Unknown reply, valid replies are YES, NO or STOP"
// }
// w.Header().Set("Content-Type", "text/plain")
// w.Write([]byte(response))
// // docs on post body
// // https://www.twilio.com/docs/messaging/guides/webhook-request
// })
// }
// // this is the test receiver that captures sms POSTS from send_game_reminders.
// func (s *Server) TestSMSReceiver() http.Handler {
// return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// body, _ := io.ReadAll(r.Body)
// log.Println("Got SMS message:", string(body))
// })
// }
// func playerByPhone(DB *QueryLogger, phone int) (p Player) {
// if phone == -1 {
// return
// }
// err := DB.Get(&p, "select * from players where phone = ?", phone)
// checkErr(err, "error loading player: ")
// return
// }