Creating Outbound Call Warm Transfer Using Twilio In Python and Django


Have you ever been connected to a call where it was transferred to a third person for further assistance? Well, that’s call transferring.

Warm transfer of calls, in contrast to cold transfer, doesn’t disconnect the call in between and thus gives the customers a  more seamless experience.

Recently at work, I was building a system to convert regular calls to a warm transfer if needed. And in this post, I’ll be sharing what I learned while building the warm transfer system.

Outbound calling and warm transfer

An outbound call is a call initiated by a call center agent to the customer. Warm transfer in the case of an outbound call means, a transfer to the second agent while the first agent is also on the call and can drop off later after introducing the customer to the new agent.

For the rest of the post, we’ll be referring the parties as:

  1. The First Agent (Leg 1 of the call)
  2. The Customer (Leg 2 of the call)
  3. The Second Agent (Leg 3 of the call, warm transferred to this agent)

We’ll be using Twilio to build our warm transfer system.

Overview of the system

The system mainly follows the workflow given below:

  1. A call is created to the first agent (leg 1).
  2. Once the first agent is connected, call to the customer is initiated (leg 2).
  3. When the customer picks the call, the agent and the customer are connected by a regular call.
  4. The agent triggers the warm transfer.
  5. The customer’s leg is updated to be in a new conference call.
  6. The first agent also joins the same conference call (Negligible delay).
  7. Now the first agent and the customer are on a conference call.
  8. The second agent is connected to the conference directly (leg 3).
  9. The call is warm transferred to the second agent.

Connecting the customer and the first agent by a regular call was done both to retrofit the existing system that supported only regular calls and also not all calls will be needing warm transfers.

Connecting the first agent and the customer

First, we’ll connect the first agent and then the customer. Note that customer is connected in the second leg making it an outbound call.

‘twiml_second_leg_url’ is the URL that Twilio will hit to get the TwiML once the first agent picks the call.

We want our URL to return a TwiML Dial verb to connect the second leg to the customer.

Ideally, we would want our TwiML views to be stateless. Thus all the parameters needed by these views are passed as query parameters.

Note the action URL. Action URL is the URL Twilio will hit when the first leg is disconnected.

We’ll use this action URL callback to connect the first agent to the conference call.

At this point, the first agent and the customer are in a regular call.

Next, we’ll start the process of warm transfer.

Triggering warm transfer

The process of warm transfer looks like following:

  1. We move the customer the conference.
  2. We move the first agent to the same conference. (negligible delay for humans to perceive).
  3. We add the second agent as the participant to this conference.

1. Updating the customer leg to be in the conference

We did the following:

  1. Created a URL that the Twilio will hit to get the updated TwiML.
  2. Updated the URL for the customer’s leg by its call sid.

As soon as we update the URL, Twilio will hit the TwiMLDialConferenceView written below and will put the customer’s leg in the conference.

2. Putting the first agent’s leg in the conference.

Remember we gave an action URL (“first_leg_disconnected_url”) for the first agent’s leg when dialing the customer’s leg. This is where we’ll leverage it to put this leg into the same conference as the customer.

Twilio will hit this view when the first leg gets disconnected because we’ve moved the second leg of the agent to the conference call.

As we can see, we are returning TwiML to put this leg also on the conference call.

For the cases when we don’t want the first leg to go into the conference upon disconnect, we can simply return TwiML HangUp verb to end the call.

So, at this point, the first agent and the customer are on a conference call.

Now, we’ll take the final step towards warm transferring the call, connect the second agent to this conference call.

3. Connecting the second agent to the conference call

As we can see, to connect the second agent on the conference call we use the conferences API of the Twilio python client.

With early media set to true, we are able to listen live call status of the second agent on the conference call like ringing, busy or answered.

4. Bonus step

Treat yourself to a chocolate pudding! We ‘ve implemented a warm transfer system using Twilio, Python, and Django!


Outbound calls are an important factor in inside sales. Creating a seamless warm transfer can help the businesses drive conversions by connecting the customer to the right person at the right time.

In this post, we implemented an outbound call warm transfer system using Twilio, Python, and Django.





That’s all, folks!



Strive to learn : 8 Ways to optimize for learning at work as a software engineer


A large open space with amazing ergonomic chairs where people discuss and execute upon disrupting ideas. It’s right next to company’s game room where you unwind after a hard work day.  Here is we as engineers, get to work on products that our customers love and we love delivering that delight by continuous delivery (or something else :P).

