Skip to main content
844-777-0017/Contact Us

Asterisk dynamic routing with MySQL database

For the next task we will need Asterisk dynamic routing using MySQL: some company has technical department that should provide emergency services on non-working hours. Based on time and date conditions, a call should be routed to a shared department’s extension (technicians) or be sent to a man on duty on non-working time and weekends. A number to dial on non-working hours should be taken from a database during a call execution. In our example, we use MySQL database with a table that contains columns “day”, “month”, “technician” and “number”. The commented extensions.conf example is below:



exten => hotline,1,NoOp(Call from ${CALLERID(all)} to tech emergency service)


same => n(duty),Set(Month=${STRFTIME(${EPOCH},,%m)}) ;get month's number (for example 10 for October)

same => n,Set(Day=${STRFTIME(${EPOCH},,%d)}) ;get a day of month

same => n,NoOp(Month=${Month}, Day=${Day})

same => n,Set(NUM=${ODBC_GETDUTYDATA(phonenum,${Day},${Month})}) ; get a phone number that should be called on this day (see func_odbc.conf below)

same => n,Set(TECHNICIAN=${ODBC_GETDUTYDATA(technician,${Day},${Month})}) ;get a name of techninician

same => n,NoOp(On ${Day}.${Month} number ${NUM} should be called)

same => n,NoOp(Dialing the duty engineer ${TECHNICIAN}: ${NUM})

same => n,Dial(Local/${NUM}@outbound/n,20,rt) ;dialing the number taken from DB

same => n,Hangup()


Here is the func_odbc.conf example:





readsql=SELECT ${ARG1} FROM dutylist WHERE day='${SQL_ESC(${ARG2})}' AND month='${SQL_ESC(${ARG3})}'

synopsis=Get the duty information based on date




Copyright © 2018