SIP (VoIP) Monitoring for incoming calls using MQTT

Nevermind, I had the wrong mqtt port number.
for your SIP Username, do you use your account number, a sub account or email address?

So, I got it to work!

Unfortunately, the main reason as to why I wanted to do this was because I have an IVR (virtual receptionist) that a caller is greeted with when they call my number, and I wanted to know when someone calls the number and gets to the IVR greeting before but they dial 0 to reach me (which is how I set it up).

So, I actually made 2 versions of the container and put 2 separate new sub accounts on each of them. I then changed the MQTT TOPIC of the 2nd one on line 98 of sip2mqtt.py from “home/sip” to “home/sip-calling”.
I created 2 of each of your sensors with different names and an automation for each action.

In Voip.ms, instead of the call going straight to the IVR, it now calls the 1st sub account for 5 seconds (1st sensor fires which sends me a notification on my phone) and then activates the IVR.
If the caller dials 0, it now calls a ring group which includes the second sub account which will call my voip phone and fire the second sensor which mutes my tv, pauses my chromecast and google home tells me who’s calling.
A bit of a workaround with voip.ms but better than nothing.

Thanks again for the help!

4 Likes

Clever! Good work and I might borrow some of your ideas :wink:

Cheers!

2 Likes

Hey Marty,
I noticed there was an update to the docker 2 days ago.
Any breaking changes I should worry about before updating? Thanks!
P.S. It’s working amazingly well for me by the way.

Hi,
I was looking for a call detection in Home assisant and stumbled open your thread.
I have VOIP at home and this is using sip. I sucessfully configured my phone and a sip client on my PC to test if both connected at the same time still work (which it did).

I havent started with your container yet but i noticed something with the optional parameters.
My SIP connections has a different domain and sip server .

For example:
SIP SERVER: 202.sip.provider.org
DOMAIN: provider.org

I guess i need to modify your script a little bit to get this working?

No need to modify the script to get this working. If you’re using the Docker container, you’d simply pass the “domain” parameter.

-d 202.sip.provider.org

Does this help?

Im a total docker noob so i did not get this far yet :frowning:

I am using openmediavault where i configured my docker containers (hass and mosquito yet) from the gui.
Pull the Image and the build the container, this worked fine for those but im failing with yours.
i pulled your image with the tag 2.x (latest is broken) and this seems to work fine. but when i try to run this i always get Exited(2) with the status dead (no extra arguments added yet)

Im not sure how i can get my custom paramters into this setup …

UPDATE:
OK got a bit further now.
It seems in the 2.x release the sip2mqtt.py download is still broken, if i mount the script in the container to /opt/sip2mqtt it starts and is complaining about the missing parameters.
Ok from here i could just go on and put my config into the py file and ignore the arguements completely but i would love to understand this instead. How do you get the paremeters into the container?

This is my docker command atm: docker run -v /host/sip2mqtt:/opt/sip2mqtt retrohunter/pjsip-docker:2.x

Ok finally got this working :slight_smile:

I used environment variables to get the data into the container but still had to modify your script cause it was still complaining that paramters were missing.

I had to change required=True to required=False for every argument.

Then the python script returned:
sip2mqtt.py: error: unrecognized arguments:

I think this is related to following line in your dockerfile:
CMD ["python", "/opt/sip2mqtt/sip2mqtt.py", ""]
as this passed an empty argument (, "").

I did not want to mess with this so i just change the script from:
args = parser.parse_args()
to
args, unknown = parser.parse_known_args()
so it will ignore it.

Then i still could not get connected with my sip credentials and i lost hours and it turned out that it was a typo in the password… For some reason i lost a letter while copying it arround from one test string to another.
I also had to modify the script a bit further because i needed my two different urls for the domain and server in the end.

For example:

acc_cfg.id = "sip:" + args.sip_username + "@" + args.sip_domain
acc_cfg.reg_uri = "sip:" + args.sip_server
acc_cfg.auth_cred = [ pj.AuthCred(args.sip_domain, args.sip_username, args.sip_password) ]

sip_domain = provider.org
sip_server = 020.sip.provider.org

Maybe you can add additional sip_server argument and if its None it will be the same as the sip domain.

Ok time to add this to home assistant now and im thinking about add caldav support to your script to replace the phone numbers with real names :slight_smile:

It took a while but i learned alot about Docker and SIP in the process, this sure will be usefull in the future :smiley:

Ok wrote a little bit of python code that extracts all contacts out of a vcard file (*.vcf) that have a phone number, then it puts this into a dictionary and checks if it finds the number in there to get the name of the caller. I ditched the carddav method for now because i could not find a good python library for this and the vcard method may work better for others anyway.

