Tropo is part of CiscoLearn More

CallerID - Reading & Setting

CallerID is a fairly well understood concept - it defines who's contacting your application as well as what displays when your app makes outbound contact with someone. This section goes over what you can and cannot do in terms of both working with inbound callerID and setting outbound.

Reading caller ID

The callerID associated with a voice call or text message will appear in the session JSON, under the from array - specifically the id field:

{
   "session":{
      "id":"1aa06515183223ec0894039c2af433f2",
      "accountId":"33932",
      "timestamp":"2010-02-18T19:07:36.375Z",
      "userType":"HUMAN",
      "initialText":null,
      "callId":"abcdef743e245c265bc2c699b4112345"
      "to":{
         "id":"2125551212",
         "e164Id": "12125551212"
         "name":"unknown",
         "channel":"VOICE",
         "network":"PSTN"
      },
      "from":{
         "id":"4155559999",
         "e164Id":"14155559999",
         "name":"unknown",
         "channel":"VOICE",
         "network":"PSTN"
      },
      "headers":{
         "Content-Length":"247",
         "To":"<sip:2125551212@10.6.61.101:5060>",
         "Contact":"<sip:4155559999@10.6.63.104:5060>",
         "x-sid":"39f4688b8896f024f3a3aebd0cfb40b2",
         "CSeq":"2 INVITE",
         "Via":"SIP/2.0/UDP 66.193.54.18:5060;received=10.6.63.104",
         "x-sbc-record-route":"<sip:195.46.253.237:5061;r2=on;lr;ftag=2a648c6e>",
         "Call-ID":"0-13c4-4b7d8ff7-1c3c1b82-7935-1d10b080",
         "Content-Type":"application/sdp",
         "From":"<sip:4155559999@10.6.63.104:5060>;tag=0-13c4-4b7d8ff7-1c3c1b82-5d8c"
      }
   }
}

The id field strips formatting - so no parentheses or dashes - and for numbers outside the US, the callerID will be prefixed with the country code. Callers that have blocked their caller ID will typically have the ID of "Unknown".

The e164Id field is the caller ID with the country code always added.

Here's an example showing how to use callerID to customize a response per person:

require 'tropo-webapi-ruby'
require 'sinatra'

post '/index.json' do
  
  v = Tropo::Generator.parse request.env["rack.input"].read
  t = Tropo::Generator.new
  
  callerID = v[:session][:from][:id]
  
  if (callerID == "4075550100")
    t.say(:value => "Sending you to Adam.")
    t.transfer(:to => "+447700900444")
  elsif (callerID == "3865550100")
    t.say(:value => "Sending you to Jason.")
    t.transfer(:to => "+611300975708")
  else
    t.say(:value => "You get nothing!")
  end
  
  t.response
  
end
var http = require('http');
var tropo_webapi = require('tropo-webapi');

var server = http.createServer(function (request, response) {  
  
  request.addListener('data', function(data){
    json = data.toString();
  });
    
  request.addListener('end', function() {

	var session = JSON.parse(json);
	var tropo = new TropoWebAPI();
	var callerID = session.session.from.id;
	
	if(callerID == "4075550100") {
		tropo.say("Sending you to Adam.");
		tropo.transfer("+447700900444");
	}
	else if(callerID == "3865550100") {
		tropo.say("Sending you to Jason.");
		tropo.transfer("+611300975708");
	}
	else 
		tropo.say("You get nothing!");	
		
    response.end(TropoJSON(tropo));
});

}).listen(8000); 
<?php

require 'tropo.class.php';

$tropo = new Tropo();
$session = new Session();

$from = $session->getFrom();
$callerID = $from["id"];

if ($callerID == '4075550100') {
	$tropo->say("Sending you to Adam.");
	$tropo->transfer("+447700900444");
}
elseif ($callerID == '3865550100') {
	$tropo->say("Sending you to Jason.");
	$tropo->transfer("+611300975708");
}
else
	$tropo->say("You get nothing!");
	
$tropo->RenderJson();

?>
from itty import *
from tropo import Tropo, Session

