Search code examples
pythonflasktwiliotwilio-api

Twilio: 'CallInstance' object has no attribute 'from_'


This is probably such an introductory thing re Python that I missed but I cannot discover how to do this. I am reusing a chunk of code that has worked in a past webapp using Twilio Python library but it can no longer work.

I'm trying to use the call_sid to retrieve some data about the call, specifically for the "from" number.

    records = twilioCli.recordings.list(limit=5)
    for rec in records:
        call_sid = rec.call_sid
        abc = twilioCli.calls(call_sid).fetch()
        print(abc)
        print(abc.from_formatted)
        print(abc.from_)

twilioCli being my Twilio Client which works fine (as I'm using it in other functions.

print(abc) gives me:

<Twilio.Api.V2010.CallInstance account_sid=[my account sid] sid=[call sid]>

print(abc.from_formatted) gives me the formatted number of my the from number. And print(abc.from_) gives me:

AttributeError: 'CallInstance' object has no attribute 'from_'

According to the Twilio API page for Call resource, I should be writing abc.from rather than abc.from_. However, when I try to just have "from" it spits me a syntax error. Assuming this is just a basic Python thing to have to add an underscore after "from" to escape it (which is maybe why it's worked in other projects before). Wondering what I'm doing wrong?

I've updated my Twilio Python library to the most recent update (twilio==9.0.5). Thanks all!


Solution

  • I learned that in Python I'm able to call dir() for it to return all attributes. When calling dir() on the CallInstance I learned that the attribute I'm looking for is actually _from.

    So in my code example, print(abc._from) gave me the result I wanted! Wish Twilio API doc updated to reflect this though!