This repository has been archived by the owner on Sep 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgo-ari-client.go
377 lines (344 loc) · 9.24 KB
/
go-ari-client.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
370
371
372
373
374
375
376
377
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"os/signal"
"syscall"
"go-ari-library"
"database/sql"
_ "github.com/go-sql-driver/mysql"
// "github.com/coopernurse/gorp"
)
/* 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 Main
authenticate:
voicemail_box
pin
check if valid
true: func(leave), goto(main)
false: decrement tries, goto(authenticate)
main:
if new messages exist
true: play(you have new messages)
false: continue
1: new messages
2: change folders
0 new
1 old
2 work
3 family
4 friends
# cancel
3: advanced options
4 outgoing call
enter message to call, then press pound, * to cancel
5 leave a message
1 extension
2 directory (won't implement)
0: mailbox options
1 unavailable msg
2 busy msg
3 name
4 temporary greeting
5 password
* main menu
*: help
#: exit
*/
/* 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
*/
var (
config Config
db *sql.DB
getMessages *sql.Stmt
getGreeting *sql.Stmt
insertNewMsg *sql.Stmt
)
type Config struct {
MySQLURL string `json:"mysql_url"`
Applications []string `json:"applications"`
MessageBus string `json:"message_bus"`
BusConfig interface{} `json:"bus_config"`
}
type vmInternal struct {
Mailbox string
Retries int
ActivePlaybacks []string
}
func (v *vmInternal) AddPlayback(id string) {
v.ActivePlaybacks = append(v.ActivePlaybacks, id)
}
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
}
}
}
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)
}
getMessages, err = db.Prepare("SELECT * FROM voicemail_messages WHERE mailbox=? AND folder=?")
if err != nil {
log.Fatal(err)
}
getGreeting, err = db.Prepare("SELECT recording_id FROM voicemail_messages WHERE mailbox=? AND folder=?")
if err != nil {
log.Fatal(err)
}
insertNewMsg, err = db.Prepare("INSERT INTO voicemail_messages values (NULL, ?, ?, 'New', NULL, 0, ?)")
if err != nil {
log.Fatal(err)
}
}
// startVMMainApp starts the primary voicemail application for retrieving messages.
func startVMMainApp(app string) {
return
}
// startVMApp starts the primary voicemail application for leaving messages.
func startVMApp(app string) {
fmt.Printf("Started application: %s", 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, vmState = state(a, vmState)
}
}
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)
a.ChannelsAnswer(s.Channel.Id)
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(s.Channel.Id, "sound:vm-theperson", "en")
vmState.AddPlayback(pb.Id)
pb, _ = a.ChannelsPlay(s.Channel.Id, g, "en")
vmState.AddPlayback(pb.Id)
pb, _ = a.ChannelsPlay(s.Channel.Id, "sound:vm-isunavail", "en")
vmState.AddPlayback(pb.Id)
} else {
a.ChannelsPlay(s.Channel.Id, g)
}
vmState.Mailbox = s.Args[0]
vmState.Retries = 0
//messageID := ari.UUID()
//a.ChannelsRecord(s.Channel.Id, messageID, "ulaw", "", "", "", "true")
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)
fmt.Printf("Playback ID is %s\n", p.Playback.Id)
vmState.RemovePlayback(p.Playback.Id)
fmt.Printf("Active Playbacks is: %s\n", vmState.ActivePlaybacks)
if len(vmState.ActivePlaybacks) == 0 {
return leaveMessage, vmState
}
return introPlayed, vmState
case "ChannelDtmfReceived":
var c ari.ChannelDtmfReceived
json.Unmarshal([]byte(event.ARI_Body), &c)
switch c.Digit {
case "#":
for _, val := range vmState.ActivePlaybacks {
a.PlaybacksStop(val)
vmState.RemovePlayback(val)
}
return leaveMessage, vmState
}
}
}
return introPlayed, vmState
}
func leaveMessage(a *ari.AppInstance, vmState *vmInternal) (vmstateFunc, *vmInternal) {
fmt.Println("entered leaveMessage")
select {
case event := <- a.Events:
//menuMaxTimesThrough := 3
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 "1":
//pb, _ := a.ChannelsPlay(c.Channel.Id, "sound:tt-monkeys", "en")
//pb, _ := a.ChannelsPlay(c.Channel.Id, "digits:1234567890", "en")
//pb, _ := a.ChannelsPlay(c.Channel.Id, "number:123890", "en")
//pb, _ := a.ChannelsPlay(c.Channel.Id, "characters:abcdefghijklmnop", "en")
pb, _ := a.ChannelsPlay(c.Channel.Id, "tone:congestion")
vmState.AddPlayback(pb.Id)
return leaveMessage, vmState
case "2":
for _, val := range vmState.ActivePlaybacks {
a.PlaybacksStop(val)
vmState.RemovePlayback(val)
}
return leaveMessage, vmState
}
}
}
return leaveMessage, vmState
}
// 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}, "")
}
// DEPRECATED: ConsumeEvents pulls events off the channel and passes to the application.
func startAppHandler(a *ari.AppInstance) {
// this is where you would hand off the information to your application
for event := range a.Events {
fmt.Println("got event")
switch event.Type {
case "StasisStart":
var s ari.StasisStart
json.Unmarshal([]byte(event.ARI_Body), &s)
a.ChannelsAnswer(s.Channel.Id)
fmt.Println("Got start message")
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 "1":
a.ChannelsPlay(c.Channel.Id, "sound:tt-monkeys", "en")
case "2":
a.ChannelsPlay(c.Channel.Id, "sound:tt-weasels")
case "3":
a.ChannelsPlay(c.Channel.Id, "sound:demo-congrats")
case "4":
err := a.MailboxesUpdate("1234@test", 0, 0)
if err != nil {
fmt.Println(err)
}
case "5":
m, err := a.MailboxesGet("1234@test")
if err != nil {
fmt.Println(err)
} else {
fmt.Printf("Mailbox info is: %v", m)
}
}
case "ChannelHangupRequest":
fmt.Println("Channel hung up")
case "StasisEnd":
fmt.Println("Got end message")
}
}
}
// 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 go-ari-client")
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{}
}