Generic SMS and Voice service, Telekom Tropo Part 3

Building M2M prototypes with voice (phone) and text (SMS) isn´t that hard. In most cases we only need a way to send a message via SMS or via phone. But once done you get bored doing it over and over again. Hence I build a simple (and quick and dirty) service for these needs (based on PHP but you can use nearly any language for your choice). This service just gets the needed parameters via URL. Based on these parameters a message will be sent or call initiated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
// Include Tropo classes.
require('tropo.class.php');
 
// $session = new Session(); or low-level usage of JSON:
$session= json_decode(file_get_contents("php://input"));
$message= $session->session->parameters->message;
$numberToDial= $session->session->parameters->numberToDial;
$method = $session->session->parameters->method;
 
$tropo = new Tropo();
 
if ($method == "text") {
	$tropo->message($message, array("network" => "SMS", "to" => $numberToDial));	
} else // if ($method == "call")  {	
	$tropo->call($numberToDial);	
	$options = array("voice" => "Simon");
	$tropo->say($message, $options);
	$tropo->hangup();
}
$tropo->renderJson();
?>

After you have assigned this script to your Tropo application (see Quickstart Tropo, sorry only available in German right now), you need the appropriated token (which you find in your application settings). Finally call the service via a simple URL request. Please note, the values have to be URL encoded and the phone number must include the country code including the plus sign. In HTML this could look like:

1
<a href="https://tropo.developergarden.com/api/sessions?token=YOUR_TOKEN&method=TEXT_OR_VOICE&message=YOUR_TEXT&numberToDial=YOUR_PHONE_NUMBER">Send SMS</a>

Using jQuery it looks like:

1
2
3
4
5
6
7
8
9
$.ajax({
   url: "https://tropo.developergarden.com/api/sessions",
   data: {
      "numberToDial": "YOUR_PHONE_NUMBER",
      "message": "YOUR_TEXT",
      "token": "YOUR_TOKEN",
      "method": " TEXT_OR_VOICE"
   }
});

And it seems this will become even easier, because the Tropo Scripting API is on its way. Stay tuned…

One thought on “Generic SMS and Voice service, Telekom Tropo Part 3

Comments are closed.