DEV Community

Smit Jethwa
Smit Jethwa

Posted on • Updated on

☁ Cloud Firestore with Actions on Google - Part 1/2

Hello AoG Devs!!

This is part one of Cloud Firestore with Actions on Google Series, in this part I will show you how to store data in Cloud Firestore.

Prerequisite:

  1. Knowledge of Dialogflow (Check terminologies post)
  2. Javascript

I'll recommend you to go through Cloud Firestore terminologies first(Check docs)

Cloud Firestore is a NoSQL, document-oriented database. Unlike a SQL database, there are no tables or rows. Instead, you store data in documents, which are organized into collections.
E.g.

Snapshot of Firestore

In short,

  1. Field - A data (e.g. 'capital':'New Delhi')
  2. Document - Set of Fields. (e.g. India:{'capital':'New Delhi','currency':'Rupee'})
  3. Collection - Set of Similar kinds of Documents (e.g. Country includes India, USA, Sri Lanka etc.)

I hope, you are now familiar with the terms, so let's get started with the implementation!

Step 1

Create the intent which will take the User's name and User's Email Address.

SetDetails Intent

Step 2

Set Parameter name and tick required field for both the parameters i.e. name and email

Step: 2

Step 3

Switch to Firebase Console and click on Database, select Cloud Firestore.

3.1 Give Collection name as userDetails
3.2 Create one Document.
It should look like the below image.
userDetails
Final Document with one collection and field.
userDetails

Step 4

Let's code! (Javascript)

Click on Fulfillment, Enable Inline Editor

  • import required modules.
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const admin = require('firebase-admin');
Enter fullscreen mode Exit fullscreen mode
  • Initialize Firestore
admin.initializeApp();
const db = admin.firestore();
Enter fullscreen mode Exit fullscreen mode
  • enables lib debugging statements
process.env.DEBUG = 'dialogflow:debug'; 
Enter fullscreen mode Exit fullscreen mode
  • Set the DialogflowApp object to handle the HTTPS POST request
exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
    const agent = new WebhookClient({ request, response });
Enter fullscreen mode Exit fullscreen mode
  • Create a function to store data. agent.parameters.name and agent.parameters.email returns the name and email address of the user respectively.
function getNameHandler(agent) {
    let name = agent.parameters.name;
    let email = agent.parameters.email;
    db.collection("userDetails").add({      // this method will insert the data in firestore
      name: name,       
      email: email
    });
Enter fullscreen mode Exit fullscreen mode
  • Map the intent name and functions
 let intentMap = new Map();
  intentMap.set('setDetails', getNameHandler);
  agent.handleRequest(intentMap);
Enter fullscreen mode Exit fullscreen mode

Complete Code:

'use strict';

const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const admin = require('firebase-admin');
admin.initializeApp();
const db = admin.firestore();

process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements

exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
  const agent = new WebhookClient({ request, response });

  function getNameHandler(agent) {
    let name = agent.parameters.name;
    let email = agent.parameters.email;
    db.collection("userDetails").add({ 
      name: name,
      email: email
    });
    agent.add('Data added successfully!!');
  }

  let intentMap = new Map();
  intentMap.set('setDetails', getNameHandler);
  agent.handleRequest(intentMap);
});
Enter fullscreen mode Exit fullscreen mode

Deploy the function

Step 5

Open Simulator

Enter the Test Name and Email Address.
Test Image

After the message Data added successfully!! Check Firestore. There will be one entry of the given input.
Test Image

I hope, this post will help to understand firestore with Actions on Google. This is part of a series of posts. In the next post, I'll explain how to read the data from Cloud Firestore. Till then Happy Coding!!

Read Part 2 here

Share your experience, and doubts in the comments or let's get connected with me on Twitter.

Top comments (0)