Search code examples
twiliotwilio-apitwilio-twimltwilio-click-to-call

Not able to do simple twilio call


I am not able to do simple 2 way audio call between 2 numbers using twilio. I have attached my python (Django) code. when I run the code, call Get connected and but after accepting call it get's disconnected immediately. also, I am not able listen to anything on Laptop.

I have joined the code and twiml from twilio.

def call_m(request):
  account_sid = os.environ['TWILIO_ACCOUNT_SID']
  auth_token = os.environ['TWILIO_AUTH_TOKEN'] 
  client = Client(account_sid, auth_token)
  call = client.calls.create(
                        url='twiml_url',
                        to='+91985XXXXXXX',
                        from_='+132YYYYyYYY',
                    )
  print("sid ",call.sid)

return render(request, 'calls/incall.html')

Here's my twiml.

<?xml version="1.0" encoding="UTF-8"?>
<Response> 
</Response>

if i changed My twiml to this

<Response>
   <Dial>+9198xxxxxxxx</Dial>
</Response>

then after accepting call from "To" number, It say's number is busy please call later.


Solution

  • I think you may have some misconceptions about how Twilio works when making calls.

    When you generate a call with Twilio using the REST API that creates a call leg between the to number and Twilio (not between your laptop and the phone).

    When that call is answered on the end of the to number, Twilio then makes an HTTP request to the url that you passed to the API. That URL should respond with TwiML to tell Twilio what to do with the call next.

    Now, when your application responded with an empty TwiML response (<Response></Response>) that tells Twilio that there is nothing left to do in this call, so Twilio hangs up.

    When your application responded with a <Dial>, like this:

    <Response>
       <Dial>+9198xxxxxxxx</Dial>
    </Response>
    

    my guess is that the number in the dial was the same number that you used in the to parameter to make the call. In this case, Twilio placed a call to your number in response to the REST API request and then placed another call to your number as directed by the TwiML <Dial> and that second call found that your number was busy because you are already on the line with Twilio.

    If you used a different number in the <Dial> then Twilio would place a call to that number and when they answered the phone you would be connected to them.

    If you want to connect two callers with their own phones, then using <Dial> like this is the way forward.

    If you want to place a call from your laptop to another device, then you will need a bit more than just the REST API. In this instance you need to build an application using the Twilio Voice SDK which allows you to make calls from a web browser or iOS or Android application. If this is of interest to you then I recommend you check out the getting started guide for the JavaScript voice SDK.