Tropo WebAPI Development Guide Home  |  Frameset Home

  Handling No Input or Bad Input  |  TOC  |  Using the Message Shortcut  

Mixing Text & Voice in a Single App


Need a multimodal, multichannel application? Tropo can operate across multiple networks using the same application; as an example, say you receive a phone call from a customer and recorded what they had to say - now you want to send a text message out to someone notifying them the recording exists. All of it can be handled in one application like the example that follows. Essentially, this app cycles back on itself in order to trigger an outbound message after the recording occurs - your first resource will evaluate the session JSON for the presence of 'parameters', which is only sent if the app is initiated using a token, in order to determine whether it should process the record or make the about call. This example definitely needs a library; note that if you use PHP, the following example includes the Limonade framework, so you would need to define the end of the start URL in Tropo to include the resource name, like http://www.example.com/test.php?uri=start::

require 'tropo-webapi-ruby'
require 'sinatra'
require 'uri'
require 'net/http'

post '/index.json' do
  
  t = Tropo::Generator.new
  
  v = Tropo::Generator.parse request.env["rack.input"].read
  
  parameters = v[:session][:parameters]
    
  if parameters
  
    t.call(:to => "+14075550100", :network => "SMS")
    t.say(:value => "Received office voice mail.")

    t.response
  
  else
  
    t.on :event => 'continue', :next => '/call.json'
    t.on :event => 'hangup', :next => '/call.json'
  
    t.record({:name => 'recording',
              :url => 'http://sendme.com/tropo'
    }) do say :value => "We're not available right now, please leave your message at the beep." end
    
    t.response
  end
end

post '/call.json' do
  
  token = 'your_token'

  url = 'http://api.tropo.com/1.0/sessions?action=create&token='+token

  response = Net::HTTP.get_print URI.parse(url)
  
end

var http = require('http');
var express = require('express');
var app = express.createServer();
var sys = require('sys');
var tropo_webapi = require('tropo-webapi');

// Required to process the HTTP body.
// req.body has the Object while req.rawBody has the JSON string.

app.configure(function(){
	app.use(express.bodyParser());
});

app.post('/', function(req, res){
	
	var tropo = new TropoWebAPI();
	
	parameters = req.body['session']['parameters'];

	if(parameters) 
		{
			//to, answerOnMedia, channel, from, headers, name, network, recording, required, timeout
			
			tropo.call("14075550100", null, null, null, null, null, "SMS", null, null, null);
			tropo.say("Received office voice mail.");
	
			res.send(TropoJSON(tropo));
		}
	else
		{
			var say = new Say("We're not available right now, please leave your message at the beep.");
		
			//attempts, bargein, beep, choices, format, maxSilence, maxTime, method, minConfidence, name, required, say, timeout, transcription, url, password, username)
	
			tropo.record(null, null, null, null, null, null, null, null, null, "recording", null, say, null, null, "http://example.com/tropo", null, null);
		
			tropo.on("continue", null, "/call", true);
			tropo.on("hangup", null, "/call", true);
		
			res.send(TropoJSON(tropo));
		}
});

app.post('/call', function(req, res){
	
	var token = 'your_token';
	var tropoSessionAPI = 'api.tropo.com';
	var path = '/1.0/sessions?action=create&token=' + token;
	
	var tropo_session_api = http.createClient(80, tropoSessionAPI);
	var request = tropo_session_api.request('GET', path, {'host': tropoSessionAPI});
	
	request.end();
	res.end();

});

app.listen(8000);
console.log('Server running on port :8000');
<?php

require 'tropo.class.php';
require 'lib/limonade.php';

dispatch_post('/start', 'app_start');
function app_start() {
	
	$session = new Session();
	
	$tropo = new Tropo();
	
	if($session->getParameters("action")) {

		$tropo->call("+14075550100", array('network'=>'SMS'));
		$tropo->say("Received office voice mail.");
		
		$tropo->RenderJson();
	}
	else {
		
		$tropo->record(array("say"=>"We're not available right now, please leave your message at the beep.", "name" => "recording", "url" => "http://example.com/tropo"));
		
		$tropo->on(array("event" => "continue", "next" => "hello_world.php?uri=call"));
		$tropo->on(array("event" => "hangup", "next" => "hello_world.php?uri=call"));
		
		$tropo->RenderJson();
	}
}

dispatch_post('/call', 'app_call');
function app_call() {
	$token = 'your_token'; 
	$curl_handle=curl_init(); 
	
	curl_setopt($curl_handle,CURLOPT_URL,'http://api.tropo.com/1.0/sessions?action=create&token='.$token); 
	curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2); 
	curl_exec($curl_handle); 
	curl_close($curl_handle); 
}

run();

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

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

	t = Tropo()
	session = Session(request.body)
	
	if('parameters' in dir(session)):
		
		#and 'action' in dir(session.parameters)
		
		t.call(to="+14075550100", network = "SMS")
		t.say("Received office voice mail.")
		
		return t.RenderJson()
	
	else :
		
		t.record(name = "recording", say = "We're not available right now, please leave your message at the beep.", url = "http://example.com/tropo")
	
		t.on(event = "continue", next ="/call")
		t.on(event = "hangup", next ="/call")
		
		return t.RenderJson()
	
@post("/call")
def index(request):
	
	token = 'token'
	f = urllib2.urlopen('http://api.tropo.com/1.0/sessions?action=create&token='+token)
	data = f.read()
	f.close()
	
run_itty(server='wsgiref', host='0.0.0.0', port=8888)

There is an alternative to using call with say to launch the outbound call, which some people may prefer for it's relative simplicity.

Next Step: Using the Message Shortcut



Additional Reading

A complete list of all the parameters for call can be found here, including all the possible values for network.


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

login
  Handling No Input or Bad Input  |  TOC  |  Using the Message Shortcut  

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