Warm Transfer with Python and Flask

Download the Code

Warm Transfer

Have you ever been disconnected from a support call while being transferred to someone else?  That couldn't have left a great impression on you...

Warm transfer eliminates this problem - it allows your agents to have the ability to dial in another agent in real time.

Today we'll add warm transfer to a Python and Flask application so we can engender warm feelings among customers talking to support.

Here's how it works at a high level:

  1. The first agent becomes available by connecting through the web client.
  2. The second agent becomes available by connecting through the web client.
  3. A customer calls our support line.
  4. The client stays on hold while the first agent joins the call.
  5. While the first agent is on the phone with the client, he or she dials the second agent into the call.
  6. Once the second agent is on the call, the first one can disconnect. The client and the second agent stay on the call.

Let's get started!  Clone the sample application from Github, and click the button below to begin.

Set Up The Voice Web Hook

First, let's configure the voice web-hook for the Twilio number that customers will dial when they want to talk to a support agent. 

Twilio Console for Warm Transfer

In production, this should be the public-facing URL for your app.

One option to expose a development URL from your local machine is to use ngrok.  Your URL would then be something like:

 https://<your-ngrok-id>.ngrok.io/conference/connect/client

Loading Code Samples...
Language
from flask import render_template, jsonify, request, url_for
from . import token
from . import call
from . import twiml_generator
from .models import ActiveCall
AGENT_WAIT_URL = 'http://twimlets.com/holdmusic?Bucket=com.twilio.music.classical'


def routes(app):
    app.route('/')(root)
    app.route('/conference/connect/client', methods=['POST'])(connect_client)
    app.route('/<agent_id>/token', methods=['POST'])(generate_token)
    app.route('/conference/<agent_id>/call', methods=['POST'])(call_agent)
    app.route('/conference/wait', methods=['POST'])(wait)
    app.route('/conference/<conference_id>/connect/<agent_id>', methods=['POST'])(connect_agent)


def root():
    return render_template('index.html')


def connect_client():
    conference_id = request.form['CallSid']
    connect_agent_url = url_for('connect_agent', agent_id='agent1',
                                conference_id=conference_id, _external=True)
    call.call_agent('agent1', connect_agent_url)
    ActiveCall.create('agent1', conference_id)
    return str(twiml_generator.generate_connect_conference(conference_id,
                                                           url_for('wait'),
                                                           False,
                                                           True))


def generate_token(agent_id):
    return jsonify(token=token.generate(agent_id), agentId=agent_id)


def call_agent(agent_id):
    conference_id = ActiveCall.conference_id_for(agent_id)
    connect_agent_url = url_for('connect_agent', agent_id='agent2',
                                conference_id=conference_id, _external=True)
    return call.call_agent('agent2', connect_agent_url)


def wait():
    return str(twiml_generator.generate_wait())


def connect_agent(conference_id, agent_id):
    exit_on_end = 'agent2' == agent_id
    return str(twiml_generator.generate_connect_conference(conference_id,
                                                           AGENT_WAIT_URL,
                                                           True,
                                                           exit_on_end))
warm_transfer_flask/views.py
Route for our Twilio webhook

warm_transfer_flask/views.py

Awesome, now you've got a webhook in place.  Next up, we'll look at some of the code.

Connect an Agent to a Call

Here you can see all front-end code necessary to connect an agent using Twilio's Voice Web Client.

We need three things to have a live web client:

  • A capability token (provided by our Flask app)
  • A unique identifier (string) for each agent
  • Event listeners to handle different Twilio-triggered events