Im using the vcard of my nextcloud instance and it is working very well.

I did not have the time to combine it with with sip2mqtt script yet as it needs another python module for the vcf parsing (vobject) so i have to put together a new docker container that has this included.

@MartyTremblay: Are you intrested in adding this to your script?

Hi,
i forked Martins work on github and added my modifications.

Check the Readme for setting this up with his docker container. If you are not using docker there are 2 new arguments i added to the script:

--sip_server  # only needed if your sip server != your sip domain
--vcard # filename of your *.vcf VCard file, must be placed in /opt/sip2mqtt

If you get a incoming call it will check a dictionary of all contacts of the csv that contain a phone number, if it can find it the mqtt message looks like this:
"caller" : "Mike Smith (012345678)"
if i cant find a matching number:
"caller" : "Unknown (876543210)"

Hope this is usefull to others :slight_smile:

@MartyTremblay and @ntuseracc Nice stuffs you guys got here. Please has anyone considered making one that allows one to make phone calls?

Like send the number to a mqtt topic and it dials the number instead of just monitoring.

Regards

Would this work with a service like Vonage where they provide a VoIP box that you connect to your broadband into and plug your regular landline phone into ?

Not likely since Vonage’s VoIP offering is a black box service, meaning that they are not likely to share the connection details needed to integrate with this project.

Services such as VoIP.ms or flowroute.com are known to work.

1 Like

@MartyTremblay i am revisiting this topic.

I have started playing around with Docker (on Synology NAS) and getting more comfortable with it.

I launched this docker container; Docker but I received this error in the docker log…

python: can’t open file ‘/opt/sip2mqtt/sip2mqtt.py’: [Errno 2] No such file or directory

Is the path to the file different for docker in Synology?

Edit 1: OK I think I have fixed it. I created a directory in the server, docker/sip2mqtt and then copied this file; sip2mqtt/sip2mqtt.py at master · MartyTremblay/sip2mqtt · GitHub into that folder and then mount it as /opt/sip2mqtt in the docker.

Everything seems to be OK except there is an error in the log…

INFO - SIP: Registration complete, status=401 (Unauthorized)

I have double checked the password I set for the extension in my IP-PBX server and the command I used in the docker. It seems to match. I wonder where else should I look for.

hey guys!

i’ve built an sip2mqtt hassio-addon, looking for testers :wink:

https://github.com/raph2i/hassio-addons

https://github.com/raph2i/hassio-addons/tree/master/sip2mqtt

4 Likes

So I get this:

Start sip2mqtt...
09/11/2019 02:50:53 PM - INFO - ------------------------
09/11/2019 02:50:53 PM - INFO - Starting up...
09/11/2019 02:50:53 PM - INFO - --- MQTT Broker Configuration ---
09/11/2019 02:50:53 PM - INFO - Domain: 172.16.0.60
09/11/2019 02:50:53 PM - INFO - Port: 1883
09/11/2019 02:50:53 PM - INFO - Protocol: MQTTv311
09/11/2019 02:50:53 PM - INFO - Username: sip2mqtt
09/11/2019 02:50:53 PM - INFO - Keepalive Interval: 60
09/11/2019 02:50:53 PM - INFO - Status Topic: home/sip
09/11/2019 02:50:53 PM - INFO - --- SIP Configuration ---
09/11/2019 02:50:53 PM - INFO - Domain: sip.my.domain
9/11/2019 02:50:53 PM - INFO - Username: HomeAssistant1
09/11/2019 02:50:53 PM - INFO - DisplayName: SIP2MQTT
14:50:53.987         os_core_unix.c !pjlib 2.7.2 for POSIX initialized
14:50:53.987         sip_endpoint.c  .Creating endpoint instance...
14:50:53.987                  pjlib  .select() I/O Queue created (0x562330a0dc20)
14:50:53.987         sip_endpoint.c  .Module "mod-msg-print" registered
14:50:53.987        sip_transport.c  .Transport manager created.
14:50:53.987           pjsua_core.c  .PJSUA state changed: NULL --> CREATED
09/11/2019 02:50:54 PM - INFO - -- Registration Complete --
09/11/2019 02:50:54 PM - INFO - SIP: Status = 100 (In Progress)
09/11/2019 02:50:54 PM - INFO - MQTT: Connected with the broker...
09/11/2019 02:50:54 PM - INFO - SIP: Registration complete, status=407 (Proxy Authentication Required)

It seems to be connected, but it doesnt work, when I call the extension it just goes to voicemail and nothing shows in mqtt

Are the credentials working with a sip client?

Yep, I use the same ones with the DSS Notifier add-on to call myself from hassii

what sip service are you using?
i’ve tested it with sipgate

im using 3CX running in house