Book Review: Asterisk AGI Programming with Packt

[Please Note: If you follow the book links from this site to Packt’s and decide to buy *any* book from their site, we will get a small commission that we’ll use towards the upkeep of our servers etc.]

Asterisk Gateway Interface ProgrammingAs you may have read previously, I was approached by Packt Publishing to see if I would like to review their new book on AGI Programming by Nir Simionovich. Time has conspired against me to actually use it for a real project so instead I resorted to choosing it as my bedtime reading for a few days.

I’ve now read the book and first off I’d like to thank Packt for asking me. I have enjoyed it far more than I thought I would. As for how to review it, I thought I’d give my overall impression and then just go through it chapter by chapter.

It isn’t a long book – about 190 pages – which to my mind is no bad thing. It managed to concentrate on the subject and had little in the way of superfluous text and language (Apart from chapter 1 that is). The author knows his subject well and writes in a fairly informal and easy-to-read style which I personally quite liked. The meat of the book concentrates on – as you would probably guess – programming with AGI and focusses almost exclusively on using PHP. As much as I am happy around PHP, I would have liked to see some examples with alternative languages such as Python perhaps. The subtitle of the book is “Design and Develop Asterisk-based VoIP telephony platforms and services using PHP and PHPAGI” so I guess that’s why other languages don’t get much of a look in. They are mentioned here and there and there are some good links to various other libraries and open source projects. I did like the fact that he mentioned throughout the book – where necessary – the functional difference between the main versions of Asterisk (1.2, 1.4 and 1.6) and ways to deal with those differences when you come up against them.

OK – so here is my chapter-by-chapter review:

Sorry, but I didn’t really get the first chapter at all. This is a book aimed at programmers and developers and yet the first chapter was a repetitive cut-and-paste of how to build the various asterisk components from source, including lots of screen shots showing the output of things like ./configure which personally I found a bit trivial and uninteresting. I was a bit concerned that the rest of the book was going to follow suit but thankfully I was mistaken.

Chapter 2 is really good. It explains the workings of Asterisk’s dialplan and applications – the infamous extension.conf – in a very clear and understandable way. I recall when I first started to look at Asterisk and was delving into as much information on-line as I could get, the Asterisk TFOT book [pdf download] and whatever else I could find, that it was several days before the penny finally dropped. It isn’t difficult really but it isn’t quite the same as “normal” programming or scripting concepts and the language itself is far from obvious (e.g. an extension is not the phone on your desk in Asterisk’s configuration files). But then this is telephony we are talking about. Using the example of a basic IVR or AA the author examines the diaplan syntax and construction.

So I got quite a lot out of chapter 2. I thought it was well written and clear and useful. Chapter 3 develops the IVR theme further, introduces other features of the Asterisk application pool and covers the scripting language in more detail examining branching, expressions, operators and flow control. It’s a fairly short chapter but covers a good deal of ground if you are unfamiliar with Asterisk programming.

Your level of knowledge and familiarity with Asterisk will dictate what you get from this first section (Chapters 1-3). Although I have used Asterisk for a couple of years now and have felt quite comfortable with the platform’s configuration and use, I got quite a lot of new information and ideas from this early part of the book. For me, this initial part has been very useful and will be a good reference for the future. I think, though, if you are very familiar with Asterisk then you might find it a bit slow going. The book hasn’t examined AGI whatsoever up to this point and we are about a third of the way through already! The author does suggest that a coffee is a good before starting on Chapter 4 as “the journey becomes more and more complicated”…

Chapter 4 introduces the reader to AGI in a fairly gentle way and also offers 10 “rules” to help make your AGI programming more successful – they all make sense to me and will I’m sure prove to be a very useful monitoring/checking tool. Packt sent me an extract from this chapter which you are free to read here if you want to get a flavour of it.

The following chapter introduces us to some real code (PHP) and we build our first, simple AGI application. Nothing to hard, but a useful introduction into how to actually get the conversation happening between Asterisk and your script. There are couple of nice flow charts which are helpful for visualising the traffic flow back and forth between Asterisk and your script too. Again not too complicated but helpful in getting the novice AGI programmer, i.e. me, thinking about things the right way.

