---
title: "Discussion on \"🐍 Answer the phone! with Python\" | Hashnode"
description: "Discussion on \"🐍 Answer the phone! with Python\". Build a simple phone answering service in Python."
url: "https://hashnode.com/posts/answer-the-phone-with-python/668aa2b843bbe12151289203"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [🐍 Answer the phone! with Python](https://kevincoder.co.za/answer-the-phone-with-python?utm_source=hashnode&utm_medium=feed)

Build a simple phone answering service in Python.

Jul 7, 2024 · 6 min read · kevincoder.co.za  
Updated Apr 23, 2025  
Read the article

-   [#machine-learning](https://hashnode.com/tag/machine-learning)
-   [#python](https://hashnode.com/tag/python)
-   [#sip](https://hashnode.com/tag/sip)
-   [#voip](https://hashnode.com/tag/voip)
-   [#ai](https://hashnode.com/tag/ai)

## Written by

[Kevin Naidoo](https://hashnode.com/@kwnaidoo)

@kwnaidoo · Durban, South Africa

  
[View profile](https://hashnode.com/@kwnaidoo)

Passionate & experienced tech enthusiast

On kevincoder.co.za, I write about my journey as a developer working across Django, Go, and everything in between, from large-scale systems to small, useful tools. Programming has been my passion for about 2 decades now, and I'm still loving it, so I experiment a lot and sometimes write about it.

## Responses 2

Markdown supported · ⌘↵ to post

[John](https://hashnode.com/@johnacapelia)

[Jan 6, 2025](https://hashnode.com/posts/answer-the-phone-with-python/668aa2b843bbe12151289203/comment/677bd4b881ee6943882cf0e3)

Amazing article ! Thanks a lot for the insights.

I am currently contemplating building an in-house client for SIP virtual phone numbers.

We are building an AI conversational agent on top open AI realtime audio and after diving into how the VOIP works I realized it was not difficult to cut out Twilio. Since it would represent most of our costs in the future it makes sense to remove it.

My issue is that I am not sure if the client alone would be a good enough replacement. In your article you mention audio quality, background noise, silence being handled properly by Twilio.

I can't find relevant information online on the behind the scene added value of using Twilio as an SIP client.

The worst would be to build the client ourselves (which is not difficult) and underestimate what we really goes on in the backend at Twilio.

Do you have any insights or resources on the matter ?

Your help would be greatly appreciated!

Happy new year

[Kevin Naidoo](https://hashnode.com/@kwnaidoo)

[Jan 6, 2025](https://hashnode.com/posts/answer-the-phone-with-python/668aa2b843bbe12151289203/comment/677bd4b881ee6943882cf0e3#reply-677c0fa13035f8c915cdce52)

Hi John, Happy 2025, hope you have an awesome year ahead :-)

Thanks for reading, and glad this was useful to you.

Twilio is not a SIP client, they provide a managed VOIP solution, so basically, you can buy telephone numbers from them and programmatically connect code to a real-time call.

With the media streams, they fork the audio from the real-time call and stream it to your WebSocket server.

The WebSocket server then receives the audio and you can transcribe or pass that on to a real-time model and then send the audio back to the client.

You can achieve the same by setting up Asterisk, but that is a bit more complicated and a pain to manage.

One problem with Twilio is that the audio is streamed at 8Khz and is base64 encoded so there is a conversion step that might slow down your call. Here's some documentation on the Twilio approach: [twilio.com/docs/voice/tutorials/consume-real-time…](https://www.twilio.com/docs/voice/tutorials/consume-real-time-media-stream-using-websockets-python-and-flask#create-a-socket-decorator)

A better approach would be WebRTC(I built a simple client here: [github.com/kevincoder-co-za/zazu-voiceai](https://github.com/kevincoder-co-za/zazu-voiceai)) or just build a SIP WebSocket server and use a provider like 3cx.

I did play with a few libraries, before going with Twilio (it was an MVP so shipping fast was essential). Maybe they'll be of use:

[github.com/sipsorcery-org/sipsorcery](https://github.com/sipsorcery-org/sipsorcery) [mizu-voip.com/Software/SIPSDK/JavaSIPSDK.aspx](https://www.mizu-voip.com/Software/SIPSDK/JavaSIPSDK.aspx) [pjsip.org](https://www.pjsip.org/) [github.com/emiago/sipgo](https://github.com/emiago/sipgo) [linphone.org/en/voip-unified-communications-softw…](https://www.linphone.org/en/voip-unified-communications-software)

Mizu worked nicely, I just didn't have much time to fully implement the SDK and it's a commercial product so there are licensing costs.

Hope this helps.

[Zaid Aman](https://hashnode.com/@aman-voip)

[Dec 16, 2024](https://hashnode.com/posts/answer-the-phone-with-python/668aa2b843bbe12151289203/comment/676030ea6558eb910fe789c4)

Hello, I just have one question. Were you able to play audio files like mp3 and wav over the phone? I have real problem with that plus it is Europe it supports puma (alaw).

[Kevin Naidoo](https://hashnode.com/@kwnaidoo)

[Dec 18, 2024](https://hashnode.com/posts/answer-the-phone-with-python/668aa2b843bbe12151289203/comment/676030ea6558eb910fe789c4#reply-6762a2cc14e94c0136a63e4d)

Hi Zaid, thanks for the question. Yes, you can but you need to format the audio into a WAV and ensure that the sample rate is 8000 Hz(mono channel).

Then you can send that WAV file back to the user:

audio = AudioSegment.from\_mp3("your\_file.mp3")

audio = audio.set\_frame\_rate(8000).set\_channels(1)

audio.export("output.wav", format="wav")

f = wave.open('output.wav', 'rb')

frames = f.getnframes()

data = f.readframes(frames)

f.close()

call.write\_audio(data)

I suggest looking into my web socket tutorial over at: [kevincoder.co.za/how-i-used-voice-ai-to-bring-ima…](https://kevincoder.co.za/how-i-used-voice-ai-to-bring-imaginary-characters-to-life)

You don't need to use WebRTC if you need the phone system, you can use Twilio media streams or forward the call from a PBX server like Asterisk.

Hope this helps.

[Zaid Aman](https://hashnode.com/@aman-voip)

[Dec 18, 2024](https://hashnode.com/posts/answer-the-phone-with-python/668aa2b843bbe12151289203/comment/676030ea6558eb910fe789c4#reply-6762e5453e133a240789f1a9)

[Kevin Naidoo](https://hashnode.com/@kwnaidoo) Thank you for the response. Actually I have tried that before and it makes the audio very noise (painful for the ear) and unbearable. I have a bit better audio by processing the raw audio into g711 (alaw) format but the RTP couldn't send the packets or the stream in un choppy manner. I have perfectly working pipeline for voice conversation on laptop but not over the phone.

[Kevin Naidoo](https://hashnode.com/@kwnaidoo)

[Dec 19, 2024](https://hashnode.com/posts/answer-the-phone-with-python/668aa2b843bbe12151289203/comment/676030ea6558eb910fe789c4#reply-6763ad11125e254662b81c5e)

I see. Yeah PyVOIP is more for educational purposes, its not efficient for production. You probably should use pjsua/pjsip or twilio media streams for your use case.
