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 [701@from-internal:1] GotoIf(“SIP/704-00000002”, “1?ext-local,701,1:followme-check,701,1”) in new stack
– Goto (ext-local,701,1)
– Executing [701@ext-local:1] Set(“SIP/704-00000002”, “__RINGTIMER=15”) in new stack
– Executing [701@ext-local:2] ExecIf(“SIP/704-00000002”, “0?Set(__CWIGNORE=)”) in new stack
– Executing [701@ext-local:3] Macro(“SIP/704-00000002”, “exten-vm,701,701,0,0,0”) in new stack
– Executing [s@macro-exten-vm:1] Macro(“SIP/704-00000002”, “user-callerid,”) in new stack
– Executing [s@macro-user-callerid:1] Set(“SIP/704-00000002”, “TOUCH_MONITOR=1641985091.2”) in new stack
– Executing [s@macro-user-callerid:2] Set(“SIP/704-00000002”, “CHANCONTEXT=”) in new stack
– Executing [s@macro-user-callerid:3] Set(“SIP/704-00000002”, “CHANCONTEXT=”) in new stack
– Executing [s@macro-user-callerid:4] Set(“SIP/704-00000002”, “CHANEXTENCONTEXT=704-00000002”) in new stack
– Executing [s@macro-user-callerid:5] Set(“SIP/704-00000002”, “CHANEXTEN=704-00000002”) in new stack
– Executing [s@macro-user-callerid:6] Set(“SIP/704-00000002”, “CALLERID(number)=704”) in new stack
– Executing [s@macro-user-callerid:7] Set(“SIP/704-00000002”, “AMPUSER=704”) in new stack
– Executing [s@macro-user-callerid:8] Set(“SIP/704-00000002”, “HOTDESCKCHAN=704-00000002”) in new stack
– Executing [s@macro-user-callerid:9] Set(“SIP/704-00000002”, “HOTDESKEXTEN=704”) in new stack
– Executing [s@macro-user-callerid:10] Set(“SIP/704-00000002”, “HOTDESKCALL=0”) in new stack
– Executing [s@macro-user-callerid:11] ExecIf(“SIP/704-00000002”, “0?Set(HOTDESKCALL=1)”) in new stack
– Executing [s@macro-user-callerid:12] ExecIf(“SIP/704-00000002”, “0?Set(CALLERID(name)=)”) in new stack
– Executing [s@macro-user-callerid:13] GotoIf(“SIP/704-00000002”, “0?report”) in new stack
– Executing [s@macro-user-callerid:14] ExecIf(“SIP/704-00000002”, “1?Set(REALCALLERIDNUM=704)”) in new stack
– Executing [s@macro-user-callerid:15] Set(“SIP/704-00000002”, “AMPUSER=704”) in new stack
– Executing [s@macro-user-callerid:16] GotoIf(“SIP/704-00000002”, “0?limit”) in new stack
– Executing [s@macro-user-callerid:17] Set(“SIP/704-00000002”, “AMPUSERCIDNAME=Tested”) in new stack
– Executing [s@macro-user-callerid:18] ExecIf(“SIP/704-00000002”, “0?Set(__CIDMASQUERADING=TRUE)”) in new stack
– Executing [s@macro-user-callerid:19] GotoIf(“SIP/704-00000002”, “0?report”) in new stack
– Executing [s@macro-user-callerid:20] Set(“SIP/704-00000002”, “AMPUSERCID=704”) in new stack
– Executing [s@macro-user-callerid:21] Set(“SIP/704-00000002”, “__DIAL_OPTIONS=HhTtr”) in new stack
– Executing [s@macro-user-callerid:22] Set(“SIP/704-00000002”, “CALLERID(all)=“Tested” <704>”) in new stack
– Executing [s@macro-user-callerid:23] ExecIf(“SIP/704-00000002”, “0?Set(CUSDIAL=701)”) in new stack
– Executing [s@macro-user-callerid:24] ExecIf(“SIP/704-00000002”, “0?Set(CALLERID(all)=“Tested” <704>)”) in new stack
– Executing [s@macro-user-callerid:25] GotoIf(“SIP/704-00000002”, “0?limit”) in new stack
– Executing [s@macro-user-callerid:26] ExecIf(“SIP/704-00000002”, “0?Set(GROUP(concurrency_limit)=704)”) in new stack
– Executing [s@macro-user-callerid:27] ExecIf(“SIP/704-00000002”, “0?Set(CHANNEL(language)=)”) in new stack
– Executing [s@macro-user-callerid:28] NoOp(“SIP/704-00000002”, “Macro Depth is 2”) in new stack
– Executing [s@macro-user-callerid:29] GotoIf(“SIP/704-00000002”, “1?report2:macroerror”) in new stack
– Goto (macro-user-callerid,s,30)
– Executing [s@macro-user-callerid:30] GotoIf(“SIP/704-00000002”, “0?continue”) in new stack
– Executing [s@macro-user-callerid:31] ExecIf(“SIP/704-00000002”, “1?Set(__CALLEE_ACCOUNCODE=)”) in new stack
– Executing [s@macro-user-callerid:32] Set(“SIP/704-00000002”, “__TTL=64”) in new stack
– Executing [s@macro-user-callerid:33] GotoIf(“SIP/704-00000002”, “1?continue”) in new stack
– Goto (macro-user-callerid,s,49)
– Executing [s@macro-user-callerid:49] Set(“SIP/704-00000002”, “CALLERID(number)=704”) in new stack
– Executing [s@macro-user-callerid:50] Set(“SIP/704-00000002”, “CALLERID(name)=Tested”) in new stack
– Executing [s@macro-user-callerid:51] GotoIf(“SIP/704-00000002”, “0?cnum”) in new stack
– Executing [s@macro-user-callerid:52] Set(“SIP/704-00000002”, “CDR(cnam)=Tested”) in new stack
– Executing [s@macro-user-callerid:53] Set(“SIP/704-00000002”, “CDR(cnum)=704”) in new stack
– Executing [s@macro-user-callerid:54] Set(“SIP/704-00000002”, “CHANNEL(language)=en”) in new stack
– Executing [s@macro-exten-vm:2] Set(“SIP/704-00000002”, “RingGroupMethod=none”) in new stack
– Executing [s@macro-exten-vm:3] Set(“SIP/704-00000002”, “__EXTTOCALL=701”) in new stack
– Executing [s@macro-exten-vm:4] Set(“SIP/704-00000002”, “__PICKUPMARK=701”) in new stack
– Executing [s@macro-exten-vm:5] Set(“SIP/704-00000002”, “RT=15”) in new stack
– Executing [s@macro-exten-vm:6] ExecIf(“SIP/704-00000002”, “0?Macro(vm,701,DIRECTDIAL,)”) in new stack
– Executing [s@macro-exten-vm:7] ExecIf(“SIP/704-00000002”, “0?MacroExit()”) in new stack
– Executing [s@macro-exten-vm:8] ExecIf(“SIP/704-00000002”, “0?Gosub(ext-intercom,*80701,1())”) in new stack
– Executing [s@macro-exten-vm:9] ExecIf(“SIP/704-00000002”, “0?MacroExit()”) in new stack
– Executing [s@macro-exten-vm:10] ExecIf(“SIP/704-00000002”, “0?ChanSpy(SIP/701,q)”) in new stack
– Executing [s@macro-exten-vm: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 [s@macro-exten-vm: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 [s@macro-exten-vm: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 [s@macro-exten-vm: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 [s@macro-exten-vm: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 [s@macro-exten-vm: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 [s@macro-exten-vm: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 [s@macro-exten-vm:18] ExecIf(“SIP/704-00000002”, “0?Macro(vm,701,DIRECTDIAL,)”) in new stack
– Executing [s@macro-exten-vm: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 [s@macro-exten-vm: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 [s@macro-exten-vm: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 [s@macro-exten-vm:22] Gosub(“SIP/704-00000002”, “sub-record-check,s,1(exten,701,dontcare)”) in new stack
– Executing [s@sub-record-check:1] GotoIf(“SIP/704-00000002”, “0?initialized”) in new stack
– Executing [s@sub-record-check:2] Set(“SIP/704-00000002”, “__REC_STATUS=INITIALIZED”) in new stack
– Executing [s@sub-record-check:3] Set(“SIP/704-00000002”, “NOW=1641985091”) in new stack
– Executing [s@sub-record-check:4] Set(“SIP/704-00000002”, “__DAY=12”) in new stack
– Executing [s@sub-record-check:5] Set(“SIP/704-00000002”, “__MONTH=01”) in new stack
– Executing [s@sub-record-check:6] Set(“SIP/704-00000002”, “__YEAR=2022”) in new stack
– Executing [s@sub-record-check:7] Set(“SIP/704-00000002”, “__TIMESTR=20220112-135811”) in new stack
– Executing [s@sub-record-check:8] Set(“SIP/704-00000002”, “__FROMEXTEN=704”) in new stack
– Executing [s@sub-record-check:9] Set(“SIP/704-00000002”, “__MON_FMT=wav”) in new stack
– Executing [s@sub-record-check:10] NoOp(“SIP/704-00000002”, “Recordings initialized”) in new stack
– Executing [s@sub-record-check:11] ExecIf(“SIP/704-00000002”, “0?Set(ARG3=dontcare)”) in new stack
– Executing [s@sub-record-check:12] Set(“SIP/704-00000002”, “REC_POLICY_MODE_SAVE=”) in new stack
– Executing [s@sub-record-check:13] ExecIf(“SIP/704-00000002”, “0?Set(REC_STATUS=NO)”) in new stack
– Executing [s@sub-record-check:14] GotoIf(“SIP/704-00000002”, “5?checkaction”) in new stack
– Goto (sub-record-check,s,17)
– Executing [s@sub-record-check:17] GotoIf(“SIP/704-00000002”, “1?sub-record-check,exten,1”) in new stack
– Goto (sub-record-check,exten,1)
– Executing [exten@sub-record-check:1] NoOp(“SIP/704-00000002”, “Exten Recording Check between 704 and 701”) in new stack
– Executing [exten@sub-record-check:2] Set(“SIP/704-00000002”, “CALLTYPE=internal”) in new stack
– Executing [exten@sub-record-check:3] ExecIf(“SIP/704-00000002”, “0?Set(CALLTYPE=)”) in new stack
– Executing [exten@sub-record-check:4] Set(“SIP/704-00000002”, “CALLEE=dontcare”) in new stack
– Executing [exten@sub-record-check:5] ExecIf(“SIP/704-00000002”, “0?Set(CALLEE=dontcare)”) in new stack
– Executing [exten@sub-record-check:6] GotoIf(“SIP/704-00000002”, “0?callee”) in new stack
– Executing [exten@sub-record-check:7] GotoIf(“SIP/704-00000002”, “1?caller”) in new stack
– Goto (sub-record-check,exten,13)
– Executing [exten@sub-record-check:13] Set(“SIP/704-00000002”, “RECMODE=dontcare”) in new stack
– Executing [exten@sub-record-check:14] Set(“SIP/704-00000002”, “CALLERRECMODE=dontcare”) in new stack
– Executing [exten@sub-record-check:15] Set(“SIP/704-00000002”, “CALEERECMODE=dontcare”) in new stack
– Executing [exten@sub-record-check:16] GotoIf(“SIP/704-00000002”, “0?processnormal”) in new stack
– Executing [exten@sub-record-check:17] ExecIf(“SIP/704-00000002”, “0?Set(RECMODE=dontcare)”) in new stack
– Executing [exten@sub-record-check:18] ExecIf(“SIP/704-00000002”, “0?Set(RECMODE=dontcare)”) in new stack
– Executing [exten@sub-record-check:19] ExecIf(“SIP/704-00000002”, “0?Set(RECMODE=dontcare)”) in new stack
– Executing [exten@sub-record-check:20] ExecIf(“SIP/704-00000002”, “0?Set(RECMODE=dontcare)”) in new stack
– Executing [exten@sub-record-check:21] ExecIf(“SIP/704-00000002”, “0?Set(RECMODE=dontcare)”) in new stack
– Executing [exten@sub-record-check:22] ExecIf(“SIP/704-00000002”, “1?Set(RECMODE=dontcare)”) in new stack
– Executing [exten@sub-record-check:23] Gosub(“SIP/704-00000002”, “recordcheck,1(dontcare,internal,701)”) in new stack
– Executing [recordcheck@sub-record-check:1] NoOp(“SIP/704-00000002”, “Starting recording check against dontcare”) in new stack
– Executing [recordcheck@sub-record-check:2] Goto(“SIP/704-00000002”, “dontcare”) in new stack
– Goto (sub-record-check,recordcheck,3)
– Executing [recordcheck@sub-record-check:3] Return(“SIP/704-00000002”, “”) in new stack
– Executing [exten@sub-record-check:24] Return(“SIP/704-00000002”, “”) in new stack
– Executing [s@macro-exten-vm:23] GotoIf(“SIP/704-00000002”, “1?macrodial”) in new stack
– Goto (macro-exten-vm,s,29)
– Executing [s@macro-exten-vm:29] GosubIf(“SIP/704-00000002”, “0?clrheader,1()”) in new stack
– Executing [s@macro-exten-vm:30] Macro(“SIP/704-00000002”, “dial-one,15,HhTtr,701”) in new stack
– Executing [s@macro-dial-one:1] Set(“SIP/704-00000002”, “DEXTEN=701”) in new stack
– Executing [s@macro-dial-one:2] Set(“SIP/704-00000002”, “__CRM_SOURCE=704”) in new stack
– Executing [s@macro-dial-one:3] ExecIf(“SIP/704-00000002”, “0?Set(__EXTTOCALL=701)”) in new stack
– Executing [s@macro-dial-one:4] Set(“SIP/704-00000002”, “DIALSTATUS_CW=”) in new stack
– Executing [s@macro-dial-one:5] GosubIf(“SIP/704-00000002”, “0?screen,1()”) in new stack
– Executing [s@macro-dial-one:6] GosubIf(“SIP/704-00000002”, “0?cf,1()”) in new stack
– Executing [s@macro-dial-one:7] GotoIf(“SIP/704-00000002”, “1?skip1”) in new stack
– Goto (macro-dial-one,s,10)
– Executing [s@macro-dial-one:10] GotoIf(“SIP/704-00000002”, “0?nodial”) in new stack
– Executing [s@macro-dial-one:11] GotoIf(“SIP/704-00000002”, “0?continue”) in new stack
– Executing [s@macro-dial-one:12] Set(“SIP/704-00000002”, “EXTHASCW=ENABLED”) in new stack
– Executing [s@macro-dial-one:13] GotoIf(“SIP/704-00000002”, “0?next1:cwinusebusy”) in new stack
– Goto (macro-dial-one,s,25)
– Executing [s@macro-dial-one:25] GotoIf(“SIP/704-00000002”, “0?next3:continue”) in new stack
– Goto (macro-dial-one,s,27)
– Executing [s@macro-dial-one:27] GotoIf(“SIP/704-00000002”, “0?nodial”) in new stack
– Executing [s@macro-dial-one:28] GosubIf(“SIP/704-00000002”, “1?dstring,1():dlocal,1()”) in new stack
– Executing [dstring@macro-dial-one:1] Set(“SIP/704-00000002”, “DSTRING=”) in new stack
– Executing [dstring@macro-dial-one:2] Set(“SIP/704-00000002”, “DEVICES=701”) in new stack
– Executing [dstring@macro-dial-one:3] ExecIf(“SIP/704-00000002”, “0?Return()”) in new stack
– Executing [dstring@macro-dial-one:4] ExecIf(“SIP/704-00000002”, “0?Set(DEVICES=01)”) in new stack
– Executing [dstring@macro-dial-one:5] Set(“SIP/704-00000002”, “LOOPCNT=1”) in new stack
– Executing [dstring@macro-dial-one:6] Set(“SIP/704-00000002”, “ITER=1”) in new stack
– Executing [dstring@macro-dial-one:7] Set(“SIP/704-00000002”, “THISDIAL=SIP/701”) in new stack
– Executing [dstring@macro-dial-one:8] GotoIf(“SIP/704-00000002”, “1?docheck”) in new stack
– Goto (macro-dial-one,dstring,14)
– Executing [dstring@macro-dial-one:14] GotoIf(“SIP/704-00000002”, “0?skipset”) in new stack
– Executing [dstring@macro-dial-one:15] Set(“SIP/704-00000002”, “DSTRING=SIP/701&”) in new stack
– Executing [dstring@macro-dial-one:16] Set(“SIP/704-00000002”, “ITER=2”) in new stack
– Executing [dstring@macro-dial-one:17] GotoIf(“SIP/704-00000002”, “0?begin”) in new stack
– Executing [dstring@macro-dial-one:18] ExecIf(“SIP/704-00000002”, “0?Return()”) in new stack
– Executing [dstring@macro-dial-one:19] Set(“SIP/704-00000002”, “DSTRING=SIP/701”) in new stack
– Executing [dstring@macro-dial-one:20] Return(“SIP/704-00000002”, “”) in new stack
– Executing [s@macro-dial-one:29] GotoIf(“SIP/704-00000002”, “0?nodial”) in new stack
– Executing [s@macro-dial-one:30] GotoIf(“SIP/704-00000002”, “0?skiptrace”) in new stack
– Executing [s@macro-dial-one:31] GosubIf(“SIP/704-00000002”, “1?ctset,1():ctclear,1()”) in new stack
– Executing [ctset@macro-dial-one:1] Set(“SIP/704-00000002”, “DB(CALLTRACE/701)=704”) in new stack
– Executing [ctset@macro-dial-one:2] Return(“SIP/704-00000002”, “”) in new stack
– Executing [s@macro-dial-one:32] Set(“SIP/704-00000002”, “D_OPTIONS=HhTtr”) in new stack
– Executing [s@macro-dial-one:33] GosubIf(“SIP/704-00000002”, “0?func-set-sipheader,s,1(Alert-Info,)”) in new stack
– Executing [s@macro-dial-one:34] NoOp(“SIP/704-00000002”, "Blind Transfer: , Attended Transfer: , User: 704, Alert Info: ") in new stack
– Executing [s@macro-dial-one:35] ExecIf(“SIP/704-00000002”, “1?Set(ALERT_INFO=)”) in new stack
– Executing [s@macro-dial-one:36] ExecIf(“SIP/704-00000002”, “0?Set(ALERT_INFO=)”) in new stack
– Executing [s@macro-dial-one:37] ExecIf(“SIP/704-00000002”, “0?Set(ALERT_INFO=)”) in new stack
– Executing [s@macro-dial-one:38] ExecIf(“SIP/704-00000002”, “0?Set(ALERT_INFO=Normal;volume=)”) in new stack
– Executing [s@macro-dial-one:39] ExecIf(“SIP/704-00000002”, “0?Set(ALERT_INFO=Normal;volume=)”) in new stack
– Executing [s@macro-dial-one:40] GosubIf(“SIP/704-00000002”, “0?func-set-sipheader,s,1(Alert-Info,)”) in new stack
– Executing [s@macro-dial-one:41] ExecIf(“SIP/704-00000002”, “0?Set(CHANNEL(musicclass)=)”) in new stack
– Executing [s@macro-dial-one:42] GosubIf(“SIP/704-00000002”, “0?qwait,1()”) in new stack
– Executing [s@macro-dial-one:43] Set(“SIP/704-00000002”, “__CWIGNORE=”) in new stack
– Executing [s@macro-dial-one:44] Set(“SIP/704-00000002”, “__KEEPCID=TRUE”) in new stack
– Executing [s@macro-dial-one:45] GotoIf(“SIP/704-00000002”, “0?usegoto,1”) in new stack
– Executing [s@macro-dial-one:46] GotoIf(“SIP/704-00000002”, “0?godial”) in new stack
– Executing [s@macro-dial-one:47] Gosub(“SIP/704-00000002”, “sub-presencestate-display,s,1(701)”) in new stack
– Executing [s@sub-presencestate-display:1] Goto(“SIP/704-00000002”, “state-not_set,1”) in new stack
– Goto (sub-presencestate-display,state-not_set,1)
– Executing [state-not_set@sub-presencestate-display:1] Set(“SIP/704-00000002”, “PRESENCESTATE_DISPLAY=”) in new stack
– Executing [state-not_set@sub-presencestate-display:2] Return(“SIP/704-00000002”, “”) in new stack
– Executing [s@macro-dial-one:48] Set(“SIP/704-00000002”, “CONNECTEDLINE(name,i)=John Doe”) in new stack
– Executing [s@macro-dial-one:49] Set(“SIP/704-00000002”, “CONNECTEDLINE(num)=701”) in new stack
– Executing [s@macro-dial-one:50] Set(“SIP/704-00000002”, “D_OPTIONS=HhTtr”) in new stack
– Executing [s@macro-dial-one:51] Macro(“SIP/704-00000002”, “dialout-one-predial-hook,”) in new stack
– Executing [s@macro-dialout-one-predial-hook:1] MacroExit(“SIP/704-00000002”, “”) in new stack
– Executing [s@macro-dial-one:52] ExecIf(“SIP/704-00000002”, “0?Set(D_OPTIONS=HhtrI)”) in new stack
– Executing [s@macro-dial-one:53] ExecIf(“SIP/704-00000002”, “0?Set(CWRING=r(callwaiting)):Set(CWRING=)”) in new stack
– Executing [s@macro-dial-one:54] NoOp(“SIP/704-00000002”, “”) in new stack
– Executing [s@macro-dial-one:55] ExecIf(“SIP/704-00000002”, “0?Set(D_OPTIONS=HhTtrg)”) in new stack
– Executing [s@macro-dial-one: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 [s@func-apply-sipheaders:1] ExecIf(“SIP/701-00000003”, “0?Set(CHANNEL(hangup_handler_push)=crm-hangup,s,1)”) in new stack
– Executing [s@func-apply-sipheaders:2] NoOp(“SIP/701-00000003”, “Applying SIP Headers to channel SIP/701-00000003”) in new stack
– Executing [s@func-apply-sipheaders:3] Set(“SIP/701-00000003”, “TECH=SIP”) in new stack
– Executing [s@func-apply-sipheaders:4] Set(“SIP/701-00000003”, “SIPHEADERKEYS=”) in new stack
– Executing [s@func-apply-sipheaders:5] While(“SIP/701-00000003”, “0”) in new stack
– Jumping to priority 13
– Executing [s@func-apply-sipheaders: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 [h@ext-local:1] Macro(“SIP/704-00000002”, “hangupcall,”) in new stack
– Executing [s@macro-hangupcall: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 [s@macro-hangupcall:3] ExecIf(“SIP/704-00000002”, “0?Set(CDR(recordingfile)=)”) in new stack
– Executing [s@macro-hangupcall:4] NoOp(“SIP/704-00000002”, "SIP/701-00000003 montior file= ") in new stack
– Executing [s@macro-hangupcall:5] GotoIf(“SIP/704-00000002”, “1?skipagi”) in new stack
– Goto (macro-hangupcall,s,7)
– Executing [s@macro-hangupcall: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 [701@from-internal: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 [701@from-internal:2] NoOp(“SIP/705-00000002”, “MANDELA”) in new stack
– Executing [701@from-internal:3] Set(“SIP/705-00000002”, “CALLERID(name)=Mr Test2 test2”) in new stack
– Executing [701@from-internal: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 [h@from-internal:1] Macro(“SIP/705-00000002”, “hangupcall”) in new stack
– Executing [s@macro-hangupcall:1] GotoIf(“SIP/705-00000002”, “1?theend”) in new stack
– Goto (macro-hangupcall,s,3)
– Executing [s@macro-hangupcall:3] ExecIf(“SIP/705-00000002”, “0?Set(CDR(recordingfile)=)”) in new stack
– Executing [s@macro-hangupcall:4] NoOp(“SIP/705-00000002”, "SIP/701-00000003 montior file= ") in new stack
– Executing [s@macro-hangupcall:5] GotoIf(“SIP/705-00000002”, “1?skipagi”) in new stack
– Goto (macro-hangupcall,s,7)
– Executing [s@macro-hangupcall: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.