Last active
March 24, 2017 19:41
-
-
Save glynnbird/76c2840a302de71d6196b529c8919151 to your computer and use it in GitHub Desktop.
Alexa Skill to fetch latest house temperature reading from Cloudant
This file contains hidden or 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
var main = function(msg) { | |
// check for mandatory parameters | |
if (!msg.url || !msg.dbname) { | |
return new Error('url and dbname are required'); | |
} | |
// form Cloudant URL | |
var url = msg.url + '/' + msg.dbname + '/_design/fetch/_view/byDate?limit=1&reduce=false&descending=true'; | |
var request = require('request'); | |
// return a Promise | |
return new Promise(function(resolve, reject) { | |
// fetch the latest reading | |
request.get(url, function(err, res, body) { | |
if (err) { | |
return reject(err); | |
} | |
body = JSON.parse(body); | |
// formulate the response | |
var response = { | |
version: '1.0', | |
response: { | |
shouldEndSession: true, | |
outputSpeech: { | |
type: 'PlainText', | |
text: 'The temperature is ' + body.rows[0].value + ' degrees Celcius.' | |
} | |
} | |
}; | |
var reply = { | |
statusCode: 200, | |
headers: { 'Content-Type': 'application/json' }, | |
body: new Buffer(JSON.stringify(response)).toString('base64') | |
}; | |
return resolve(reply); | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment