Easily Downloading Voicemails Left To A Specific Active Phone Number

Reed
Reed

I am not a programmer so please bear with me. I am currently trying to figure out an easy way to download all of the voicemails left to a specific active phone number without having to download each one at a time. Does anybody know an easy way to do this?

Comments

  • mcoser
    mcoser
    edited July 15

    Hi @Reed. Matt from Twilio Support here.
    The most effective way is to run a script, but it isn't very complex.

    First, you want to pull a list of Calls to the number in question.
    https://www.twilio.com/docs/voice/api/call-resource

    Then iterate through those call SIDs to download a recording, if present.
    https://www.twilio.com/docs/voice/api/recording

    Here is a basic python sample to can expand upon, but you can use the same logic in any language that supports making HTTP requests.

    import requests # needed for http client - https://docs.python-requests.org/en/master/
    
    from twilio.rest import Client # Twilio!
    
    # Twilio Account SID and Auth Token - https://www.twilio.com/console
    account_sid = ''
    auth_token = ''
    
    # initialize the http client
    client = Client(account_sid, auth_token)
    
    # List all calls to the specified number
    # https://www.twilio.com/docs/voice/api/call-resource
    calls = client.calls.list(
                to='+15552224444',
                limit=20 # use a limit to test if you have a lot of calls
                )
    
    # iterate through the list of calls to check for associated recordings
    for c in calls:
        recordings = client.recordings.list(
                        call_sid=c.sid
                        )
    
        # iterate through the list of recordings to save the media locally
        # https://www.twilio.com/docs/voice/api/recording
        for r in recordings:
            print(r.sid + ' - ' + c.sid)
            url = "https://api.twilio.com/2010-04-01/Accounts/" + account_sid + "/Recordings/" + r.sid + ".wav" # construct the Recording media url
            req = requests.get(url, allow_redirects=True) # make HTTP GET request to Recording media url
    
            filename = r.sid + ".wav" # name and location where you want to save the file - here we just name it with the Recording SID
            with open(filename, "wb") as file:  # these two lines save the file. here, we are just saving to the same dir as this script - https://docs.python.org/3/tutorial/inputoutput.html
                file.write(req.content)   
    
            print("Downloaded and saved...")
    
    print("D0ne!")
    
  • Hey Matt! Thank you so much this was super insightful and much appreciated. :)

Sign In or Register to comment.