Tropo WebAPI Development Guide Home  |  Frameset Home

  Making a Call  |  TOC  |  Playing Audio Files  

Passing in Parameters


Another useful feature is the ability to pass parameters into your application as global variables. If you launch your application using REST with a GET request, any query string variable (everything after the ?) will get converted into a global variable in your application; if the URL contains numbertodial=14075550100, the numbertodial variable in your application will be set to 14075550100. When using POST, you can define variables within the request body, which will be treated the same way. Check out the Additional Reading at the bottom of this page for more info.

Note that you should HTML encode anything that isn't alphanumeric - so for example if you wanted to include the + sign in the REST request instead of in the application, you would reference it as %2B instead of + (otherwise it's detected as a space, which is intentionally used in the example below to insert a space between words in the "msg").

This example URL contains three variables: numbertodial, customername, and msg. The code is written to accept those variables in certain places to complete the app. When the following app is launched, it will send a text message to the number defined in the URL that says “OMG John Dyer, the sky is falling!”, then disconnect.


JSON alone cannot be used to parse parameters, so all the following examples include a library - note that we add in a "+" to for the outbound number. This way you don't have to encode it in the URL:

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

post '/index.json' do
  
  v = Tropo::Generator.parse request.env["rack.input"].read
  
  to = v[:session][:parameters][:numbertodial]
  name = v[:session][:parameters][:customername]
  msg = v[:session][:parameters][:msg]
    
  t = Tropo::Generator.new
  
  t.call(:to => "+" + to)
  t.say(:value => "OMG " + name + ", " + msg + "!")
  
  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 to = session.session.parameters.numbertodial;
		var name = session.session.parameters.customername;
		var msg = session.session.parameters.msg;
	
		tropo.call("+" + to);
		tropo.say("OMG " + name + ", " + msg + "!");
	
		response.end(TropoJSON(tropo));
	});

}).listen(8000); 
<?php 

require 'tropo.class.php'; 

$session = new Session(); 
$to = $session->getParameters("numbertodial"); 
$name = $session->getParameters("customername"); 
$msg = $session->getParameters("msg"); 
    
$tropo = new Tropo(); 
    
$tropo->call("+".$to); 
$tropo->say("OMG ".$name.", ".$msg."!"); 

return $tropo->RenderJson(); 

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

@post('/index.json')

def index(request):

    s = Session(request.body)

    t = Tropo()

    t.call("+" + to=s.parameters['numbertodial'])
    t.say("OMG " + s.parameters['customername'] + ", " + s.parameters['msg'] + "!")

    return t.RenderJson()

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

Want to play audio in your outbound call instead of using Text To Speech (TTS)?

Next Step: Playing Audio Files



Additional Reading

As mentioned in the main Sending Text Messages QuickStart, you can also use a POST with XML if you'd prefer - check out this page for the syntax.


  ANNOTATIONS: EXISTING POSTS
0 posts - click the button below to add a note to this page

login
  Making a Call  |  TOC  |  Playing Audio Files  

© 2012 Voxeo Corporation  |  Voxeo IVR  |  VoiceXML & CCXML IVR Developer Site