In chapter 6 Nir examines and recommends the use of a set of PHP classes (library) called PHPAGI. Being completely new to AGI programming I am in no position to contradict the author’s recommendation, but looking on the Sourceforge site for this library, it is quite old and has not been updated for 3 years or more. Of course that may be because it is perfect and needs no further development, or there might be other reasons but I would have really liked to have had some more discussion regarding this choice of library before continuing – just for my own piece of mind more than anything else. Perhaps if Nir reads this he could leave a comment about this? My own assumptions after reading the rest of the chapter are that the AGI interface itself is fairly simple and so – perhaps – the need for a more dynamic or complex library is just not there and this one does the job just fine. Anyway, the rest of the chapter we look at a new AGI application using the class library above and also we discover the main – and what seems to me to be a first-class – concept for building AGI applications: Atomic AGI or Particle Programming. Sounds great doesn’t it? It really does make a lot of sense. Basically it’s a bit like the traditional ideals behind Unix/Linux command line applications; write small applications that do one thing and do it well. To summarise then, in chapter 6 we are shown the author’s recommended path to AGI Nirvana through adopting some rules, and practices. It is hard for me to draw any solid conclusions from his approach as I am a novice with AGI and so have nothing by way of comparison, but it certainly seems to make a great deal of sense and is clear and well explained. Good stuff.

The remainder of the book goes a bit wider than just pure AGI. Chapters 7 and 8, examine some of the closely related applications and facilities of Asterisk. We get an overview of FastAGI (AGI over TCP) and Nir shows us some further PHP libraries that are available to assist with producing FastAGI applications. Chapter 8 offers an overview of the AMI (Asterisk Manager Interface) and some example code to get you started.

In the penultimate chapter, the reader is given a challenge: to create an application that is used in the real world – an Asterisk Call Recording Gateway. There is no code in this chapter – that’s for you to do. But Nir provides some useful guidance about the way to think about the development and plan the project itself.

And finally, chapter 10 discusses how to make sure your Asterisk applications can scale and offers several ideas and techniques to improve performance such as database query caching and using web services.

As I said at the beginning – I enjoyed reading this book much more than I thought I would. It is not overly technical and Nir has an engaging style of writing. The book is a great introduction into Asterisk programming. It is not “The Bible of Asterisk Programming” and does not set out to be. It is clearly aimed at developers who have not had much to do with Asterisk before but are familiar with traditional programming methods. I really liked the fact that it is quite short. You can read the whole book in a couple of evenings and being laid out the way it is it will become a very useful reference document for me in the future.

Nir Simionovich has a blog.

Asterisk: AGI Programming

Recently I received a flattering email from Packt Publishing telling me about a new book entitled: “Asterisk Gateway Interface 1.4 and 1.6 Programming” – by Nir Simionovich. The author of the email refered to this blog and asked if I would like to review their book for my blog. Very flattering indeed.

[Update: Book review is done now. You can read it here]

I was, of course, already familiar with Packt. They do produce lots of books on Open Source technologies. What I wasn’t aware of, until I looked, was this (from their “about us” page):

Packt Open Source Project Royalty Scheme

Packt believes in Open Source. When we sell a book written on an Open Source project, we pay a royalty directly to that project. As a result of purchasing one of our Open Source books, Packt will have given some of the money received to the Open Source project.

In the long term, we see ourselves and yourselves, as customers and readers of our books, as part of the Open Source ecosystem, providing sustainable revenue for the projects we publish on. Our aim at Packt is to establish publishing royalties as an essential part of the service and support business model that sustains Open Source.

Now that seems like a fine scheme to me and is one that would certainly weight my purchasing decision toward a Packt book over another similar publication. I am aware that they will obviously get some free publicity from our review but having read the above I am quite happy about that.

Anyway, back to AGI.

Asterisk Gateway Interface (AGI)

The Asterisk Gateway Interface is an interface for adding functionality to Asterisk with many different programming languages. Perl, PHP, C, Pascal, Bourne Shell – it’s your choice, really.

  • AGI may control the dial plan, called in extensions.conf.
  • Async AGI Introduced in Asterisk 1.6, allows asynchronous AGI scripting.
  • EAGI gives the application the possibility to access and control the sound channel in addition to interaction with the dial plan.
  • FastAGI can be used to do the processing on a remote machine via a network connection.
  • DeadAGI gives access to a dead channel, after hangup. Deprecated since Asterisk 1.6

Being a fan and user of Asterisk in our business, I think what we will do is actually use the book and create or modify an application that will be of use to us. This should allow us to give a decent overview of the content and how easy/hard it was to transfer the knowledge into a real world result.

We use vtiger as our CRM, OpenEMM for email marketing and Asterisk for our telephony – all running on Ubuntu Servers. Tying these together would be a fine goal.

But I am open to other suggestions too. Anyone got any good ideas for a fairly simple AGI application?