AMI Originate PreDialGoSub and macro-dialout-trunk-predial-hook Not Executing on Outbound Call

Greetings!
I have a system with FreePBX 16.0.40.7 and Asterisk 20.5.2.
I’m originating an AMI call with PHP via sockets, but it seems both macro-dialout-trunk-predial-hook (which is supposed to be called by FreePBX core automatically?) and the Asterisk AMI Originate parameter PreDialGoSub are failing to execute. We are using SIP (yes, I know it should be PJSIP and we’ll get around to that soon.)
Here is the core of the PHP:

fwrite($socket, "Action: Originate\r\n");
fwrite($socket, "Channel: SIP/telnyx-out/$did\r\n");
fwrite($socket, "Exten: $ext\r\n");
fwrite($socket, "Context: from-internal\r\n");
fwrite($socket, "Priority: 1\r\n");
fwrite($socket, "Variable: XDID=$did\r\n");
fwrite($socket, "Variable: AGENT=$ext\r\n");
fwrite($socket, "CallerID: $cid\r\n");
fwrite($socket, "Account: Outbound\r\n");
fwrite($socket, "PreDialGoSub: dialouttrunkpredialhook,s,1\r\n");
fwrite($socket, "Async: true\r\n\r\n");

I have confirmed all variables a populated correctly. Here is the sub dialouttrunkpredialhook which is just renamed from [macro-dialout-trunk-predial-hook] and modified to be a sub routine:
[dialouttrunkpredialhook]

exten => s,1,NoOp(CUSTOM: Outbound ${EXTEN}  ${CALLERID(num)})
exten => s,n,AGI(cid-did-outbound.php)
exten => s,n,Return()

With AGI debug on in the CLI and verbosity at 30 I do not see the NoOp or the AGI script being called. The call goes through with no issues. Can someone tell me what I am doing wrong?

PreDialGoSub isn’t in a release yet. It will be in 20.8.0.

1 Like

If you’re expecting to use the FreePBX internals on your calls, then you need to use local channels for both legs of the originate. Not only will this ensure that the dialplan hooks get called, the calls will show up in CDRs, they will follow the recording rules you have set, etc.

For the outbound leg, you want to use Local/$DID@from-internal and for the inbound you would use Local/$EXT@originate-skipvm. More generally this would be Local/${dialed-digits}@${Context} where the priority is always 1.

As opposed to the way the AMI is configured above, it’s conventional to originate the call first to the local extension, ensure they answer, that the channel is up and only then dial the outbound leg of the call. This will ensure that any early media audio is not missed, and also ensures that the caller is not left with dead air if nobody is answering on the local side.

For those unfamiliar with the originate-skipvm context noted above, it’s a somewhat new context in FreePBX Core that allows you to originate to a local extension and prevent the call from being answered by the user’s voicemail, which you almost always want to avoid. When originating calls automatically, you need to anticipate whenever a call might be answered by a non-human and try to avoid that.

Thanks @lgaetz.

Sorry for being dense but I now have this:

fwrite($socket, "Channel: Local/telnyx-out/$did\r\n");
fwrite($socket, "Exten: Local/$ext@originate-skipvm\r\n");
fwrite($socket, "Context: from-internal\r\n");
fwrite($socket, "Priority: 1\r\n");

But the AMI response is “Error originating call. Check Asterisk logs for details.” But sadly, there is nothing in "/var/log/asterisk/full except “Successful Auth” that occurs when I login to AMI. Can you help me a little more?

fwrite($socket, "Channel: Local/$did@from-internal\r\n");

This is incorrect. If the goal is to all the local extension and then dialout to the PSTN then this is completely backwards, plus the Exten setting is completely wrong. This is what it should look like:

fwrite($socket, "Channel: $ext@originate-skipvm\r\n");
fwrite($socket, "Exten: $did\r\n");
fwrite($socket, "Context: from-internal\r\n");
fwrite($socket, "Priority: 1\r\n");

Remember Exten, Context, Priority are used together. So the original originate would end up looking like when tryng to connect to Exten/Context/Priority…from-internal,$ext@originate-skipvm,1 < this is how Asterisk would put it together and it would fail.

Document for AMI Event: Originate - Asterisk Documentation