Karthick Mohanraj
Karthick Mohanraj

Reputation: 1658

Transfer a live call in twilio to another secondary number that is not in twilio - Python

I am using the python SDK for handling twilio incoming calls. I have created a flask application for this. All calls coming in to my twilio number get routed through the flask application URL(Webhook). Now, when the call is in progress, I want to transfer the caller to a secondary number (which is not in twilio) and then disconnect my call with the caller once the connection is established between the caller and the secondary number.

Can someone tell me how this can be done in python?

I already tried calling a number but was not able to achieve the required functionality.

Upvotes: 2

Views: 792

Answers (1)

Alex Baban
Alex Baban

Reputation: 11702

I'm not sure if this is the functionality you're trying to achieve, but, try this:

First grab the call id when Twilio hits your webhook and connects the incoming call
(in this example CAe1644a7eed5088b159577c5802d8be38)

Then when you're ready to transfer to the secondary number, make a POST request to instruct to switch to executing a new TwiML, like this:

# Download the helper library from https://www.twilio.com/docs/python/install
from twilio.rest import Client

# Your Account Sid and Auth Token from twilio.com/console
account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
auth_token = 'your_auth_token'
client = Client(account_sid, auth_token)

call = client.calls("CAe1644a7eed5088b159577c5802d8be38") \
             .update(
                  method="POST",
                  url="http://example.com/transfer.xml"
              )

print(call.to)

The transfer.xml contains the <Dial> to the secondary number:

<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <Dial>
        <Number>415-123-4567</Number>
    </Dial>
</Response>

Upvotes: 1

Related Questions