-
Notifications
You must be signed in to change notification settings - Fork 1
/
ari-voicemail.go
369 lines (335 loc) · 10.1 KB
/
ari-voicemail.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"os/signal"
"strings"
"syscall"
"time"
"database/sql"
_ "github.com/go-sql-driver/mysql"
"go-ari-library"
)
/* Voicemail Users
id (auto)
mailbox (varchar)
domain (varchar)
pin (varchar)
email (varchar)
first_name (varchar)
last_name (varchar)
*/
/* Voicemail Messages
id (auto)
mailbox (varchar)
domain (varchar)
folder (varchar)
timestamp (date)
read (bool)
recording_id (varchar)
*/
/* Voicemail
Get mailbox
Play unavailable / busy message
check if should play temporary greeting
if no message recorded, try name
if no name, play generic message
Leave message
Record message
1 to accept this recording
2 to listen to it
3 to re-record this message
* help
*/
// setup global variables
var (
config Config
db *sql.DB
getMessages *sql.Stmt
getMessCount *sql.Stmt
getGreeting *sql.Stmt
insertNewMsg *sql.Stmt
checkPass *sql.Stmt
)
// Config struct contains the variable configuration options from the config file.
type Config struct {
MySQLURL string `json:"mysql_url"`
Applications []string `json:"applications"`
MessageBus string `json:"message_bus"`
BusConfig interface{} `json:"bus_config"`
}
// vmInternal struct holds information about the internal state of a running voicemail application instance.
type vmInternal struct {
Mailbox string
Retries int
ChannelID string
ActiveRecording string
ActivePlaybacks []string
}
// AddPlayback adds a playback to the string slice that holds the list of file IDs of the in-flight playback files.
func (v *vmInternal) AddPlayback(id string) {
v.ActivePlaybacks = append(v.ActivePlaybacks, id)
}
// RemovePlayback removes the file ID of the in-flight playback files as they have been played to the channel.
func (v *vmInternal) RemovePlayback(id string) {
for i := range v.ActivePlaybacks {
if v.ActivePlaybacks[i] == id {
v.ActivePlaybacks = append(v.ActivePlaybacks[:i], v.ActivePlaybacks[i+1:]...)
return
}
}
}
// initialize the client side configuration and setup.
func init() {
var err error
// parse the configuration file and get data from it
configpath := flag.String("config", "./config_client.json", "Path to config file")
flag.Parse()
configfile, err := ioutil.ReadFile(*configpath)
if err != nil {
log.Fatal(err)
}
// read in the configuration file and unmarshal the json, storing it in 'config'
json.Unmarshal(configfile, &config)
db, err = sql.Open("mysql", config.MySQLURL)
if err != nil {
log.Fatal(err)
}
// retrieve the messages for the current mailbox and folder
getMessages, err = db.Prepare("SELECT * FROM voicemail_messages WHERE mailbox=? AND domain=? AND folder=?")
if err != nil {
log.Fatal(err)
}
// get the count of messages in a particular folder
getMessCount, err = db.Prepare("SELECT COUNT(*) FROM voicemail_messages WHERE mailbox=? AND domain=? AND folder=?")
if err != nil {
log.Fatal(err)
}
// get the recording_id containing the appropriate greeting for the current mailbox and folder
getGreeting, err = db.Prepare("SELECT recording_id FROM voicemail_messages WHERE mailbox=? AND domain=? AND folder=?")
if err != nil {
log.Fatal(err)
}
// insert a new message into the list of voicemail messages for the New folder
insertNewMsg, err = db.Prepare("INSERT INTO voicemail_messages values (NULL, ?, ?, 'New', NULL, 0, ?)")
if err != nil {
log.Fatal(err)
}
// check if a password is correct for the provided mailbox and domain
checkPass, err = db.Prepare("SELECT mailbox from voicemail_users where mailbox=? and domain=? and pin=SHA2(?, 256)")
if err != nil {
log.Fatal(err)
}
}
// startVMApp starts the primary voicemail application for leaving messages.
func startVMApp(app string) {
fmt.Printf("Started application: %s\n", app)
application := new(ari.App)
application.Init(app, startVMHandler)
select {
case <-application.Stop:
return
}
}
// New stuff that we need to figure out and clean up
type vmstateFunc func(a *ari.AppInstance, vmState *vmInternal) (vmstateFunc, *vmInternal)
// startVMHandler initializes a new voicemail application instance
func startVMHandler(a *ari.AppInstance) {
v := new(vmInternal)
state, vmState := vmstartState(a, v)
for state != nil {
state, vmState = state(a, vmState)
}
fmt.Println("exiting app instance")
}
func vmstartState(a *ari.AppInstance, vmState *vmInternal) (vmstateFunc, *vmInternal) {
select {
case event := <-a.Events:
switch event.Type {
case "StasisStart":
fmt.Println("Got start message")
var s ari.StasisStart
json.Unmarshal([]byte(event.ARI_Body), &s)
vmState.ChannelID = s.Channel.Id
a.ChannelsAnswer(vmState.ChannelID)
g := getGreetURI(s.Args[0], "unavailable")
fmt.Printf("Greeting URI is %s\n", g)
if strings.HasPrefix(g, "digits") {
fmt.Println("Has digits")
pb, _ := a.ChannelsPlay(vmState.ChannelID, "sound:vm-theperson", "en")
vmState.AddPlayback(pb.Id)
pb, _ = a.ChannelsPlay(vmState.ChannelID, g, "en")
vmState.AddPlayback(pb.Id)
pb, _ = a.ChannelsPlay(vmState.ChannelID, "sound:vm-isunavail", "en")
vmState.AddPlayback(pb.Id)
pb, _ = a.ChannelsPlay(vmState.ChannelID, "sound:vm-intro")
vmState.AddPlayback(pb.Id)
} else {
a.ChannelsPlay(vmState.ChannelID, g)
}
vmState.Mailbox = s.Args[0]
vmState.Retries = 0
return introPlayed, vmState
}
}
return vmstartState, vmState
}
func introPlayed(a *ari.AppInstance, vmState *vmInternal) (vmstateFunc, *vmInternal) {
select {
case event := <-a.Events:
switch event.Type {
case "PlaybackFinished":
var p ari.PlaybackFinished
json.Unmarshal([]byte(event.ARI_Body), &p)
vmState.RemovePlayback(p.Playback.Id)
if len(vmState.ActivePlaybacks) == 0 {
return startRecording, vmState
}
return introPlayed, vmState
case "ChannelDtmfReceived":
var c ari.ChannelDtmfReceived
json.Unmarshal([]byte(event.ARI_Body), &c)
switch c.Digit {
case "#":
playbacksStop(a, vmState)
return startRecording, vmState
}
}
}
return introPlayed, vmState
}
func startRecording(a *ari.AppInstance, vmState *vmInternal) (vmstateFunc, *vmInternal) {
fmt.Println("entered startRecording")
messageID := ari.UUID()
vmState.ActiveRecording = messageID
pb, _ := a.ChannelsPlay(vmState.ChannelID, "sound:beep")
vmState.AddPlayback(pb.Id)
a.ChannelsRecord(vmState.ChannelID, messageID, "ulaw")
return leaveMessage, vmState
}
func leaveMessage(a *ari.AppInstance, vmState *vmInternal) (vmstateFunc, *vmInternal) {
fmt.Println("entered leaveMessage")
select {
case event := <-a.Events:
switch event.Type {
case "ChannelDtmfReceived":
var c ari.ChannelDtmfReceived
fmt.Println("Got DTMF")
json.Unmarshal([]byte(event.ARI_Body), &c)
fmt.Printf("We got DTMF: %s\n", c.Digit)
switch c.Digit {
case "#":
a.RecordingsStop(vmState.ActiveRecording)
pb, _ := a.ChannelsPlay(vmState.ChannelID, "sound:vm-review")
vmState.AddPlayback(pb.Id)
return listenMessage, vmState
}
case "ChannelHangupRequest":
var c ari.ChannelHangupRequest
json.Unmarshal([]byte(event.ARI_Body), &c)
a.RecordingsStop(vmState.ActiveRecording)
saveMessage(vmState)
return hangupVM, vmState
}
}
return leaveMessage, vmState
}
func listenMessage(a *ari.AppInstance, vmState *vmInternal) (vmstateFunc, *vmInternal) {
select {
case event := <-a.Events:
//menuMaxTimesThrough := 3
switch event.Type {
case "ChannelDtmfReceived":
var c ari.ChannelDtmfReceived
json.Unmarshal([]byte(event.ARI_Body), &c)
switch c.Digit {
case "1":
playbacksStop(a, vmState)
saveMessage(vmState)
return hangupVM, vmState
case "2":
playbacksStop(a, vmState)
pb, _ := a.ChannelsPlay(vmState.ChannelID, strings.Join([]string{"recording:", vmState.ActiveRecording}, ""))
vmState.AddPlayback(pb.Id)
case "3":
playbacksStop(a, vmState)
a.RecordingsDeleteStored(vmState.ActiveRecording)
return leaveMessage, vmState
case "*":
playbacksStop(a, vmState)
pb, _ := a.ChannelsPlay(vmState.ChannelID, "sound:vm-review")
vmState.AddPlayback(pb.Id)
}
}
case <-time.After(10 * time.Second):
playbacksStop(a, vmState)
pb, _ := a.ChannelsPlay(vmState.ChannelID, "sound:vm-review")
vmState.AddPlayback(pb.Id)
}
return listenMessage, vmState
}
func hangupVM(a *ari.AppInstance, vmState *vmInternal) (vmstateFunc, *vmInternal) {
a.ChannelsHangup(vmState.ChannelID)
return nil, vmState
}
func playbacksStop(a *ari.AppInstance, vmState *vmInternal) {
for _, val := range vmState.ActivePlaybacks {
a.PlaybacksStop(val)
vmState.RemovePlayback(val)
}
}
// getGreetURI returns the recording ID for the mailbox playback.
// Returns a recording: <greetingID> if a recording URI is available.
// Returns a digits: <mailbox> value if no recording was available.
func getGreetURI(mailbox string, greetType string) string {
var greetingID string
db.Ping()
rows, err := getGreeting.Query(mailbox, greetType)
if err != nil {
fmt.Println(err)
return strings.Join([]string{"digits:", mailbox}, "")
}
for rows.Next() {
err = rows.Scan(&greetingID)
fmt.Printf("greetingID is %s\n", greetingID)
if err != nil || greetingID == "" {
return strings.Join([]string{"digits:", mailbox}, "")
}
}
if greetingID == "" {
return strings.Join([]string{"digits:", mailbox}, "")
}
return strings.Join([]string{"recording:", greetingID}, "")
}
func saveMessage(vmState *vmInternal) error {
insertNewMsg.Exec(vmState.Mailbox, "example.com", vmState.ActiveRecording);
return nil;
}
// signalCatcher is a function to allows us to stop the application through an
// operating system signal.
func signalCatcher() {
ch := make(chan os.Signal)
signal.Notify(ch, syscall.SIGINT)
sig := <-ch
log.Printf("Signal received: %v", sig)
os.Exit(0)
}
func main() {
fmt.Println("Welcome to the ARI voicemail written in GO")
ari.InitBus(config.MessageBus, config.BusConfig)
for _, app := range config.Applications {
// create consumer that uses the inboundEvents and parses them onto the parsedEvents channel
switch app {
case "voicemail":
go startVMApp(app)
case "voicemailmain":
go startVMMainApp(app)
}
}
go signalCatcher() // listen for os signal to stop the application
select {}
}