-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoc.go
84 lines (61 loc) · 2.17 KB
/
doc.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
/*
Package gami implements simple Asterisk Manager Interface library.
It's not handle any network layer, just parsing or creating packets
and runs callback for it (if registered).
Start working:
conn, err := net.Dial("tcp", "astserver:5038")
if err != nil {
// error handling
}
var a gami.Asterisk
a = gami.NewAsterisk(&conn, nil) // 2nd parameter network error callback
err = a.Login("user", "password") // will block until receive response
if err != nil {
// login error handling
}
Placing simple command:
ping := gami.Message{"Action":"Ping"} // ActionID will be overwritten
pingCallback := func(m Message) {
if m["Message"] == "Pong" {
fmt.Println("Hurray!")
}
}
a.SendAction(ping, &pingCallback) // callback will be automatically executed and deleted
Placing a call:
o := gami.NewOriginateApp("SIP/1234", "Playback", "hello-world")
a.Originate(o, nil, nil) // 2nd parameter - variables passed to channel, 3rd - callback
Event handlers:
hangupHandler := func(m gami.Message) {
fmt.Printf("Hangup event received for channel %s\n", m["Channel"])
}
a.RegisterHandler("Hangup", &hangupHandler)
...
a.UnregisterHandler("Hangup")
Default handler:
This handler will execute for each message received from Asterisk, useful for debugging.
dh := func(m gami.Message) {
fmt.Println(m)
}
a.DefaultHandler(&dh)
Multi-message handlers:
Some actions (CoreShowChannels example) has multi-message output. For this point need to use
"self-delete" callbacks.
// this callback will run but never be deleted until own
cscf := func() func(gami.Message) { // using closure for storing data
ml := []gami.Message{}
return func(m gami.Message) {
ml = append(ml, m)
if m["EventList"] == "Complete" { // got multi-message end
// doing something
fmt.Println("Destroying self...")
a.DelCallback(m) // when finished must be removed
}
}
}()
m := gami.Message{"Action": "CoreShowChannels"}
a.HoldCallbackAction(m, &cscf)
Finishing:
a.Logoff() // if a created with network error callback it will be executed
*/
package gami
// Author: Vasiliy Kovalenko <[email protected]>