Loading Code Samples...
Language
$(function() {
  var currentAgentId;
  var currentConnection;
  var $callStatus = $('#call-status');
  var $connectAgent1Button = $("#connect-agent1-button");
  var $connectAgent2Button = $("#connect-agent2-button");

  var $answerCallButton = $("#answer-call-button");
  var $hangupCallButton = $("#hangup-call-button");
  var $dialAgent2Button = $("#dial-agent2-button");

  $connectAgent1Button.on('click', { agentId: 'agent1' }, agentClickHandler);
  $connectAgent2Button.on('click', { agentId: 'agent2' }, agentClickHandler);
  $hangupCallButton.on('click', hangUp);
  $dialAgent2Button.on('click', dialAgent2);

  function fetchToken(agentId) {
    $.post('/' + agentId + '/token', {}, function(data) {
      currentAgentId = data.agentId;
      connectClient(data.token)
    }, 'json');
  }

  function connectClient(token) {
    Twilio.Device.setup(token);
  }

  Twilio.Device.ready(function (device) {
    updateCallStatus("Ready");
    agentConnectedHandler(device._clientName);
  });

  // Callback for when Twilio Client receives a new incoming call
  Twilio.Device.incoming(function(connection) {
    currentConnection = connection;
    updateCallStatus("Incoming support call");

    // Set a callback to be executed when the connection is accepted
    connection.accept(function() {
      updateCallStatus("In call with customer");
      $answerCallButton.prop('disabled', true);
      $hangupCallButton.prop('disabled', false);
      $dialAgent2Button.prop('disabled', false);
    });

    // Set a callback on the answer button and enable it
    $answerCallButton.click(function() {
      connection.accept();
    });
    $answerCallButton.prop('disabled', false);
  });

  /* Report any errors to the call status display */
  Twilio.Device.error(function (error) {
    updateCallStatus("ERROR: " + error.message);
    disableConnectButtons(false);
  });

  // Callback for when the call finalizes
  Twilio.Device.disconnect(function(connection) {
    callEndedHandler(connection.device._clientName);
  });

  function dialAgent2() {
    $.post('/conference/' + currentAgentId + '/call')
  }

  /* End a call */
  function hangUp() {
    Twilio.Device.disconnectAll();
  }

  function agentClickHandler(e) {
    var agentId = e.data.agentId;
    disableConnectButtons(true);
    fetchToken(agentId);
  }

  function agentConnectedHandler(agentId) {
    $('#connect-agent-row').addClass('hidden');
    $('#connected-agent-row').removeClass('hidden');
    updateCallStatus("Connected as: " + agentId);

    if (agentId === 'agent1') {
      $dialAgent2Button.removeClass('hidden').prop('disabled', true);
    }
    else {
      $dialAgent2Button.addClass('hidden')
    }
  }

  function callEndedHandler(agentId) {
    $dialAgent2Button.prop('disabled', true);
    $hangupCallButton.prop('disabled', true);
    $answerCallButton.prop('disabled', true)
    updateCallStatus("Connected as: " + agentId);
  }

  function disableConnectButtons(disable) {
    $connectAgent1Button.prop('disabled', disable);
    $connectAgent2Button.prop('disabled', disable);
  }

  function updateCallStatus(status) {
    $callStatus.text(status);
  }
});
warm_transfer_flask/static/js/main.js
Create a new call from the browser

warm_transfer_flask/static/js/main.js

In the next step we'll take a closer look at capability token generation.

Generate a Capability Token

In order to connect the Twilio Voice Web Client we need a capability token.

To allow incoming connections through the web client an identifier must be provided when generating the token.

Loading Code Samples...
Language
from twilio.jwt.client import ClientCapabilityToken
from os import environ as ENV


def generate(agent_id):
    account_sid = ENV['TWILIO_ACCOUNT_SID']
    auth_token = ENV['TWILIO_AUTH_TOKEN']
    capability = ClientCapabilityToken(account_sid, auth_token)
    capability.allow_client_incoming(agent_id)
    jwt = capability.to_jwt()

    try:
        jwt_str = jwt.decode('utf-8')
    except Exception as e:
        jwt_str = jwt

    return jwt_str
warm_transfer_flask/token.py
Generate a capability token

warm_transfer_flask/token.py

Next up let's see how to handle incoming calls.

Handle Incoming Calls

For this tutorial we used fixed identifier strings like agent1 and agent2 but you can use any unique application generated string for your call center clients. These identifiers will be used to create outbound calls to the specified agent using the Twilio REST API.

When a client makes a call to our Twilio number the application receives a POST request asking for instructions. We'll use TwiML to instruct the client to join a conference room and use the Twilio REST API client to invite (and initiate a call to) the first agent.

