Chapter 18. Asterisk Gateway Interface

Caffeine. The gateway drug.

Eddie Vedder

The Asterisk dialplan has evolved into a simple yet powerful programming interface for call handling. Many people, however, especially those with a programming background, prefer to implement call handling in a traditional programming language. The Asterisk Gateway Interface (AGI) allows for the development of first-party call control in the programming language of your choice.

Quick Start

This section gives a quick example of using the AGI.

First, let’s create the script that we’re going to run. AGI scripts are usually placed in /var/lib/asterisk/agi-bin.

$ cd /var/lib/asterisk/agi-bin

$ vim hello-world.sh

#!/bin/bash

# Consume all variables sent by Asterisk
while read VAR && [ -n ${VAR} ] ; do : ; done

# Answer the call.
echo "ANSWER"
read RESPONSE

# Say the letters of "Hello World"
echo 'SAY ALPHA "Hello World" ""'
read RESPONSE

exit 0

$ chown asterisk:asterisk hello-world.sh

$ chmod 700 hello-world.sh

Now, add the following line to /etc/asterisk/extensions.conf, in your [sets] context:

exten => 237,1,AGI(hello-world.sh)

Save and reload your dialplan, and when you call extension 237 you should hear Allison spell out “Hello World.”

AGI Variants

There are a few variants of AGI that differ primarily in the method used to communicate with Asterisk. It is good to be aware of all the options so you can make the best choice based on the needs of your application.

Process-Based AGI

Process-based AGI is the simplest variant ...

Get Asterisk: The Definitive Guide, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.