Posts Tagged ‘JavaScript’

Node.JS Magic 8 Ball Voice App

Friday, December 23rd, 2011

Remember the Magic 8 Ball game from yesteryear?

How would you like to have the game with you in your pocket when difficult answers are required.

I built this Magic 8 Ball game using Node.JS and the Tropo WebAPI. If you roll the array up to a single line of code, you’re only looking at a 25-line Node.JS application!

You can call this application at 415-889-8684!

Here is the Node.JS source code that runs the application.

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

// http://en.wikipedia.org/wiki/Magic_8-ball
var answers = [
"It is certain",
"It is decidedly so",
"Without a doubt",
"Yes – definitely",
"You may rely on it",
"As I see it, yes",
"Most likely",
"Outlook good",
"Signs point to yes",
"Yes",
"Reply hazy, try again",
"Ask again later",
"Better not tell you now",
"Cannot predict now",
"Concentrate and ask again",
"Don't count on it",
"My reply is no",
"My sources say no",
"Outlook not so good",
"Very doubtful"
];

http.createServer(function (req, res) {

	// Create a new instance of the TropoWebAPI object.
	var tropo = new tropowebapi.TropoWebAPI(); 	
	var answer = answers[Math.floor(Math.random()*answers.length)];

	tropo.say("Welcome to the tropo node J S magic 8 ball.");

	var say = new Say("Ask your question now after the beep.");
    var choices = new Choices(null, null, "#");

    // Action classes can be passed as parameters to TropoWebAPI class methods.
    // use the record method https://www.tropo.com/docs/webapi/record.htm
    tropo.record(3, false, null, choices, "audio/wav", 5, 60, null, null, "recording", null, say, 5, null, "http://example.com/tropo", null, null);

    tropo.say(answer);

	tropo.say("Please call back to play again!");

    res.writeHead(200, {'Content-Type': 'application/json'}); 
    res.end(tropowebapi.TropoJSON(tropo));

}).listen(13188);

Be careful what you ask for…

Dial and SMS your APIs with Tropo

Thursday, December 22nd, 2011

The number of APIs available are increasing by the day and so is the popularity of Node.JS, the server-side event-driven javascript framework. This got me thinking… How cool would it be to be able to call or sms an API with your phone using Node.JS?

This is a pretty simple thing to do with Tropo and Nodester. Tropo has an NPM module available for adding Voice, SMS, and Instant Messaging to Node.JS applications and Nodester has REST APIs available to report on the status of the platform. 25 lines of Node.JS code later, we now have an Node.JS application using the Tropo API to dial and sms the API to retrieve the status of the platform.

Here’s the phone number: (480) 428-8723 to call or SMS.

Install the Tropo WebAPI and Mikeal Rogers’ Request NPM modules:

npm install tropo-webapi -g
npm install request -g

Run this code on localhost or deploy it to a Node.JS hosting service like Nodester.

var http = require('http');
var tropowebapi = require('tropo-webapi');
var request = require('request');

http.createServer(function (req, res) {

	// Create a new instance of the TropoWebAPI object.
	var tropo = new tropowebapi.TropoWebAPI(); 	

	// Fetch Nodester status -> http://api.nodester.com/status
	// returns {"status":"up","appshosted":2878,"appsrunning":1759}
	request('http://api.nodester.com/status', function (error, response, body) {
	     if (!error && response.statusCode == 200) {

	       // console.log(body) // Print status
	       var statusreq = JSON.parse(body);

	       tropo.say('nodester is ' + statusreq["status"] + ' and hosting ' + statusreq["appshosted"] + ' applications with '+ statusreq["appsrunning"] + ' running at the moment ');

	       res.writeHead(200, {'Content-Type': 'application/json'}); 
           res.end(tropowebapi.TropoJSON(tropo));
	     }
	   })

}).listen(13151);

One of the really cool things about the Tropo API is that you can write your app one time and it automatically runs on multiple communications channels such as: Voice, SMS, and Instant Message.

LA .NET Hackathon 2011

Monday, December 5th, 2011

Tropo is partnering with the LA .NET Developers Group and Outlook Amusements to sponsor this weekend’s LA (Burbank) Hackathon at Outlook Amusements on Saturday, December 10, 2011 from 9:00 AM to 6:30 PM (PT).

