Automating the Nespresso Coffee Machine part 2

In this part I explain how to hook up api.ai agent with particle using hook.io as middleware.

Hook.io will get the invocation call from api.ai and acts based on the action and action parameters by calling the correct function on the particle cloud. The responding with the api.io payload to be displayed to the requester.

The first step is building a new hook on hook.io and pasting the following script, modify the access token and device id.


module['exports'] = function coffeeNator (hook) {
 var myResponse;
 var Particle = require('particle-api-js');
var particle = new Particle();

 function output(data) {
 hook.res.end(JSON.stringify(data, true, 2));
 }

 var token = 'YOUR TOKEN';

 console.log(hook.params.result.parameters.action + " coffee request recieved");

 if(hook.params.result.parameters.action=="warm")
 {
 warmCoffeeMachine();

 var fnPr = particle.callFunction({ deviceId: 'YOUR DEVICE ID', name: 'warmmachine', argument: 'D0:HIGH', auth: token });;

 fnPr.then(
 function(data) {
 console.log("success called warmcoffee succesfuly");
 // output('Function called succesfully:', data);
 myResponse =new MyResponse("Warming coffee machine for you","Coffee Machine Being Warmed","Coffee Machine");
 hook.res.writeHead(200, {"Content-Type": "application/json"});

 hook.res.end(JSON.stringify(myResponse, true, 2));

 }, function(err) {
 output('An error occurred:', err);
 });

 myResponse =new MyResponse("Warming coffee machine for you","Coffee Machine Being Warmed","Coffee Machine");

 }else if (hook.params.result.parameters.action=="make")
 {
 makeCoffeeMachine();

 var fnPr = particle.callFunction({ deviceId: 'YOUR DEVICE ID', name: 'makecoffee', argument: 'D0:HIGH', auth: token });;

 fnPr.then(
 function(data) {
 console.log("success called make coffee successfully");
 myResponse =new MyResponse("Making coffee for you","Coffee Being made ","Coffee Machine");
 hook.res.writeHead(200, {"Content-Type": "application/json"});

 hook.res.end(JSON.stringify(myResponse, true, 2));

 }, function(err) {
 output('An error occurred:', err);
 });

 myResponse =new MyResponse("Making coffee for you","Coffee Being made ","Coffee Machine");

 }

 console.log("****************end end end end end end ");

 function MyResponse(aSpeech,aDisplaytext,aSource){
 this.speech = aSpeech;
 this.displayText= aDisplaytext;
 this.source=aSource;

 }

};

function warmCoffeeMachine()
{

 console.log("machine is warming");

}
function makeCoffeeMachine()
{
 console.log("machine is making");
}

Advertisements

One thought on “Automating the Nespresso Coffee Machine part 2

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s