Initiating a call from a script (AMI)

From Etel

Jump to: navigation, search

Contents

Initiating a Call from a Script

Problem

You need a way to initiate a call from a script or external system (e.g., web-based click-to-call).

Solution

First, you'll need to enable and configure the manager feature in Asterisk. You'll want at least the following in /etc/asterisk/manager.conf:

[general]
enabled = yes
port = 5038
bindaddr = 0.0.0.0

[bob]
secret=bobpass
read = system,call,log,verbose,command,agent,user
write = system,call,log,verbose,command,agent,user
deny=0.0.0.0/0.0.0.0
permit=127.0.0.1/255.255.255.0

This allows the script, with a username of bob and a password of bobpass, to authenticate to the Asterisk Manager Interface (AMI) from the local machine. The "read" and "write" statements declare specifically what "bob" can do and the "deny" and "permit" statements declare where "bob" can connect from.

The syntax for logging in and off (already taken care of in the above script) is quite simple. Logging in, if you were to type it by hand, looks like:

Action: login
Username: bob
Secret: bobpass
Events: on

AMI will respond with:

Response: Success
Message: Authentication accepted

For logging off, if you were to type it by hand, looks like:

Action: Logoff

AMI will respond with:

Response: Goodbye
Message: Thanks for all the fish.

Generating a call, if you were to type it by hand, looks like:

Action: Originate
Channel: Local/202@stream
Context: stream
Exten: 201
Priority: 1
CallerID: Music Stream

Automating this with Perl is quite easy: just use the IO::Socket::Inet module. In its simplest form, the subroutine for the function call to AMI should look something like:

sub asterisk_command () {
  my $command=$_[0];
  my $ami=IO::Socket::INET->new(PeerAddr=>'127.0.0.1',PeerPort=>5038,Proto=>'tcp') or die "failed to connect to AMI!";                       
  print $ami "Action: Login\r\nUsername: bob\r\nSecret: bobpass\r\n\r\n$command\r\nAction: Logoff\r\n\r\n";
}

This will connect to the AMI, pass a command, and politely disconnect from the AMI.

Calling the subroutine in a script should look something like:

&asterisk_command("Action: Originate\r\nChannel: Local/202\r\nExten: 201\r\nContext: stream\r\nCallerID: Music Stream\r\nPriority: 1\r\n");

Where:

"Channel" indicates what channel the call should go to. 
"Context" indicates what context to use, on connect.
"Exten" indicates what extension to call from.
"Priority" indicates what priority to use, on connect.
"CallerID" declares the caller ID info to use.

There are other commands that can be used in this string (see this), but calls can be made with only the above. "Channel" can be "Local","SIP","IAX", or similar. As an example: to call the echo test line at FWD, the function call would look like:

&asterisk_command("Action: Originate\r\nChannel: SIP/613@fwd.pulver.com\r\nExten: 201\r\nContext: stream\r\nCallerID: Music Stream\r\nPriority: 1\r\n");

Discussion

Tying the above to a database and a web front-end creates a nice click-to-call interface. The author has also used the AMI to create a simple multi-room conference manager.

One important note about writing your own click-to-call interface: you'll want to add a bit of error checking to prevent multiple calls to the same destination. Doing so has very annoying results (phone line feedback sounds quite odd and is quite loud).

See Also:

Asterisk Manager API Action Originate (voip-info.org)

Metadata

  • By: Tim Kramer
Personal tools