Get CDR records and insert in a custom DB table

I want to capture duration, billsec, disposition & calldate and save them in my own custom database table for every outbound call.
The problem is in my update_outbound.php despite having it sleep(5), before querying the cdr table in asteriskcdrdb based on the $(UNIQUEID} value from the dial plan, the querying comes up empty.
This is part of my php script;

$servername = “asterisk-IP-address”;
$username = “name”;
$password = “pass”;
$dbname = “asteriskcdrdb”;

try {
$con = new mysqli($servername, $username, $password, $dbname);
$sql1 = “SELECT duration, billsec, calldate, disposition FROM cdr WHERE uniqueid=’$unqID’;”;
$result = $con->query($sql1);
$data = $result->fetch_assoc();
file_put_contents("/home/mandela/debug.txt",“yes DATA”);
file_put_contents("/home/mandela/debug.txt",“NO DATA”);
$duration = $data[‘duration’];
$billsec = $data[‘billsec’];
$calldate = strtotime($data[‘calldate’]);
$dispo = $data[‘disposition’];
} catch (Exception $e) {
echo "Connection failed: " . $e->getMessage();

I have this in my extensions_custom_conf:

include => outbound

;exten => 701,1,NoOp(Starting outbound call!)
exten => _254XXXXXXXXX,1,NoOp(Starting outbound call!)
same => n,NoOp(Customer number is ${EXTEN})
same => n,NoOp(Calling Extension number is ${CALLERID(num)})
same => n,DumpChan
same => n,NoOp(UNIQUE ID IS ${UNIQUEID})
same => n,AGI(fmscurl.php,${EXTEN},${CALLERID(num)})
same => n,Set(GLOBAL(lastID)=${lastID})
same => n,Set(GLOBAL(agentNo)=${CALLERID(num)})
same => n,Set(GLOBAL(custNo)=${EXTEN})
same => n,NoOp(THIS IS THE LAST ID ${lastID})
same => n,Dial(SIP/${EXTEN},10,U(record-on-answer))
same => n,GotoIf($["${DIALSTATUS}"=“NOANSWER”]?noanswer)
same => n,GotoIf($["${DIALSTATUS}"=“BUSY”]?busy)
same => n(noanswer),NoOp(NO ANSWER REACHED)
same => n,Set(CHANNEL(hangup_handler_push)=run-on-noAnswer-hangup,s,1)
same => n,Hangup
same => n(busy),NoOp(BUSY REACHED)
same => n,Set(CHANNEL(hangup_handler_push)=run-on-Busy-hangup,s,1)
same => n,MacroExit

exten => s,1,Noop(Entering call record context)
same => n,Monitor(wav,Call-Record,m)
same => n,Set(CHANNEL(hangup_handler_push)=run-on-outbound-hangup,s,1)
same => n,MacroExit

exten => s,1,Noop(We are here, after outbound hangup)
same => n,AGI(update_outbound.php,${lastID},${agentNo},${custNo},${UNIQUEID})
same => n,Return

exten => s,1,AGI(update_outbound.php,${lastID},${agentNo},${custNo},${UNIQUEID})
same => n,Return

exten => s,1,AGI(update_outbound.php,${lastID},${agentNo},${custNo},${UNIQUEID})
same => n,Return

Your basic problem is that the sleep is synchronous with the dialplan, and the CDR is not written until the dialplan ends.

However, you also need to be aware that uniqueid is for the channel, and is not a candidate key for the CDR; more than one CDR can have the same uniqueid in some scenarios.

You could force out the CDR with ResetCDR.

Also note that hangup handlers are not intended to be long running.

I would suggest just designing your own custom ‘backend’

Thanks for your feedback, I have searched and seen a previous answer you gave on setting up a custom back-end and so far this is what i have done.
In odbc.ini i have added the following connection to my cdr database;

Description=MySQL connection to ‘cdr’ database

Then in my res_odbc_custom.conf I have this context so far though i don’t wuite understand whether so far I am doing the right thing; how do i insert into a table in this db? Kindly clarify?

unanswered = yes

just ‘map’ the cdr ones to ypur ones on a one to one basis, use triggers and stored procedures to further massage the records

@dicko kindly if you don’t mind clarify on how to “map” or point me to some where I can read further on this, thanks.

Google is occasionally useful also :wink:

@dicko on it thanks.

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