Transferring Calls with Tropo

In this lab, you'll learn how to transfer a call to different numbers based on user input. The basic technique you learn in this lab can be applied to transfer a call based on user input or based on call properties (such as Caller Id).

Objective

Completion Time: 5 minutes

  • Use Javascript to transfer a call to another number

Prerequisites

Create Your Application

  1. From the Tropo dashboard, log into your account and click Create New App.
  2. Name your app 'Transferring a Call'.
  3. Select Scripting API under Type of Application.
  4. Open the script editor by clicking the pencil icon titled New script.

The Script

  1. Ask the caller a question and assign their answer to a variable with the following code:

    var result = ask("To hear a Chemistry Joke, press 1. To be transferred to the manager, press 2.", {
       choices:"1,2",
       mode: "dtmf"
    });
    

    In this statement, we're using the ask method to inform the user of the valid choices. We then set the choices option to tell Tropo what the valid responses are, and set the mode option to 'dtmf' to specify that we expect the user to make their choices by pressing numbers.

  2. Add an onChoice method after the mode property in order to handle valid user input.

    mode: "dtmf",
    onChoice: function(result) {
           say("Transferring.");
           /* branching code goes here */
    }
    
  3. Add a method to handle invalid user input.

    mode: "dtmf",
    onChoice: function(result) {
    
      say("Transferring.");
    
      /* branching code goes here */
    
      },
    
      onBadChoice: function() {
         say("That is not a valid option");
     }
    
  4. When the user enters an invalid option, the message inside the onBadChoice method will play and the call will be ended.

  5. Create a local variable inside the onChoice method to hold the user's input. It's important to specifically convert user input to the expected data type (integer in this case) so that comparisons can be done.

    onChoice: function(result) {
    
    say("Transferring.");
    
    var userInput = parseInt(result.value);
    
  6. Create a switch statement to select between the different options. A switch statement should always include a default option, so we include one here, even though it's highly unlikely to ever be used.

    var userInput = parseInt(result.value);
    
    switch(userInput) {
    
               case 1:
                   //what to do when the user selects 1
                   break;
    
               case 2:
                   //what to do when the user selects 2
                   break;
    
               default:
                   say("I don't know how you got to this message.");
                   break;
    }
    

    By using a switch statement, you can easily create branches for as many different options as you need.

  7. Use a transfer method inside each of the cases of the switch statement. The transfer method takes a phone number (the destination to transfer to) and an optional configuration object.

    switch(userInput) {
     case 1:
       transfer("+13134516844", {});
       break;
    
     case 2:
       transfer("+15555551212", {});
       break;
    
  8. Use an onTimeout method inside the configuration objects of the two transfer methods to play a message if the destination phone number doesn't answer.

    transfer("+13134516844", {
       onTimeout: function() {
           say("Sorry, there was no answer.");
       }
    });
    
  9. Check your work carefully and save your script as 'transferring.js'.

    Your finished script should look like this:

    var result = ask("To hear a Chemistry Joke, press 1. To be transferred to WatzThis, press 2.", {
       choices:"1,2",
       mode: "dtmf",
       onChoice: function(result) {
    
           say("Transferring.");
           var userInput = parseInt(result.value);
    
           switch(userInput) {
               case 1:
                   transfer("+13134516844", {
                       onTimeout: function() {
                           say("Sorry, there was no answer.");
                       }
                   });
                   break;
    
               case 2:
                   transfer(myConfig.numbers[1], {
                       onTimeout: function() {
                           say("Sorry, there was no answer.");
                       }
                   });
                   break;
    
               default:
                   say("I don't know how you got to this message.");
                   break;
           }
    
       },
       onBadChoice: function() {
           say("That is not a valid option");
       }
    });
    

Test your Application

  1. Select a Country and Region to add a phone number to your app.

  2. Test your app by calling the number associated with it.