@post('/index.json')

def index(request):

	s = Session(request.body)
	callerID = s.fromaddress['id']
	t = Tropo()

	if(callerID == '4075550100') :
		t.say("Sending you to Adam.")
		t.transfer("+447700900444")
	elif (callerID == '3865550100') :
		t.say("Sending you to Jason.")
		t.transfer("+611300975708")
	else :
		t.say("You get nothing!")
		
	return t.RenderJson()

run_itty(server='wsgiref', host='0.0.0.0', port=8888)

Setting callerID

When making a phone call, the callerID will be set to "Unknown" by default. You can override this by setting the from parameter on the call method. The callerID can be any number you like, but should be a real phone number - we explain why in the next section, Tips on Setting a Valid Caller ID.

require 'tropo-webapi-ruby'
require 'sinatra'

post '/index.json' do
  
  t = Tropo::Generator.new
  
  t.call(:to => "+14075551212",
        :from => "+61491570157")
  t.say(:value => "Tag, you're it!")

  t.response
  
end
var http = require('http');
var tropo_webapi = require('tropo-webapi');

var server = http.createServer(function (request, response) {
	
    var tropo = new TropoWebAPI();
    
    //to, answerOnMedia, channel, from, headers, name, network, recording, required, timeout
    tropo.call("+14075551212", null, null, "+61491570157");
    tropo.say("Tag, you're it!");
	
    response.end(TropoJSON(tropo));

}).listen(8000); 
<?php

require 'tropo.class.php';

$tropo = new Tropo();

$tropo->call("+14075551212", array(
	"from" => "+61491570157"
));
$tropo->say("Tag, you're it!");

$tropo->RenderJson();
?>
from itty import *
from tropo import Tropo

@post('/index.json')
def index(request):

	t = Tropo()

	t.call("+14075551212", _from = "+61491570157")
	t.say("Tag, you're it!")
	
	return t.RenderJson()

run_itty(server='wsgiref', host='0.0.0.0', port=8888)
{
   "tropo":[
      {
         "call":{
            "to":"+14075551212",
            "from":"+61491570157"
         }
      },
      {
         "say":[{"value":"Tag, you're it!"}]
      }
   ]
}

For transfers, you can similarly set callerID:

require 'tropo-webapi-ruby'
require 'sinatra'

post '/index.json' do
  
  t = Tropo::Generator.new
  
  t.say(:value => "Transferring you now, please wait.")
  t.transfer(:to => "+14075551212",
            :from => "+61491570157")
  t.response
  
end
var http = require('http');
var tropo_webapi = require('tropo-webapi');

var server = http.createServer(function (request, response) {  
  
	var tropo = new TropoWebAPI();

	tropo.say("Transferring you now, please wait.");
	
	//to, answerOnMedia, choices, from, name, on, required, timeout
	tropo.transfer("+14075551212", null, null, "+61491570157", null, null, null, null);
	
	response.end(TropoJSON(tropo));

}).listen(8000); 
<?php

require 'tropo.class.php';

$tropo = new Tropo();

$tropo->say("Transferring you now, please wait");
$tropo->transfer("+14075551212", array('from' => "+61491570157"));
	
$tropo->RenderJson();

?>
from itty import *
from tropo import Tropo

@post('/index.json')

def index(request):

	t = Tropo()
	
	t.say("Transferring you now, please wait.")
	t.transfer("+14075551212", _from = "+61491570157")

	return t.RenderJson()

run_itty(server='wsgiref', host='0.0.0.0', port=8888)
{"tropo":[
      {
         "say":[
            {"value":"Transferring you now, please wait."}
         ]
      },
      {
         "transfer":{
            "to":"+14075551212",
            "from":"+61491570157"
         }
      }
   ]
}

Text messages must have a callerID/senderID attached, so we can't send "unknown". Instead, we use the first SMS-enabled phone number attached to your application as the senderID. While you can manually set the callerID to a specific number, it must be set to one of the SMS-enabled phone numbers attached to your application. This is a carrier restriction: many mobile carriers will drop any messages that contain a spoofed callerID.