2

I have started to use Twilio Voice call recently for sending OTP to users using Django. I am referring to the given link to customise the Twilio response. https://www.twilio.com/docs/tutorials/walkthrough/click-to-call/python/flask

views.py

def voice_call(otp, mobile_no):
    client = TwilioRestClient(settings.ACCOUNT_SID, settings.AUTH_TOKEN)
    client.calls.create(from_=settings.OTP_FROM_NUMBER,
                        to=mobile_no,
                        url='http://localhost:8000/outbound/',
                        method='POST')


def outbound(self):
    response = twiml.Response()
    response.say("Thank you for contacting our department",
                 voice='alice')
    return HttpResponse(response, content_type="application/xml")

In urls.py, I have /outbound/ that points to my django view module.

If I hit '/outbound/' in browser it renders the correct xml response but in the voice call, it gives an error message saying 'Sorry application error'

Not sure where i am going wrong in rendering the xml. Thanks in advance.

Shagun
  • 35
  • 6

1 Answers1

1

Twilio developer evangelist here.

I think the problem is that you are trying to point Twilio to your localhost. When Twilio connects the call, it will try to make an HTTP request to the URL you pass in the REST API call. If you pass localhost then Twilio won't be able to reach it as that is only available on your machine.

There is a solution though! We recommend using a tool called ngrok. It allows external services to tunnel through to your localhost so that you can test webhooks like this. Check out these blog posts on how to set up ngrok for use with Twilio and all the reasons I like using ngrok for developing with Twilio.

Let me know if that helps!

philnash
  • 70,667
  • 10
  • 60
  • 88
  • Thanks @philnash for the suggestion. I was little occupied with the other stuff so I couldn't respond sooner. I tried with ngrok and gave permissions to the URL from my Twilio account but it is still giving me the same error. I also looked into logs where it is showing me this error **11200 HTTP retrieval failure** I am not sure where is that coming from. Could you please guide me a little as I am completely a newbie in this. – Shagun Apr 01 '16 at 19:42
  • You say that when you load the XML in browser it works, which is a GET request. Is your phone number in Twilio set to GET or POST? – philnash Apr 01 '16 at 20:07
  • It is set to POST.. I just realized it was a csrf issue which was forbidding it from making a request to Twilio. I have used crsf_exempt for that. It is working now. Although I am not able to access my session variable now.. but I guess i will figure out a way to do that. Thanks a lot for your time and help. – Shagun Apr 01 '16 at 20:22