Skip to content

Instantly share code, notes, and snippets.

@laurenceHR
Created February 12, 2017 07:45
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 laurenceHR/3ec0c44a2adbe0e8d9dd69cd5e48c504 to your computer and use it in GitHub Desktop.
Save laurenceHR/3ec0c44a2adbe0e8d9dd69cd5e48c504 to your computer and use it in GitHub Desktop.
Wit & Cayenne MQTT
'use strict';
const Wit = require('node-wit').Wit;
const interactive = require('node-wit').interactive;
const Cayenne = require('cayenne-mqtt');
const accessToken = 'QRV6QVSA6L...';
var cayenne_options = {
'clientId' : 'c1d7df70...',
'username' : 'f671f020...',
'password' : '6c7dbdfd...',
'debug': true
}
var Client = new Cayenne.Client(cayenne_options);
Client.connect();
var Ch4 = Client.Channel('4');
// Quickstart example
// See https://wit.ai/ar7hur/quickstart
const firstEntityValue = (entities, entity) => {
const val = entities && entities[entity] &&
Array.isArray(entities[entity]) &&
entities[entity].length > 0 &&
entities[entity][0].value
;
if (!val) {
return null;
}
return typeof val === 'object' ? val.value : val;
};
const actions = {
send(request, response) {
const {sessionId, context, entities} = request;
const {text, quickreplies} = response;
console.log('sending...', JSON.stringify(response));
},
turn({context, entities}) {
//console.log('context'); console.log(context);
//console.log('entities'); console.log(entities);
var v = firstEntityValue(entities, 'on_off');
console.log('value: ' + v);
if(v == 'on'){
Ch4.switchOn();
}
if(v == 'off'){
Ch4.switchOff();
}
return context;
},
};
const client = new Wit({accessToken, actions});
interactive(client);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment