Node.JS Magic 8 Ball Voice App

December 23rd, 2011 by cmatthieu

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…

Related posts:

  1. Use Node.js & Javascript to Write Your Tropo Apps
  2. Naked Node.JS
  3. Telephony Black Magic with Tropo, Node.js and Redis
  4. Get Your Node on with Tropo and Node.js
  5. Dial and SMS your APIs with Tropo

Tags: , , , , ,

One Response to “Node.JS Magic 8 Ball Voice App”

  1. Trent says:

    I did a quick follow-up with an SMS based Magic 8 Ball using Tropo. You can send a question to 435-915-6379

    Here is my hosted php code:

    $answers = array(“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”);

    $rand_keys = array_rand($answers, 2);

    $answer = $answers[$rand_keys[0]];

    if($currentCall->network == “SMS” && $numberToDial == ”){ say($answer); }

Leave a Reply

Please note: By submitting a comment you agree to comply with our Comment Policy. We welcome all comments, positive or negative, but do reserve the right to remove all or part of blog comments that do not comply with our policy.

Additionally, the first time you leave a comment on this blog, it will be held for moderation. After that first comment has been approved, future comments will be posted without delay.

Additional comments powered by BackType