public
Created

Dial-O-spresso

  • Download Gist
coffee.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
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);
}
});
package.json
JSON
1 2 3 4 5 6 7 8 9 10
{
"name": "dial-o-spresso",
"author" : "@askpete",
"version": "0.0.1",
"dependencies": {
"express": "2.x",
"ninja-blocks" : "latest",
"twilio" : "latest"
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.