Monday, 11 October 2010

Tropo Blog - Simple phone, SMS Text Messaging, IM and Twitter web service API

Today we would like to announce the availability of the Tropo WebAPI Node library for Node.js and the Tropo WebAPI. I have had a lot of fun working with the author – Mark Headd – to both grok Node.js and play with Tropo using this rising star of a framework.

For those of you new to Node.js it is a server side evented Javascript framework. If you have ever used the Python Twisted framework, or Ruby’s Eventmachine, you already have an idea of what Node.js is. You may write asynchronous apps that easily handle sockets or HTTP using Javascript on both the server and the browser.

A nice compliment to Node.js, in the context of the Tropo WebAPI, is the Express.js framework. Express is to Node.js what Sinatra is to Ruby. You may easily assign clojures to your various REST resources and serve up an application in no time. Here is an example:

/** * Showing with the Express framwork http://expressjs.com/ * Express must be installed for this sample to work */  require('../../lib/tropo-webapi'); var express = require('express'); var app = express.createServer();  /** * Required to process the HTTP body * req.body has the Object while req.rawBody has the JSON string */ app.configure(function(){ app.use(express.bodyDecoder()); });  app.post('/', function(req, res){ // Create a new instance of the TropoWebAPI object. var tropo = new TropoWebAPI(); // Use the say method https://www.tropo.com/docs/webapi/say.htm tropo.say("Welcome to my Tropo Web API node demo.");  // Demonstrates how to use the base Tropo action classes. var say = new Say("Please enter your 5 digit zip code."); var choices = new Choices("[5 DIGITS]");  // Action classes can be passes as parameters to TropoWebAPI class methods. // use the ask method https://www.tropo.com/docs/webapi/ask.htm tropo.ask(choices, 3, false, null, "foo", null, true, say, 5, null); // use the on method https://www.tropo.com/docs/webapi/on.htm tropo.on("continue", null, "/answer", true);  res.send(TropoJSON(tropo)); });  app.post('/answer', function(req, res){ // Create a new instance of the TropoWebAPI object. var tropo = new TropoWebAPI(); tropo.say("Your zip code is " + req.body['result']['actions']['interpretation']);  res.send(TropoJSON(tropo)); });  app.listen(8000); console.log('Server running on http://0.0.0.0:8000/')

So what are you waiting for? Go to Github and get started!

Stay tuned as Mark Headd cooks up more good stuff, I hear he might be doing some interesting things with CouchDB. Every developer community would be so lucky to have a member like him. Thanks Mark!