0
votes

I am trying to use my laptop (running Twilio) to dial a land line using python 3 and have have a voice conversation. This is the simplest use case.

Here is the python code I am running on my laptop.

def test_voice_command_inline(self):
    '''calls landline from laptop via twilio'''

    twilio_client = twilio.rest.Client(self.twilio_account_sid,
                                        self.twilio_auth_token)
    vps_url = self.get_vps_url()

    twilio_voice_call = twilio_client.calls.create(
        from_=self.twilio_phone_number,
        to=self.land_line_number,
        url=vps_url
        )

When I run this code, my land line rings, and I answer it. The call is connected. Next I get prompted by Twilio to press a key to "execute my code" (aka the webhook)

Since I already have a connected call, I don't need a webhook. I just want to start talking. However, the Twilio rest api requires me to put in a webhook url that returns TwiML to direct the call flow. And when the TwiML is finished processing, the call is hung up. I don't need a webhook, yet I am required to have one.

I tried various things in the webhook code in an attempt to let the already connected call continue. I tried returning blank TwiML from my webhook, which causes an immediate hangup. I also tried dialing the land line number from inside the webhook. Since the rest api already connected the call, trying to re-dial the number from the webhook caused a busy status (Twilio console). Looking at all the TwiML verbs I can use in the webhook, none look appropriate for my needs.

I must be missing something simple. Why can't I use the code above to make a voice call between my laptop and my land line (or any other phone)?

1

1 Answers

1
votes

Twilio developer evangelist here.

When you make an outbound call using the REST API, Twilio is setting up a call between our server and the to number. The from number that you supply is just the callerId for the call.

In order to connect the call you make with the REST API to another phone, you need to return TwiML from your twiml_url that dials another number. Currently your TwiML is just an empty <Response> but it should be something like this:

<Response>
  <Dial><Number>OTHER_NUMBER</Number></Dial>
</Response>

This can be written out by Python code too, if you want it to be dynamic. That's where you use the VoiceResponse that you mention. The important thing is that the second leg of the call that you are making should be controlled by TwiML returned from your URL.

Let me know if that helps at all.