When providing instructions to the client, we also provide a waitUrl. This URL is another end point of our application and will return more TwiML to SAY welcome to the user and also PLAY some music while on hold. Take a look at the code here.

We use the client's CallSid as the conference identifier. Since all participants need this identifier to join the conference, we'll need to store it in a database so that we can grab it when we dial the second agent into the conference.

Loading Code Samples...
Language
from flask import render_template, jsonify, request, url_for
from . import token
from . import call
from . import twiml_generator
from .models import ActiveCall
AGENT_WAIT_URL = 'http://twimlets.com/holdmusic?Bucket=com.twilio.music.classical'


def routes(app):
    app.route('/')(root)
    app.route('/conference/connect/client', methods=['POST'])(connect_client)
    app.route('/<agent_id>/token', methods=['POST'])(generate_token)
    app.route('/conference/<agent_id>/call', methods=['POST'])(call_agent)
    app.route('/conference/wait', methods=['POST'])(wait)
    app.route('/conference/<conference_id>/connect/<agent_id>', methods=['POST'])(connect_agent)


def root():
    return render_template('index.html')


def connect_client():
    conference_id = request.form['CallSid']
    connect_agent_url = url_for('connect_agent', agent_id='agent1',
                                conference_id=conference_id, _external=True)
    call.call_agent('agent1', connect_agent_url)
    ActiveCall.create('agent1', conference_id)
    return str(twiml_generator.generate_connect_conference(conference_id,
                                                           url_for('wait'),
                                                           False,
                                                           True))


def generate_token(agent_id):
    return jsonify(token=token.generate(agent_id), agentId=agent_id)


def call_agent(agent_id):
    conference_id = ActiveCall.conference_id_for(agent_id)
    connect_agent_url = url_for('connect_agent', agent_id='agent2',
                                conference_id=conference_id, _external=True)
    return call.call_agent('agent2', connect_agent_url)


def wait():
    return str(twiml_generator.generate_wait())


def connect_agent(conference_id, agent_id):
    exit_on_end = 'agent2' == agent_id
    return str(twiml_generator.generate_connect_conference(conference_id,
                                                           AGENT_WAIT_URL,
                                                           True,
                                                           exit_on_end))
warm_transfer_flask/views.py
Create a conference call, connect the first agent, and put the client on hold

warm_transfer_flask/views.py

Now let's see how to provide TwiML instructions to the client.

Provide TwiML Instructions For The Client

Here we create a TwiMLResponse that will contain a DIAL verb with a CONFERENCE noun that will instruct the JavaScript client to join a specific conference room.

Loading Code Samples...
Language
from twilio.twiml.voice_response import VoiceResponse, Dial


def generate_wait():
    twiml_response = VoiceResponse()
    wait_message = 'Thank you for calling. Please wait in line for a few seconds. An agent will be with you shortly.'
    wait_music = 'http://com.twilio.music.classical.s3.amazonaws.com/BusyStrings.mp3'
    twiml_response.say(wait_message)
    twiml_response.play(wait_music)
    return str(twiml_response)


def generate_connect_conference(call_sid, wait_url, start_on_enter, end_on_exit):
    twiml_response = VoiceResponse()
    dial = Dial()
    dial.conference(call_sid,
                    start_conference_on_enter=start_on_enter,
                    end_conference_on_exit=end_on_exit,
                    wait_url=wait_url)
    return str(twiml_response.append(dial))
warm_transfer_flask/twiml_generator.py
Provide TwiML instructions to the client

warm_transfer_flask/twiml_generator.py

Next up, we will look at how to dial our first agent into the call.

Dial The First Agent Into the Call

For our app we created a call module to handle dialing our agents. This module uses Twilio's REST API to create a new call. The create method receives the following parameters:

  1. from_: Your Twilio number
  2. to: The agent web client's identifier (agent1 or agent2)
  3. url: A URL to ask for TwiML instructions when the call connects

Once the agent answers the call in the web client, a request is made to the callback URL instructing this call to join the conference where the client is already waiting.

Loading Code Samples...
Language
from twilio.rest import Client
from os import environ as ENV


