0

I want to print/get the incoming caller number in Twilio Flask app. I was referring to the twilio-ivr-phone-tree for this. I tried print(request.args) in the welcome function i.e the first method which is invoked when I receive an incoming call. But it prints an empty ImmutableMultiDict([]).

Am I missing something? Any help is appreciated. Thanks in advance.

To -ve voters, post a comment for doing so. It helps to increase the quality of the question and make stack a better place.

lu5er
  • 3,229
  • 2
  • 29
  • 50
  • Have you configured Number for incoming: webhook properly, recheck **HttpGet/HttpPost** [ImageLink](https://s3.amazonaws.com/com.twilio.prod.twilio-docs/images/ivr-webhook.width-800.png) and as you mentioned `welcome()` func is invoked, check if there are any error [here](https://www.twilio.com/console/runtime/debugger?quickDate=24) – Chenna Oct 27 '18 at 10:30
  • @Chenna, request.get_data() is what I wanted. Thanks for your efforts. For more details on empty dict, refer to https://stackoverflow.com/questions/10999990/get-raw-post-body-in-python-flask-regardless-of-content-type-header – lu5er Oct 27 '18 at 10:46
  • If that has solved your problem close the question – Chenna Oct 27 '18 at 10:50
  • @Chenna I guess you know that you cannot accept your own answer before a certain time. Hope that helps. – lu5er Oct 27 '18 at 10:51

2 Answers2

1

The incoming call request information can be obtained in a Flask app using request.values

these are they keys of the diction returned: atts = ('AccountSid', 'ApiVersion', 'CallSid', 'CallStatus', 'Called', 'CalledCity', 'CalledCountry', 'CalledState', 'CalledZip', 'Caller', 'CallerCity', 'CallerCountry', 'CallerState', 'CallerZip', 'Direction', 'From', 'FromCity', 'FromCountry', 'FromState', 'FromZip', 'To', 'ToCity', 'ToCountry', 'ToState', 'ToZip')

rbp
  • 1,850
  • 15
  • 28
0

The main problem is the Twilio request has a form content type and the raw data will be consumed leading it to appear empty as described in this answer.

However request.get_data() can print the whole POST and you can easily find that the key From holds the caller's phone number.

So, to access it you need request.form['From'] and it fetches the caller's number to the local environment.

lu5er
  • 3,229
  • 2
  • 29
  • 50