esl

package module
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2024 License: MIT Imports: 17 Imported by: 1

README

ESL

Another library for working with the FreeSWITCH server on Golang.

In this version, only the client connection is supported, and the outbound connection has not yet been implemented, because it simply was not necessary.

// initialize buffered events channel
events := make(chan esl.Event, 1)
// read events
go func() {
    for ev := range events {
        fmt.Println(ev.Name(), ev.Get("Job-UUID"))
    }
}()

// connect to FreeSWITCH & init events channel with auto-close flag
client, err := esl.Connect("10.10.61.76", "ClueCon",
    esl.WithEvents(events, true))
if err != nil {
    panic(err)
}
defer client.Close()

// send a command
msg, err := client.API("show calls count")
if err != nil {
    panic(err)
}
fmt.Println(msg)

// subscribe to BACKGROUND_JOB events
if err = client.Subscribe("BACKGROUND_JOB"); err != nil {
    panic(err)
}

// send a background command
if err = client.JobWithID("uptime s", "test-xxx"); err != nil {
    panic(err)
}

Documentation

Overview

Package esl is another library for working with the FreeSWITCH server on Golang.

In this version, only the client connection is supported, and the outbound connection has not yet been implemented, because it simply was not necessary.

Example
package main

import (
	"fmt"

	"github.com/mdigger/esl"
)

func main() {
	// initialize buffered events channel
	events := make(chan esl.Event, 1)

	// connect to FreeSWITCH & init events channel with auto-close flag
	client, err := esl.Connect("10.10.61.76", "ClueCon",
		esl.WithEvents(events, true))
	if err != nil {
		panic(err)
	}
	defer client.Close()

	// send a command
	msg, err := client.API("show calls count")
	if err != nil {
		panic(err)
	}

	fmt.Println(msg)

	// subscribe to BACKGROUND_JOB events
	if err = client.Subscribe("BACKGROUND_JOB"); err != nil {
		panic(err)
	}

	// send a background command
	if err = client.JobWithID("uptime s", "test-xxx"); err != nil {
		panic(err)
	}

	// read events
	for ev := range events {
		fmt.Println(ev.Name(), ev.Get("Job-UUID"))
	}
}
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	DialTimeout = time.Second * 5
	AuthTimeout = time.Second * 2
)

Default timeout options.

View Source
var (
	ErrMissingAuthRequest = errors.New("missing auth request")
	ErrAccessDenied       = errors.New("access denied")
	ErrInvalidPassword    = errors.New("invalid password")
	ErrTimeout            = errors.New("timeout")
)

Authentication errors.

Functions

This section is empty.

Types

type Client

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

Client represents a client FreeSWITCH connection.

func Connect

func Connect(addr, password string, opts ...Option) (*Client, error)

Connect connects to the given address with an optional password and options.

The address should include the host and port. If the port is missing, the default port 8021 will be used. The password is optional and can be empty. The options are variadic and can be used to customize the connection.

Returns a new Client and an error if there was a failure in connecting.

func NewClient

func NewClient(rwc io.ReadWriteCloser, password string, opts ...Option) (*Client, error)

NewClient creates a new Client instance.

func (*Client) API

func (c *Client) API(command string) (string, error)

API sends a command to the API and returns the response body or an error.

Send a FreeSWITCH API command, blocking mode. That is, the FreeSWITCH instance won't accept any new commands until the api command finished execution.

func (*Client) Close

func (c *Client) Close() error

Close closes the client connection.

func (*Client) DivertEvents

func (c *Client) DivertEvents(on ...bool) error

The divert_events switch is available to allow events that an embedded script would expect to get in the inputcallback to be diverted to the event socket.

An inputcallback can be registered in an embedded script using setInputCallback(). Setting divert_events to "on" can be used for chat messages like gtalk channel, ASR events and others.

func (*Client) Done added in v0.2.3

func (c *Client) Done() <-chan struct{}

Done returns a channel that will be closed when the client connection is closed.

func (*Client) Filter

func (c *Client) Filter(eventHeader, valueToFilter string) error

Filter performs a filter operation on the Client.

Specify event types to listen for. Note, this is not a filter out but rather a "filter in," that is, when a filter is applied only the filtered values are received. Multiple filters on a socket connection are allowed.

You can filter on any of the event headers. To filter for a specific channel you will need to use the uuid:

filter Unique-ID d29a070f-40ff-43d8-8b9d-d369b2389dfe

This method is an alternative to the myevents event type. If you need only the events for a specific channel then use myevents, otherwise use a combination of filters to narrow down the events you wish to receive on the socket.

To filter multiple unique IDs, you can just add another filter for events for each UUID. This can be useful for example if you want to receive start/stop-talking events for multiple users on a particular conference.

func (*Client) FilterDelete

func (c *Client) FilterDelete(eventHeader, valueToFilter string) error

