Asterisk-AMI
view release on metacpan or search on metacpan
lib/Asterisk/AMI.pm view on Meta::CPAN
#!/usr/bin/perl
=head1 NAME
Asterisk::AMI - Perl module for interacting with the Asterisk Manager Interface
=head1 VERSION
0.2.8
=head1 SYNOPSIS
use Asterisk::AMI;
my $astman = Asterisk::AMI->new(PeerAddr => '127.0.0.1',
PeerPort => '5038',
Username => 'admin',
Secret => 'supersecret'
);
die "Unable to connect to asterisk" unless ($astman);
my $action = $astman->({ Action => 'Command',
Command => 'sip show peers'
});
=head1 DESCRIPTION
This module provides an interface to the Asterisk Manager Interface. It's goal is to provide a flexible, powerful, and
reliable way to interact with Asterisk upon which other applications may be built. It utilizes AnyEvent and therefore
can integrate very easily into event-based applications, but it still provides blocking functions for us with standard
scripting.
=head2 SSL SUPPORT INFORMATION
For SSL support you will also need the module that AnyEvent::Handle uses for SSL support, which is not a required
dependency. Currently that module is 'Net::SSLeay' (AnyEvent:Handle version 5.251) but it may change in the future.
=head3 CentOS/Redhat
If the version of Net:SSLeay included in CentOS/Redhat does not work try installing an updated version from CPAN.
=head2 Constructor
=head3 new([ARGS])
Creates a new AMI object which takes the arguments as key-value pairs.
Key-Value Pairs accepted:
PeerAddr Remote host address <hostname>
PeerPort Remote host port <service>
Events Enable/Disable Events 'on'|'off'
Username Username to access the AMI
Secret Secret used to connect to AMI
AuthType Authentication type to use for login 'plaintext'|'MD5'
UseSSL Enables/Disables SSL for the connection 0|1
BufferSize Maximum size of buffer, in number of actions
Timeout Default timeout of all actions in seconds
Handlers Hash reference of Handlers for events { 'EVENT' => \&somesub };
Keepalive Interval (in seconds) to periodically send 'Ping' actions to asterisk
TCP_Keepalive Enables/Disables SO_KEEPALIVE option on the socket 0|1
Blocking Enable/Disable blocking connects 0|1
on_connect A subroutine to run after we connect
on_connect_err A subroutine to call if we have an error while connecting
on_error A subroutine to call when an error occurs on the socket
on_disconnect A subroutine to call when the remote end disconnects
on_timeout A subroutine to call if our Keepalive times out
OriginateHack Changes settings to allow Async Originates to work 0|1
'PeerAddr' defaults to 127.0.0.1.
'PeerPort' defaults to 5038.
'Events' default is 'off'. May be anything that the AMI will accept as a part of the 'Events' parameter for the
login action.
'Username' has no default and must be supplied.
'Secret' has no default and must be supplied.
'AuthType' sets the authentication type to use for login. Default is 'plaintext'. Use 'MD5' for MD5 challenge
authentication.
'UseSSL' defaults to 0 (no ssl). When SSL is enabled the default PeerPort changes to 5039.
'BufferSize' has a default of 30000. It also acts as our max actionid before we reset the counter.
'Timeout' has a default of 0, which means no timeout on blocking.
'Handlers' accepts a hash reference setting a callback handler for the specified event. EVENT should match
the contents of the {'Event'} key of the event object will be. The handler should be a subroutine reference that
will be passed the a copy of the AMI object and the event object. The 'default' keyword can be used to set
a default event handler. If handlers are installed we do not buffer events and instead immediately dispatch them.
If no handler is specified for an event type and a 'default' was not set the event is discarded.
'Keepalive' only works when running with an event loop. Used with on_timeout, this can be used to detect if
asterisk has become un-responsive.
'TCP_Keepalive' default is disabled. Activates the tcp keep-alive at the socket layer. This does not require
an event-loop and is lightweight. Useful for applications that use long-lived connections to Asterisk but
do not run an event loop.
'Blocking' has a default of 1 (block on connecting). A value of 0 will cause us to queue our connection
and login for when an event loop is started. If set to non blocking we will always return a valid object.
'on_connect' is a subroutine to call when we have successfully connected and logged into the asterisk manager.
it will be passed our AMI object.
'on_connect_err', 'on_error', 'on_disconnect'
These three specify subroutines to call when errors occur. 'on_connect_err' is specifically for errors that
occur while connecting, as well as failed logins. If 'on_connect_err' or 'on_disconnect' it is not set,
but 'on_error' is, 'on_error' will be called. 'on_disconnect' is not reliable, as disconnects seem to get lumped
under 'on_error' instead. When the subroutine specified for any of theses is called the first argument is a copy
( run in 0.342 second using v1.01-cache-2.11-cpan-140bd7fdf52 )