Have you ever had your call disconnect while you were on hold with customer service and waiting for a transfer from one department to another? You probably felt like this when the call dropped:

A warm phone call transfer, where a caller is on the phone with an agent then the agent brings on another agent and introduces them to the customer, shouldn’t be so difficult. There are only a few steps that need to be done properly for a warm phone call transfer to work, as we can visualize in the following diagram.

As the diagram shows, a customer, who in this post we’ll refer to as Trinity, dials into a typical phone number for service. An agent, known as Agent Smith, answers the calls. However, Agent Smith discovers he can’t solve the problem and needs to connect Trinity to Agent Johnson, who’s better equipped to handle the issue. Agent Smith dials in Agent Johnson, introduces Agent Johnson to the customer, then hangs up so Trinity and Agent Johnson can solve the problem and finish the support call.



In this blog post, we’ll use Twilio’s Voice API and a bit of Python code to implement the above scenario and help rid the world of warm transfer failures so that this problem never happens to your callers.

What We’ll Need



Let’s walk through how to perform a warm transfer with Python and a simple web application built with the Flask framework. The following dependencies will be used to create and run the application:

We’ll also need three phones with phone numbers to represent Trinity, Agent Smith and Agent Johnson. If you don’t have multiple phones lying around for testing, you can use Twilio Client to turn three browser tabs into different phones. Use this Twilio Client quickstart project with the deploy to Heroku button to create phones as you need them.

There is also a GitHub repository with the code we’ll be creating in this post. It’s a handy reference in case you make a typo or get stuck somewhere along the way.

Also, if Python isn’t your jam and Java is more your speed, check out my colleague’s post that handles a warm transfer in that language instead.

Now that we know our dependencies, let’s get our coding environment established.

Python Environment Setup



It’s time to get our Python development environment ready for this application. In this section we’ll handle the following steps to make sure you’re ready to run the code:

Check the Python installation Set up a new virtualenv then activate it Install a couple of dependencies via pip Fire up Ngrok to handle localhost tunneling Obtain a phone number for our customer service line Export a few critical environment variables for our application to use

Setup Step 1: Check the Python installation



If you’re on Mac OS X or Linux, you’ve already got Python installed on your system. Check the version by going into the terminal and typing python --version. You’ll see the precise version number, something like Python 2.7.6. If you’re on Python 2.6 or greater, you’re good to go.

For Windows, make sure you download and install the Python .exe installation package. Either Python 2 or 3 is fine. My recommendation is to use Python 3.5 unless you have a pressing reason to use an earlier version, because the Python community is now migrating en masse to the Python 3.

Once Python is installed on your system and you can run it with the python command, we’re ready to set up a virtualenv.



Setup Step 2: Set up a new virtualenv and activate it



Virtualenv is a dependency isolation library that makes it much easier to switch between Python versions and various code packages you’re using on different projects. Create a virtualenv in a location you’ll remember using the following command. In my case, I have a folder called Envs under my home directory that keeps my virtualenv organized.