Created
February 12, 2017 07:45
-
-
Save laurenceHR/3ec0c44a2adbe0e8d9dd69cd5e48c504 to your computer and use it in GitHub Desktop.
Wit & Cayenne MQTT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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