Context for scripts (incoming calls)

Hi,

I am still pretty new to this, so please dont get angry :wink:
What I want is to do some custom things as soon as a call from a trunk comes in (should not matter on which DID).
I have a a sip-trunk with “context=from-trunk” in the USER details in incoming config.
Still not sure, if this is relevant for MY asterisk or the SIP providers?

However, I have added the following to extensions_custom.conf:
[from-trunk-mqtt]
exten => _X.,1,AGI(/root/bla.sh,${ARG1})
exten => _X.,n,Goto(from-trunk,${EXTEN},1)

I want to find out, what exactly ARG1 here is, so the script just pipes all arguments to a txt-file.
Unfortunately, the script is not called.
Now, I have no glue why and what to do to debug this.
Could anyone of the knowing people please be so kind to give me a hint?
Best regards and thanks in advance,
Otto

Because you have:

You need to change that context to:

Makes sense?

You can use Noop() to show in the log what ARG1 is, although I don’t think there’s anything in that argument.

exten => _X.,n,Noop(Arg1 is: ${ARG1})
1 Like

Hi!

Thanks for the fast answer!
Now at least the trunk-entry is called. But not the script tough…
I tried several things I found, I think ExternalIVR should be the right one:
exten => _X.,1,ExternalIVR(/usr/sbin/bla.sh ${ARG1})
but in log I am getting:
[2020-10-06 13:47:13] NOTICE[32373][C-00000026] app_externalivr.c: SIP/ 43720349892-0000001d: stderr: Failed to execute '/usr/sbin/bla.sh ': No such file or directory
I am having
[root@pbx asterisk]# ll /usr/sbin/bla.sh
-rwxrwxrwx 1 asterisk root 91 6. Okt 13:44 /usr/sbin/bla.sh
though, what I do not understand.
regarding to ps, asterisk is run as asterisk user.
Does anyone maybe know more here please?
Best regards and thanks in advance,
Otto

Why did you put that? As far as I know there’s no such a function command or application in Asterisk.
Why not use what you posted above?

Because it did not work as I thought…
When using AGI, the script is called over and over again - only when I do a tescall only once…
Do you maybe know more?

You seem to have different paths to the script in both posts…

Can you post a full call trace using pastebin? see instructions:
https://wiki.freepbx.org/display/SUP/Providing+Great+Debug#ProvidingGreatDebug-AsteriskLogs-PartII

You may also try to use

exten => _X.,1,System(/path/to/script)

Thanks for the hint with System. The script is executed now, but over and over again.
This is a excerpt from the log (I cut out hundreds of equal lines):

