How to get the return status when originating a call?

Hello, i try to originate a call from my mobile phone. If the mobile answers, it will ring an internal extension.

sudo asterisk -rx "originate SIP/[email protected]_08154711 extension [email protected]"

Then the log shows

– Called [email protected]_08154711
– SIP/DT_08154711-00000011 is making progress
– SIP/DT_08154711-00000011 is making progress
– SIP/DT_08154711-00000011 is ringing
– SIP/DT_08154711-00000011 is making progress
– SIP/DT_08154711-00000011 is ringing
– SIP/DT_08154711-00000011 is making progress
– Got SIP response 486 “Besetzt (0)” back from 217.0.23.100:5060
– SIP/DT_08154711-00000011 is busy

How do i get this busy status as a return code on the command line?

It also works with this php-socket file to originate the file, but i don’t know how to get the return code / hangup cause:

<?php

$strUser = 'manager_user';
$strSecret = 'manager_pwd';

$ext = $_GET['ext'];
$number = $_GET['number'];
$callerid = $_GET['callerid'] . " <$number>";
$strChannel = "local/".$number . "@my_trunk";
$strContext = "ext-local";
$strTimeout = 15000;
$strPriority = "1";

$errno=0 ;
$errstr=0 ;

$oSocket = fsockopen ("localhost", 5038, $errno, $errstr, 20);
if (!$oSocket) {
		echo "Socket error: $errstr ($errno)<br>\n";
} else {
		fputs($oSocket, "Action: login\r\n");
		fputs($oSocket, "Username: $strUser\r\n");
		fputs($oSocket, "Secret: $strSecret\r\n\r\n");

		fputs($oSocket, "Action: originate\r\n");
		fputs($oSocket, "Channel: $strChannel\r\n");
		fputs($oSocket, "Context: $strContext\r\n");
		fputs($oSocket, "Exten: $ext\r\n");
		fputs($oSocket, "Priority: $strPriority\r\n");
		fputs($oSocket, "Timeout: $strTimeout\r\n");
		fputs($oSocket, "CallerId: $callerid\r\n");
		fputs($oSocket, "Async: false\r\n\r\n");
		fputs($oSocket, "Variable: DID=$ext\r\n");
		fputs($oSocket, "Action: Logoff\r\n\r\n");

		$lineno=0;
		$match_texts = array("Asterisk Call Manager/2.8.0\r\n", "Response: Success\r\n", "Message: Authentication accepted\r\n");
		while (($wrets=fgets($oSocket)) !== false) {
				if($lineno++ < 3) {
						if(!in_array($wrets, $match_texts)) {
								echo $wrets . "<br />";
								exit(1);
						} else {
								continue;
						}
				}
				if($wrets == "Response: Goodbye\r\n") {
						break;
				}
				if(preg_match('[Cause:|Cause-txt|Response:|Message:]', $wrets)) {
						echo $wrets . "<br />";
				}
				flush();
				ob_flush();
		}
		fclose($oSocket);
}
?>

Whould the hangupcause variable be set? http://www.voip-info.org/wiki/view/Asterisk+variable+hangupcause

I think you might want to check out “PHP-ARI”, a slightly different interface that gives you much more interactive control over your interaction with the system. In this scheme, you log on and monitor the call throughout it’s life, getting information back to your application that you can then log and/or use.

I know there are ways to connect bidirectionally to the old Asterisk AMI interface that will get you this information as well, but ARI is exciting and new, so obviously all the cool kids are playing with it.

note that PHP is blocking… so if you need “non-blocking” you will want to use something like node.js

@jfinstrom Yeah - I did something like this for my Call Center app a long time ago using PHP and ended up doing something special to avoid blocking. That’s one of the nice things about ARI - I think it is more interactive than even the old AMI code.

The code provided is not nearly sufficient for what the OP wants to do, and because of that I think AMI and ARI are really his only good options.

Add

fputs($oSocket, "Async:false\r\n");

Side note. Voip-info is terrible. Use the official Asterisk wiki: https://wiki.asterisk.org/wiki/display/AST/Asterisk+11+ManagerAction_Originate

It used to be good, but that was a long time ago. The official Wiki is much better.

The Hangup Cause Code will be returned once the DIAL() application is completed. As far as I know, this cannot be passed back via AMI since you’re just originating the call via AMI. Since I’ve haven’t used ARI yet, I can’t speak to if it will let you send that back but I’m not seeing how in the Wiki. @tm1000 can probably answer that better.

If you want the Cause Code you’re going to have to get it via dialplan and handle it there. You can write it to the CDR, you’ll just have to make an update to your CDR table to include that field. You could probably use AGI to write it out to a file somewhere as well.

${DIALSTATUS} will get you the returned STATUS such as ANSWERED, BUSY, FAILED, NO ANSWER, etc.

${HANGUPCAUSE} will get you the cause code such as 1, 2, 16, 17, 18, 21, 34, etc.

I will play with flush and php-ari. Is there any way to originate from an outbound route rather than a trunk? I would like to set the internal callerid to 700 and let asterisk select the correct outbound route.

@ladiko You need to understand how FreePBX writes the dialplan but yes you can do this. So as an example, you can do Channel: SIP/[email protected]\n - [outbound-allroutes] context includes all your outbound routes in the order you set them to execute.

That should do what you would like.

1 Like

@BlazeStudios This just gives me:

Connected to Asterisk 13.8.0 currently running on asterisk (pid = 14736)
asterisk*CLI> originate SIP/[email protected] extension [email protected]
[2016-03-31 13:51:51] ERROR[27083]: netsock2.c:305 ast_sockaddr_resolve: getaddrinfo("outbound-allroutes", "(null)", ...): Name or service not known
[2016-03-31 13:51:51] WARNING[27083]: chan_sip.c:6267 create_addr: No such host: outbound-allroutes

Try this:

local/[email protected]\n
1 Like

@lgaetz Yup, that’s the way to do it.

1 Like

Great, great, i got a little further regarding the hang up cause by changing async=false and events=true as @tm1000 Andrew suggested. I updated my php code in the first posting. This produces responses like:

Response: Success
Message: Originate successfully queued 

Response: Error 
Message: Originate failed 
Cause: 16 
Cause-txt: Normal Clearing 

Response: Error 
Message: Originate failed 
Cause: 17 
Cause-txt: User busy 

Response: Error 
Message: Originate failed 
Cause: 58 
Cause: 58 
Cause-txt: Bearer capability not available 

And the codes can be looked up on evil voip-info: http://www.voip-info.org/wiki/view/Asterisk+variable+hangupcause

I already love it and you all were a great help! I will also look at ARI a little later.

here it goes on regarding the outbound-allroutes issue which still exists: Originate a call via outbound-allroutes instead of a trunk