def call_agent(agent_id, callback_url):
    account_sid = ENV['TWILIO_ACCOUNT_SID']
    auth_token = ENV['TWILIO_AUTH_TOKEN']
    my_number = ENV['TWILIO_NUMBER']
    client = Client(account_sid, auth_token)

    to = 'client:' + agent_id
    from_ = my_number
    call = client.calls.create(to, from_, url=callback_url)

    return call.sid
warm_transfer_flask/call.py
Dial an agent into the call

warm_transfer_flask/call.py

With that in mind, let's see how to add the second agent to the call.

Dial The Second Agent Into the Call

When the client and the first agent are both in the call we are ready to perform the warm transfer to a second agent.

The first agent makes a request passing its identifier to allow us to look for the conference_id needed to invite the second agent. Since we already have a call module, we can simply use the call_agent function to connect the second agent.

Loading Code Samples...
Language
from flask import render_template, jsonify, request, url_for
from . import token
from . import call
from . import twiml_generator
from .models import ActiveCall
AGENT_WAIT_URL = 'http://twimlets.com/holdmusic?Bucket=com.twilio.music.classical'


def routes(app):
    app.route('/')(root)
    app.route('/conference/connect/client', methods=['POST'])(connect_client)
    app.route('/<agent_id>/token', methods=['POST'])(generate_token)
    app.route('/conference/<agent_id>/call', methods=['POST'])(call_agent)
    app.route('/conference/wait', methods=['POST'])(wait)
    app.route('/conference/<conference_id>/connect/<agent_id>', methods=['POST'])(connect_agent)


def root():
    return render_template('index.html')


def connect_client():
    conference_id = request.form['CallSid']
    connect_agent_url = url_for('connect_agent', agent_id='agent1',
                                conference_id=conference_id, _external=True)
    call.call_agent('agent1', connect_agent_url)
    ActiveCall.create('agent1', conference_id)
    return str(twiml_generator.generate_connect_conference(conference_id,
                                                           url_for('wait'),
                                                           False,
                                                           True))


def generate_token(agent_id):
    return jsonify(token=token.generate(agent_id), agentId=agent_id)


def call_agent(agent_id):
    conference_id = ActiveCall.conference_id_for(agent_id)
    connect_agent_url = url_for('connect_agent', agent_id='agent2',
                                conference_id=conference_id, _external=True)
    return call.call_agent('agent2', connect_agent_url)


def wait():
    return str(twiml_generator.generate_wait())


def connect_agent(conference_id, agent_id):
    exit_on_end = 'agent2' == agent_id
    return str(twiml_generator.generate_connect_conference(conference_id,
                                                           AGENT_WAIT_URL,
                                                           True,
                                                           exit_on_end))
warm_transfer_flask/views.py
Invite a second agent to the conference

warm_transfer_flask/views.py

Next up, we'll look at how to handle the first agent leaving the call.

The First Agent Leaves the Call

When the three participants have joined the same call, the first agent has served his or her purpose. Now agent #1 can drop the call, leaving agent #2 and the client to discuss support matters.

It is important to notice the differences between the TwiML each one of the participants received when joining the call:

  • Both agent one and two have startConferenceOnEnter set to true.
  • For the client calling and for agent two, endConferenceOnExit is set to true.

Translated, this means a conference will start when either agent joins the call.  It also means the client or agent #2 disconnecting will hang up the call.

Loading Code Samples...
Language
from twilio.twiml.voice_response import VoiceResponse, Dial


def generate_wait():
    twiml_response = VoiceResponse()
    wait_message = 'Thank you for calling. Please wait in line for a few seconds. An agent will be with you shortly.'
    wait_music = 'http://com.twilio.music.classical.s3.amazonaws.com/BusyStrings.mp3'
    twiml_response.say(wait_message)
    twiml_response.play(wait_music)
    return str(twiml_response)


def generate_connect_conference(call_sid, wait_url, start_on_enter, end_on_exit):
    twiml_response = VoiceResponse()
    dial = Dial()
    dial.conference(call_sid,
                    start_conference_on_enter=start_on_enter,
                    end_conference_on_exit=end_on_exit,
                    wait_url=wait_url)
    return str(twiml_response.append(dial))