Here’s the address: Outlook Amusements 2900 W. Alameda Ave suite 400 Burbank, CA 91505

The theme of the LA Hackathon is “Build Voice/SMS apps for Holidays”! Here are a few ideas to get you thinking:

  • Santa Caller (similar to http://santacall.us) – Build a website that allows a parent to schedule a call to their kids from Santa. Have Santa ask the kid what they would like for Christmas and then email the parents their kid’s recorded message and/or transcribe the message for the email.
  • Santa Tag (similar to http://www.bloggingbistro.com/jc-penney-comes-up-with-a-new-use-for-qr-codes-video/) – Build a website that calls someone to record a message then associates the message with a QR code that plays when scanned.
  • Holiday Greetings Hotline – Build a holiday greetings hot line where user can leave a voice message that will be transcribed into text and posted to “Holiday Greetings” twitter account.

Even though this event is sponsored by the .NET Developers Group, we will be supporting all development languages and will have Tropo expertise onsite in .NET, PHP, Ruby/Rails, and Javascript, and Node.JS!

Register today!

Announcing Phono Mobile

Friday, July 22nd, 2011

If you haven’t yet played with Phono, our in-browser softphone and messaging API, now you have a new excuse to try it. Phono is a jQuery plugin that adds voice over IP and real-time messaging to any browser with only a few lines of JavaScript. Now it’s available for mobile devices, too.

We’ve built native iOS and Android media plugins and teamed with PhoneGap so that any developer can easily add crystal clear HD voice and two way chat to their iPhone, iPod, and Android applications. Phono’s wideband audio makes Phono-to-Phono callers sound like they’re in the same room and it works both on the desktop and on mobile devices.

Oh, did we mention it’s all open source?

Over on the Phono blog, we’ve got some ideas on what you can build.

PhoneGap also provides tools for packaging HTML5 code as a native mobile app for inclusion in the Apple App Store or Android Marketplace. Phono Mobile integrates seamlessly with PhoneGap to add voice over Internet (VoIP) calling, IM and real-time messaging to any HTML5 application!

  • Games – Mobile game developers can now easily add in-game audio conferencing and chat to their titles.
  • Social Apps – Since Phono makes calls over the Internet, social apps can connect people without exchanging phone numbers or using the caller’s airtime minutes. Call anywhere in the world without long distance charges.
  • Collaboration – Whiteboard and screen sharing apps can instantly add audio and chat with just a few lines of code
  • Click-to-Call – Dial any phone number directly from your application or web page – think Salesforce, LinkedIn, etc.
  • Customer Service – Combine Phono Mobile with Tropo’s Cloud Communications API to create voice-enabled self-service applications which can include: speech recognition and text-to-speech in 24 languages, conferencing, call recording, voice transcription, and call transfers.

The Phono site also has some great demos and sample code, both for mobile applications and for adding native voice and messaging to your browser. Your Tropo username and password works over on the Phono site, so join the nearly 10,000 developers who are already building with Phono.

JavaScript Remote Call Center Solution: Part 3

Monday, March 28th, 2011

As promised in a previous post, this entry will conclude a series we started over on the Phono blog discussing an effort to build a remote call center solution in JavaScript.

In the latest post of that series, we used Asterisk (the open source telephony platform) to make a call to a remote agent with a Phono instance open in their browser. Our call was routed to the remote Phono instance courtesy of Node.js and CouchDB.

In the screncast below, I’ll talk about how you can run an Asterisk AGI application with the Tropo platform.

In the JavaScript remote call center solution we’ve been talking about in this series, our Asterisk FastAGI script is written in Node.js.

Regardless of the language or framework used, you can run your AGI solution through Tropo using AGItate.

For detailed instruction on how to set up and use AGItate to run your Asterisk AGI application on the Tropo platform, check out this earlier post.

So, if you are looking to start a remote call center business, or if you want to work on getting a solution in place for your company, there has never been an easier way to get started.

Tropo and Phono coupled with powerful tools like CouchDB, Node,js and Asterisk can form the basis for a sophisticated call center application.

Any one of the components discussed in this series can be run either in your own data center, or hosted remotely in the cloud. The choice of how to deploy and operate a solution of this type is completely up to you.

Make sure you visit the GitHub repository for this solution – fork it, modify it, make it your own.

Drop us a line if we can help, or just send a pull request.

We’re excited to see what developers can do with this powerful combination of technologies!

Conference Call Apps Made Easy with Tropo

Wednesday, March 16th, 2011

Tropo makes it super simple to create a conference call application.

In just a few lines of code, you can have a functional conference call application that you can use with Skype, SIP or a PSTN number.

Creating an app is as simple as this:

// Welcome the caller to the conference.
say("Welcome to the conference line. Press the star key to exit.");

// Start the conference.
conference("1337", {
    terminator: "*",
    playTones: true
    });

// Play a message when the caller leaves the conference.
say("Thanks for calling the conference line. Goodbye.");

A conference call application – in JavaScript! How easy is that?

But it gets even better!

Tropo also makes it super simple to record a conference call if your use case requires it. With just a few extra lines of code (and a small bit of server side logic) you can record conference calls.

Again, in JavaScript:

// Start recording the conference call.
startCallRecording("http://url-for-your-server/record.php", { format: "audio/mp3" });

// Welcome the caller to the conference.
say("Welcome to the conference line. Press the star key to exit.");

// Start the conference.
conference("1337", {
		terminator : "*",
		playTones : true
	});

// Play a message when the caller leaves the conference.
say("Thanks for calling the conference line. Goodbye.");

// Stop call recording.
stopCallRecording();

Using the Tropo startCallRecording() method, you can designate where your recorded audio file is sent and the format of the audio file. Saving the audio files on the server side can be done with a trivial amount of code.

For example, in PHP:

<?php

// Audio files saved to a parallel directory called 'recordings'

$target_path = 'recordings/' . $_FILES['filename']['name'];
move_uploaded_file($_FILES['filename']['tmp_name'], $target_path);

?>

With Tropo, it’s never been easier or more efficient to create powerful conference call applications in JavaScript, or any of the other languages support by the Tropo Scripting platform.

Happy conferencing!

$40,000 in prizes up for grabs. Win with your source code.

Saturday, January 29th, 2011

Binpress is a new startup that’s built a marketplace for code. Sell your code, buy code from others. Whether you have a content management system, a plugin for WordPress or Drupal, or a great new UI widget built with jQuery, Binpress helps you market and sell your code. They take care of the license agreements, the ecommerce, and all the hassles associated with selling software.

To kick off their marketplace, they’re running a developer contest with $40,000 in prizes. They came to Tropo, wondering if we might be interested in providing a prize for the best code in one of the languages they support: PHP, Ruby, Javascript, Java, Python, or ASP.net.

Hmm, that list of languages looks pretty familiar. Tropo lets you host your code in PHP, Ruby, Javascript, Groovy (a Java-based scripting language), or Python. We have code libraries for our REST and WebAPI platforms using .Net, PHP, Ruby, Python, and Node.js.

We jumped right in and offered to sponsor not one language, but all of them. While we’re at it, we’re kicking in prizes for the rest of the contest, too.

Build the best app in one of Binpress’s languages and Binpress will give you $350 and a $100 Tropo credit for production usage. Win the Grand Prize — the best of show, as it were — and they’ll give you $1000 in Tropo credit. The second and third place winners will get $500 and $250 in Tropo credit, respectively.

There’s a whole lot of prizes totaling over $40,000, including service vouchers from Amazon Web Services, Media Temple, Sendgrid, Github, and Zencoder, cash from Conduit and Microsoft, and books from O’Reilly and Wrox.

Go jump in and build something awesome. You’ve got a month to create your masterpiece and win.

Evented Communication Apps with Tropo and Node.js

Tuesday, December 21st, 2010

Here at Tropo, we understand the need for speed.

Consider that in the last few weeks we’ve written one, two, three, four blog posts covering different ways to leverage the unique nature of our scripting platform to build blazing fast, real-time communication apps.

And the promise of speed is one of the things that has us so excited about Node.js – the server side framework for writing JavaScript applications. Node.js does not block

We’re constantly adding new features to our Node.js library for building Tropo applications. Just this week we added support for the new Tropo REST APIs that allow you to provision phone/SMS numbers and IM addresses for your apps, and for injecting events into running Tropo apps.

And we’re also working on other goodies to make it easy for Node.js and JavaScript developers to build cutting edge, multi-channel communication apps. You’ll be hearing more about these in the weeks ahead, in future blog posts.

Learning to Node

Node.js is a good candidate for applications that have high concurrency or low latency requirements because of one of it’s chief characteristics – it’s non blocking. When you ask Node.js to perform an operation, it does not wait for the completion of that operation before executing subsequent instructions. This lets you write more efficient (faster) applications because you don’t have to wait for the completion of a specific operation before doing something else.

Node.js is also event-based rather than thread-based. This makes it different from other web technologies (like Apache) that spawn new threads to handle concurrent connections. Node.js uses an event loop instead of threading, which provides a much more efficient approach to concurrency. This makes Node.js particularly well suited for applications that have lots of connections that are non-trivial in length – like real-time applications.

There are add-ons and libraries for other languages that provide a similar construct to building event-based apps; Twisted in Python, EventMachine in Ruby or AnyEvent in Perl.

With Node.js you don’t need to use an external library – events are baked in. (more…)

Real-Time Communications with Tropo and Node.js

Friday, December 17th, 2010

The other day, Jason Goecke wrote an awesome post about using Tropo with WebSockets.

I was inspired by Jason’s work to try and replicate it with a slightly different kind of technology. I also wanted to use Node.js.

I had come across the very cool faye Node.js library a while ago and wanted to try it out on a small project. This project seemed like the perfect fit.

Having now used faye for this project, I can tell you that I am amazed at how easy it is to use, and how powerful it is.

var faye = require('faye'), sys = require('sys'),
      http = require('http'), port = process.ARGV[2] || '8000';

var bayeux  = new faye.NodeAdapter({ mount: '/tropo' });

var server = http.createServer(function(request, response) {
  response.writeHead(200, {'Content-Type': 'text/plain'});
  response.write('Move along. Nothing to see here.');
  response.end();
});

bayeux.attach(server);
sys.puts('Server listening on port ' + port + "...");
server.listen(port);    

Examine the code for the Bayeux server used in this example above – in less than 15 lines of code I was able to create a server that can act as a real-time DTMF relay between a phone application running in the Tropo cloud, and multiple clients who can consume that input. How awesome is that!

(Note – all of the code used in this example is available on GitHub.)

To provide a bit more clarity and some contrast to the approach Jason used in his post, let’s talk about the Bayeux protocol and Comet servers.

Comet is really an overarching term used to describe push technologies in web browsers. Bayeux is one protocol for implementing a Comet server. From the spec:

Bayeux is a protocol for transporting asynchronous messages (primarily over HTTP), with low latency between a web server and a web client. The messages are routed via named channels and can be delivered:

  • server to client
  • client to server
  • client to client (via the server)

Bayeux seeks to reduce the complexity of developing Comet web applications by allowing implementors to more easily interoperate, to solve common message distribution and routing problems, and to provide mechanisms for incremental improvements and extensions.

As Jason described in his post, the WebSocket spec is still in flux and will likely change going forward. How things shake out with the WebSockets spec and where Comet technoloiges fit into the mix is still the subject of some debate.

That aside, it is worth noting that you can use either technology with Tropo, and other real-time technologies as well.

With Tropo, there are a number of options available for building real-time applications across multiple channels.

Naked Node.JS

Tuesday, December 14th, 2010

Mark Headd recorded an awesome screencast on getting Tropo running on Node.js using the Tropo Node.js library.  While libraries make code easier to write, I wanted to see what was happening under the covers when writing a Tropo application using Node.js without any magic.

This experiment was easier that you may think!  Since Tropo speaks JSON natively, all you need to do is spin up a node.js server like the hello world demo on the node.js home page. Next substitute “application/json” for Content-Type and send Tropo JSON text in place of hello world as shown below:

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'application/json'});
  res.end('{"tropo":[{"say":[{"value":"This call is running from node J.S. Have a nice day. Goodbye."}]},{"hangup":null}]}');
}).listen(3000, "127.0.0.1");
console.log('Server running at http://127.0.0.1:3000/');

Next, deploy this script to your node.js hosting provider or run Tunnlr as demonstrated by Mark and create a new Tropo WebAPI application pointing at the deployed URL.  Now call your application and listen to Node.js in action.

There you have it – Naked Node.JS!