go-ami

command module
v0.0.0-...-0f20043 Latest Latest
Warning

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

Go to latest
Published: May 20, 2021 License: MIT Imports: 5 Imported by: 0

README

go-ami

Go-AMI is a Go library for dealing with Asterisk Manager Interface.

Installation

go get -u github.com/pooladkhay/go-ami/ami

Usage

package main

import (
	"fmt"
	"log"
	"os"

	"github.com/joho/godotenv"
	ami "github.com/pooladkhay/go-ami/ami"
)

const PBXID = "yourPBXID"

func init() {
	err := godotenv.Load()
	if err != nil {
		log.Fatal("Error loading .env file")
	}
}

func main() {

	amiClient := &ami.Opts{
		Address:  os.Getenv("ASTERISK_ADDR"),
		Port:     os.Getenv("AMI_PORT"),
		Username: os.Getenv("AMI_USERNAME"),
		Secret:   os.Getenv("AMI_PASSWORD"),

		PingInterval:      5,
		PongTimeout:       20,
		ReconnectInterval: 2,

		EventsToListen: []string{"All"},
		EventHandler:   eventHandler,
	}

	amiClient.Connect()

	defer amiClient.StartListening()

	if amiClient.Connected {
		amiClient.SendAction("Action:PJSIPShowEndpoints")
	}
}

func eventHandler(event map[string]string) {
	if event["Ping"] != "" {
		fmt.Println("Pong!")
	}
	if event["Event"] != "" {
		switch event["Event"] {
		case "EndpointList":
			_endpointListHandler(event)
		case "DeviceStateChange":
			_deviceStateChangeHandler(event)
		case "PeerStatus":
			_peerStatusHandler(event)
		default:
			// fmt.Println("None of listed events!!")
		}
	}
}

func _endpointListHandler(e map[string]string) {
	fmt.Println(e)
}
func _deviceStateChangeHandler(e map[string]string) {
	fmt.Println(e)
}
func _peerStatusHandler(e map[string]string) {
	fmt.Println(e)
}

To-Do

  • Ability to add custom event handlers
  • Add Tests
  • Add Custom Action Handlers
  • Ability to add Redis as DB

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

License

MIT

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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