Fetch DID from my MYSQL DB

How to route calls landing on asterisk with the help of DID mentioned in the MYSQL database table.

You’ll need to be more specific in what you mean by route. For example func_odbc[1] allows you to do queries to a database and then based on that you can do things.

[1] https://github.com/asterisk/asterisk/blob/master/configs/samples/func_odbc.conf.sample

I do something like this (If I’m understanding your request correctly)

When a call comes in I grab the DID and CallerId off of the incoming call and use them in a SQL query that give me back the information that I need to route the call.

If that’s what you are looking for, take a look at this:

[dynroute-5] ; DA

exten => s,1,NoOp(DYN-ROUTE-DA) ; Logging

exten => s,n,Set(ARRAY(dynrout,dynrou,dynroute)={MYSQL_LastConnect({CALLERID(num)})}); My mysql query returns three items

exten => s,n,GotoIf(["{dynroute}" = “”]?dynroute,3,1)

exten => s,n,Set(V=${dynroute}) ; must be set

exten => s,n,Goto(dynroute,1,1) ; code that actually handles the routing

[LastConnect]

prefix=MYSQL

dsn=asteriskcdrdb

readsql=SELECT max(c1.calldate) as calldate , c1.src, dst

readsql+= FROM cdr as c1

readsql+= where length(dst) <4 and length(dst) >2 and length(src) > 6 and

c1.src = ‘${ARG1}’ and (c1.dst < 900 and disposition = “ANSWERED”)

readsql+= group by c1.dst

readsql+= having date_add(calldate, interval 7 day) > now()

readsql+= order by calldate desc

readsql+= limit 5;