Skip to content

Instantly share code, notes, and snippets.

@askpete
Created October 22, 2012 09:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save askpete/3930720 to your computer and use it in GitHub Desktop.
Save askpete/3930720 to your computer and use it in GitHub Desktop.
Dial-O-spresso
var ninjaBlocks = require('ninja-blocks');
// IMPORTANT - express 2.x must be availailable for the node-twilio lib to work
var TwilioClient = require('twilio').Client,
Twiml = require('twilio').Twiml;
// Instantiate a Ninja Blocks client - get your _user_access_token from a.ninja.is/hacking
var ninja = ninjaBlocks.app({user_access_token:'YOUR_API_TOKEN'});
// Instantiate a Twilio client and phone number - note the host must be publicly accessible
var client = new TwilioClient('TWILIO_SID', 'TWILIO_TOKEN', 'HOSTNAME');
var phone = client.getPhoneNumber('+14158309667');
var STR = {
POWER : 'Power',
SHORT : 'Espresso',
LONG : 'Long Black'
}
// Fetch our ninja devices
ninja.devices(function(err,devices) {
if (err) {
throw 'Error fetching devices: "'+ err.error +'"'
} else {
// Create a quick map of device name to guid and check that the required relays exist
var guids = {};
Object.keys(devices).forEach(function(guid) {
guids[devices[guid].shortName] = guid;
});
if (!guids[STR.POWER] || !guids[STR.SHOT] || !guids[STR.LONG]) {
throw 'ERROR: You must have relays named "'+STR.POWER+'", "'+STR.SHORT+'", and "'+STR.LONG+'"'
}
phone.setup(function(){
// Listen for incoming calls
phone.on('incomingCall', function(reqParams,res){
res.append(new Twiml.Say('Hi there . . . Coffee is delicious! . . . Would you like a cup?'));
res.append(new Twiml.Pause());
// Gather a response from callers (times out after 5 seconds)
var gather = new Twiml.Gather(new Twiml.Say('Press 1 for Espresso, Press 2 for Long Black'));
gather.on('gathered',function(gParams,gRes){
var choice = "never enough";
if (gParams.Digits == 1){
coffee.pour(STR.SHORT);
} else if (gParams.Digits == 2) {
coffee.pour(STR.LONG);
} else {
choice = "bad for you";
}
// send positive reinforcement
gRes.append(new Twiml.Say('Good choice. Too much coffee is '+choice)).send();
});
// send twiml response to twilio
res.append(gather).send();
});
})
var coffee = {
// using a relay to do a button press requires the relay to be turned on (press
// down) and turned off (relase). This machine goes into programming mode if the
// button is held too long, and doesn't trigger if not pressed long enough
pressButton : function(relayName, delay) {
ninja.device(guids[relayName]).actuate('1');
setTimeout(function(){ninja.device(guids[relayName]).actuate('0')},delay);
},
pour : function(coffeeType){
// Power up machine with a short press of the power button
coffee.pressButton(STR.POWER,1000);
// wait 5 seconds for machine to start then press appropriate coffee button
setTimeout(function(){
coffee.pressButton(coffeeType,1000)}
,5000);
// wait 20 seconds then power down machine with a long press
setTimeout(function(){
coffee.pressButton(STR.POWER,5000)}
,25000);
}
};
// Test pour on start
// coffee.pour(STR.SHORT);
}
});
{
"name": "dial-o-spresso",
"author" : "@askpete",
"version": "0.0.1",
"dependencies": {
"express": "2.x",
"ninja-blocks" : "latest",
"twilio" : "latest"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment