How to call PHP Scripts to search details in DB based on caller phone number

I have a PHP script in /var/lib/asterisk/agi-bin/lookup.php which looks like this:

#!/usr/bin/php -q
<?php
require 'phpagi.php';
$agi = new AGI();
$no=preg_replace("#[^0-9]#","",$agi->request[agi_callerid]);
$db = 'cdr';
$dbuser = 'root';
$dbpass = '';
$dbhost = 'localhost';

mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db("$db"); or die("could not open database");
$row=mysql_query("select title,firstname,lastname from customers where customers.phone='$no' LIMIT 1");
if (mysql_num_rows($row)==1){
        $row=mysql_fetch_array($row);
        if ($row[title]) $name .= $row[title]." ";
        if ($row[firstname]) $name .= $row[firstname]." ";
        if ($row[lastname]) $name .= $row[lastname];
        }
else $name=$agi->request[agi_callerid];
$agi->set_variable("lookupcid", $name);
?>

I have 3 CHAN_SIP extensions and have updated extensions_custom.conf as follows in attempts to to read the script to no avail:
[tigapii]
exten => 701,1,AGI(/var/lib/asterisk/agi-bin/lookup.php)
exten => 701,n,Set(CALLERID(name)=${lookupcid})
exten => 701,n,Dial(SIP/701,20,t)
exten => 701,n,Hangup

exten => 702,1,AGI(/var/lib/asterisk/agi-bin/lookup.php)
exten => 702,n,Set(CALLERID(name)=${lookupcid})
exten => 702,n,Dial(SIP/702,20,t)
exten => 702,2,Hangup()

exten => 703,1,AGI(/var/lib/asterisk/agi-bin/lookup.php)
exten => 703,n,Set(CALLERID(name)=${lookupcid})
exten => 703,n,Dial(SIP/703,20,t)
exten => 703,2,Hangup()

Any help will be appreciated.

Is this AGI intended to run on inbound calls? If so, the conventional way is to use a Custom Destination and in the GUI set up the call flow like:
inbound route → Custom Destination → etc.

@lgaetz thanks for your reply, Yes it is intended to capture all incoming calls numbers and check in the DB.
let me start by saying I am not so much experienced in Asterisk, so kindly clarify further if you may on setting the call flow. Thanks.
PBX Version:15.0.17.64
PBX Distro:12.7.8-2107-3.sng7
Asterisk Version:16.20.0

First, what you’re trying to do is a GUI feature of CID Superfecta using the SFDatabase module.

If you want to continue using an AGI, you will want something like:

[tigapii]
exten => s,1,Noop(Entering user defined context tigapii in extensions_custom.conf)
exten => s,n,AGI(/var/lib/asterisk/agi-bin/lookup.php)
exten => s,n,Set(CALLERID(name)=${lookupcid})
exten => s,n,Return

Then create a Custom Destination with a target of

tigapii,s,1

Enable the return option and then config your destinations like normal.

@lgaetz Thanks a lot let me try that.

1 Like

@lgaetz Once again thanks for the above.
I am still in the woods with this, as I said earlier I am really not experience with Asterisk, so in my extensions_custom.conf I had included the extensions 701 to 703 which I have registered on Zoiper soft-phone. On dialing them is when I expect to call the lookup.php script. So what exactly do you mean by custom destination and how do i go about it. A direction to learning resource for PHPAGI will be appreciated as well.

If you’re a beginner, just forget everything you’ve attempted so far and create a new CID Superfecta scheme. Enable the SFDdabase module and populate the fields with your dbase creds and query. Then enable the superfeca scheme for the inbound route.

Might I suggest the Dynamic Routes extension? In your script return the extension (or connect it directly to a MySQL database) and use the output to route to the corresponding extension.

Thanks @lgaetz and @vespino I will check on those two and give feedback by tomorrow.

Hello @vespino & @lgaetz , so i opted for dynamic routes as explained by @lgaetz in this https://www.youtube.com/watch?v=LjTVrXrlXds&list=PLwArJQCnXvSP3Uqf1MiGmISn6Q8KcDsHs&index=1&ab_channel=OfficialAsteriskYouTubeChannel tutorial.
I did a mysql look up and the query as seen from the screenshot attached returns a dst on 701, which is the match i want found and redirecting the call to extension 705 as shown.

Problem is when i dial extension 701 from extension 704, the call is not redirected to extension 705 as i expected.
My in bound route screen shot is attached here as well.

Your goal is to have a lookup and have the caller forwarded to the result from Dynamic Routes correct? If so, the inbound route comes from outside, so you should leave DID (or set it to the number that is called). 701 is an internal extension.

@vespino yep that’s is my goal, I have changed the DID Number to ANY, but still this is what i get when i dial 701 from 704:
== Using SIP RTP TOS bits 184
== Using SIP RTP CoS mark 5
> 0x7f5ba0007020 – Strict RTP learning after remote address set to: 197.232.55.88:8020
– Executing [[email protected]:1] GotoIf(“SIP/704-00000002”, “1?ext-local,701,1:followme-check,701,1”) in new stack
– Goto (ext-local,701,1)
– Executing [[email protected]:1] Set(“SIP/704-00000002”, “__RINGTIMER=15”) in new stack
– Executing [[email protected]:2] ExecIf(“SIP/704-00000002”, “0?Set(__CWIGNORE=)”) in new stack
– Executing [[email protected]:3] Macro(“SIP/704-00000002”, “exten-vm,701,701,0,0,0”) in new stack
– Executing [[email protected]:1] Macro(“SIP/704-00000002”, “user-callerid,”) in new stack
– Executing [[email protected]:1] Set(“SIP/704-00000002”, “TOUCH_MONITOR=1641985091.2”) in new stack
– Executing [[email protected]:2] Set(“SIP/704-00000002”, “CHANCONTEXT=”) in new stack
– Executing [[email protected]:3] Set(“SIP/704-00000002”, “CHANCONTEXT=”) in new stack
– Executing [[email protected]:4] Set(“SIP/704-00000002”, “CHANEXTENCONTEXT=704-00000002”) in new stack
– Executing [[email protected]:5] Set(“SIP/704-00000002”, “CHANEXTEN=704-00000002”) in new stack
– Executing [[email protected]:6] Set(“SIP/704-00000002”, “CALLERID(number)=704”) in new stack
– Executing [[email protected]:7] Set(“SIP/704-00000002”, “AMPUSER=704”) in new stack
– Executing [[email protected]:8] Set(“SIP/704-00000002”, “HOTDESCKCHAN=704-00000002”) in new stack
– Executing [[email protected]:9] Set(“SIP/704-00000002”, “HOTDESKEXTEN=704”) in new stack
– Executing [[email protected]:10] Set(“SIP/704-00000002”, “HOTDESKCALL=0”) in new stack
– Executing [[email protected]:11] ExecIf(“SIP/704-00000002”, “0?Set(HOTDESKCALL=1)”) in new stack
– Executing [[email protected]:12] ExecIf(“SIP/704-00000002”, “0?Set(CALLERID(name)=)”) in new stack
– Executing [[email protected]:13] GotoIf(“SIP/704-00000002”, “0?report”) in new stack
– Executing [[email protected]:14] ExecIf(“SIP/704-00000002”, “1?Set(REALCALLERIDNUM=704)”) in new stack
– Executing [[email protected]:15] Set(“SIP/704-00000002”, “AMPUSER=704”) in new stack
– Executing [[email protected]:16] GotoIf(“SIP/704-00000002”, “0?limit”) in new stack
– Executing [s[email protected]:17] Set(“SIP/704-00000002”, “AMPUSERCIDNAME=Tested”) in new stack
– Executing [[email protected]:18] ExecIf(“SIP/704-00000002”, “0?Set(__CIDMASQUERADING=TRUE)”) in new stack
– Executing [[email protected]:19] GotoIf(“SIP/704-00000002”, “0?report”) in new stack
– Executing [[email protected]:20] Set(“SIP/704-00000002”, “AMPUSERCID=704”) in new stack
– Executing [[email protected]:21] Set(“SIP/704-00000002”, “__DIAL_OPTIONS=HhTtr”) in new stack
– Executing [[email protected]:22] Set(“SIP/704-00000002”, “CALLERID(all)=“Tested” <704>”) in new stack
– Executing [[email protected]:23] ExecIf(“SIP/704-00000002”, “0?Set(CUSDIAL=701)”) in new stack
– Executing [[email protected]:24] ExecIf(“SIP/704-00000002”, “0?Set(CALLERID(all)=“Tested” <704>)”) in new stack
– Executing [[email protected]:25] GotoIf(“SIP/704-00000002”, “0?limit”) in new stack
– Executing [[email protected]:26] ExecIf(“SIP/704-00000002”, “0?Set(GROUP(concurrency_limit)=704)”) in new stack
– Executing [[email protected]:27] ExecIf(“SIP/704-00000002”, “0?Set(CHANNEL(language)=)”) in new stack
– Executing [[email protected]:28] NoOp(“SIP/704-00000002”, “Macro Depth is 2”) in new stack
– Executing [[email protected]:29] GotoIf(“SIP/704-00000002”, “1?report2:macroerror”) in new stack
– Goto (macro-user-callerid,s,30)
– Executing [[email protected]:30] GotoIf(“SIP/704-00000002”, “0?continue”) in new stack
– Executing [[email protected]:31] ExecIf(“SIP/704-00000002”, “1?Set(__CALLEE_ACCOUNCODE=)”) in new stack
– Executing [[email protected]:32] Set(“SIP/704-00000002”, “__TTL=64”) in new stack
– Executing [[email protected]:33] GotoIf(“SIP/704-00000002”, “1?continue”) in new stack
– Goto (macro-user-callerid,s,49)
– Executing [[email protected]:49] Set(“SIP/704-00000002”, “CALLERID(number)=704”) in new stack
– Executing [[email protected]:50] Set(“SIP/704-00000002”, “CALLERID(name)=Tested”) in new stack
– Executing [[email protected]:51] GotoIf(“SIP/704-00000002”, “0?cnum”) in new stack
– Executing [[email protected]:52] Set(“SIP/704-00000002”, “CDR(cnam)=Tested”) in new stack
– Executing [[email protected]:53] Set(“SIP/704-00000002”, “CDR(cnum)=704”) in new stack
– Executing [[email protected]:54] Set(“SIP/704-00000002”, “CHANNEL(language)=en”) in new stack
– Executing [[email protected]:2] Set(“SIP/704-00000002”, “RingGroupMethod=none”) in new stack
– Executing [[email protected]:3] Set(“SIP/704-00000002”, “__EXTTOCALL=701”) in new stack
– Executing [[email protected]:4] Set(“SIP/704-00000002”, “__PICKUPMARK=701”) in new stack
– Executing [[email protected]:5] Set(“SIP/704-00000002”, “RT=15”) in new stack
– Executing [[email protected]:6] ExecIf(“SIP/704-00000002”, “0?Macro(vm,701,DIRECTDIAL,)”) in new stack
– Executing [[email protected]:7] ExecIf(“SIP/704-00000002”, “0?MacroExit()”) in new stack
– Executing [[email protected]:8] ExecIf(“SIP/704-00000002”, “0?Gosub(ext-intercom,*80701,1())”) in new stack
– Executing [[email protected]:9] ExecIf(“SIP/704-00000002”, “0?MacroExit()”) in new stack
– Executing [[email protected]:10] ExecIf(“SIP/704-00000002”, “0?ChanSpy(SIP/701,q)”) in new stack
– Executing [[email protected]:11] ExecIf(“SIP/704-00000002”, “0?MacroExit()”) in new stack
[2022-01-12 13:58:11] ERROR[11214][C-00000003]: res_pjsip_header_funcs.c:622 func_read_header: This function requires a PJSIP channel.
– Executing [[email protected]:12] ExecIf(“SIP/704-00000002”, “0?Macro(vm,701,DIRECTDIAL,)”) in new stack
[2022-01-12 13:58:11] ERROR[11214][C-00000003]: res_pjsip_header_funcs.c:622 func_read_header: This function requires a PJSIP channel.
[2022-01-12 13:58:11] ERROR[11214][C-00000003]: res_pjsip_header_funcs.c:622 func_read_header: This function requires a PJSIP channel.
– Executing [[email protected]:13] ExecIf(“SIP/704-00000002”, “0?MacroExit()”) in new stack
[2022-01-12 13:58:11] ERROR[11214][C-00000003]: res_pjsip_header_funcs.c:622 func_read_header: This function requires a PJSIP channel.
[2022-01-12 13:58:11] ERROR[11214][C-00000003]: res_pjsip_header_funcs.c:622 func_read_header: This function requires a PJSIP channel.
– Executing [[email protected]:14] ExecIf(“SIP/704-00000002”, “0?Gosub(ext-intercom,*80701,1())”) in new stack
[2022-01-12 13:58:11] ERROR[11214][C-00000003]: res_pjsip_header_funcs.c:622 func_read_header: This function requires a PJSIP channel.
[2022-01-12 13:58:11] ERROR[11214][C-00000003]: res_pjsip_header_funcs.c:622 func_read_header: This function requires a PJSIP channel.
– Executing [[email protected]:15] ExecIf(“SIP/704-00000002”, “0?MacroExit()”) in new stack
[2022-01-12 13:58:11] ERROR[11214][C-00000003]: res_pjsip_header_funcs.c:622 func_read_header: This function requires a PJSIP channel.
[2022-01-12 13:58:11] ERROR[11214][C-00000003]: res_pjsip_header_funcs.c:622 func_read_header: This function requires a PJSIP channel.
– Executing [[email protected]:16] ExecIf(“SIP/704-00000002”, “0?ChanSpy(SIP/701,q)”) in new stack
[2022-01-12 13:58:11] ERROR[11214][C-00000003]: res_pjsip_header_funcs.c:622 func_read_header: This function requires a PJSIP channel.
[2022-01-12 13:58:11] ERROR[11214][C-00000003]: res_pjsip_header_funcs.c:622 func_read_header: This function requires a PJSIP channel.
– Executing [[email protected]:17] ExecIf(“SIP/704-00000002”, “0?MacroExit()”) in new stack
[2022-01-12 13:58:11] ERROR[11214][C-00000003]: res_pjsip_header_funcs.c:622 func_read_header: This function requires a PJSIP channel.
– Executing [[email protected]:18] ExecIf(“SIP/704-00000002”, “0?Macro(vm,701,DIRECTDIAL,)”) in new stack
– Executing [[email protected]:19] ExecIf(“SIP/704-00000002”, “0?MacroExit()”) in new stack
[2022-01-12 13:58:11] ERROR[11214][C-00000003]: res_pjsip_header_funcs.c:622 func_read_header: This function requires a PJSIP channel.
– Executing [[email protected]:20] ExecIf(“SIP/704-00000002”, “0?Macro(vm,701,DIRECTDIAL,)”) in new stack
[2022-01-12 13:58:11] ERROR[11214][C-00000003]: res_pjsip_header_funcs.c:622 func_read_header: This function requires a PJSIP channel.
[2022-01-12 13:58:11] ERROR[11214][C-00000003]: res_pjsip_header_funcs.c:622 func_read_header: This function requires a PJSIP channel.
– Executing [[email protected]:21] ExecIf(“SIP/704-00000002”, “0?MacroExit()”) in new stack
[2022-01-12 13:58:11] ERROR[11214][C-00000003]: res_pjsip_header_funcs.c:622 func_read_header: This function requires a PJSIP channel.
– Executing [[email protected]:22] Gosub(“SIP/704-00000002”, “sub-record-check,s,1(exten,701,dontcare)”) in new stack
– Executing [[email protected]:1] GotoIf(“SIP/704-00000002”, “0?initialized”) in new stack
– Executing [[email protected]:2] Set(“SIP/704-00000002”, “__REC_STATUS=INITIALIZED”) in new stack
– Executing [[email protected]:3] Set(“SIP/704-00000002”, “NOW=1641985091”) in new stack
– Executing [[email protected]:4] Set(“SIP/704-00000002”, “__DAY=12”) in new stack
– Executing [[email protected]:5] Set(“SIP/704-00000002”, “__MONTH=01”) in new stack
– Executing [[email protected]:6] Set(“SIP/704-00000002”, “__YEAR=2022”) in new stack
– Executing [[email protected]:7] Set(“SIP/704-00000002”, “__TIMESTR=20220112-135811”) in new stack
– Executing [[email protected]:8] Set(“SIP/704-00000002”, “__FROMEXTEN=704”) in new stack
– Executing [[email protected]:9] Set(“SIP/704-00000002”, “__MON_FMT=wav”) in new stack
– Executing [[email protected]:10] NoOp(“SIP/704-00000002”, “Recordings initialized”) in new stack
– Executing [[email protected]:11] ExecIf(“SIP/704-00000002”, “0?Set(ARG3=dontcare)”) in new stack
– Executing [[email protected]:12] Set(“SIP/704-00000002”, “REC_POLICY_MODE_SAVE=”) in new stack
– Executing [[email protected]:13] ExecIf(“SIP/704-00000002”, “0?Set(REC_STATUS=NO)”) in new stack
– Executing [[email protected]:14] GotoIf(“SIP/704-00000002”, “5?checkaction”) in new stack
– Goto (sub-record-check,s,17)
– Executing [[email protected]:17] GotoIf(“SIP/704-00000002”, “1?sub-record-check,exten,1”) in new stack
– Goto (sub-record-check,exten,1)
– Executing [[email protected]:1] NoOp(“SIP/704-00000002”, “Exten Recording Check between 704 and 701”) in new stack
– Executing [[email protected]:2] Set(“SIP/704-00000002”, “CALLTYPE=internal”) in new stack
– Executing [[email protected]:3] ExecIf(“SIP/704-00000002”, “0?Set(CALLTYPE=)”) in new stack
– Executing [[email protected]:4] Set(“SIP/704-00000002”, “CALLEE=dontcare”) in new stack
– Executing [[email protected]:5] ExecIf(“SIP/704-00000002”, “0?Set(CALLEE=dontcare)”) in new stack
– Executing [[email protected]:6] GotoIf(“SIP/704-00000002”, “0?callee”) in new stack
– Executing [[email protected]:7] GotoIf(“SIP/704-00000002”, “1?caller”) in new stack
– Goto (sub-record-check,exten,13)
– Executing [[email protected]:13] Set(“SIP/704-00000002”, “RECMODE=dontcare”) in new stack
– Executing [[email protected]:14] Set(“SIP/704-00000002”, “CALLERRECMODE=dontcare”) in new stack
– Executing [[email protected]:15] Set(“SIP/704-00000002”, “CALEERECMODE=dontcare”) in new stack
– Executing [[email protected]:16] GotoIf(“SIP/704-00000002”, “0?processnormal”) in new stack
– Executing [[email protected]:17] ExecIf(“SIP/704-00000002”, “0?Set(RECMODE=dontcare)”) in new stack
– Executing [[email protected]:18] ExecIf(“SIP/704-00000002”, “0?Set(RECMODE=dontcare)”) in new stack
– Executing [[email protected]:19] ExecIf(“SIP/704-00000002”, “0?Set(RECMODE=dontcare)”) in new stack
– Executing [[email protected]:20] ExecIf(“SIP/704-00000002”, “0?Set(RECMODE=dontcare)”) in new stack
– Executing [[email protected]:21] ExecIf(“SIP/704-00000002”, “0?Set(RECMODE=dontcare)”) in new stack
– Executing [[email protected]:22] ExecIf(“SIP/704-00000002”, “1?Set(RECMODE=dontcare)”) in new stack
– Executing [[email protected]:23] Gosub(“SIP/704-00000002”, “recordcheck,1(dontcare,internal,701)”) in new stack
– Executing [[email protected]:1] NoOp(“SIP/704-00000002”, “Starting recording check against dontcare”) in new stack
– Executing [[email protected]:2] Goto(“SIP/704-00000002”, “dontcare”) in new stack
– Goto (sub-record-check,recordcheck,3)
– Executing [[email protected]:3] Return(“SIP/704-00000002”, “”) in new stack
– Executing [[email protected]:24] Return(“SIP/704-00000002”, “”) in new stack
– Executing [[email protected]:23] GotoIf(“SIP/704-00000002”, “1?macrodial”) in new stack
– Goto (macro-exten-vm,s,29)
– Executing [[email protected]:29] GosubIf(“SIP/704-00000002”, “0?clrheader,1()”) in new stack
– Executing [[email protected]:30] Macro(“SIP/704-00000002”, “dial-one,15,HhTtr,701”) in new stack
– Executing [[email protected]:1] Set(“SIP/704-00000002”, “DEXTEN=701”) in new stack
– Executing [[email protected]:2] Set(“SIP/704-00000002”, “__CRM_SOURCE=704”) in new stack
– Executing [[email protected]:3] ExecIf(“SIP/704-00000002”, “0?Set(__EXTTOCALL=701)”) in new stack
– Executing [[email protected]:4] Set(“SIP/704-00000002”, “DIALSTATUS_CW=”) in new stack
– Executing [[email protected]:5] GosubIf(“SIP/704-00000002”, “0?screen,1()”) in new stack
– Executing [[email protected]:6] GosubIf(“SIP/704-00000002”, “0?cf,1()”) in new stack
– Executing [[email protected]:7] GotoIf(“SIP/704-00000002”, “1?skip1”) in new stack
– Goto (macro-dial-one,s,10)
– Executing [[email protected]:10] GotoIf(“SIP/704-00000002”, “0?nodial”) in new stack
– Executing [[email protected]:11] GotoIf(“SIP/704-00000002”, “0?continue”) in new stack
– Executing [[email protected]:12] Set(“SIP/704-00000002”, “EXTHASCW=ENABLED”) in new stack
– Executing [[email protected]:13] GotoIf(“SIP/704-00000002”, “0?next1:cwinusebusy”) in new stack
– Goto (macro-dial-one,s,25)
– Executing [[email protected]:25] GotoIf(“SIP/704-00000002”, “0?next3:continue”) in new stack
– Goto (macro-dial-one,s,27)
– Executing [[email protected]:27] GotoIf(“SIP/704-00000002”, “0?nodial”) in new stack
– Executing [[email protected]:28] GosubIf(“SIP/704-00000002”, “1?dstring,1():dlocal,1()”) in new stack
– Executing [[email protected]:1] Set(“SIP/704-00000002”, “DSTRING=”) in new stack
– Executing [[email protected]:2] Set(“SIP/704-00000002”, “DEVICES=701”) in new stack
– Executing [[email protected]:3] ExecIf(“SIP/704-00000002”, “0?Return()”) in new stack
– Executing [[email protected]:4] ExecIf(“SIP/704-00000002”, “0?Set(DEVICES=01)”) in new stack
– Executing [[email protected]:5] Set(“SIP/704-00000002”, “LOOPCNT=1”) in new stack
– Executing [[email protected]:6] Set(“SIP/704-00000002”, “ITER=1”) in new stack
– Executing [[email protected]:7] Set(“SIP/704-00000002”, “THISDIAL=SIP/701”) in new stack
– Executing [[email protected]:8] GotoIf(“SIP/704-00000002”, “1?docheck”) in new stack
– Goto (macro-dial-one,dstring,14)
– Executing [[email protected]:14] GotoIf(“SIP/704-00000002”, “0?skipset”) in new stack
– Executing [[email protected]:15] Set(“SIP/704-00000002”, “DSTRING=SIP/701&”) in new stack
– Executing [[email protected]:16] Set(“SIP/704-00000002”, “ITER=2”) in new stack
– Executing [[email protected]:17] GotoIf(“SIP/704-00000002”, “0?begin”) in new stack
– Executing [[email protected]:18] ExecIf(“SIP/704-00000002”, “0?Return()”) in new stack
– Executing [[email protected]:19] Set(“SIP/704-00000002”, “DSTRING=SIP/701”) in new stack
– Executing [[email protected]:20] Return(“SIP/704-00000002”, “”) in new stack
– Executing [[email protected]:29] GotoIf(“SIP/704-00000002”, “0?nodial”) in new stack
– Executing [[email protected]:30] GotoIf(“SIP/704-00000002”, “0?skiptrace”) in new stack
– Executing [[email protected]:31] GosubIf(“SIP/704-00000002”, “1?ctset,1():ctclear,1()”) in new stack
– Executing [[email protected]:1] Set(“SIP/704-00000002”, “DB(CALLTRACE/701)=704”) in new stack
– Executing [[email protected]:2] Return(“SIP/704-00000002”, “”) in new stack
– Executing [[email protected]:32] Set(“SIP/704-00000002”, “D_OPTIONS=HhTtr”) in new stack
– Executing [[email protected]:33] GosubIf(“SIP/704-00000002”, “0?func-set-sipheader,s,1(Alert-Info,)”) in new stack
– Executing [[email protected]:34] NoOp(“SIP/704-00000002”, "Blind Transfer: , Attended Transfer: , User: 704, Alert Info: ") in new stack
– Executing [[email protected]:35] ExecIf(“SIP/704-00000002”, “1?Set(ALERT_INFO=)”) in new stack
– Executing [[email protected]:36] ExecIf(“SIP/704-00000002”, “0?Set(ALERT_INFO=)”) in new stack
– Executing [[email protected]:37] ExecIf(“SIP/704-00000002”, “0?Set(ALERT_INFO=)”) in new stack
– Executing [[email protected]:38] ExecIf(“SIP/704-00000002”, “0?Set(ALERT_INFO=Normal;volume=)”) in new stack
– Executing [[email protected]:39] ExecIf(“SIP/704-00000002”, “0?Set(ALERT_INFO=Normal;volume=)”) in new stack
– Executing [[email protected]:40] GosubIf(“SIP/704-00000002”, “0?func-set-sipheader,s,1(Alert-Info,)”) in new stack
– Executing [[email protected]:41] ExecIf(“SIP/704-00000002”, “0?Set(CHANNEL(musicclass)=)”) in new stack
– Executing [[email protected]:42] GosubIf(“SIP/704-00000002”, “0?qwait,1()”) in new stack
– Executing [[email protected]:43] Set(“SIP/704-00000002”, “__CWIGNORE=”) in new stack
– Executing [[email protected]:44] Set(“SIP/704-00000002”, “__KEEPCID=TRUE”) in new stack
– Executing [[email protected]:45] GotoIf(“SIP/704-00000002”, “0?usegoto,1”) in new stack
– Executing [[email protected]:46] GotoIf(“SIP/704-00000002”, “0?godial”) in new stack
– Executing [[email protected]:47] Gosub(“SIP/704-00000002”, “sub-presencestate-display,s,1(701)”) in new stack
– Executing [[email protected]:1] Goto(“SIP/704-00000002”, “state-not_set,1”) in new stack
– Goto (sub-presencestate-display,state-not_set,1)
– Executing [[email protected]:1] Set(“SIP/704-00000002”, “PRESENCESTATE_DISPLAY=”) in new stack
– Executing [[email protected]:2] Return(“SIP/704-00000002”, “”) in new stack
– Executing [[email protected]:48] Set(“SIP/704-00000002”, “CONNECTEDLINE(name,i)=John Doe”) in new stack
– Executing [[email protected]:49] Set(“SIP/704-00000002”, “CONNECTEDLINE(num)=701”) in new stack
– Executing [[email protected]:50] Set(“SIP/704-00000002”, “D_OPTIONS=HhTtr”) in new stack
– Executing [[email protected]:51] Macro(“SIP/704-00000002”, “dialout-one-predial-hook,”) in new stack
– Executing [[email protected]:1] MacroExit(“SIP/704-00000002”, “”) in new stack
– Executing [[email protected]:52] ExecIf(“SIP/704-00000002”, “0?Set(D_OPTIONS=HhtrI)”) in new stack
– Executing [[email protected]:53] ExecIf(“SIP/704-00000002”, “0?Set(CWRING=r(callwaiting)):Set(CWRING=)”) in new stack
– Executing [[email protected]:54] NoOp(“SIP/704-00000002”, “”) in new stack
– Executing [[email protected]:55] ExecIf(“SIP/704-00000002”, “0?Set(D_OPTIONS=HhTtrg)”) in new stack
– Executing [[email protected]:56] Dial(“SIP/704-00000002”, “SIP/701,15,HhTtrb(func-apply-sipheaders^s^1)”) in new stack
== Using SIP RTP TOS bits 184
== Using SIP RTP CoS mark 5
– SIP/701-00000003 Internal Gosub(func-apply-sipheaders,s,1) start
– Executing [[email protected]:1] ExecIf(“SIP/701-00000003”, “0?Set(CHANNEL(hangup_handler_push)=crm-hangup,s,1)”) in new stack
– Executing [[email protected]:2] NoOp(“SIP/701-00000003”, “Applying SIP Headers to channel SIP/701-00000003”) in new stack
– Executing [[email protected]:3] Set(“SIP/701-00000003”, “TECH=SIP”) in new stack
– Executing [[email protected]:4] Set(“SIP/701-00000003”, “SIPHEADERKEYS=”) in new stack
– Executing [[email protected]:5] While(“SIP/701-00000003”, “0”) in new stack
– Jumping to priority 13
– Executing [[email protected]:14] Return(“SIP/701-00000003”, “”) in new stack
== Spawn extension (from-internal, 701, 1) exited non-zero on ‘SIP/701-00000003’
– SIP/701-00000003 Internal Gosub(func-apply-sipheaders,s,1) complete GOSUB_RETVAL=
– Called SIP/701
– SIP/701-00000003 is ringing
> 0x7f5ba80760a0 – Strict RTP learning after remote address set to: 192.168.1.143:8000
– SIP/701-00000003 answered SIP/704-00000002
– Channel SIP/701-00000003 joined ‘simple_bridge’ basic-bridge
– Channel SIP/704-00000002 joined ‘simple_bridge’ basic-bridge
[2022-01-12 13:58:53] NOTICE[2939]: chan_sip.c:29971 check_rtp_timeout: Disconnecting call ‘SIP/704-00000002’ for lack of RTP activity in 31 seconds
[2022-01-12 13:58:53] NOTICE[2939]: chan_sip.c:29971 check_rtp_timeout: Disconnecting call ‘SIP/701-00000003’ for lack of RTP activity in 31 seconds
– Channel SIP/704-00000002 left ‘simple_bridge’ basic-bridge
== Spawn extension (macro-dial-one, s, 56) exited non-zero on ‘SIP/704-00000002’ in macro ‘dial-one’
== Spawn extension (macro-exten-vm, s, 30) exited non-zero on ‘SIP/704-00000002’ in macro ‘exten-vm’
== Spawn extension (ext-local, 701, 3) exited non-zero on ‘SIP/704-00000002’
– Executing [[email protected]:1] Macro(“SIP/704-00000002”, “hangupcall,”) in new stack
– Executing [[email protected]:1] GotoIf(“SIP/704-00000002”, “1?theend”) in new stack
– Goto (macro-hangupcall,s,3)
– Channel SIP/701-00000003 left ‘simple_bridge’ basic-bridge
– Executing [[email protected]:3] ExecIf(“SIP/704-00000002”, “0?Set(CDR(recordingfile)=)”) in new stack
– Executing [[email protected]:4] NoOp(“SIP/704-00000002”, "SIP/701-00000003 montior file= ") in new stack
– Executing [[email protected]:5] GotoIf(“SIP/704-00000002”, “1?skipagi”) in new stack
– Goto (macro-hangupcall,s,7)
– Executing [[email protected]:7] Hangup(“SIP/704-00000002”, “”) in new stack
== Spawn extension (macro-hangupcall, s, 7) exited non-zero on ‘SIP/704-00000002’ in macro ‘hangupcall’
== Spawn extension (ext-local, h, 1) exited non-zero on ‘SIP/704-00000002’

Dynamic Routes isn’t used when calling from 701 to 705, you know that, right? It’s only used when an external call is coming through your inbound route and if your script returns 701, it’s forwarded to extension “705 XXX”. Maybe you should post your call flow and also post your method of testing.

I didn’t know that, I think I am doing something totally different, this is what I want to achieve, I have these extensions pre-filled in a table called customers[customer_name,phone(701 to 705 extensions with their respective names of course)] in my DB.
What I want to do is just a proof of concept whereby if I dial from extension 701, which already exists in my DB as a customer with full names [say John Doe], I use the extension number in this case 701, then return the names of the customer, that is John Doe.

Essentially in production mode, this should work say when a customer calls the customer support team, the customer number is used to search records in DB, then displayed back to the customer service team/agent, so that they know before picking the call who they are talking to, this can also be used to route the call to the relevant agent, even before picking.
That’s what I want to implement.
So with this explanation am I doing the right thing so far @vespino ?

Routing based on CID could work using Dynamic Routes. When a call comes in lookup the number using your script, it returns the extension and you then use that as “Dynamic Route Entries”. If you’re also interested in “CID to client name”, you can set a variant of your script which returns the name under Inbound Routes > Other > CID Lookup Source.

Hello @vespino & @lgaetz , first thanks for your help with this, happy to report that I made it to work!!!
The first mistake I was making is in extensions_custom.conf, apparently when you create an extension, it is saved in sip_additional.conf with context=from-internal.
So updated it as follows;

[from-internal-custom]
include => tigapii

[ tigapii]
exten => 701,1,AGI(test.php)
exten => 701,n,NoOp(MANDELA)
exten => 701,n,Set(CALLERID(name)=${lookupcid})
exten => 701,n,Dial(SIP/701,10,t)
exten => 701,n,Hangup()

My test.php script remained the same except the ‘$no’, which i changed to just phone=$no.
Made the script executable and changed it to be owned by asterisk, and that’s it. My script is executed as shown below, and the title, firstname & lastname**[Mr Test2 test2]** are substituted for the callerid[e.g 705] variable when a call is made to extension 701.
Hope this helps any other person.

– Executing [[email protected]:1] AGI(“SIP/705-00000002”, “test.php”) in new stack
– Launched AGI Script /var/lib/asterisk/agi-bin/test.php
– <SIP/705-00000002>AGI Script test.php completed, returning 0
– Executing [[email protected]:2] NoOp(“SIP/705-00000002”, “MANDELA”) in new stack
– Executing [[email protected]:3] Set(“SIP/705-00000002”, “CALLERID(name)=Mr Test2 test2”) in new stack
– Executing [[email protected]:4] Dial(“SIP/705-00000002”, “SIP/701,10,t”) in new stack
== Using SIP RTP TOS bits 184
== Using SIP RTP CoS mark 5
– Called SIP/701
– SIP/701-00000003 is ringing
> 0x7f1038014710 – Strict RTP learning after remote address set to: 192.168.1.143:8000
– SIP/701-00000003 answered SIP/705-00000002
– Channel SIP/701-00000003 joined ‘simple_bridge’ basic-bridge <12c81c89-9617-4b65-a139-2553d4d34798>
– Channel SIP/705-00000002 joined ‘simple_bridge’ basic-bridge <12c81c89-9617-4b65-a139-2553d4d34798>
– Channel SIP/705-00000002 left ‘simple_bridge’ basic-bridge <12c81c89-9617-4b65-a139-2553d4d34798>
– Channel SIP/701-00000003 left ‘simple_bridge’ basic-bridge <12c81c89-9617-4b65-a139-2553d4d34798>
== Spawn extension (from-internal, 701, 4) exited non-zero on ‘SIP/705-00000002’
– Executing [[email protected]:1] Macro(“SIP/705-00000002”, “hangupcall”) in new stack
– Executing [[email protected]:1] GotoIf(“SIP/705-00000002”, “1?theend”) in new stack
– Goto (macro-hangupcall,s,3)
– Executing [[email protected]:3] ExecIf(“SIP/705-00000002”, “0?Set(CDR(recordingfile)=)”) in new stack
– Executing [[email protected]:4] NoOp(“SIP/705-00000002”, "SIP/701-00000003 montior file= ") in new stack
– Executing [[email protected]:5] GotoIf(“SIP/705-00000002”, “1?skipagi”) in new stack
– Goto (macro-hangupcall,s,7)
– Executing [[email protected]:7] Hangup(“SIP/705-00000002”, “”) in new stack
== Spawn extension (macro-hangupcall, s, 7) exited non-zero on ‘SIP/705-00000002’ in macro ‘hangupcall’
== Spawn extension (from-internal, h, 1) exited non-zero on ‘SIP/705-00000002’

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.