Tropo WebAPI Development Guide Home  |  Frameset Home

  Sending Text Messages  |  TOC  |  Receiving Text Responses  

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, an earthquake!”, then disconnect.

You may notice this code is only slightly different than the code used in the Passing Parameters subsection of the Making a Call QuickStart - we changed the message slightly, just to make it stand out, and then just added the network parameter to define this app as SMS instead of a voice. You also need a U.S. or Canadian phone number attached to your application, otherwise there is no "transport" for the SMS to use for delivery.


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, :network => "SMS")
  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;

		//to, answerOnMedia, channel, from, headers, name, network, recording, required, timeout
		tropo.call(to, null, null, null, null, null, "SMS", null, null, null);
		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, array('network'=>'SMS')); 
$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'], network = "SMS"))
    t.say("OMG " + s.parameters['customername'] + ", " + s.parameters['msg'] + "!")

    return t.RenderJson()

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


What if you need to work with a response sent back to your initial text message?

Next Step: Receiving Text Responses



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
egblue
4/25/2012 9:05 PM (EDT)
Is there a way to know if a parameter is defined? How would you do that in Javascript?
parameter_name == undefined
doesn't exist.
Thanks,
VoxeoBrianChurch
4/25/2012 10:31 PM (EDT)
Hello,

I'm forwarding this issue to our Tropo team so that they can help you find a solution as soon as possible. In the meantime, if you have any other questions or concerns that we can address for you, please don't hesitate to ask us.

Regards,
Brian Church
Customer Support Engineer
Voxeo Corporation
kbond
4/26/2012 2:39 PM (EDT)
Hello,

Are you trying to extract the parameters from the HTTP request that initiates your app? If you are, there will always be parameters - "action" = "create" and "token" = "You apps token". You can use these variables as if they are already declared in your app. You can see an example on this from the URL below:

https://www.tropo.com/docs/rest/starting_session.htm

Is that what you are looking for? If not, please explain and give examples of your questions and issues. Besides that, if you have any other questions or concerns, please don't hesitate to ask!

Regards,

Kevin Bond
Voxeo Labs

login
  Sending Text Messages  |  TOC  |  Receiving Text Responses  

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