Yet the most prominent thing that excites and should excite an effective engineer is the opportunity for learning at work. Optimizing for learning is a high leverage activity and should be on the top priority for every engineer.

Here are 8 ways to optimize our work for learning, deeply inspired by the book The Effective Engineer.

1. Study code for core abstractions written by the best engineers at your company


I have been lucky enough to be the dumbest engineer at Squad. But that has allowed me to learn very aggressively during working hours just by reading through libraries and modules written by other awesome engineers.

So next morning, open that black box that you’ve been importing for so long in your code and dig through it.

2. Do more of what you want to improve

In more relatable terms, if you think you want to improve writing SQL queries, do more of that. If you want to improve at doing code reviews, do more of that.

Practice and deliberately touch your weak points instead of cutting corners. You’ll be amazed how helpful your fellow engineers/friends will be in helping you do so.

3. Go through as many technical materials available

We at Squad have a dedicated slack channel where engineers share good to read articles, blogs and podcasts.

I’ve made a pact to go through each and every article that is shared on that channel irrespective of the domain or the tech it. And so far this has been a catalyst for my learnings on things that I didn’t even know were there to learn.

4. Master the programming language that you use

Read a good book or two on them. Get to the internals of the language that you use primarily at work. We at Squad use python heavily for the back-end, machine learning, data analytics and everything.

Personally, I’ve added 2 great books to my reading list that I’ll be picking next:

  1. Fluent Python
  2. Mastering Python Design Patterns.

5. Send your code reviews to the hardest critics


At Squad, code reviews are in the DNA of engineering processes. I’ve been very fortunate to be on-boarded to the Squad codebase by one of the best and hardest code critics at the company. It really helped me in developing high code quality standards and also the art of reviewing code.

Not only that taught me how to write better code but also how to deliver your code reviews in a respectful manner that the other person doesn’t feel discouraged, something that I always keep in mind while doing code-reviews myself.

6. Enroll in classes on areas where you want to improve

Courses on sites like edx, coursera, Udacity have amazing courses that we can take in our spare time. Let it be compilers, database, machine learning, infrastructure, these platforms have amazing courses on all of them.

Personally, I try to keep exactly one online course in-progress all the time.

7. Participate in design discussions of projects you are interested in


Don’t ask for an invitation. Ask the engineers if they’d mind you being a silent observer or even a participant in the design discussion.

8. Make sure you are on a team with at least a few senior engineers whom you can learn from

This will help increase your learning rate at least 80% of the time.

At Squad, I get to work with one of the most awesome engineers I’ve got an opportunity to work with. That has helped me in learning and polishing things like estimations, product thinking, designing, communication etc.


Our work fills a large part of our life. Making sure that our work is driving our learning and improvements helps big time in maintaining contempt and keep progressing on the path to become a better effective engineer.



That’s all, folks!


Philosophy Behind The Offensive Programming


Recently I was listening to a podcast and there was this really smart guy Piwai talking about something that instantly captivated by attention. That was the coining of the term Offensive Programming.

What is offensive programming?

Well, you can find the literature on  Wikipedia and also I am not the best person to explain that. So check that out please. But fundamentally, offensive programming refers to a style of programming that is exact opposite of the more famous counter-part the defensive programming.

Defensive programming refers to coding style which adheres to dealing gracefully with conditions that should not happen.

Offensive programming on the other hand, well just tells you to let the app crash. Don’t try to recover, don’t try to handle the exception, just log the stack trace and crash.

The reason behind this is that in reality the problem can be much bigger and somewhere else in the code, as a side effect of you are getting this error in first place. This forces you to fix the problem at the source and will possibly result in a healthier code base.

When it makes sense to be offensive?

This was my exact concern while I was listening to this podcast. Thankfully, Piwai answered that himself. I also, talked about it with a really smart guy at the office and he also made the same remarks.

So at Square (the company who do payments and author libraries) what they do is, they stick to a defensive style of programming  for interfaces and parts of code that deals with external interfaces and/or user interactions. Basically, something that is not in your control.

But, for the internal interfaces, where the classes you wrote are going to interact with each other, you don’t have to be that paranoid about that. This is where he (Piwai) said you should switch to the offensive approach. You have full control over the classes you wrote, and the expected behaviour is in your control. If it fails to do so, it’s better to just crash and let the problem to be fixed at the source.

That is the exact reason he said at Square, they make very liberal use of assertions in the code. Assertions are not forgiving at all.

Example Please!

I would attempt to point to examples here, one that the Piwai himself talked in very brief and the one that I’ve encountered myself where I thought it made sense.

