Skip to content

Instantly share code, notes, and snippets.

@manekinekko
Created February 13, 2018 16:59
Show Gist options
  • Select an option

  • Save manekinekko/75f71b59e30fff69bc9979005c80a557 to your computer and use it in GitHub Desktop.

Select an option

Save manekinekko/75f71b59e30fff69bc9979005c80a557 to your computer and use it in GitHub Desktop.
'use strict';
// ///////////////////////// IMPORT /////////////////////////
const functions = require('firebase-functions');
const DialogflowApp = require('actions-on-google').DialogflowApp;
const api = require('node-fetch');
// ///////////////////////// LOGIC /////////////////////////
function getIssPosition() {
return getIssData()
.then(body => api(`https://api.wheretheiss.at/v1/coordinates/${body.latitude},${body.longitude}`))
.then(res => res.json());
}
function getIssData() {
return api('https://api.wheretheiss.at/v1/satellites/25544')
.then(res => res.json());
}
function location(app) {
getIssPosition()
.then(body => {
if (body && body.timezone_id) {
app.tell(`Je suis acctuellement ${body.timezone_id}`);
}
else {
app.tell(`Ma position terrestre actuelle indique que je suis au dessus de l'océan.`);
}
})
.catch(error => {
app.tell(`Désolé, je n'arrive pas à interroger la station. Ressaye dans quelques minutes.`);
});
}
// //////////////////// CLOUD FUNCTION ////////////////////
exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
const app = new DialogflowApp({request, response});
const actions = new Map();
actions.set('com.sfeir.iss.location', location);
app.handleRequest(actions);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment