eastrisk icon indicating copy to clipboard operation
eastrisk copied to clipboard

An Erlang interface to Asterisk.

(c) 2010 Erlang Solutions, Ltd

Introduction

Eastrisk consists of two separate parts, AGI, Asterisk Gateway Intefrace, and the Asterisk Manager Interface.

AGI

The Asterisk Gateway Interface is a way for external programs to control the dialplan. An AGI application is usually used to do advanced logic, or communicate with external resources, such as Mnesia databases, Erlang applications etc. The Eastrisk application is capable of doing both FastAGI and normal AGI.

AGI

The Erlang AGI interface consists of both an Erlang application and a small program written in C. The C program will be executed by Asterisk and pass on information to a Erlang node through TCP/IP, usually on port 6666. To use the AGI, place the binary created from the C sources in the agi-bin (usually in /var/lib/asterisk/) directory and add something similar to
exten => _X.,1,AGI(erl.agi)
to your dialplan.

FastAGI

FastAGI allows AGI scripts to be executed over a network and thus eliminates the use of C code to connect to the AGI server. To use the FastAGI put something similar to exten => _X.,1,AGI(agi://localhost:6666)
in your dialplan.

Using the connection:

When the AGI Sever receives a connection an {@link agi_channel} process is spawned. When this process has initialised it will notify any added event handlers (see gen_event(3)) through the {@link agi_events}. The event handler will be supplied with channel environment variables and the pid of the channel. The event handler should then spawn a process which will communicate with the AGI channel using the {@link agi} module.

Example of simple AGI session:

The event handler:

EventHandler:handle_event({new_channel, ChannelPid, ChannelEnv}, State) ->
    spawn(agi_com, start, [ChannelPid, ChannelEnv]),
    {ok, State}

agi_com:

agi_com:start(ChannelPid, ChannelEnv) ->
    agi:answer(ChannelPid),
    case agi:stream_file(ChannelPid, "filename", "123") of
		{ok, {Value, _Endpos}} ->
			save_data(Value);
		_Else -> % never pressed any digits / hung up
			ok
	end,
    agi:hangup(ChannelPid),
    agi_channel:close(ChannelPid).
To disable the AGI server to run, set the environment variable agi_server to false before the application is started.

Asterisk Manager Interface

The Asterisk Manager Interface is used to allow a program to issue commands on an Asterisk server and also listen to PBX events. The Erlang Asterisk Manager interface can be used in two ways, either as a behaviour with a callback module or as an by subscribing to events from an event manager.

Manager Event Manager

To use the manager event manager, make sure that the environment variables mgr_name and mgr_secret are set appropriately. Then use {@link ast_manager_events} to subscribe to events.

Manager behaviour

To use the Manager interface as a behaviour, just add -behaviour(manager). and make sure to implement init/1, handle_event/2, code_change/3 and terminate/2.

Please note that you have to set the environment variable manager_server to false before the application is started to be able to do this. Or, don't start the application, but just use the behaviour.