warm_transfer_flask/twiml_generator.py
Instruction to dial into a conference call

warm_transfer_flask/twiml_generator.py

And that's it!

We have just implemented warm transfers using Python and Flask. Now your clients won't get disconnected from support calls while they are being transferred.

Now let's look at some other great features Twilio makes it easy to add.

Where to Next?

If you're a Python developer working with Twilio you'll almost certainly love these tutorials:

Automated-Survey-Flask

Instantly collect structured data from your users with a survey conducted over a call or SMS text messages.

Browser-Calls

Learn how to use Twilio Client to make browser-to-phone and browser-to-browser calls with ease.

Did this help?

Thanks for checking this tutorial out! Let us know on Twitter what you've built... or what you're building.

Samuel Mendes
Paul Kamp
Agustin Camino

Need some help?

We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd browsing the Twilio tag on Stack Overflow.

1 / 1
Loading Code Samples...
from flask import render_template, jsonify, request, url_for
from . import token
from . import call
from . import twiml_generator
from .models import ActiveCall
AGENT_WAIT_URL = 'http://twimlets.com/holdmusic?Bucket=com.twilio.music.classical'


def routes(app):
    app.route('/')(root)
    app.route('/conference/connect/client', methods=['POST'])(connect_client)
    app.route('/<agent_id>/token', methods=['POST'])(generate_token)
    app.route('/conference/<agent_id>/call', methods=['POST'])(call_agent)
    app.route('/conference/wait', methods=['POST'])(wait)
    app.route('/conference/<conference_id>/connect/<agent_id>', methods=['POST'])(connect_agent)


def root():
    return render_template('index.html')


def connect_client():
    conference_id = request.form['CallSid']
    connect_agent_url = url_for('connect_agent', agent_id='agent1',
                                conference_id=conference_id, _external=True)
    call.call_agent('agent1', connect_agent_url)
    ActiveCall.create('agent1', conference_id)
    return str(twiml_generator.generate_connect_conference(conference_id,
                                                           url_for('wait'),
                                                           False,
                                                           True))


def generate_token(agent_id):
    return jsonify(token=token.generate(agent_id), agentId=agent_id)


def call_agent(agent_id):
    conference_id = ActiveCall.conference_id_for(agent_id)
    connect_agent_url = url_for('connect_agent', agent_id='agent2',
                                conference_id=conference_id, _external=True)
    return call.call_agent('agent2', connect_agent_url)


def wait():
    return str(twiml_generator.generate_wait())


def connect_agent(conference_id, agent_id):
    exit_on_end = 'agent2' == agent_id
    return str(twiml_generator.generate_connect_conference(conference_id,
                                                           AGENT_WAIT_URL,
                                                           True,
                                                           exit_on_end))
