gami

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2018 License: MIT Imports: 10 Imported by: 2

README

GAMI

GO - Asterisk AMI Interface

communicate with the Asterisk AMI, Actions and Events.

Example connecting to Asterisk and Send Action get Events.

package main
import (
	"log"
	"github.com/bit4bit/gami"
	"github.com/bit4bit/gami/event"
)

func main() {
	ami, err := gami.Dial("127.0.0.1:5038")
	if err != nil {
		log.Fatal(err)
	}
	
	//install manager
	go func() {
		for {
			select {
			//handle network errors
			case err := <-ami.NetError:
				log.Println("Network Error:", err)
				//try new connection every second
				<-time.After(time.Second)
				if err := ami.Reconnect(); err == nil {
					//call start actions
					ami.Action("Events", gami.Params{"EventMask": "on"})
				}
				
			case err := <-ami.Error:
				log.Println("error:", err)
			//wait events and process
			case ev := <-ami.Events:
				log.Println("Event Detect: %v", *ev)
				//if want type of events
				log.Println("EventType:", event.New(ev))
			}
		}
	}()
	
	if err := ami.Login("admin", "root"); err != nil {
		log.Fatal(err)
	}
	
	
	if rs, err = ami.Action("Ping", nil); err != nil {
		log.Fatal(rs)
	}
	
	//async actions
	rsPing, rsErr := ami.AsyncAction("Ping", gami.Params{"ActionID": "pingo"})
	if rsErr != nil {
		log.Fatal(rsErr)
	}
						
	if rs, err = ami.Action("Events", ami.Params{"EventMask":"on"}); err != nil {
		log.Fatal(err)
	}
	
	log.Println("ping:", <-rsPing)
	
	ami.Close()
}

###TLS SUPPORT In order to use TLS connection to manager interface you could Dial with additional parameters

//without TLS
ami, err := gami.Dial("127.0.0.1:5038")

//if certificate is trusted
ami, err := gami.Dial("127.0.0.1:5039", gami.UseTLS)

//if self signed certificate
ami, err := gami.Dial("127.0.0.1:5039", gami.UseTLS, gami.UnsecureTLS)

//if custom tls configuration
ami, err := gami.Dial("127.0.0.1:5039", gami.UseTLSConfig(&tls.Config{}))

WARNING: Only Asterisk >=1.6 supports TLS connection to AMI and it needs additional configuration(follow the Asterisk AMI configuration documentation)

CURRENT EVENT TYPES

The events use documentation and struct from PAMI.

use bit4bit/gami/event.New() for get this struct from raw event

EVENT ID TYPE TEST
Newchannel YES
Newexten YES
Newstate YES
Dial YES
ExtensionStatus YES
Hangup YES
PeerStatus YES
PeerEntry YES
VarSet YES
AgentLogin YES
Agents YES
AgentLogoff YES
AgentConnect YES
RTPReceiverStats YES
RTPSenderStats YES
Bridge YES
CEL YES

Documentation

Overview

Package gami provites primitives for interacting with Asterisk AMI

Basic Usage

ami, err := gami.Dial("127.0.0.1:5038")
if err != nil {
	fmt.Print(err)
	os.Exit(1)
}
ami.Run()
defer ami.Close()

//install manager
go func() {
	for {
		select {
		//handle network errors
		case err := <-ami.NetError:
			log.Println("Network Error:", err)
			//try new connection every second
			<-time.After(time.Second)
			if err := ami.Reconnect(); err == nil {
				//call start actions
				ami.Action("Events", gami.Params{"EventMask": "on"})
			}

		case err := <-ami.Error:
			log.Println("error:", err)
		//wait events and process
		case ev := <-ami.Events:
			log.Println("Event Detect: %v", *ev)
			//if want type of events
			log.Println("EventType:", event.New(ev))
		}
	}
}()

if err := ami.Login("admin", "root"); err != nil {
	log.Fatal(err)
}

