Skip to content

Instantly share code, notes, and snippets.

@glynnbird
Last active March 24, 2017 19:41
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 glynnbird/76c2840a302de71d6196b529c8919151 to your computer and use it in GitHub Desktop.
Save glynnbird/76c2840a302de71d6196b529c8919151 to your computer and use it in GitHub Desktop.
Alexa Skill to fetch latest house temperature reading from Cloudant
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