Last active
February 12, 2019 15:23
-
-
Save mandnyc/344882630a30f4eb30aef0184776f46c to your computer and use it in GitHub Desktop.
Planet Glossary Part 2.js
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
| // Copyright 2018 Google LLC.SPDX-License-Identifier: Apache-2.0 | |
| 'use strict'; | |
| const {dialogflow} = require('actions-on-google'); | |
| const functions = require('firebase-functions'); | |
| const admin = require('firebase-admin'); | |
| const app = dialogflow({debug: true}); | |
| admin.initializeApp(); | |
| const db = admin.firestore(); | |
| const collectionRef = db.collection('planets'); | |
| app.intent('ask_planet_intent', (conv, {planet}) => { | |
| const term = planet.toLowerCase(); | |
| const termRef = collectionRef.doc(`${term}`); | |
| return termRef.get() | |
| .then((snapshot) => { | |
| const {definition, word, count} = snapshot.data(); | |
| // This example should use a transaction in order to avoid race conditions. | |
| // In this example, we're keeping it simple. | |
| // To learn how to use a transaction, please go to https://firebase.google.com/docs/firestore/manage-data/transactions | |
| return termRef.update({count: count+1}) | |
| .then(() => { | |
| conv.ask(`Here you go, ${word}, ${definition}. ` + | |
| `What else do you want to know?`); | |
| }); | |
| }).catch((e) => { | |
| console.log('error:', e); | |
| conv.close('Sorry, try again and tell me another planet.'); | |
| }); | |
| }); | |
| app.intent('ask_top_three_intent', (conv, {number}) => { | |
| const topCount = +number; // convert to number | |
| return collectionRef.where('type', '==', 'planet') | |
| .orderBy('count', 'desc') | |
| .limit(topCount) | |
| .get() | |
| .then((querySnapshot) => { | |
| const text = querySnapshot.docs.map((doc) => doc.get('word')).join(', '); | |
| conv.close(`Here are the top ${topCount} planets that have been` + | |
| ` asked ${text}`); | |
| }).catch((e) => { | |
| console.log('error:', e); | |
| conv.close(`Sorry, I can't get the top ${topCount} words for you.`); | |
| }); | |
| }); | |
| exports.actionsOracle = functions.https.onRequest(app); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment