amigo

package module
v0.11.1-0...-82f128c Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2020 License: MIT Imports: 11 Imported by: 0

README

amigo

Asterisk AMI connector on golang.

Attention! API has been changed in v0.1.2. Please read the godoc.

Usage is pretty simple.

Installation:

go get github.com/ivahaev/amigo

Using

Import module to your project:

import "github.com/ivahaev/amigo"

Then use:

package main

import (
	"fmt"

	"github.com/ivahaev/amigo"
)

// Creating hanlder functions
func DeviceStateChangeHandler(m map[string]string) {
	fmt.Printf("DeviceStateChange event received: %v\n", m)
}

func DefaultHandler(m map[string]string) {
	fmt.Printf("Event received: %v\n", m)
}

func main() {
	fmt.Println("Init Amigo")

	settings := &amigo.Settings{Username: "username", Password: "password", Host: "host"}
	a := amigo.New(settings)

	a.Connect()

	// Listen for connection events
	a.On("connect", func(message string) {
		fmt.Println("Connected", message)
	})
	a.On("error", func(message string) {
		fmt.Println("Connection error:", message)
	})

	// Registering handler function for event "DeviceStateChange"
	a.RegisterHandler("DeviceStateChange", DeviceStateChangeHandler)

	// Registering default handler function for all events.
	a.RegisterDefaultHandler(DefaultHandler)

	// Optionally create channel to receiving all events
	// and set created channel to receive all events
	c := make(chan map[string]string, 100)
	a.SetEventChannel(c)

	// Check if connected with Asterisk, will send Action "QueueSummary"
	if a.Connected() {
		result, err := a.Action(map[string]string{"Action": "QueueSummary", "ActionID": "Init"})
		// If not error, processing result. Response on Action will follow in defined events.
		// You need to catch them in event channel, DefaultHandler or specified HandlerFunction
		fmt.Println(result, err)
	}
	
	ch := make(chan bool)
	<-ch
}

SIC!

You should not modify received events, because it can be read in another amigo goroutine. If you need to modify, you should copy all values to another map and modify it.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Amigo

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

Amigo is a main package struct

func New

func New(settings *Settings) *Amigo

New creates new Amigo struct with credentials provided and returns pointer to it Usage: New(username string, secret string, [host string, [port string]])

func (*Amigo) Action

func (a *Amigo) Action(action map[string]string) (map[string]string, error)

Action used to execute Actions in Asterisk. Returns immediately response from asterisk. Full response will follow. Usage amigo.Action(action map[string]string)

func (*Amigo) AgiAction

func (a *Amigo) AgiAction(channel, command string) (map[string]string, error)

AgiAction used to execute Agi Actions in Asterisk. Returns full response. Usage amigo.AgiAction(channel, command string)

func (*Amigo) CapitalizeProps

func (a *Amigo) CapitalizeProps(c bool)

CapitalizeProps used to capitalise all prop's names when true provided.

func (*Amigo) Connect

func (a *Amigo) Connect()

Connect with Asterisk. If connect fails, will try to reconnect every second.

func (*Amigo) Connected

func (a *Amigo) Connected() bool

Connected returns true if successfully connected and logged in Asterisk and false otherwise.

func (*Amigo) On

func (a *Amigo) On(event string, handler func(string))

On register handler for package events. Now amigo will emit two types of events: "connect" fired on connection success and "error" on any error occured.

func (*Amigo) RegisterDefaultHandler

func (a *Amigo) RegisterDefaultHandler(f handlerFunc) error

RegisterDefaultHandler registers handler function that will called on each event

func (*Amigo) RegisterHandler

func (a *Amigo) RegisterHandler(event string, f handlerFunc) error

RegisterHandler registers handler function for provided event name

func (*Amigo) SetEventChannel

func (a *Amigo) SetEventChannel(c chan map[string]string)

SetEventChannel sets channel for receiving all events

func (*Amigo) UnregisterDefaultHandler

func (a *Amigo) UnregisterDefaultHandler(f handlerFunc) error

UnregisterDefaultHandler removes default handler function

func (*Amigo) UnregisterHandler

func (a *Amigo) UnregisterHandler(event string, f handlerFunc) error

UnregisterHandler removes handler function for provided event name

type Settings

type Settings struct {
	Username          string
	Password          string
	Host              string
	Port              string
	ActionTimeout     time.Duration
	DialTimeout       time.Duration
	ReconnectInterval time.Duration
	Keepalive         bool
}

Settings represents connection settings for Amigo. Default: Username = admin, Password = amp111, Host = 127.0.0.1, Port = 5038, ActionTimeout = 3s DialTimeout = 10s

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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