-
Notifications
You must be signed in to change notification settings - Fork 73
/
main.go
82 lines (63 loc) · 1.59 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
package main
import (
"context"
"github.com/inconshreveable/log15"
"github.com/CyCoreSystems/ari"
"github.com/CyCoreSystems/ari/client/native"
"github.com/CyCoreSystems/ari/ext/play"
)
var log = log15.New()
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// connect
native.Logger = log
log.Info("Connecting to ARI")
cl, err := native.Connect(&native.Options{
Application: "test",
Username: "admin",
Password: "admin",
URL: "http://localhost:8088/ari",
WebsocketURL: "ws://localhost:8088/ari/events",
})
if err != nil {
log.Error("Failed to build ARI client", "error", err)
return
}
// setup app
log.Info("Listening for new calls")
sub := cl.Bus().Subscribe(nil, "StasisStart")
for {
select {
case e := <-sub.Events():
v := e.(*ari.StasisStart)
log.Info("Got stasis start", "channel", v.Channel.ID)
go app(ctx, cl.Channel().Get(v.Key(ari.ChannelKey, v.Channel.ID)))
case <-ctx.Done():
return
}
}
}
func app(ctx context.Context, h *ari.ChannelHandle) {
defer h.Hangup()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
log.Info("Running app", "channel", h.ID())
end := h.Subscribe(ari.Events.StasisEnd)
defer end.Cancel()
// End the app when the channel goes away
go func() {
<-end.Events()
cancel()
}()
if err := h.Answer(); err != nil {
log.Error("failed to answer call", "error", err)
return
}
if err := play.Play(ctx, h, play.URI("sound:tt-monkeys")).Err(); err != nil {
log.Error("failed to play sound", "error", err)
return
}
log.Info("completed playback")
return
}