[2020-10-06 14:17:08] VERBOSE[2553][C-00000029] netsock2.c: Using SIP RTP TOS bits 184
[2020-10-06 14:17:08] VERBOSE[2553][C-00000029] netsock2.c: Using SIP RTP CoS mark 5
[2020-10-06 14:17:08] SECURITY[2577] res_security_log.c: SecurityEvent=“SuccessfulAuth”,EventTV=“2020-10-06T14:17:08.613+0200”,Severity=“Informational”,Service=“SIP”,EventVersion=“1”,AccountID=“4123456789011”,SessionID=“0x7f7444046d20”,LocalAddress=“IPV4/UDP/188.23.124.40/5160”,RemoteAddress=“IPV4/UDP/213.185.165.114/5060”,UsingPassword=“0”
[2020-10-06 14:17:08] VERBOSE[3730][C-00000029] pbx.c: Executing [4123456789011@from-trunk:1] System(“SIP/ 41234567890-00000020”, "/usr/sbin/bla.sh ") in new stack
[2020-10-06 14:17:08] VERBOSE[3730][C-00000029] pbx.c: Executing [4123456789011@from-trunk:2] Goto(“SIP/ 41234567890-00000020”, “from-trunk,4123456789011,1”) in new stack
[2020-10-06 14:17:08] VERBOSE[3730][C-00000029] pbx_builtins.c: Goto (from-trunk,4123456789011,1)
[2020-10-06 14:17:08] VERBOSE[3730][C-00000029] pbx.c: Executing [4123456789011@from-trunk:1] System(“SIP/ 41234567890-00000020”, "/usr/sbin/bla.sh ") in new stack
[2020-10-06 14:17:08] VERBOSE[3730][C-00000029] pbx.c: Executing [4123456789011@from-trunk:2] Goto(“SIP/ 41234567890-00000020”, “from-trunk,4123456789011,1”) in new stack
[2020-10-06 14:17:08] VERBOSE[3730][C-00000029] pbx_builtins.c: Goto (from-trunk,4123456789011,1)
[2020-10-06 14:17:08] VERBOSE[3730][C-00000029] pbx.c: Executing [4123456789011@from-trunk:1] System(“SIP/ 41234567890-00000020”, "/usr/sbin/bla.sh ") in new stack
[2020-10-06 14:17:08] VERBOSE[3730][C-00000029] pbx.c: Executing [4123456789011@from-trunk:2] Goto(“SIP/ 41234567890-00000020”, “from-trunk,4123456789011,1”) in new stack
[2020-10-06 14:17:08] VERBOSE[3730][C-00000029] pbx_builtins.c: Goto (from-trunk,4123456789011,1)
[2020-10-06 14:17:08] VERBOSE[3730][C-00000029] pbx.c: Executing [4123456789011@from-trunk:1] System(“SIP/ 41234567890-00000020”, "/usr/sbin/bla.sh ") in new stack
[2020-10-06 14:17:08] VERBOSE[3730][C-00000029] pbx.c: Executing [4123456789011@from-trunk:2] Goto(“SIP/ 41234567890-00000020”, “from-trunk,4123456789011,1”) in new stack
[2020-10-06 14:17:08] VERBOSE[3730][C-00000029] pbx_builtins.c: Goto (from-trunk,4123456789011,1)
[2020-10-06 14:17:08] VERBOSE[3730][C-00000029] pbx.c: Executing [4123456789011@from-trunk:1] System(“SIP/ 41234567890-00000020”, "/usr/sbin/bla.sh ") in new stack
[2020-10-06 14:17:08] VERBOSE[3730][C-00000029] pbx.c: Executing [4123456789011@from-trunk:2] Goto(“SIP/ 41234567890-00000020”, “from-trunk,4123456789011,1”) in new stack
[2020-10-06 14:17:08] VERBOSE[3730][C-00000029] pbx_builtins.c: Goto (from-trunk,4123456789011,1)

[2020-10-06 14:17:16] VERBOSE[3730][C-00000029] pbx.c: Executing [4123456789011@from-trunk:1] System(“SIP/ 41234567890-00000020”, "/usr/sbin/bla.sh ") in new stack
[2020-10-06 14:17:16] VERBOSE[3730][C-00000029] pbx.c: Executing [h@from-trunk:1] Macro(“SIP/ 41234567890-00000020”, “hangupcall,”) in new stack
[2020-10-06 14:17:16] VERBOSE[3730][C-00000029] pbx.c: Executing [s@macro-hangupcall:1] GotoIf(“SIP/ 41234567890-00000020”, “1?theend”) in new stack
[2020-10-06 14:17:16] VERBOSE[3730][C-00000029] pbx_builtins.c: Goto (macro-hangupcall,s,3)
[2020-10-06 14:17:16] VERBOSE[3730][C-00000029] pbx.c: Executing [s@macro-hangupcall:3] ExecIf(“SIP/ 41234567890-00000020”, “0?Set(CDR(recordingfile)=)”) in new stack
[2020-10-06 14:17:16] VERBOSE[3730][C-00000029] pbx.c: Executing [s@macro-hangupcall:4] NoOp(“SIP/ 41234567890-00000020”, " montior file= ") in new stack
[2020-10-06 14:17:16] VERBOSE[3730][C-00000029] pbx.c: Executing [s@macro-hangupcall:5] GotoIf(“SIP/ 41234567890-00000020”, “1?skipagi”) in new stack
[2020-10-06 14:17:16] VERBOSE[3730][C-00000029] pbx_builtins.c: Goto (macro-hangupcall,s,7)
[2020-10-06 14:17:16] VERBOSE[3730][C-00000029] pbx.c: Executing [s@macro-hangupcall:7] Hangup(“SIP/ 41234567890-00000020”, “”) in new stack
[2020-10-06 14:17:16] VERBOSE[3730][C-00000029] app_macro.c: Spawn extension (macro-hangupcall, s, 7) exited non-zero on ‘SIP/ 41234567890-00000020’ in macro ‘hangupcall’
[2020-10-06 14:17:16] VERBOSE[3730][C-00000029] pbx.c: Spawn extension (from-trunk, h, 1) exited non-zero on ‘SIP/ 41234567890-00000020’