FilterDelete removes a filter from the Client.

Specify the events which you want to revoke the filter. filter delete can be used when some filters are applied wrongly or when there is no use of the filter.

func (*Client) Job

func (c *Client) Job(command string) (id string, err error)

Job sends a background command and returns the job-ID.

Send a FreeSWITCH API command, non-blocking mode. This will let you execute a job in the background.

The same API commands available as with the api command, however the server returns immediately and is available for processing more commands.

When the command is done executing, FreeSWITCH fires an event with the result and you can compare that to the Job-UUID to see what the result was. In order to receive this event, you will need to subscribe to BACKGROUND_JOB events.

func (*Client) JobWithID

func (c *Client) JobWithID(command, id string) error

JobWithID sends a background command with a specified ID.

Send a FreeSWITCH API command, non-blocking mode. This will let you execute a job in the background, and the result will be sent as an event with an indicated UUID to match the reply to the command.

When the command is done executing, FreeSWITCH fires an event with the result and you can compare that to the Job-UUID to see what the result was. In order to receive this event, you will need to subscribe to BACKGROUND_JOB events.

func (*Client) MyEvent

func (c *Client) MyEvent(uuid string) error

The 'myevents' subscription allows your inbound socket connection to behave like an outbound socket connect. It will "lock on" to the events for a particular uuid and will ignore all other events, closing the socket when the channel goes away or closing the channel when the socket disconnects and all applications have finished executing.

Once the socket connection has locked on to the events for this particular uuid it will NEVER see any events that are not related to the channel, even if subsequent event commands are sent. If you need to monitor a specific channel/uuid and you need watch for other events as well then it is best to use a filter.

func (*Client) SendEvent

func (c *Client) SendEvent(name string, headers map[string]string, body string) error

Send an event into the event system.

func (*Client) SendMsg

func (c *Client) SendMsg(uuid string, headers map[string]string, body string) error

SendMsg is used to control the behavior of FreeSWITCH. UUID is mandatory, and it refers to a specific call (i.e., a channel or call leg or session).

func (*Client) Subscribe

func (c *Client) Subscribe(names ...string) error

Subscribe is a function that subscribes the client to events with the given names.

You may specify any number events on the same line that should be separated with spaces.

Subsequent calls to event won't override the previous event sets.

func (*Client) Unsubscribe

func (c *Client) Unsubscribe(names ...string) error

Unsubscribe unsubscribes the client from one or more events.

Suppress the specified type of event. If name is empty then all events will be suppressed.

type Event

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

Event represents an ESL event with headers and a body.

func NewEvent

func NewEvent(name string, headers map[string]string, body []byte) Event

NewEvent returns a new Event with the given name, headers and body.

It panics if the name is empty or if the name is CUSTOM without the Event-Subclass name.

func (Event) Body

func (e Event) Body() string

Body returns the body of the event as a string.

func (Event) ContentLength

func (e Event) ContentLength() int

ContentLength returns the length of the body in the Event.

func (Event) ContentType

func (e Event) ContentType() string

ContentType returns the content type of the event.

func (Event) Get

func (e Event) Get(key string) string

Get returns the value associated with the given key from the Event's headers.

func (Event) LogValue

func (e Event) LogValue() slog.Value

LogValue returns the log value of the Event.

It returns a slog.Value that contains the name and sequence of the Event.

func (Event) MarshalJSON

func (e Event) MarshalJSON() ([]byte, error)

MarshalJSON is a Go function that marshals the Event to JSON.

func (Event) Name

func (e Event) Name() string

Name returns the name of the event.

func (Event) Sequence

func (e Event) Sequence() int64

Sequence returns the event sequence as an int64.

func (Event) String

func (e Event) String() string

String returns a string representation of the Event.

func (Event) Timestamp

func (e Event) Timestamp() time.Time

Timestamp returns the timestamp of the event.

func (Event) Variable

func (e Event) Variable(name string) string

Variable returns the value of the variable with the given name.

func (Event) WriteTo

func (e Event) WriteTo(w io.Writer) (int64, error)

WriteTo writes the event to the given writer.

type Option

type Option func(*config)

Option is a function type used to modify configuration options.

func WithDumpIn

func WithDumpIn(w io.Writer) Option

WithDumpIn sets the writer to record all incoming messages. It is used for logging during debugging.

func WithDumpOut

func WithDumpOut(w io.Writer) Option

WithDumpOut sets the writer to record all outgoing commands. It is used for logging during debugging.

func WithEvents

func WithEvents(events chan<- Event, autoClose ...bool) Option

WithEvents returns an Option that sets the events channel of a config.

If the autoClose parameter is specified, the channel will be automatically closed when the connection to the server is closed.

func WithLog

func WithLog(log *slog.Logger) Option

WithLog returns an Option that sets the logger for the configuration.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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