Created
February 13, 2018 16:59
-
-
Save manekinekko/75f71b59e30fff69bc9979005c80a557 to your computer and use it in GitHub Desktop.
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
| '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