Context for scripts (incoming calls)


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

(O T To001) #12

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…


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.

(O T To001) #14

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

(Itzik) #15

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

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

(Lorne Gaetz) #16

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

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)

How to add prefix to incoming calls
Protecting callers data “Caller ID”
(O T To001) #17


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?

(Itzik) #18

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

(Dave Burgess) #19

… 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.

(O T To001) #20

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,

(O T To001) #21


My small thing is working so far - meaning, that I am getting the incoming number signaled using mqtt. Where on the forum should I post my “solution” - just in case anyone needs this in the future?

Another question: I think it would be nice to also get informed about other things (like hook off of an extension or dialing out, ending call and so on). Are there possibilities to make asterisk call scripts also in those situations?

Best regards and thanks in advance,

(Itzik) #22

Post it in this ticket, a Google Search should return this post as a result

(O T To001) #23

Thanks a lot! Looks very promising!

(O T To001) #24

Well, as promised. Here is a short thing I did today for making freepbx signal incoming calls on one/more trunks using mqtt (as the “old” mqtt script was still python2 and this is not really supported anymore).
This was tested under freepbx 15
At first we have to install some package (using ssh):

yum upgrade
yum install python-paho-mqtt

For better convenience we link python3.6 to python3
ln -s /usr/bin/python3.6 /usr/bin/python3

Then we use the python-script enlcosed.
trunk_mqtt.tgz (660 Bytes)

Save it to /var/lib/asterisk/agi-bin/ and set mqtt-server and credentials at your need.
do a chmod +x on it

You can easily test it:

/var/lib/asterisk/agi-bin/ your/topic sip:0044123456789012@`

and you should see a mqtt-message in your brokers logfiles (or wherever)

Then, we navigate to our trunk in freepbx and set a context (eg from-sipprovider).
We now edit /etc/asterisk/extensions_custom.conf and add:

exten =&gt; _X.,1,System(/var/lib/asterisk/agi-bin/ your/topic ${SIPURI})
exten =&gt; _X.,2,DumpChan()
exten =&gt; _X.,3,Goto(from-trunk,${EXTEN},1)

and now we reload the dialplan using the shell:
rasterisk -x'dialplan reload'

Thats all. Now we should get a notification with the calling number by mqtt whenever there is an incoming call on our trunk.

I hope, this helps someone. It is surely not beautiful and maybe there are better possibilities for this, but at least it works…
And sorry for the weird formatting. But this forum does not seem to know source-code tags (or maybe I am just too stupid)

  • You can use ‘markdown’ to fornat

(O T To001) #26

@dicko Thanks for the hint. Worked!

(Dave Burgess) #27

Also, don’t forget to search for other people sojourns into MQTT. Believe it or not, you’re joining an august group of contributors.

(O T To001) #28

I looked around a lot, but I am not really familiar with asterisk/freepbx (yet).
What I found did not work (any more - python2). So I just decided to try it myself - also as some kind of exercise for asterisk :wink:
And maybe my small post can also help others later. I hope, at least


(There is no reason that you can’t have python2 and python3 co-exist nicely in your system)

(O T To001) #30

yes, i know.
the problem was one of the modules. (I think it was python-mosquitto) which would have been necessary. but as far as I remember, it was not available in package repos any more. and installing it using pip also failed. But I do not remember exactly…