Also the argument does not seem to get passed (what this is what you already thought):
[from-trunk]
exten => _X.,1,System(/usr/sbin/bla.sh ${ARG1})
exten => _X.,n,Goto(from-trunk,${EXTEN},1)

Just for information: I am going trying to pass incoming numbers to mqtt as soon as I found out how to pass incoming numbers to a script. The mqtt-script for agi does not work anymore, so I want to write something new…

The asterisk user needs execute permissions on the script, so don’t put it in /root, and needs to be in the PATH so not im /usr/sbin, either.

/var/lib/aserisk/agi-bin

would be the traditional patht

It has the permissions and it gets executed, thanks!
But it is executed many, many times while a call is coming in…

It’s because you use the from-trunk context

As mentioned earlier, you need to use the from-trunk-mqtt

well your context is re-entrant, you need to call it [from-trunk-custom] perhaps but ${ARGn} need to be ‘literals’ like 7856 or ${EXTEN} use DumpChan() to see the channel variables available. and in the CLI
agi set debug on
to track your script’s progress

1 Like

Thanks a lot for the hint with DumpChan() ! Will investigate in that!

@PitzKey: Now both the contexts (in the config of the trunk and in extensions_custom.conf) are from-trunk. Is that NOT correct? Seems that I do not understand something here…

https://wiki.asterisk.org/wiki/display/AST/Contexts%2C+Extensions%2C+and+Priorities

look for how ‘includes’ affect ‘search order’ and why having two contexts named the same is ‘a bad thing’ but FreePBX has ‘override’ files if you insist and heed the warnings.

Thank you! Have to leave now, but will investigate further within the next days…

Right, it’s going to be in a loop.

You need both context to be: from-trunk-mqtt

This is very simple. Set your trunk context to from-trunk-preprocess and then create a context in extensions_custom.conf with

[from-trunk-preprocess]
exten => _.,1,NoOp(Entering user defined context from-trunk-preprocess in extensions_custom.conf)
exten => _.,n,  <whatever>
; whatever additional lines you need ending with ...
exten => _.,n,Goto(from-trunk,${EXTEN},1)
2 Likes

Hi,

thanks for the answers.
What I do NOT understand is WHY the context can not just be from-trunk, but must be from-trunk-something?

Because of how the dialplan structure works in FreePBX.
Incoming calls are sent to the from-trunk context, so if you want to intercept the calls you need to send it to from-trunk-somthing, process whatever you want to do with the call, and then send to the from-trunk

Even more, as you saw above, if you used from-trunk you are going to be in a loop

… or ‘hello-kitty-something’ or ‘run-my-script-first-before-processing-the-call-through-the-standard-from-trunk-context’.

The ‘from-trunk’ context does something very specific. If you want to change that, you need to duplicate what ‘from-trunk’, change the name, and execute that, or add a ‘wedge’ in front by adding a new context and sending the call back to the ‘standard’ context.

It’s like adding a driveway to a road. You can’t build your house on the street - you have to add a way to get there.

Hi!
Thanks all!
@cynjut: thanks for this clarification! The missing link for me was, that it does not matter what name the context has. I thought, that it has to be from-trunk-something and that in a magical manner every entry for from-trunk-* is getting processed.
Now I just renamed the context in the trunk and in extensions_custom.conf as well to something different and the script is being called just once.

Now I can proceed working on my mqtt stuff. Will post in the forum as soon as I am done, just in case anyone else could need it…
Best regards,
Otto