Trigger AGI script from inbound route

Hello,

Hopefully someone can help me here. Reading forums and tutorials all day and still cannot figure this out.

What I’m trying to do:
Receive a call from a specific phone number (as the local phone number service allways routes return calls to the same number),
Trigger an agi script (PHP) to query a database and and find the last extension that called the this number,
Forward the call to that extension.


What I’ve done.

  • I have set up a custom destination under Admin/Custom Destinations using lt-inbound,s,1 as the destination.
  • I have added the below code to extensions_custom.conf

[lt-inbound]
exten => s,1,AGI(lt-inbound.php)

  • I have set up an inbound route for the phone number as the CallerID

  • Number and the DID number blank. In this inbound route I have added the custom destination that I created as the destination.

  • I have made sure that lt-inbound.php is in /var/lib/asterisk/agi-bin and that the permissions and ownership are correct.

The Problem
The lt-inbound.php script has a query that I know works that should be inserting a record into the database, however the record is not being inserted and I see no mention of the script when I check the logs. All I can assume is that the agi script is not being called.

I’m sure I am missing something simple here but I just cannot figure out what it is.

Any help would be greatly appreciated

As with everything related to AGI, start off with setting debug on

agi set debug on

include noops in your script to further refine, if you cared to post your script someone might comment, otherwise we can’t mindread :slight_smile:

I’m pretty sure that if any active call runs into your [lt-inbound] context it will be exposed in the asterisk CLI as you have at least an s (start) exten, if it doesn’t get to that context it just won’t run.

I would suggest :-

[lt-inbound]
exten => s,1,DumpChan()
exten => s,n,AGI(lt-inbound.php)
exten => s,n,DumpChan()

as an absolute minimum for an inbound route’s context as you get it working.

(thinking . . . .)

You should run the agi script on the trunk level, not the inbound route level, it is cleaner and all those calls arrive exclusively on that trunk, no?

Use _X. EXTEN matching not s, (you will get out quicker)

Thank you for the help!

I was able to make some progress using your suggestions in that the script now shows up in the log

[2015-06-24 11:24:01] VERBOSE[27050][C-000033ed] pbx.c: -- Goto (lt-inbound,s,1)
[2015-06-24 11:24:01] VERBOSE[27050][C-000033ed] pbx.c: -- Executing [s@lt-inbound:1] AGI("SIP/65.248.16.43-000027d6", "lt-inbound.php") in new stack
[2015-06-24 11:24:01] VERBOSE[27050][C-000033ed] res_agi.c: -- Launched AGI Script /var/lib/asterisk/agi-bin/lt-inbound.php
[2015-06-24 11:24:01] VERBOSE[27050][C-000033ed] res_agi.c: -- <SIP/65.248.16.43-000027d6>AGI Script lt-inbound.php completed, returning 0
[2015-06-24 11:24:01] VERBOSE[27050][C-000033ed] pbx.c: -- Auto fallthrough, channel 'SIP/65.248.16.43-000027d6' status is 'UNKNOWN'

However the AGI script doesnt seem to execute anything. Right now I am just trying to create a text file with php however it appears as though it never runs. I have tried a number of ways to write to the log file but have had no luck.

I’m not entirely sure how to run the script at the trunk level. You are correct in assuming that all calls should arrive exclusively on that trunk. I don’t see anywhere to set a destination for a trunks inbound settings. Although if this is the cleaner way to do it, I would prefer to do things correctly.


Here is the script

#!/usr/bin/env php -q
<?php
	$myfile = fopen("/var/www/html/test.txt", "w") or die("Unable to open file!");
	$txt = "Line 1\n";
	fwrite($myfile, $txt);
	$txt = "Line 2\n";
	fwrite($myfile, $txt);
	fclose($myfile);
?>

Since the log appears to say that the script was launched and completed this should be creating a test.txt file and yet it does not.

Is there a way that I can write to the log from the AGI script to debug this?

Again the help is greatly appreciated

Anything the agi script attempts to do must be readable/writable/executable by the user asterisk,
can you imagine the consequences FreePBX allowing you to code

$myfile = fopen("/var/www/html/index.php", “w”)

?
easy way to start , write your logs to /var/lib/asterisk/agi-bin

try liberally sprinkling your code with the $AGI->noop() commands

The trunk context is conveniently defined in your trunk definition

.
.
context=lt-inbound
.
.

Beautiful!

After using your pointers I was able to query the remote database and forward calls to the appropriate extension!

Thank you so much for your help.