Rion
Rion

Reputation: 545

How do I retrieve inbound SMS phone number from twilio request in Python

I'm attempting to get the "from" number from an SMS message that I send to my Twilio number.

I have a Django server running that takes this message and performs some back-end processing, but I can't seem to retrieve the "from" number that sends that SMS to the Twilio number via Python. I keep getting 500 errors that state that this 'from' attribute doesn't exist but the Twilio documentation states otherwise.

def respond_to_sms(request):

# save the http request message
twilio_request = decompose(request)

# this works fine
twilio_message_body = twilio_request.body

# this does not
from_number = twilio_request.from

# perform backend stuff on from_number

Anyone know how to do this? I'm using the Django-Twilio package.

Upvotes: 0

Views: 157

Answers (1)

Marcos Placona
Marcos Placona

Reputation: 21730

Twilio developer evangelist here.

You almost got it right, but it turns out that in the Python library, the name of the attribute is from_ as opposed to just from. That's because in Python, from is a reserved keyword as you can see here.

So try changing your code to the following:

from_number = twilio_request.from_

And you'll get it working. More information about the library can be found here.

Hope this helps you out!

Upvotes: 2

Related Questions