☁ Cloud Firestore with Actions on Google - Part 2/2
Tags: firestore, dialogflow, googleassistant, actionongoogle
Published: 2020-03-26 22:26:23

Hello AoG Devs!!

In this little tutorial, I’ll show you how to read information from a Cloud Firestore and use it to dynamically generate responses for DialogFlow fulfilment.

I'll recommend you read Part 1 of this post. [Click here](https://dev.to/smitjethwa/cloud-firestore-with-actions-on-google-part-1-2-406m)

Prerequisite:
1. Database with the collection and some documents.
2. Basic knowledge of Javascript

I'll take the example of "DevFest Mumbai Action" which I've built with the help of Team GDG MAD.

So without wasting the time, Let's get started with the steps!

#### Step 1:
##### Create an intent inside the Dialogflow, we'll use this intent to call the function.
I've created the _speakerInformation_ intent: This intent will read the data for a particular speaker from the database.
![Intent](https://firebasestorage.googleapis.com/v0/b/megahack-146b8.appspot.com/o/firestore-part-2%2F1.PNG?alt=media&token=ac8e44f5-2628-4d8e-a619-1ed9e49f4442 "Intent")
Here, the speaker name will be stored in the _person_ parameter. 
Don't forget to _Enable the webhook for this intent_

#### Step 2:
##### Firestore with the collection and a few documents.
The following image shows the collection _of speakers_ and documents for each speaker.
![Firestore data](https://firebasestorage.googleapis.com/v0/b/megahack-146b8.appspot.com/o/firestore-part-2%2F2.PNG?alt=media&token=b148a5ff-9da0-4fe2-b2ef-2253e669f6b8 "Firestore data")

#### Step 3:
##### Let's code!
* I've used [_ActionsSDK_](https://developers.google.com/assistant/actions/actions-sdk) in this action. So We'll import the necessary packages first.

```javascript
const {
  dialogflow,
  Image,
  BasicCard,
  Button,
} = require('actions-on-google')

const functions = require('firebase-functions')
const { firestore } = require('firebase-admin')
const domain = 'https://mumbai-devfest19.firebaseapp.com'
const app = dialogflow({ debug: false })
```

* Create a function to handle _speakerInformation_ intent.
```javascript
app.intent('speakerInformation', async (conv, param, option) => {
.
.
.
}
```

* Create a variable to store the parameter (User Input)
```javascript
  const option_intent = conv.contexts.get('actions_intent_option');
  const option_text = option_intent.parameters.text;
  const speakerRef = await firestore().collection('speakers').where('name', '==', option_text).get()
```

* Loop to visit every document. _doc_ variable will store the document.
```javascript
speakerRef.forEach((doc) => {
    const data = doc.data() // Document data stored in _data_ variable
    conv.ask(`Meet the Speaker`);
```

* Card added to display the data
```javascript
    conv.ask(new BasicCard({
          text: data.bio,   
          subtitle: data.title,
          title: data.name,
          image: new Image({
            url: data.photoUrl,
            alt: `${data.name} photo`,
          }),
          display: 'CROPPED',
          buttons: new Button({
              title: 'Visit Profile',
              url: `${domain}/speakers/${data.id}`,
          }),
    }));
```

*  exports the app
```javascript
exports.fulfilment = functions.https.onRequest(app)
```

Complete code: [Github](https://github.com/gdgmad/hoverboard/blob/google-assistant/functions/src/assistant.js#L116-L148)

Output:
![Output](https://firebasestorage.googleapis.com/v0/b/megahack-146b8.appspot.com/o/firestore-part-2%2FspeakerInfo.jpg?alt=media&token=e1086b56-b5da-4974-8dd8-f2cd3faf6c9b)

#### Want to explore [*Devfest Mumbai*](https://assistant.google.com/services/a/uid/000000b735d74491?source=web) action? Just say "_Hey Google, Talk to Devfest Mumbai_"

This is it! You can now create dynamic responses in the Dialogflow fulfilment.
Learn More: [Docs](https://firebase.google.com/docs/firestore/quickstart)


Share your experience, and doubts in the comments or connect with me on [Twitter](https://twitter.com/jethwa_smit)