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}) ...

Get Asterisk Cookbook 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.