In this example, say we are handling credit card objects. There is no point to internally validate the credit card object every time you deal with it.

As soon as we get a credit card, we decorate it with a validated credit card. That’s all the defensiveness we had to offer.

Now internally, we go offensive and throw exceptions or assertions every time we encounter an invalidate credit card object.

The code below is not perfect, but can give you an idea.

class ValidatedCreditCard extends CreditCard{

    CreditCard creditCard;
    ValidatedCreditCard(CreditCard creditCard){
      // Handling external user interactions defensively.
      catch (CreditCardValidationError e) {
        // Handle and try to fix the error
      this.creditCard = creditCard;

public static void main(String[] args){

    CreditCard c  = getCreditCardFromUser();
    c = ValidatedCreditCard(c);
    // Time to go offensive
    // ...
    if (c == null){
      throw new CardInvalidException();

Another example I can think of is a much simpler one and more relatable.
Suppose, we have a utility function that uploads a file to s3.
It would make sense to follow offensive programming style and just throw an exception if somehow they file or the key reaching the function is None.

def upload_file_to_s3(file, key):
    if file is None or key is None:
        raise TypeError


Few more tips from the podcast

1. How to start with offensive programming?

Best way is to start putting assertions in the code, where you think is suitable. Yeah, we’ll experience more crashes and that’s awesome!

Because now we know that we have a problem.

2.  We feel more confident about the code base:

We just know that, this method doesn’t try to handle nulls, thus I can confidently say that it was not null or it would’ve crashed.

3. Do incremental roll outs.

When you ship a code, roll it out like for 1% of users. We’ll have a ton of crash reports, and that’s good! I mean not for the 1% users but they are taking one for the team!

4. Crash at preventable errors and recover from expect-able errors :

Preventable errors are invalid arguments, NPEs etc. Go offensive on these.

Expectable errors are like resource depletion, invalid user inputs etc.

Try to recover from these.


Overall, it was nice to listen to a guy who works at a company like Square talking about how they use offensive programming for a healthier code base. And if Square is doing something, we all can learn something from that!

6.00.1x: Introduction to Computer Science and Programming Using Python : The Experience

Hi, guys. I am a very active participant of MOOC. In my previous post I shared my experience with LFS101x : Introduction to linux .And this time I am back with a new course.

It was summer of 2014, I was having my semester break. And 6.00.1x was on my enrollment list on edx. I was really excited to get started with it.

As this course says, it is meant for people with little to no programming experience, I was there having some programming experience but quiet a novice to python. And will to learn python was my driving force here (of-course the MIT and Honor Code Certificate were also there 😉 ) . I started this course around May,14 and managed to finish it with all the quizzes and psets by August,14. There by I got my honor code certificate issued on 15th of August.

The course starts with very gentle introduction to computing : Prof. Eric Grimson very effectively introduces us to legendary  problems in CS like Tower of Hanoi , Fabonacci series etc.

Each week they give a pset, based on the lectures delivered that week. And trust me guys, these pset are very interesting and kind off invokes the sleeping programmer in you.  It will be no wonder to see you are skipping meals to work on the psets. Psets sometimes get brainstorming and those hide and seek champion bugs really pisses us, but in the end it is fun, and coding is all about fun :).

By week three , in pset3 we build a Hangman game. They provided us with the distribution code which we were supposed to complete. This pattern helps to improve code reading habit and understand how to write well designed code.

By week 5 comes the first quiz. It was not that tough. I scored 94% in it. So nothing to worry about it. You can do that easily too.

Week 6 introduces us to OOP(Object Oriented Programming , for the non-programming people). And in the corresponding pset6 we coded an application to decrypt   the encrypted text present in a text file. It was amazing to see it working , and feeling like a cryptography expert for a while ;).

Week 7 and pset 7 was my favorite. We get to code a RSS reader in python using Tkinter as GUI library (of-course there is the template provided by them as distribution code) . This RSS reader fetched news from Google News according to the tags provided by the users (Cool na!). This was the project I will be continuing to make something out of it.

On the basis of this pset, I am working on a intelligent RSS news reader. (Stay tuned for that).

By the last week of the course they provide additional research videos highlighting applications of python and programming in various fields of engineering and medical.

Then it is there, the day has come for the final exam. Believe it was not too difficult at all. I passed it. And managed to get a score of 97% overall in the course.

There by i received my Honor Code certificate by edx.

At the end of this Post! I want to say It was great experience for me to learn from world best professors & its not last I’ll keep learning. If you are also passionate about technology and believe in learning then you can explore yourself with best universities of the World.