if rs, err = ami.Action("Ping", nil); err == nil {
	log.Fatal(rs)
}

//or with can do async
pingResp, pingErr := ami.AsyncAction("Ping", gami.Params{"ActionID": "miping"})
if pingErr != nil {
	log.Fatal(pingErr)
}

if rs, err = ami.Action("Events", ami.Params{"EventMask":"on"}); err != nil {
	fmt.Print(err)
}

log.Println("future ping:", <-pingResp)

Index

Constants

This section is empty.

Variables

View Source
var ErrNotAMI = errors.New("Server not AMI interface")

ErrNotAMI indicates that the connection established is not to an Asterisk AMI service

Functions

func UnsecureTLS

func UnsecureTLS(c *AMIClient)

UnsecureTLS configures the AMIClient that it should ignore certificate errors when connecting via TLS -- DO NOT USE FIXME: this function is pointless; this should be supplied as a part of the AMI config

func UseTLS

func UseTLS(c *AMIClient)

UseTLS configures the AMIClient to use TLS -- DO NOT USE FIXME: this function is pointless; this should be supplied as a part of the AMI config

func UseTLSConfig

func UseTLSConfig(config *tls.Config) func(*AMIClient)

UseTLSConfig configures the AMIClient to use the given TLS Config -- DO NOT USE FIXME: this function is pointless; this should be supplied as a part of the AMI config

Types

type AMIClient

type AMIClient struct {
	Conn io.ReadWriteCloser

	// Events for client parse
	Events chan *AMIEvent

	// Error Raise on logic
	Error chan error

	//NetError a network error
	NetError chan error
	// contains filtered or unexported fields
}

AMIClient a connection to AMI server

func Dial

func Dial(addr string, options ...func(*AMIClient)) (*AMIClient, error)

Dial create a new connection to AMI

func NewFromRWC added in v0.2.0

func NewFromRWC(conn io.ReadWriteCloser, options ...func(*AMIClient)) (*AMIClient, error)

NewFromRWC takes an existing ReadWriteCloser and uses it as the connection for AMI

func (*AMIClient) Action

func (client *AMIClient) Action(action string, params Params) (*AMIResponse, error)

Action send with params

func (*AMIClient) AsyncAction

func (client *AMIClient) AsyncAction(action string, params Params) (<-chan *AMIResponse, error)

AsyncAction return chan for wait response of action with parameter *ActionID* this can be helpful for massive actions,

func (*AMIClient) Close

func (client *AMIClient) Close()

Close the connection to AMI

func (*AMIClient) Login

func (client *AMIClient) Login(username, password string) error

Login authenticate to AMI

func (*AMIClient) Reconnect

func (client *AMIClient) Reconnect() error

Reconnect the session, autologin if a new network error it put on client.NetError

func (*AMIClient) Run

func (client *AMIClient) Run()

Run process socket waiting events and responses

type AMIEvent

type AMIEvent struct {
	//Identification of event Event: xxxx
	ID string

	Privilege []string

	// Params  of arguments received
	Params map[string]string
}

AMIEvent it's a representation of Event readed

type AMIResponse

type AMIResponse struct {
	ID     string
	Status string
	Params map[string]string
}

AMIResponse from action

type Params

type Params map[string]string

Params for the actions

Directories

Path Synopsis
_examples
Package event for AMI Package event for AMI Package event for AMI Package event for AMI Package event for AMI Package event for AMI Package event decoder This Build Type of Event received Package event for AMI Package event for AMI Package event for AMI Package event for AMI Package event for AMI Package event for AMI Package event for AMI Package event for AMI Package event for AMI Package event for AMI
Package event for AMI Package event for AMI Package event for AMI Package event for AMI Package event for AMI Package event for AMI Package event decoder This Build Type of Event received Package event for AMI Package event for AMI Package event for AMI Package event for AMI Package event for AMI Package event for AMI Package event for AMI Package event for AMI Package event for AMI Package event for AMI

Jump to

Keyboard shortcuts

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