[asterisk-users] Lookup ${EXTEN} in database, update context/route if found... AGI?

Leif Madsen leif.madsen at asteriskdocs.org
Tue May 11 16:47:48 CDT 2010


Doug Lytle wrote:
> Tim Nelson wrote:
>> Greetings all-
>>
>> box on the same network. Instead of paying twice for the call to go out to the PSTN on one channel and back in on another channel, I'd like the ability to lookup the destination number in a MySQL database
>>    
> 
> I use the mysql add-on, I'd create a subroutine that gets called at dial 
> time.  As an example, I set outbound caller-id with the below 
> subroutine.  You would use ${EXTEN} for the lookup and decide the route 
> to take on the results.
> 
> [set_callerid]
> 
> exten => s,1,MYSQL(Connect connid 192.168.103.15 username 'password' did)
> exten => s,n,GosubIf($["${MYSQL_STATUS}" = "-1"]?mysql_failed,s,6)
> exten => s,n,MYSQL(Query resultid ${connid} SELECT setcid FROM Corporate 
> WHERE number = ${CALLERID(number)})
> exten => s,n,MYSQL(Fetch fetchid ${resultid} set.callerid)
> exten => s,n,MYSQL(Disconnect ${connid})
> exten => s,n,MYSQL(Clear ${resultid})
> exten => s,n,GotoIf($["${set.callerid}" != ""]?8:10)
> exten => s,n,Set(CALLERID(number)=${set.callerid})
> exten => s,n,Return()
> exten => s,n,Set(CALLERID(number)=269xxxxxxx)
> exten => s,n,Return()

You could do something similar with func_odbc:

[set_callerid]
exten => start,1,Verbose(2,Set CallerID from Database)
exten => start,n,Set(RESULT=${ODBC_GET_CID(${CALLERID(number)})})
exten => start,n,GotoIf($["${RESULT}" != ""]?set_returned,1:set_static,1)

exten => set_returned,1,Set(CALLERID(number)=${RESULT})
exten => set_returned,n,Return()

exten => set_static,1,Set(CALLERID(number)=269nxxxxxx)
exten => set_static,n,Return()



As for the lookup and routing, you could do something similar:

exten => _1NXXNXXXXXX,1,GoSub(checkInternalRoute,start,1(${EXTEN}))
same => n,Set(requestedDestination=${EXTEN})
same => n,GotoIf($["${GOSUB_RETVAL}" = "1"]?internal,1:external,1)

[checkInternalRoute]
exten => start,1,Verbose(2,Check if dialed number is internal)
exten => start,n,Set(isInternal=${ODBC_GET_INTERNAL_STATUS(${ARG1})})
exten => start,n,Return(${isInternal})


Then in func_odbc.conf you set your readsql to something like:

SELECT 1 FROM internal WHERE routes = '${ARG1}'



...or something like that ;)  Totally untested.

Leif.



More information about the asterisk-users mailing list