Agi work in Background/Dial Plan Continue

I was hoping to use agi to implement a limit on inbound calls. Ideally an inbound would after 9 minutes play a sound recording to inform the user that 9 minutes has elapsed, even better if this can be played to the local user only and not be heard by the caller. After a further 1 minute the call is then hung up.

I have created in extension_override_freepbx.conf a replica of macro-auto-blkvm with a few additions
[macro-auto-blkvm] exten => s,1,Set(__MACRO_RESULT=) exten => s,n,Set(CFIGNORE=) exten => s,n,Set(MASTER_CHANNEL(CFIGNORE)=) exten => s,n,Set(FORWARD_CONTEXT=from-internal) exten => s,n,Set(MASTER_CHANNEL(FORWARD_CONTEXT)=from-internal) exten => s,n,Macro(blkvm-clr,) exten => s,n,ExecIf($[!${REGEX("[^0-9]" ${DIALEDPEERNUMBER})} && "${DB(AMPUSER/${AMPUSER}/cidname)}" != ""]?Set(MASTER_CHANNEL(CONNECTEDLINE(num))=${DIALEDPEERNUMBER})) exten => s,n,ExecIf($[!${REGEX("[^0-9]" ${DIALEDPEERNUMBER})} && "${DB(AMPUSER/${AMPUSER}/cidname)}" != ""]?Set(MASTER_CHANNEL(CONNECTEDLINE(name))=${DB(AMPUSER/${DIALEDPEERNUMBER}/cidname)})) exten => s,n,NoOp("************************************* THIS IS THE MACRO CONTEXT") exten => s,n,NoOp("************************************* CALL ANSWERED") exten => s,n,AGI(callInboundLimit.php) ;--== end of [macro-auto-blkvm] ==--;

I have a quick php script like so:

//Create the AGI Object
$agi = new AGI();
$chan_vars = $agi->request;
writeFile($agi->request);
$agi->Answer();

if($chan_vars['agi_callerid'] == 401)
{
	errorLog("**************************************  We have 401");
	sleep(10);
	errorLog("**************************************  Beeping");
	$this->exec('PLAYBACK','/var/lib/asterisk/sounds/en/beep');
}

The issue I seem to be having is that the AGI script needs to finish executing, for example I dial in and ext 401 picks up the call. Under the current circumstances 401 gets an answered message but on my line I continue to hear it ringing for approx 10 more seconds ie the value of the first sleep. I need to be able to continue with the call with the agi sitting in the background sleeping for 9 minutes until it needs to kick in play its sound file and then sleep for a further 1 minute before hanging up the call.

Can anyone advise if this is possible with agi and what can be done to make the script function as expected.

You can do this with the Dial command, no need for agi. Check the following link, the L option

https://wiki.asterisk.org/wiki/display/AST/Asterisk+13+Application_Dial

1 Like

Many thanks for this it certainly seems this is the easiest approach and the methodology already exists. Forgive me for being slightly daft, when receiving an inbound call what is the best way to implement this as I do not see dial used within the debug output. For example i see

-- SIP/401-00000000 answered Local/401@from-queue-00000003;2
    -- Executing [s@macro-auto-blkvm:1] Set("SIP/401-00000000", "__MACRO_RESULT=") in new stack
    -- Executing [s@macro-auto-blkvm:2] Set("SIP/401-00000000", "CFIGNORE=") in new stack
    -- Executing [s@macro-auto-blkvm:3] Set("SIP/401-00000000", "MASTER_CHANNEL(CFIGNORE)=") in new stack
    -- Executing [s@macro-auto-blkvm:4] Set("SIP/401-00000000", "FORWARD_CONTEXT=from-internal") in new stack
    -- Executing [s@macro-auto-blkvm:5] Set("SIP/401-00000000", "MASTER_CHANNEL(FORWARD_CONTEXT)=from-internal") in new stack
    -- Executing [s@macro-auto-blkvm:6] NoOp("SIP/401-00000000", ""************************************* THIS IS THE MACRO CONTEXT"") in new stack
    -- Executing [s@macro-auto-blkvm:7] NoOp("SIP/401-00000000", ""************************************* CALL ANSWERED"") in new stack
    -- Executing [s@macro-auto-blkvm:8] AGI("SIP/401-00000000", "callInboundLimit.php") in new stack
    -- Launched AGI Script /var/lib/asterisk/agi-bin/callInboundLimit.php
       > 0xb6e1d5b0 -- Probation passed - setting RTP source address to 172.26.40.9:16988
    -- <SIP/401-00000000>AGI Script callInboundLimit.php completed, returning 0
    -- Executing [s@macro-auto-blkvm:9] Macro("SIP/401-00000000", "blkvm-clr,") in new stack
    -- Executing [s@macro-blkvm-clr:1] Set("SIP/401-00000000", "SHARED(BLKVM,DAHDI/i4/965XXXXXX-1)=") in new stack
    -- Executing [s@macro-blkvm-clr:2] Set("SIP/401-00000000", "GOSUB_RETVAL=") in new stack
    -- Executing [s@macro-blkvm-clr:3] MacroExit("SIP/401-00000000", "") in new stack
    -- Executing [s@macro-auto-blkvm:10] ExecIf("SIP/401-00000000", "0?Set(MASTER_CHANNEL(CONNECTEDLINE(num))=401)") in new stack
    -- Executing [s@macro-auto-blkvm:11] ExecIf("SIP/401-00000000", "0?Set(MASTER_CHANNEL(CONNECTEDLINE(name))=Scott)") in new stack
    -- Local/401@from-queue-00000003;1 answered DAHDI/i4/965XXXXXX-1

If using a dial plan as I had done previously I wanted to implement this. Could I simply use DIAL_OPTIONS and include this option (not sure if this would destroy previous options)? I believe I may still have to pass to the agi as I need to ensure this is only applied to certain extensions. I will need to add an array of extensions or add this via the db to the agi for this side of the processing.

Any advice would be much appreciated.

There is an ‘Asterisk dial option’ in each extension.
You could use that to acheivecthe same limits on selected extensions

2 Likes

As dicko mentioned use this option, keeps the system clean from any modifications that are not needed and difficult to port in case of an upgrade.

1 Like

I will check this out, but presumably this option would affect both outgoing and incoming calls as opposed to just incoming. A quick search on using variables in gui fields doesn’t seem to bring much up. Other than this I believe I have all the information I need to cobble together a solution, ideally as you say though the more I use available features the better for porting/upgrading. Many thanks for all the help and support.

Great info, many thanks! I’m clearly over complicating things. I do have one potential concern described above with this. I will be testing shortly.

No there are two different fields, for incoming and outbound calls. Use the field for incoming.

1 Like

I only seem to have a single field when editing extensions. I have looked at a few versions and a few youtube videos to see if I can spot it.

Not sure why I only get one field. Is this on the latest version, with a module installed? The only instances of inboud/outbound I see refer to the DID/Recording. Apologies for the barrage of questions not sure if I have misunderstood somewhere along the line.

Are you by any chance saying the Dial Options on extensions are for inbound and outbound dial options are configured on the trunk instead?

Check the general options of FreePBX not per extension. This is only for outbound as you can guess.

1 Like

Understood. Many thanks again for all your help, much appreciated.

Settings->Advanced settings->Diaplan and operational->Asterisk Dial Options

Keep the options that you will find in the field and add the L option with the values that you want.