Update database after hangup

Hello all,

I have integrated sugarcrm with elastix 1.6.2. I have a created a table in sugarcrm almost same as asterisk cdr table. I would like to update crm table with the call log the same time table cdr does.

By the way I am very new in asterisk. Forgive me if I am wrong. In extensions.conf file I see every calls initiates throug dialparties.agi file and then finally it calls hangup in macro-hangupcall.

In my understanding and test, I found cdr table updated after hangup finishes.I have created a agi file updatecrm.php and this file is suppose to connect crm database table after copying information from cdr table fields.

My little but very important question is where should I add the line of code and how could I write the code to call the AGI to do this job.

Please help.
Thanks

For execute scripts when hangup use DEADAGI in your context add the especial extension h for use dead agi.

[yourcontext]
exten=>your_rules,1,yourapp
exten=>your_rules,n,yourapp
exten=>h,1,DeadAGi(yourScript)
exten=>h,n,hangup

Thank you for your posting. As you wrote

[yourcontext] exten=>your_rules,1,yourapp exten=>your_rules,n,yourapp exten=>h,1,DeadAGi(yourScript) exten=>h,n,hangup

Basically I don’t have any rules. What I want I would like to override macro-hangupcall , so that when asterisk update cdr table (usually after hangup I guess) , I would like to call my agi script and script will update crm table by copying the calldate,src,dst,channel, duration etc…

I may need to hangup in my script. In the script I have
pcntl_signal(SIGHUP, “sig_handler”);

Function sig_handler will catch the hangup signal and script will then query cdr table from the function and insert into crm tables.

As I am a new developer, If you can guide me to how to write the agi code to hangup the call.

Thanking you.

Hi navaismo,

I am pretty close to make it work according to your code. In the extensions.conf file I put your code
and change the next line replacing 1 to n.

exten =>h,1,AGI(updatecrm.php)
exten => h,n,Macro(hangupcall)

However, in my agi file sig_handler only catch the hangup If the call is hung by the receiver. But If callee hangs up No hangup signal is passed.I would be grateful If you tell me how to write the code so that It works for both caller and receiver.

See the CLI plz.
If receiver hangsup

AGI Tx >> agi_request: updatecrm.php AGI Tx >> agi_channel: SIP/405-092cd048 AGI Tx >> agi_language: en AGI Tx >> agi_type: SIP AGI Tx >> agi_uniqueid: 1268306924.278 AGI Tx >> agi_callerid: 405 AGI Tx >> agi_calleridname: Agent405 AGI Tx >> agi_callingpres: 0 AGI Tx >> agi_callingani2: 0 AGI Tx >> agi_callington: 0 AGI Tx >> agi_callingtns: 0 AGI Tx >> agi_dnid: 403 AGI Tx >> agi_rdnis: unknown AGI Tx >> agi_context: macro-dial AGI Tx >> agi_extension: h AGI Tx >> agi_priority: 1 AGI Tx >> agi_enhanced: 0.0 AGI Tx >> agi_accountcode: AGI Tx >> AGI Rx << VERBOSE "uniqueid: 1268306924.278" 3 -- agitest.php: uniqueid: 1268306924.278 AGI Tx >> 200 result=1 AGI Rx << VERBOSE "" 3 [color=#FF0000][b] -- agitest.php: AGI Tx >> 200 result=1 AGI Rx << HANGUP SIP/405-092cd048 AGI Tx >> 200 result=1[/b][/color] == Spawn h extension (macro-dial, h, 1) exited non-zero on 'SIP/405-092cd048' == Spawn extension (macro-dial, s, 7) exited non-zero on 'SIP/405-092cd048' in macro 'dial' == Spawn extension (macro-exten-vm, s, 9) exited non-zero on 'SIP/405-092cd048' in macro 'exten-vm' == Spawn extension (from-internal, 403, 1) exited non-zero on 'SIP/405-092cd048'
If caller hangs up

AGI Tx >> agi_request: agitest.php
AGI Tx >> agi_channel: SIP/405-092cd048
AGI Tx >> agi_language: en
AGI Tx >> agi_type: SIP
AGI Tx >> agi_uniqueid: 1268307602.280
AGI Tx >> agi_callerid: 405
AGI Tx >> agi_calleridname: Agent405
AGI Tx >> agi_callingpres: 0
AGI Tx >> agi_callingani2: 0
AGI Tx >> agi_callington: 0
AGI Tx >> agi_callingtns: 0
AGI Tx >> agi_dnid: 403
AGI Tx >> agi_rdnis: unknown
AGI Tx >> agi_context: macro-dial
AGI Tx >> agi_extension: h
AGI Tx >> agi_priority: 1
AGI Tx >> agi_enhanced: 0.0
[color=#FF0000]AGI Tx >> agi_accountcode:
AGI Tx >> I>
  == Spawn h extension (macro-dial, h, 1) exited non-zero on 'SIP/405-092cd048'[/color]  == Spawn extension (macro-dial, s, 7) exited non-zero on 'SIP/405-092cd048' in macro 'dial'
  == Spawn extension (macro-exten-vm, s, 9) exited non-zero on 'SIP/405-092cd048' in macro 'exten-vm'
  == Spawn extension (from-internal, 403, 1) exited non-zero on 'SIP/405-092cd048'

My little php snipped

#!/usr/bin/php -q
 <?php                
                declare(ticks = 1);
	pcntl_signal(SIGHUP,  "sig_handler");
	require('/var/lib/asterisk/agi-bin/phpagi.php');
	$agi = new AGI();
	$uniqueid = $agi->request['agi_uniqueid'];
	$callerid = $agi->request['agi_callerid'];
	$channel  = $agi->request['agi_channel'];
	$agi->verbose("uniqueid: ".$uniqueid ."\n",3);
	$agi->hangup($channel);
	exit(0);

function sig_handler($signo) 
{
                 GLOBAL $uniqueid,$callerid;
                 //query asterisk database
	$conn1 = mysql_connect("localhost", "root", "eLaStIx.2oo7");
	mysql_select_db("asteriskcdrdb", $conn1);
                -
                -
               //update crm database
                -
                exit(0);//not sure need this or not.  
}

Thanking you.