-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
157 lines (142 loc) · 3.39 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
package main
import (
"encoding/json"
"fmt"
log "github.com/sirupsen/logrus"
twiml "github.com/wherethebitsroam/twiml"
"io/ioutil"
"math/rand"
"net/http"
"strconv"
)
type House struct {
AccessNumber int64
SecretNumber int64
Roommates []Roommate
}
type Roommate struct {
Name string
Phone string
}
var sounds = []string{
"http://phonebox.isaqt.com/sfx/lionking.mp3",
"http://phonebox.isaqt.com/sfx/opensesame.mp3",
"http://phonebox.isaqt.com/sfx/itsdeath.mp3",
}
func randomSound() string {
return sounds[rand.Intn(len(sounds))]
}
func main() {
houseConfig := House{}
bytes, err := ioutil.ReadFile("roomies.json")
if err != nil {
panic(err)
}
err = json.Unmarshal(bytes, &houseConfig)
if err != nil {
panic(err)
}
http.HandleFunc("/secret", func(w http.ResponseWriter, req *http.Request) {
err := req.ParseForm()
if err != nil {
panic(err)
}
if val, ok := req.Form["Digits"]; ok {
intDigits, err := strconv.ParseInt(val[0], 0, 64)
if err != nil {
panic(err)
}
r := twiml.NewResponse()
w.Write([]byte("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"))
if intDigits == houseConfig.SecretNumber {
r.Say("Welcome.", &twiml.SayAttr{Voice: "woman"})
r.Play("", &twiml.PlayAttr{Digits: "9"})
r.Play(randomSound(), &twiml.PlayAttr{})
r.Hangup()
r.ToXML(w)
return
}
r.Hangup()
r.ToXML(w)
return
}
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("no digits"))
})
http.HandleFunc("/sort", func(w http.ResponseWriter, req *http.Request) {
err := req.ParseForm()
if err != nil {
panic(err)
}
if val, ok := req.Form["Digits"]; ok {
intDigits, err := strconv.ParseInt(val[0], 0, 64)
if err != nil {
panic(err)
}
r := twiml.NewResponse()
w.Write([]byte("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"))
if intDigits == houseConfig.AccessNumber {
// Gather for the secret handler
ga := twiml.GatherAttr{
Action: "/secret",
Method: "GET",
Timeout: 15,
NumDigits: 4,
}
g := r.Gather(&ga)
g.Say("Give it to me baby.", &twiml.SayAttr{
Voice: "man",
})
r.Say("Didn't hear you, goodbye.", &twiml.SayAttr{})
r.Hangup()
r.ToXML(w)
return
}
if intDigits == 0 {
intDigits = 1 // lol
} else {
intDigits -= 1
}
if int(intDigits) > len(houseConfig.Roommates) {
r.Say("Try again.", &twiml.SayAttr{})
r.Redirect("/greet", &twiml.RedirectAttr{})
r.ToXML(w)
return
}
// Dial the rooomate!
d := r.Dial(&twiml.DialAttr{})
d.Number(houseConfig.Roommates[intDigits].Phone, &twiml.NumberAttr{})
r.Say("Goodbye.", &twiml.SayAttr{})
r.Hangup()
r.ToXML(w)
return
}
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("no digits"))
})
http.HandleFunc("/greet", func(w http.ResponseWriter, req *http.Request) {
var speech string
for i, roomie := range houseConfig.Roommates {
speech += fmt.Sprintf("For %s press %d", roomie.Name, i+1)
if i+1 != len(houseConfig.Roommates) {
speech += ", "
}
}
w.Write([]byte("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"))
r := twiml.NewResponse()
ga := twiml.GatherAttr{
Action: "/sort",
Method: "GET",
Timeout: 15,
NumDigits: 1,
}
g := r.Gather(&ga)
g.Say(speech, &twiml.SayAttr{
Voice: "woman",
})
r.Say("Didn't hear you, goodbye.", &twiml.SayAttr{})
r.Hangup()
r.ToXML(w)
})
log.Fatal(http.ListenAndServe(":8080", nil))
}