goami2

package module
v1.7.6 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2024 License: MIT Imports: 11 Imported by: 0

README

goami2: Simple Asterisk Manager Interface (AMI) library

Build Status Go Report Card codecov GitHub go.mod Go version of a Go module GitHub release Go Reference

Go library that provides interface to Asteris AMI. The library uses given net.Conn (tcp or tls), login and starts reading AMI messages from connection and parse them into *Message object. Provides simple interface to send messages to AMI. It uses re2c to create very fast protocol parser. Run "make bench" for benchmark tests.

Usage example:


import (
	"errors"
	"fmt"
	"log"
	"net"

	"github.com/staskobzar/goami2"
)

func main() {
	conn, err := net.Dial("tcp", "asterisk.host:5038")
	if err != nil {
		log.Fatalln(err)
	}

	// login and create client
	client, err := goami2.NewClient(conn, "admin", "pa55w0rd")
	if err != nil {
		log.Fatalln(err)
	}

	log.Println("client connected and logged in")

	// create AMI Action message and send to the asterisk.host
	msg := goami2.NewAction("CoreStatus")
	msg.AddActionID()
	client.Send(msg.Byte())

	defer client.Close() // close connections and all channels
	for {
		select {
		case msg := <-client.AllMessages():
			log.Printf("Got message: %s\n", msg.JSON())
		case err := <-client.Err():
			// terminate on network closed
			if errors.Is(err, goami2.ErrEOF) {
				log.Fatalf("Connection error: %s", err)
			}
			log.Printf("WARNING: AMI client error: %s", err)
		}
	}
}

Docs

See documentation at https://pkg.go.dev/github.com/staskobzar/goami2

Documentation

Overview

Package goami2 library for Asterisk Manager Interface

Index

Constants

This section is empty.

Variables

View Source
var (
	Error   = errors.New("goami2")
	ErrConn = fmt.Errorf("%w: net conn", Error)
	ErrAMI  = fmt.Errorf("%w: AMI proto", Error)
	ErrEOF  = fmt.Errorf("%w: terminated", Error)
)

Functions

This section is empty.

Types

type Client

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

Client is a AMI connection management object

func NewClient

func NewClient(conn net.Conn, username, password string) (*Client, error)

NewClient creates client. It is using NewClientWithContext in the background with a bogus context. For better context control use NewClientWithContext function.

func NewClientWithContext added in v1.7.0

func NewClientWithContext(ctx context.Context, conn net.Conn, username, password string) (*Client, error)

NewClientWithContext creates client with provided connection net.Conn and login into AMI server. It returns error if fials to login. Runs internal connection loop and provides AMI messages via AllMessages and error via Err methods.

func (*Client) Action

func (c *Client) Action(action *Message) bool

Action sends AMI action to an Asterisk server Returns true on success and false if fails This function is deprecated and will be removed Use Send or MustSend instead

func (*Client) AllMessages added in v1.5.1

func (c *Client) AllMessages() <-chan *Message

AllMessages returns a channel that receives any AMI messages from the client connection

func (*Client) Close

func (c *Client) Close()

Close client

func (*Client) Err added in v1.5.1

func (c *Client) Err() <-chan error

Err returns channel of errors of the client

func (*Client) MustSend added in v1.7.0

func (c *Client) MustSend(msg []byte) error

MustSend sends a message to nework and returns network errors if any write away. May block until network timeout

func (*Client) Send added in v1.4.1

func (c *Client) Send(msg []byte)

Send AMI message as a bytes array None-blocking method. Any network errors will be send back via Client.Err() channel

type Header struct {
	Name  string
	Value string
}

Header of AMI Message

type Message added in v1.5.1

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

Message represents AMI message object

func FromJSON added in v1.5.1

func FromJSON(input string) (*Message, error)

func NewAction

func NewAction(name string) *Message

NewAction creats action message

func NewMessage added in v1.5.1

func NewMessage() *Message

NewMessage creates new Message

func Parse added in v1.7.0

func Parse(data string) (*Message, error)

Parse AMI message as string into *Message structure

func (*Message) ActionID added in v1.5.1

func (m *Message) ActionID() string

ActionID get AMI message action id as string

func (*Message) AddActionID added in v1.5.1

func (m *Message) AddActionID()

AddActionID create random ID and add ActionID field to the message

func (*Message) AddField added in v1.5.1

func (m *Message) AddField(key, value string)

AddField add field with key and name

func (*Message) Byte added in v1.7.0

func (m *Message) Byte() []byte

Byte AMI message as byte array

func (*Message) DelField added in v1.5.1

func (m *Message) DelField(key string)

DelField removes field from message by name

func (*Message) Field added in v1.5.1

func (m *Message) Field(key string) string

Field return field value. Case insensitive field search by name

func (*Message) FieldValues added in v1.5.1

func (m *Message) FieldValues(key string) []string

FieldValues list of values for multiple headers with the same name

func (*Message) Headers added in v1.7.0

func (m *Message) Headers() []Header

Headers list of the Message

func (*Message) IsEvent added in v1.5.1

func (m *Message) IsEvent() bool

IsEvent returns true if message is event

func (*Message) IsResponse added in v1.5.1

func (m *Message) IsResponse() bool

IsResponse returns true if message is response

func (*Message) IsSuccess added in v1.5.1

func (m *Message) IsSuccess() bool

IsSuccess returns true if successfully response

func (*Message) JSON added in v1.5.1

func (m *Message) JSON() string

func (*Message) Len added in v1.7.0

func (m *Message) Len() int

Len returns number of headers in the message

func (*Message) SetField added in v1.7.0

func (m *Message) SetField(name, value string)

SetField get first available field by name and updates its value. If field not in list then append new field

func (*Message) String added in v1.5.1

func (m *Message) String() string

String AMI message as string

func (*Message) Var added in v1.5.1

func (m *Message) Var(key string) (string, bool)

Var search in AMI message variable fields like Variable or ChanVariable etc for a value of the type key=value or just key. If found, returns value as string and true. Variable name is case insensative.

Jump to

Keyboard shortcuts

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