-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
291 lines (268 loc) · 7.68 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package main
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"path"
"strconv"
"strings"
"time"
"cloud.google.com/go/datastore"
"google.golang.org/api/iterator"
)
const (
ConfigPath = "./config.json"
TwilioFromNumber = "+14427776437"
TwilioKeySid = "_REMOVED_"
TwilioKeySecret = "_REMOVED_"
TwilioMessages = "https://api.twilio.com/2010-04-01/Accounts/_REMOVED_/Messages"
)
const Response = `<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Say>Please leave a message after the tone.</Say>
<Record maxLength="30" />
<Say>Sorry, no message could be recorded.</Say>
</Response>`
const VoicemailText = `You have new voicemail in Roger. First, please verify your phone number to listen.
Open Roger > Settings > Connect accounts > Add phone number.
http://rgr.im/get`
type Config struct {
ListenAddr string
ProjectId string
AccessToken string
}
var (
config Config
ctx = context.Background()
store *datastore.Client
apiURL, _ = url.Parse("https://api.rogertalk.com/v17/")
)
type Identity struct {
Account *datastore.Key `datastore:"account"`
Available bool `datastore:"available"`
Status string `datastore:"status"`
}
type Participant struct {
Id int64
}
type PendingVoicemail struct {
From string `datastore:"from",noindex`
To string `datastore:"to"`
AudioURL string `datastore:"audio_url",noindex`
Delivered bool `datastore:"delivered"`
}
type Stream struct {
Id int64
Others []Participant
}
func main() {
// Load configuration from file.
data, err := ioutil.ReadFile(ConfigPath)
if err != nil {
log.Fatalf("Failed to load config (ioutil.ReadFile: %v)", err)
}
err = json.Unmarshal(data, &config)
if err != nil {
log.Fatalf("Failed to load config (json.Unmarshal: %v)", err)
}
// Set up the Datastore client.
store, err = datastore.NewClient(ctx, config.ProjectId)
if err != nil {
log.Fatalf("Failed to create Datastore client (datastore.NewClient: %v)", err)
}
// Set up server for handling incoming requests.
http.HandleFunc("/v1/call", callHandler)
log.Printf("Starting server on %s...", config.ListenAddr)
if err := http.ListenAndServe(config.ListenAddr, nil); err != nil {
log.Fatalf("Failed to serve (http.ListenAndServe: %v)", err)
}
}
func callHandler(w http.ResponseWriter, r *http.Request) {
defer logRequestTime(r.Method, r.URL.Path, time.Now())
// GET requests don't contain the recording.
if r.Method == "GET" {
log.Printf("Incoming call: %s", r.URL.Query())
w.Write([]byte(Response))
return
}
err := r.ParseForm()
if err != nil {
log.Printf("Failed to parse body: %v", err)
return
}
from, to := r.Form.Get("From"), r.Form.Get("ForwardedFrom")
audioURL := r.Form.Get("RecordingUrl")
if ext := path.Ext(audioURL); ext == "" || ext == ".wav" {
// Using MP3 directly is faster.
audioURL = audioURL[:len(audioURL)-len(ext)] + ".mp3"
}
log.Printf("%s -> %s (%s)", from, to, audioURL)
err = deliverVoicemail(from, to, audioURL, false)
if err != nil {
log.Printf("Failed to deliver voicemail: %v", err)
}
}
func deliverPendingVoicemail(key *datastore.Key, voicemail PendingVoicemail) (err error) {
err = deliverVoicemail(voicemail.From, voicemail.To, voicemail.AudioURL, true)
if err != nil {
return
}
voicemail.Delivered = true
_, err = store.Put(ctx, key, &voicemail)
return
}
func deliverVoicemail(from, to, audioURL string, retrying bool) (err error) {
if to == "" {
return fmt.Errorf("empty recipient (did someone call us?)")
}
if from == "" {
from = "unknownuser"
}
fromIdentity, toIdentity, err := getIdentityPair(from, to)
if toIdentity == nil || toIdentity.Available {
if retrying {
// The voicemail is already in the queue, so don't add it.
return fmt.Errorf("retried delivery but %s still doesn't have an account", to)
}
pending := PendingVoicemail{
From: from,
To: to,
AudioURL: audioURL,
}
key, storeErr := store.Put(ctx, datastore.IncompleteKey("PendingVoicemail", nil), &pending)
if storeErr != nil {
err = fmt.Errorf("receiver %s doesn't have an account, failed to store pending voicemail: %v", to, storeErr)
} else {
err = fmt.Errorf("receiver %s doesn't have an account, stored pending voicemail (%d)", to, key.ID)
}
return
}
toId := toIdentity.Account.ID
var fromId int64
if fromIdentity != nil && !fromIdentity.Available {
fromId = fromIdentity.Account.ID
_, err = postStream(fromId, 0, url.Values{
"participant": {strconv.FormatInt(toId, 10)},
"audio_url": {audioURL},
})
return
}
// Sender has no account, so we need to create the stream in "reverse" first.
// TODO: Give the sender a formatted display name from Twilio.
stream, err := postStream(toId, 0, url.Values{
"participant": {from},
"reason": {"voicemail"},
})
if err != nil {
return
}
if len(stream.Others) > 0 {
fromId = stream.Others[0].Id
} else {
// This is a monologue stream (the person left themselves a voicemail).
fromId = toId
}
_, err = postStream(fromId, stream.Id, url.Values{
"audio_url": {audioURL},
})
return
}
func flushPendingQueue() {
q := datastore.NewQuery("PendingVoicemail").Filter("delivered =", false)
t := store.Run(ctx, q)
for {
var voicemail PendingVoicemail
key, err := t.Next(&voicemail)
if err == iterator.Done {
break
} else if err != nil {
log.Printf("Failed to get a pending voicemail: %v", err)
continue
}
if err := deliverPendingVoicemail(key, voicemail); err != nil {
log.Printf("Failed to deliver a pending voicemail: %v", err)
} else {
log.Printf("Delivered pending voicemail to %s", voicemail.To)
}
}
}
func getIdentityPair(a, b string) (aa, bb *Identity, err error) {
// Ugly hack due to strange design of the Go datastore package.
// TODO: Clean up.
aa = new(Identity)
if err := store.Get(ctx, datastore.NameKey("Identity", a, nil), aa); err != nil {
aa = nil
}
bb = new(Identity)
if err := store.Get(ctx, datastore.NameKey("Identity", b, nil), bb); err != nil {
bb = nil
}
return
}
func logRequestTime(method, path string, start time.Time) {
log.Printf("Handled %s %s in %s", method, path, time.Since(start))
}
func postStream(accountId, streamId int64, fields url.Values) (stream *Stream, err error) {
var path string
if streamId > 0 {
path = fmt.Sprintf("streams/%d/chunks", streamId)
} else {
path = "streams"
}
ref, err := url.Parse(path)
if err != nil {
return
}
streamsURL := apiURL.ResolveReference(ref)
query := url.Values{
"on_behalf_of": {strconv.FormatInt(accountId, 10)},
}
streamsURL.RawQuery = query.Encode()
req, err := http.NewRequest("POST", streamsURL.String(), strings.NewReader(fields.Encode()))
if err != nil {
return
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", config.AccessToken))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, fmt.Errorf("%s (on behalf of %d) returned %s", req.URL.Path, accountId, resp.Status)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}
stream = new(Stream)
err = json.Unmarshal(body, stream)
return
}
func sendSMS(to, message string) (err error) {
fields := url.Values{
"From": {TwilioFromNumber},
"To": {to},
"Body": {message},
}
req, err := http.NewRequest("POST", TwilioMessages, strings.NewReader(fields.Encode()))
if err != nil {
return
}
req.SetBasicAuth(TwilioKeySid, TwilioKeySecret)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return
}
defer resp.Body.Close()
if resp.StatusCode != 201 {
return fmt.Errorf("%s returned %s", req.URL.Path, resp.Status)
}
return
}