Twilio 500 Error (11200 & 12200 on Twilio Call Log) when requesting Recording from Twilio

40 Views Asked by At

I am building a simple text transcription program that takes records your voice then sends it to another endpoint which downloads the audio file and then transcribes it and reads out the response. To clarify, I would like to move the call from the incoming-call endpoint to the handle-audio endpoint because I want it to process and read a response.

Currently, it has two endpoints, one "incoming-call" endpoint with the following code:

@app.route('/incoming_call', methods=['POST'])
def handle_call():
    response = VoiceResponse()
    response.play("./static/audio/intro.mp3")
    response.record(action="/handle_audio", recording_status_callback_event="completed",
                    recording_format='mp3', timeout=3, play_beep=False)
    return Response(str(response), 200, mimetype='application/xml')

As well as a "handle-audio" endpoint with the following code:

@app.route('/handle_audio', methods=['POST'])
 def transcribe_audio():
    audio_url = request.values.get('RecordingUrl')
    credentials = f"{accSID}:{accAUTH}"
    encoded_credentials = base64.b64encode(credentials.encode('utf-8')).decode('utf-8')
    headers = {
        'Authorization': f'Basic {encoded_credentials}'
    }

    print(audio_url)

    # Make a GET request to download the audio file
    audio_response = requests.get(audio_url+".mp3", headers=headers, stream=True)
    audio_response.raise_for_status()  # Raise an error for bad responses (e.g., 4xx or 5xx)

When I use the "action="/handle_audio parameter in my /incoming-call endpoint, my program gives me a "requests.exceptions.HTTPError: 404 Client Error: Not Found for url: (Twilio Audio Recording Url). However, when I use a recording_status_callback="handle_audio", it makes the file request just fine. What could be causing this and how can I fix it?

On the Twilio console log it throws me a 11200 error (linked here) as well as a 12200 error (linked here) but I still cannot figure out why it works when I use record_status_callback but not when it uses the action parameter.

I appreciate any and all help!

1

There are 1 best solutions below

0
mattkosze On

I ended up figuring it out!

First: the record endpoint has both the "action" and "recording_status_callback" methods; both of which seem very similar. HOWEVER, the action method calls the provided endpoint right after the recording is finished, meanwhile the recording_status_callback endpoint only calls it after the recording has been uploaded to the Twilio server and is marked as "completed" as you specify in the "recording_status_callback_event". As such, if you (using the action method) immediately try to download the file Twilio will throw you a 11200 error saying the resource is not available. This is because it has not yet finished uploading and that is why it cannot yet be accessed. To circumvent this issue, I used a time.sleep() method in a function I created called "download_audio" and I implemented it as such:

def download_audio(recording_url):
    time.sleep(2)
    credentials = f"{accSID}:{accAUTH}"
    encoded_credentials = base64.b64encode(credentials.encode('utf-8')).decode('utf-8')
    headers = {
        'Authorization': f'Basic {encoded_credentials}',
    }

    audio_response = requests.get(headers=headers, url=recording_url)
    audio_response.raise_for_status()

    return audio_response

I hope that helped!