Documentation
¶
Overview ¶
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
Index ¶
- Constants
- type Aid
- type Asterisk
- func (a *Asterisk) Bridge(chan1, chan2 string, tone bool, f *func(Message)) error
- func (a *Asterisk) Command(cmd string, f *func(Message)) error
- func (a *Asterisk) ConfbridgeKick(conf, chann string, f *func(Message)) error
- func (a *Asterisk) ConfbridgeList(conference string, f *func(Message)) error
- func (a *Asterisk) ConfbridgeStartRecord(conf, file string, f *func(Message)) error
- func (a *Asterisk) ConfbridgeStopRecord(conf string, f *func(Message)) error
- func (a *Asterisk) ConfbridgeToggleMute(conf, chann string, mute bool, f *func(Message)) error
- func (a *Asterisk) CreateConfig(filename string, f *func(Message)) error
- func (a *Asterisk) DbDel(family, key string, f *func(Message)) error
- func (a *Asterisk) DbDelTree(family, key string, f *func(Message)) error
- func (a *Asterisk) DbGet(family, key string, f *func(Message)) error
- func (a *Asterisk) DbPut(family, key, value string, f *func(Message)) error
- func (a *Asterisk) DefaultHandler(f *func(Message))
- func (a *Asterisk) DelCallback(m Message)
- func (a *Asterisk) GetConfbridgeList(conference string) ([]Message, error)
- func (a *Asterisk) GetConfig(filename, category string, json bool, f *func(Message)) error
- func (a *Asterisk) GetMeetmeList(conference string) ([]Message, error)
- func (a *Asterisk) GetVar(name, channel string, f *func(Message)) error
- func (a Asterisk) Hangup(channel string, f *func(Message)) error
- func (a *Asterisk) HoldCallbackAction(m Message, f *func(m Message)) error
- func (a *Asterisk) Login(login string, password string) error
- func (a Asterisk) Logoff() error
- func (a *Asterisk) MeetmeList(conference string, f *func(Message)) error
- func (a *Asterisk) MessageSend(to, from, body string, useBase64 bool, vars map[string]string, ...) error
- func (a *Asterisk) ModuleLoad(module, tload string, f *func(Message)) error
- func (a *Asterisk) Originate(o *Originate, vars map[string]string, f *func(Message)) error
- func (a *Asterisk) Reaload(module string, f *func(Message)) error
- func (a Asterisk) Redirect(channel string, context string, exten string, priority string, ...) error
- func (a *Asterisk) RegisterHandler(event string, f *func(m Message)) error
- func (a *Asterisk) SendAction(m Message, f *func(m Message)) error
- func (a *Asterisk) SetVar(name, value, channel string, f *func(Message)) error
- func (a *Asterisk) UnregisterHandler(event string)
- func (a *Asterisk) Updateconfig(srcFile, dstFile, reaload string, actions []UpdateConfigAction, ...) error
- func (a *Asterisk) UserEvent(name string, headers map[string]string, f *func(Message)) error
- type ConfigAction
- type Message
- type Originate
- type UpdateConfigAction
Constants ¶
const ( ORIG_TMOUT = 30000 // Originate timeout VER = 0.2 )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Asterisk ¶
type Asterisk struct {
// contains filtered or unexported fields
}
main working entity
func NewAsterisk ¶
NewAsterisk, Asterisk factory
func (*Asterisk) ConfbridgeKick ¶
ConfbridgeKick, kick a Confbridge user
func (*Asterisk) ConfbridgeList ¶
ConfbridgeList, list participants in a conference (generates multimessage response)
func (*Asterisk) ConfbridgeStartRecord ¶
ConfbridgeStartRecord, start conference record
func (*Asterisk) ConfbridgeStopRecord ¶
ConfbridgeStopRecord, stop conference record
func (*Asterisk) ConfbridgeToggleMute ¶
ConfbridgeToggleMute, mute/unmute a Confbridge user
func (*Asterisk) CreateConfig ¶
CreateConfig, create empty Asterisk config
func (*Asterisk) DefaultHandler ¶
DefaultHandler, set default handler for all Asterisk messages
func (*Asterisk) DelCallback ¶
DelCallback, delete action callback (used by self-delete callbacks)
func (*Asterisk) GetConfbridgeList ¶
GetConfbridgeList, returns conference participants, blocks untill end
func (*Asterisk) GetConfig ¶
GetConfig, get Asterisk config content (category ignored for JSON), response should be handled in callback function
func (*Asterisk) GetMeetmeList ¶
GetMeetmeList, returns MeetMe conference participants, blocks untill end
func (*Asterisk) HoldCallbackAction ¶
HoldCallbackAction, send action with callback which deletes itself (used for multi-line responses) IMPORTANT: callback function must delete itself by own
func (*Asterisk) MeetmeList ¶
MeetmeList, list participants in a MeetMe conference (generates multimessage response) if conference empty string will return for all conferences
func (*Asterisk) MessageSend ¶
func (a *Asterisk) MessageSend(to, from, body string, useBase64 bool, vars map[string]string, f *func(Message)) error
MessageSend, send message (pjsip, sip, xmpp)
func (*Asterisk) ModuleLoad ¶
ModuleLoad, loads, unloads or reloads an Asterisk module in a running system
func (Asterisk) Redirect ¶
func (a Asterisk) Redirect(channel string, context string, exten string, priority string, f *func(Message)) error
Redirect, redirect Asterisk channel
func (*Asterisk) RegisterHandler ¶
RegisterHandler, register callback for Asterisk event (one handler per event) return err if handler already exists
func (*Asterisk) SendAction ¶
SendAction, universal action send
func (*Asterisk) UnregisterHandler ¶
UnregisterHandler, deregister callback for event
func (*Asterisk) Updateconfig ¶
func (a *Asterisk) Updateconfig(srcFile, dstFile, reaload string, actions []UpdateConfigAction, f *func(Message)) error
UpdateConfig, modify Asterisk config
type ConfigAction ¶
type ConfigAction string
const ( ConfNewCat ConfigAction = "NewCat" ConfRenameCat ConfigAction = "RenameCat" ConfDelCat ConfigAction = "DelCat" ConfEmptyCat ConfigAction = "EmptyCat" ConfUpdate ConfigAction = "Update" ConfDelete ConfigAction = "Delete" ConfAppend ConfigAction = "Append" ConfInsert ConfigAction = "Insert" )
type Originate ¶
type Originate struct {
Channel string // channel to which originate
Context string // context to move after originate success
Exten string // exten to move after originate success
Priority string // priority to move after originate success
Timeout int // originate timeout ms
CallerID string // caller identification string
Account string // used for CDR
Application string // application to execute after successful originate
Data string // data passed to application
Async bool // asynchronous call
}
Originate, struct used in Originate command if pointed Context and Application, Context has higher priority
func NewOriginate ¶
NewOriginate, Originate default values constructor (to context)
func NewOriginateApp ¶
NewOriginateApp, constructor for originate to application