gami

package
v0.0.0-...-e23add2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 14, 2020 License: Apache-2.0 Imports: 7 Imported by: 0

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

View Source
const (
	ORIG_TMOUT = 30000 // Originate timeout
	VER        = 0.2
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Aid

type Aid struct {
	// contains filtered or unexported fields
}

action id generator

func NewAid

func NewAid() *Aid

NewAid, Aid factory

func (*Aid) Generate

func (a *Aid) Generate() string

GenerateId, generate new action id

type Asterisk

type Asterisk struct {
	// contains filtered or unexported fields
}

main working entity

func NewAsterisk

func NewAsterisk(conn *net.Conn, f *func(error)) *Asterisk

NewAsterisk, Asterisk factory

func (*Asterisk) Bridge

func (a *Asterisk) Bridge(chan1, chan2 string, tone bool, f *func(Message)) error

Bridge, bridge two channels already in the PBX

func (*Asterisk) Command

func (a *Asterisk) Command(cmd string, f *func(Message)) error

Command, execute Asterisk CLI Command

func (*Asterisk) ConfbridgeKick

func (a *Asterisk) ConfbridgeKick(conf, chann string, f *func(Message)) error

ConfbridgeKick, kick a Confbridge user

func (*Asterisk) ConfbridgeList

func (a *Asterisk) ConfbridgeList(conference string, f *func(Message)) error

ConfbridgeList, list participants in a conference (generates multimessage response)

func (*Asterisk) ConfbridgeStartRecord

func (a *Asterisk) ConfbridgeStartRecord(conf, file string, f *func(Message)) error

ConfbridgeStartRecord, start conference record

func (*Asterisk) ConfbridgeStopRecord

func (a *Asterisk) ConfbridgeStopRecord(conf string, f *func(Message)) error

ConfbridgeStopRecord, stop conference record

func (*Asterisk) ConfbridgeToggleMute

func (a *Asterisk) ConfbridgeToggleMute(conf, chann string, mute bool, f *func(Message)) error

ConfbridgeToggleMute, mute/unmute a Confbridge user

func (*Asterisk) CreateConfig

func (a *Asterisk) CreateConfig(filename string, f *func(Message)) error

CreateConfig, create empty Asterisk config

func (*Asterisk) DbDel

func (a *Asterisk) DbDel(family, key string, f *func(Message)) error

DbDel, remove value from Asterisk DB

func (*Asterisk) DbDelTree

func (a *Asterisk) DbDelTree(family, key string, f *func(Message)) error

DbDelTree, remove family tree from Asterisk DB

func (*Asterisk) DbGet

func (a *Asterisk) DbGet(family, key string, f *func(Message)) error

DbGet, retrive data from Asterisk DB, response must be processed in f

func (*Asterisk) DbPut

func (a *Asterisk) DbPut(family, key, value string, f *func(Message)) error

DbPut, put data to Asterisk DB

func (*Asterisk) DefaultHandler

func (a *Asterisk) DefaultHandler(f *func(Message))

DefaultHandler, set default handler for all Asterisk messages

func (*Asterisk) DelCallback

func (a *Asterisk) DelCallback(m Message)

DelCallback, delete action callback (used by self-delete callbacks)

func (*Asterisk) GetConfbridgeList

func (a *Asterisk) GetConfbridgeList(conference string) ([]Message, error)

GetConfbridgeList, returns conference participants, blocks untill end

func (*Asterisk) GetConfig

func (a *Asterisk) GetConfig(filename, category string, json bool, f *func(Message)) error

GetConfig, get Asterisk config content (category ignored for JSON), response should be handled in callback function

func (*Asterisk) GetMeetmeList

func (a *Asterisk) GetMeetmeList(conference string) ([]Message, error)

GetMeetmeList, returns MeetMe conference participants, blocks untill end

func (*Asterisk) GetVar

func (a *Asterisk) GetVar(name, channel string, f *func(Message)) error

GetVar, get variable (response must be handled with callback function)

func (Asterisk) Hangup

func (a Asterisk) Hangup(channel string, f *func(Message)) error

Hangup, hangup Asterisk channel

func (*Asterisk) HoldCallbackAction

func (a *Asterisk) HoldCallbackAction(m Message, f *func(m Message)) error

HoldCallbackAction, send action with callback which deletes itself (used for multi-line responses) IMPORTANT: callback function must delete itself by own

func (*Asterisk) Login

func (a *Asterisk) Login(login string, password string) error

Login, logins to AMI and starts read dispatcher

func (Asterisk) Logoff

func (a Asterisk) Logoff() error

Logoff, logoff from AMI

func (*Asterisk) MeetmeList

func (a *Asterisk) MeetmeList(conference string, f *func(Message)) error

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

func (a *Asterisk) ModuleLoad(module, tload string, f *func(Message)) error

ModuleLoad, loads, unloads or reloads an Asterisk module in a running system

func (*Asterisk) Originate

func (a *Asterisk) Originate(o *Originate, vars map[string]string, f *func(Message)) error

Originate, make a call

func (*Asterisk) Reaload

func (a *Asterisk) Reaload(module string, f *func(Message)) error

Reload, reload Asterisk module

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

func (a *Asterisk) RegisterHandler(event string, f *func(m Message)) error

RegisterHandler, register callback for Asterisk event (one handler per event) return err if handler already exists

func (*Asterisk) SendAction

func (a *Asterisk) SendAction(m Message, f *func(m Message)) error

SendAction, universal action send

func (*Asterisk) SetVar

func (a *Asterisk) SetVar(name, value, channel string, f *func(Message)) error

SetVar, set variable

func (*Asterisk) UnregisterHandler

func (a *Asterisk) UnregisterHandler(event string)

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

func (*Asterisk) UserEvent

func (a *Asterisk) UserEvent(name string, headers map[string]string, f *func(Message)) error

UserEvent, send an arbitrary event

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 Message

type Message map[string]string

basic Asterisk message

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

func NewOriginate(channel, context, exten, priority string) *Originate

NewOriginate, Originate default values constructor (to context)

func NewOriginateApp

func NewOriginateApp(channel, app, data string) *Originate

NewOriginateApp, constructor for originate to application

type UpdateConfigAction

type UpdateConfigAction struct {
	Action   ConfigAction
	Category string
	Variable string
	Value    string
	Match    string
	Line     string
}

Directories

Path Synopsis
Asterisk Gateway Interface support Usage: a := agi.NewAgi() r := a.Answer() checkErr(r.Err) ...
Asterisk Gateway Interface support Usage: a := agi.NewAgi() r := a.Answer() checkErr(r.Err) ...

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL