# Final Code

1.Click on `fulfilment` on left menu

2.In the `Inline Editor(Powered by Cloud Functions for Firebase)` you can add the final code below

3\. Click the `deploy` button.

<div align="left"><img src="https://716139281-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LH94nRI9twh0ULEzq9v%2F-LHlVFaQSSgzGBb_aV9L%2F-LHlVd6VrVQDUO_xpx8u%2FScreen%20Shot%202018-07-19%20at%2008.47.27.png?alt=media&#x26;token=858a624d-35b7-43a5-bf6e-ea4486a8f2b3" alt="Firebase deploy button"></div>

{% hint style="info" %}
**IMPORTANT**: **Make sure you have your own Database URL, instead of**: [`https://gdg-cloud-fc257.firebaseio.com/`](https://gdg-cloud-fc257.firebaseio.com/)&#x20;
{% endhint %}

{% tabs %}
{% tab title="index.js" %}

```javascript
const functions = require(firebase-functions); 
const { WebhookClient } = require(dialogflow-fulfillment); 
const { Card, Suggestion } = require(dialogflow-fulfillment);
process.env.DEBUG = 'dialogflow:debug'; 
// enables lib debugging statements
var admin = require(firebase-admin); 
  admin.initializeApp({ databaseURL: `https://gdg-cloud-fc257.firebaseio.com/`
});
var db = admin.database();
process.env.DEBUG = "dialogflow:debug"; // enables lib debugging statements
exports.dialogflowFirebaseFulfillment = functions.https.onRequest( (request, response) => { const agent = new WebhookClient({ request, response }); console.log( "Dialogflow Request headers: " + JSON.stringify(request.headers) ); console.log("Dialogflow Request body: " + JSON.stringify(request.body));
function welcome(agent) {
  agent.add(`Hello! Welcome to the GDG Cloud directory conversational app. Would you like to find a lead by 1. Name,  2. Skill`);
}

function fallback(agent) {
  agent.add(`Sorry but could you say that one more time?`);
  agent.add(`I missed what you said. Say it again?`);
}

function getSearchTerm(agent) {
  if (agent.parameters.search_terms == 1) {
    agent.add(`What's the GDG Cloud lead's name?`);
  } else {
    agent.add(`Which skill are you looking for?`);
  }
}

function searchName(agent) {
  var results = db.ref("myDatabase");
  results
    .orderByChild("Name")
    .startAt(agent.parameters.person_name)
    .endAt(`${agent.parameters.person_name}~`)
    .on("value", function(snapshot) {
      snapshot.forEach(function(data) {
          console.log(`The GDG Cloud lead's email is ${data.val().Email}`);
        agent.add(`The GDG Cloud lead's email is ${data.val().Email}`);
      });
    });
}
​
function searchSkill(agent) {
  var scoresRef = db.ref("myDatabase");
  scoresRef
    .orderByChild("Skill")
    .equalTo(agent.parameters.person_skill)
    .on("value", function(snapshot) {
      snapshot.forEach(function(data) {
        agent.add(
          "The GDG Cloud lead expert at " +
            data.val().Skill +
            " is " +
            data.val().Name
        );
      });
    });
}
​
let intentMap = new Map();
intentMap.set('Default Welcome Intent', welcome);
intentMap.set('Default Fallback Intent', fallback);
intentMap.set("Default Welcome Intent - search", getSearchTerm);
intentMap.set("Default Welcome Intent - search - get_name", searchName);
intentMap.set("Default Welcome Intent - search - get_skill", searchSkill);
agent.handleRequest(intentMap);
} );
```

{% endtab %}

{% tab title="package.json" %}

```javascript
{ "name": "dialogflowFirebaseFulfillment", "description": "This is the default fulfillment for a Dialogflow agents using Cloud Functions for Firebase", "version": "0.0.1", "private": true, "license": "Apache Version 2.0", "author": "Google Inc.", "engines": { "node": "~6.0" }, "scripts": { "start": "firebase serve --only functions:dialogflowFirebaseFulfillment", "deploy": "firebase deploy --only functions:dialogflowFirebaseFulfillment" }, "dependencies": { "actions-on-google": "2.0.0-alpha.4", "firebase-admin": "^5.4.2", "firebase-functions": "^0.5.7", "dialogflow": "^0.1.0", "dialogflow-fulfillment": "0.3.0-beta.3" }}
```

{% endtab %}
{% endtabs %}

And the `package.json` tab:

{% code title="package.json" %}

```javascript
{ "name": "dialogflowFirebaseFulfillment", "description": "This is the default fulfillment for a Dialogflow agents using Cloud Functions for Firebase", "version": "0.0.1", "private": true, "license": "Apache Version 2.0", "author": "Google Inc.", "engines": { "node": "~6.0" }, "scripts": { "start": "firebase serve --only functions:dialogflowFirebaseFulfillment", "deploy": "firebase deploy --only functions:dialogflowFirebaseFulfillment" }, "dependencies": { "actions-on-google": "2.0.0-alpha.4", "firebase-admin": "^5.4.2", "firebase-functions": "^0.5.7", "dialogflow": "^0.1.0", "dialogflow-fulfillment": "0.3.0-beta.3" }}
```

{% endcode %}
