Context for scripts (incoming calls)

(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 => _X.,1,System(/var/lib/asterisk/agi-bin/ your/topic ${SIPURI})
exten => _X.,2,DumpChan()
exten => _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…

(O T To001) #31

Unfortunately it turned out that not everything is working as it should now.
When I am trying to dial in without an extension I am getting:

Call from ' 44123456789' ( to extension '44123456789' rejected because extension not found in context 'from-sipphone'

Plus: On the other trunk, no extension is ringing. I suppose, that this line

exten => _X.,3,Goto(from-trunk,${EXTEN},1)

is causing this. I also tried from-pstn in that line instead of from-trunk (this is the context of the second trunk). The same.
I am getting
in DumpChan()

Somehow I still do not understand the concepts here, I think.

(Lorne Gaetz) #32

Assuming you’re using a context formatted thusly:

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

The part of the line that follows => is called the extension, which in this case is _X.. The underscore tells asterisk that this is a pattern match, the X indicates any digit between 0-9 and the . says 1 or more additional characters to follow. This is the Asterisk stand in for “any numeric value”. If you send an invite to this context without an extension, or with a non-numeric extension, it will fail. Which is why my advice from 2 days ago says to use an extension of _. without the X.

(O T To001) #33

Thank you! Now everything seems to work.
Sorry for stupid me not understanding your post from two days ago. I realized that the X was missing then, but did not understand what this meant. Then I was very focussed on the context name…
asterisk is still a little bit confusing for me…

(Lorne Gaetz) #34

No apologies necessary, we’ve all been where you are. In no time at all a decade has gone by and you will be the one telling the newbs to use _.

(O T To001) #35

Hm. My second trunk is pjsip. There DumpChan() delivers the following:

Dumping Info For Channel: PJSIP/Fritz_pj_66666666-00000089:
Name=               PJSIP/Fritz_pj_66666666-00000089
Type=               PJSIP
UniqueID=           1602179221.226
LinkedID=           1602179221.226
CallerIDNum=        01234567890123
CallerIDName=       (N/A)
ConnectedLineIDNum= (N/A)
DNIDDigits=         66666666
RDNIS=              (N/A)
Language=           de_DE
State=              Ring (4)
Rings=              1
NativeFormat=       (alaw)
WriteFormat=        alaw
ReadFormat=         alaw
RawWriteFormat=     alaw
RawReadFormat=      alaw
WriteTranscode=     No 
ReadTranscode=      No 
1stFileDescriptor=  -1
Framesin=           0 
Framesout=          0 
TimetoHangup=       0
ElapsedTime=        0h0m0s
BridgeID=           (Not bridged)
Context=            from-post
Extension=          66666666
Priority=           1
Application=        DumpChan
Data=               (Empty)
Blocking_in=        (Not Blocking)


There is no SIPURI Variable (ok, no variable at all).
Ist there a possibility to pass things from the Info section to scripts?

(O T To001) #36

@lgaetz thank you and I hope so… :slight_smile:

(Lorne Gaetz) #37

Are you looking for the CallerID number of the caller? If so you can use ${CALLERID(number)}

(O T To001) #38

Thanks a lot! That was exactly what I am looking for!
Just have to find out where to get more info about that “special vars”…

(Lorne Gaetz) #39

Everything here would probably make an excellent Superfecta module, send-to-MQTT. It would have to be written in PHP, but then you could just input your host, port, user, pass and have the API call made from Superfecta on inbound calls instead of fussing with custom dialplan.

(O T To001) #40

Well, yes. but the last time I dealt with PHP was 20 years ago…
Actually I am into bash, python and perl…
And I want even more. I want to get every movement (hook on/off, dialing and so on) of every extension signalled using mqtt…