Posted on by 1 comment

LinkedInGoogle+TwitterFacebook

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…

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

  1. Trent

    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);
    }

    Reply

Leave a Reply

  • (will not be published)