O'Reilly logo

Asterisk Cookbook by Russell Bryant, Leif Madsen

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 1. Dialplan Fundamentals

Introduction

This chapter is designed to show you some fundamental dialplan usage concepts that we use in nearly every dialplan. We’ve developed these recipes to show you how we’ve found the usage of these dialplan applications to be of the greatest value and flexibility.

Counting and Conditionals

Problem

You need to perform basic math—such as increasing a counting variable—and do it using a conditional statement.

Solution

In many cases you will need to perform basic math, such as when incrementing the counter variable when performing loops. To increase the counter variable we have a couple of methods which are common. First, we can use the standard conditional matching format in Asterisk:

[CounterIncrement]
exten => start,1,Verbose(2,Increment the counter variable)

; Set the initial value of the variable
   same => n,Set(CounterVariable=1)
   same => n,Verbose(2,Current value of CounterVariable is: ${CounterVariable})

; Now we can increment the value of CounterVariable
   same => n,Set(CounterVariable=$[${CounterVariable} + 1])
   same => n,Verbose(2,Our new value of CounterVariable is: ${CounterVariable})

   same => n,Hangup()

Alternatively, in versions of Asterisk greater than and including Asterisk 1.8, we can use the INC() dialplan function:

[CounterIncrement] exten => start,1,Verbose(2,Increment the counter variable) ; Set the inital value of the variable same => n,Set(CounterVariable=1) same => n,Verbose(2,Current value of CounterVariable is: ${CounterVariable}) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required