MATH Dialplan Function in Asterisk

Asterisk provides the MATH function to do mathematical operations from dialplan. It allows to perform mathematical operations between two parameters.

The syntax for math function is

MATH(expression,type)

The operators supported by math function are

+,-,/,*,%,<<,>>,^,AND,OR,XOR,<,>,<=,>=,==

The possible output types are

f : float,

i: int,

h: hex ,

c: char

if type int is used for the math function, all the decimals from output will be removed.

Examples

division with output type as float

exten => 111,1,Set(i=${MATH(30/60,f)})

output : 0.500000

 

division with output type as integer

exten => 111,1,Set(i=${MATH(30/60,i)})

output : 0 // integer type removes all the decimal part from output

 

modular division with output type as integer

exten => 111,1,Set(i=${MATH(1%2,i)})

output : 1

 

 

Configure SIP Trunk on Grandstream PBX

For Configuring Grandstream PBX with didforsale, you need to create four sip trunks. Two trunks for incoming calls and two trunks for outgoing.

 

For creating trunks, go to PBX => VoIP Trunks => Create new SIP Trunk. Add the details as shown in below figure

trunks

 

Similarly create three more sip trunks with the following IP address

209.216.15.70

209.216.15.71

209.216.2.212

 

Once all the trunks are added, the page will looks like

trunksall

 

 

Now you need to configure your DID number.

For configuring DID number, go to PBX => Inbound Routes => Create New Inbound Rule and add the details as shown in below figure

 

inbound

 

Now configure outbound routes for sending outgoing calls through didforsale.

For outbound routes, go to PBX => outbound Routes => Create New Outbound Rule as shown in below figure

 

outbound

 

 

Select any one of the outgoing trunks in outbound routes. If you want to do a failover, click the option “click to add failover trunk” on the bottom side and select the second outbound trunk here.

Configure SIP Trunk on goautodial

For configuring our DID number with goautodial, you will have to create two trunks in your system to allow calls from our server. You can do that by going to Admin section in your goautodial and choose carriers. Click on “Add a New Carrier” and add the following parameters

Carrier Name : DIDforsale_in1

Account Entry :
host=209.216.2.211
type=peer
context=trunkinbound
disallow=all
allow=ulaw
nat=yes
canreinvite=yes
insecure=very
dtmfmode=rfc2833

Protocol : SIP
Active : Y
Leave other fields as default or blank. Create one more carrier with same options except the host parameter in Account Entry field. Change the host from 209.216.2.211 to 209.216.15.70 in the new carrier.

Now you will have to route the DID number to the extension you want. You can do that by selecting Inbound –> Add a new DID, Add your DID number, and add the extension to where you want to forward the calls coming to DID in the Extension field and submit.

Local Phone Numbers for your Business

Get the SIP Trunking for your Asterisk, we guarantee best price, best coverage and Free Support for Asterisk. 

Try it for Free Now!

Add Condition in Asterisk Dial-plan

While building a dial plan you will always run in scenario where you have to choose the action based on a if statement. In this example we can use a counter variable and based on the value of the variable we can make another decision.  Lets start with normal counter variable and use that in a conditional statement in asterisk.

[Counter] exten => start,1,Verbose(2,Increment the counter variable)
; Set the initial value of the variable
same => n,Set(localCounter=1)
same => n,Verbose(2,Current value of localCounter is: ${ localCounter })
; Now lets increment the value of localCounter
same => n,Set(localCounter =$[${ localCounter } + 1])
same => n,Verbose(2,New counter is: ${localCounter })
same => n,Hangup()

You can also use int() aka increment function

[Counter] exten => start,1,Verbose(2,Increment the counter variable)
; Set the inital value of the variable
same => n,Set(localCounter =1)
same => n,Verbose(2,Current value of localCounter is: ${ localCounter })
; Now we can increment the value of localCounter
same => n,Set(localCounter =${INC(localCounter)})
same => n,Verbose(2,New counter is: ${localCounter })
same => n,Hangup()

Now we have a variable, lets us a if condition

[Counter]

exten => start,1,Verbose(2,Increment the counter variable)
; Set the inital value of the variable
same => n,Set(localCounter=1)
same => n,Verbose(2,Current value of localCounter is: ${ localCounter })
; Lets Here we use the RAND() function to randomly decide whether we should increment
; the localCounter. Use 0 as false and 1 as true
same => n,Set(IncrementValue=${RAND(0,1)})
; Now we can increment the value of localCounter if IncrementValue returns 1
same => n,Set(localCounter=${IF($[${IncrementValue} = 1]?
${INC(localCounter)}:${localCounter})
same => n,Verbose(2,Our IncrementValue returned: ${IncrementValue})
same => n,Verbose(2,Our new value of localCounter is: ${ localCounter })
same => n,Hangup()

When you start building a complex dial plan, you will find incrementing and decrementing a variable in asterisk is very useful tool. Int() or dec() for decrementing.
Usage is

INC(variablename)
DEC(variablename)

If() syntax
${IF($[…conditional statement…]?true_return_value:false_return_value)}

 

Asterisk Realtime conference

For asterisk 1.6 and above

Create a new database and table in your mysql database. For adding the table use the below query

CREATE TABLE meetme (
confno char(80) NOT NULL default ‘0’,
starttime datetime NOT NULL default ‘0000-00-00 00:00:00’,
endtime datetime NOT NULL default ‘2099-12-31 23:59:59’ ,
pin char(20) default NULL,
opts char(100) default NULL,
adminpin char(20) default NULL,
adminopts char(100) default NULL,
members int(11) NOT NULL default ‘0’,
maxusers int(11) NOT NULL default ‘0’,
PRIMARY KEY (confno,starttime)
);

The above query will create a table meetme, with the needed fields in it.

Now add the following database connection details to /etc/asterisk/res_config_mysql.conf

[context]

dbhost = 127.0.0.1

dbname =dbname

dbuser = user

dbpass = password

dbport = 3306

dbsock = /var/lib/mysql/mysql.sock

dbcharset = latin1

requirements=warn ; or createclose or createchar

 

Now edit your extconfig.conf file inside /etc/asterisk and add the following line at the bottom

meetme => mysql,dbname,meetme

 

Also make sure you have the following parameters in your meetme.conf file inside /etc/asterisk

schedule=yes (starttime and endtime options for meetme conference will work only if this parameter is set to yes. You can schedule a conference with this and the conference will be active only between the startime and endtime)

logmembercount=yes (This parameter helps keep track of members in an active conference. maxusers option in a conference will work only if this parameter is set to yes)

 

Now reload asterisk and start using asterisk realtime conference