$(function() {
  var currentAgentId;
  var currentConnection;
  var $callStatus = $('#call-status');
  var $connectAgent1Button = $("#connect-agent1-button");
  var $connectAgent2Button = $("#connect-agent2-button");

  var $answerCallButton = $("#answer-call-button");
  var $hangupCallButton = $("#hangup-call-button");
  var $dialAgent2Button = $("#dial-agent2-button");

  $connectAgent1Button.on('click', { agentId: 'agent1' }, agentClickHandler);
  $connectAgent2Button.on('click', { agentId: 'agent2' }, agentClickHandler);
  $hangupCallButton.on('click', hangUp);
  $dialAgent2Button.on('click', dialAgent2);

  function fetchToken(agentId) {
    $.post('/' + agentId + '/token', {}, function(data) {
      currentAgentId = data.agentId;
      connectClient(data.token)
    }, 'json');
  }

  function connectClient(token) {
    Twilio.Device.setup(token);
  }

  Twilio.Device.ready(function (device) {
    updateCallStatus("Ready");
    agentConnectedHandler(device._clientName);
  });

  // Callback for when Twilio Client receives a new incoming call
  Twilio.Device.incoming(function(connection) {
    currentConnection = connection;
    updateCallStatus("Incoming support call");

    // Set a callback to be executed when the connection is accepted
    connection.accept(function() {
      updateCallStatus("In call with customer");
      $answerCallButton.prop('disabled', true);
      $hangupCallButton.prop('disabled', false);
      $dialAgent2Button.prop('disabled', false);
    });

    // Set a callback on the answer button and enable it
    $answerCallButton.click(function() {
      connection.accept();
    });
    $answerCallButton.prop('disabled', false);
  });

  /* Report any errors to the call status display */
  Twilio.Device.error(function (error) {
    updateCallStatus("ERROR: " + error.message);
    disableConnectButtons(false);
  });

  // Callback for when the call finalizes
  Twilio.Device.disconnect(function(connection) {
    callEndedHandler(connection.device._clientName);
  });

  function dialAgent2() {
    $.post('/conference/' + currentAgentId + '/call')
  }

  /* End a call */
  function hangUp() {
    Twilio.Device.disconnectAll();
  }

  function agentClickHandler(e) {
    var agentId = e.data.agentId;
    disableConnectButtons(true);
    fetchToken(agentId);
  }

  function agentConnectedHandler(agentId) {
    $('#connect-agent-row').addClass('hidden');
    $('#connected-agent-row').removeClass('hidden');
    updateCallStatus("Connected as: " + agentId);

    if (agentId === 'agent1') {
      $dialAgent2Button.removeClass('hidden').prop('disabled', true);
    }
    else {
      $dialAgent2Button.addClass('hidden')
    }
  }

  function callEndedHandler(agentId) {
    $dialAgent2Button.prop('disabled', true);
    $hangupCallButton.prop('disabled', true);
    $answerCallButton.prop('disabled', true)
    updateCallStatus("Connected as: " + agentId);
  }

  function disableConnectButtons(disable) {
    $connectAgent1Button.prop('disabled', disable);
    $connectAgent2Button.prop('disabled', disable);
  }

  function updateCallStatus(status) {
    $callStatus.text(status);
  }
});
from twilio.jwt.client import ClientCapabilityToken
from os import environ as ENV


def generate(agent_id):
    account_sid = ENV['TWILIO_ACCOUNT_SID']
    auth_token = ENV['TWILIO_AUTH_TOKEN']
    capability = ClientCapabilityToken(account_sid, auth_token)
    capability.allow_client_incoming(agent_id)
    jwt = capability.to_jwt()

    try:
        jwt_str = jwt.decode('utf-8')
    except Exception as e:
        jwt_str = jwt

    return jwt_str
from flask import render_template, jsonify, request, url_for
from . import token
from . import call
from . import twiml_generator
from .models import ActiveCall
AGENT_WAIT_URL = 'http://twimlets.com/holdmusic?Bucket=com.twilio.music.classical'


def routes(app):
    app.route('/')(root)
    app.route('/conference/connect/client', methods=['POST'])(connect_client)
    app.route('/<agent_id>/token', methods=['POST'])(generate_token)
    app.route('/conference/<agent_id>/call', methods=['POST'])(call_agent)
    app.route('/conference/wait', methods=['POST'])(wait)
    app.route('/conference/<conference_id>/connect/<agent_id>', methods=['POST'])(connect_agent)


def root():
    return render_template('index.html')


def connect_client():
    conference_id = request.form['CallSid']
    connect_agent_url = url_for('connect_agent', agent_id='agent1',
                                conference_id=conference_id, _external=True)
    call.call_agent('agent1', connect_agent_url)
    ActiveCall.create('agent1', conference_id)
    return str(twiml_generator.generate_connect_conference(conference_id,
                                                           url_for('wait'),
                                                           False,
                                                           True))


def generate_token(agent_id):
    return jsonify(token=token.generate(agent_id), agentId=agent_id)


def call_agent(agent_id):
    conference_id = ActiveCall.conference_id_for(agent_id)
    connect_agent_url = url_for('connect_agent', agent_id='agent2',
                                conference_id=conference_id, _external=True)
    return call.call_agent('agent2', connect_agent_url)


def wait():
    return str(twiml_generator.generate_wait())


def connect_agent(conference_id, agent_id):
    exit_on_end = 'agent2' == agent_id
    return str(twiml_generator.generate_connect_conference(conference_id,
                                                           AGENT_WAIT_URL,
                                                           True,
                                                           exit_on_end))
from twilio.twiml.voice_response import VoiceResponse, Dial


def generate_wait():
    twiml_response = VoiceResponse()
    wait_message = 'Thank you for calling. Please wait in line for a few seconds. An agent will be with you shortly.'
    wait_music = 'http://com.twilio.music.classical.s3.amazonaws.com/BusyStrings.mp3'
    twiml_response.say(wait_message)
    twiml_response.play(wait_music)
    return str(twiml_response)


def generate_connect_conference(call_sid, wait_url, start_on_enter, end_on_exit):
    twiml_response = VoiceResponse()
    dial = Dial()
    dial.conference(call_sid,
                    start_conference_on_enter=start_on_enter,
                    end_conference_on_exit=end_on_exit,
                    wait_url=wait_url)
    return str(twiml_response.append(dial))
from twilio.rest import Client
from os import environ as ENV


def call_agent(agent_id, callback_url):
    account_sid = ENV['TWILIO_ACCOUNT_SID']
    auth_token = ENV['TWILIO_AUTH_TOKEN']
    my_number = ENV['TWILIO_NUMBER']
    client = Client(account_sid, auth_token)

    to = 'client:' + agent_id
    from_ = my_number
    call = client.calls.create(to, from_, url=callback_url)

    return call.sid
from flask import render_template, jsonify, request, url_for
from . import token
from . import call
from . import twiml_generator
from .models import ActiveCall
AGENT_WAIT_URL = 'http://twimlets.com/holdmusic?Bucket=com.twilio.music.classical'


def routes(app):
    app.route('/')(root)
    app.route('/conference/connect/client', methods=['POST'])(connect_client)
    app.route('/<agent_id>/token', methods=['POST'])(generate_token)
    app.route('/conference/<agent_id>/call', methods=['POST'])(call_agent)
    app.route('/conference/wait', methods=['POST'])(wait)
    app.route('/conference/<conference_id>/connect/<agent_id>', methods=['POST'])(connect_agent)


def root():
    return render_template('index.html')


def connect_client():
    conference_id = request.form['CallSid']
    connect_agent_url = url_for('connect_agent', agent_id='agent1',
                                conference_id=conference_id, _external=True)
    call.call_agent('agent1', connect_agent_url)
    ActiveCall.create('agent1', conference_id)
    return str(twiml_generator.generate_connect_conference(conference_id,
                                                           url_for('wait'),
                                                           False,
                                                           True))


def generate_token(agent_id):
    return jsonify(token=token.generate(agent_id), agentId=agent_id)


def call_agent(agent_id):
    conference_id = ActiveCall.conference_id_for(agent_id)
    connect_agent_url = url_for('connect_agent', agent_id='agent2',
                                conference_id=conference_id, _external=True)
    return call.call_agent('agent2', connect_agent_url)


def wait():
    return str(twiml_generator.generate_wait())


def connect_agent(conference_id, agent_id):
    exit_on_end = 'agent2' == agent_id
    return str(twiml_generator.generate_connect_conference(conference_id,
                                                           AGENT_WAIT_URL,
                                                           True,
                                                           exit_on_end))
from twilio.twiml.voice_response import VoiceResponse, Dial


def generate_wait():
    twiml_response = VoiceResponse()
    wait_message = 'Thank you for calling. Please wait in line for a few seconds. An agent will be with you shortly.'
    wait_music = 'http://com.twilio.music.classical.s3.amazonaws.com/BusyStrings.mp3'
    twiml_response.say(wait_message)
    twiml_response.play(wait_music)
    return str(twiml_response)


def generate_connect_conference(call_sid, wait_url, start_on_enter, end_on_exit):
    twiml_response = VoiceResponse()
    dial = Dial()
    dial.conference(call_sid,
                    start_conference_on_enter=start_on_enter,
                    end_conference_on_exit=end_on_exit,
                    wait_url=wait_url)
    return str(twiml_response.append(dial))