Tropo WebAPI Development Guide Home  |  Frameset Home

  Mixing Text & Voice in a Single App  |  TOC  |  Recording Caller Input  

Using the Message Shortcut


The message shortcut is an alternative to using call with say to make a quick outbound call or send a message. If you wanted to mix inbound and outbound, the code will be almost identical to that in the Mixing Text & Voice Quickstart; you'll just have the single message method instead of the combination of call and say. 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.message({:to => "+14075550100", :network => "SMS"
      }) do say :value => "Received office voice mail." end

    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) 
		{
			var say1 = new Say("Received office voice mail.");
			//say, to, answerOnMedia, channel, from, name, network, required, timeout, voice
			tropo.message(say1, "+14075550100", null, null, null, "message", "SMS", null, null, null);			
    		res.send(TropoJSON(tropo));
		}
	else
		{
			var say2 = 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, say2, 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->message("Received office voice mail.", array("to"=>"+14075550100", "network"=>"SMS")); 

		$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)):
		
		t.message("Received office voice mail", to="+14075550100", network = "SMS")
		
		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 = 'your_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)

Modifying message to send a voice message or send an IM is very easy - just omit or adjust the network value. This example makes an outbound voice call:

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

post '/index.json' do
  
  t = Tropo::Generator.new
  
    t.message({:to => "+14075550100"}) do say :value => "Received office voice mail." end

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

var server = http.createServer(function (request, response) {
	
	var tropo = new TropoWebAPI();
	var say = new Say("Received office voice mail.");
		
	//say, to, answerOnMedia, channel, from, name, network, required, timeout, voice
		
	tropo.message(say, "+14075550100", null, null, null, "message", null, null, null, null);
			
	response.end(TropoJSON(tropo));

}).listen(8000); 
<?php

require 'tropo.class.php';
	
	$tropo = new Tropo();
	
	$tropo->message("Received office voice mail.", array("to"=>"+14075550100")); 

	$tropo->RenderJson();

?>
from itty import *
from tropo import Tropo

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

	t = Tropo()

	t.message("Received office voice mail.", to="+14075550100")
		
	return t.RenderJson()
		
run_itty(server='wsgiref', host='0.0.0.0', port=8888)

Then this example sends a message to AIM user ‘tropocloud’:

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

post '/index.json' do
  
  t = Tropo::Generator.new
  
    t.message({:to => "tropocloud", :network => "AIM"
      }) do say :value => "Received office voice mail." end

    t.response
   
end

var http = require('http');
var tropo_webapi = require('tropo-webapi');

var server = http.createServer(function (request, response) {
	
	var tropo = new TropoWebAPI();
	var say = new Say("Received office voice mail.");
		
	//say, to, answerOnMedia, channel, from, name, network, required, timeout, voice
		
	tropo.message(say, "tropocloud", null, null, null, "message", "AIM", null, null, null);
			
	response.end(TropoJSON(tropo));

}).listen(8000); 
<?php

require 'tropo.class.php';
	
	$tropo = new Tropo();
	
	$tropo->message("Received office voice mail.", array("to"=>"tropocloud", "network"=>"AIM")); 

	$tropo->RenderJson();

?>
from itty import *
from tropo import Tropo

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

	t = Tropo()

	t.message("Received office voice mail", to="tropocloud", network = "AIM")
		
	return t.RenderJson()
		
run_itty(server='wsgiref', host='0.0.0.0', port=8888)

Those above examples can be used tin the original SMS app, just swap out the code, or used all by themselves - if all you want is an application that makes a call, says something and then hangs up, message combines all three together for a much simpler app.

Good to go using blended applications?

Next Step: Recording Caller Input



Additional Reading

A complete list of all the parameters for message 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
  Mixing Text & Voice in a Single App  |  TOC  |  Recording Caller Input  

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