Skip to content

Instantly share code, notes, and snippets.

@anandprabhakar0507
Forked from pkarthikr/index.js
Created August 4, 2019 17:25
Show Gist options
  • Select an option

  • Save anandprabhakar0507/17b7b8e605e01d729e60484b15e05c31 to your computer and use it in GitHub Desktop.

Select an option

Save anandprabhakar0507/17b7b8e605e01d729e60484b15e05c31 to your computer and use it in GitHub Desktop.

Revisions

  1. @pkarthikr pkarthikr created this gist Jan 25, 2019.
    87 changes: 87 additions & 0 deletions index.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,87 @@
    // See https://github.com/dialogflow/dialogflow-fulfillment-nodejs
    // for Dialogflow fulfillment library docs, samples, and to report issues
    'use strict';

    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

    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(`Welcome to my agent!`);
    }

    function fallback(agent) {
    agent.add(`I didn't understand`);
    agent.add(`I'm sorry, can you try again?`);
    }

    function random(agent) {
    console.log("We are in webhook demo");
    var number = Math.floor(Math.random() * 6) + 1;
    agent.add(`Here's your random number `+number);
    }

    function booktickets(agent) {
    const city = agent.parameters['city'];
    const name = agent.parameters['name'];

    const cityLength = city.length > 0;
    const nameLength = name.length > 0;

    if(cityLength && nameLength){
    agent.add(`I got your city and name`);
    } else if(!cityLength && nameLength){
    agent.add(`I got your name but not your city`);
    } else if(cityLength && !nameLength){
    agent.add(`I got your city but not your name`);
    } else {
    agent.add(`Did not get anything! What is your name and city?`);
    }
    }

    // // Uncomment and edit to make your own intent handler
    // // uncomment `intentMap.set('your intent name here', yourFunctionHandler);`
    // // below to get this function to be run when a Dialogflow intent is matched
    // function yourFunctionHandler(agent) {
    // agent.add(`This message is from Dialogflow's Cloud Functions for Firebase editor!`);
    // agent.add(new Card({
    // title: `Title: this is a card title`,
    // imageUrl: 'https://developers.google.com/actions/images/badges/XPM_BADGING_GoogleAssistant_VER.png',
    // text: `This is the body text of a card. You can even use line\n breaks and emoji! 💁`,
    // buttonText: 'This is a button',
    // buttonUrl: 'https://assistant.google.com/'
    // })
    // );
    // agent.add(new Suggestion(`Quick Reply`));
    // agent.add(new Suggestion(`Suggestion`));
    // agent.setContext({ name: 'weather', lifespan: 2, parameters: { city: 'Rome' }});
    // }

    // // Uncomment and edit to make your own Google Assistant intent handler
    // // uncomment `intentMap.set('your intent name here', googleAssistantHandler);`
    // // below to get this function to be run when a Dialogflow intent is matched
    // function googleAssistantHandler(agent) {
    // let conv = agent.conv(); // Get Actions on Google library conv instance
    // conv.ask('Hello from the Actions on Google client library!') // Use Actions on Google library
    // agent.add(conv); // Add Actions on Google library responses to your agent's response
    // }
    // // See https://github.com/dialogflow/dialogflow-fulfillment-nodejs/tree/master/samples/actions-on-google
    // // for a complete Dialogflow fulfillment library Actions on Google client library v2 integration sample

    // Run the proper function handler based on the matched Dialogflow intent name
    let intentMap = new Map();
    intentMap.set('Default Welcome Intent', welcome);
    intentMap.set('Default Fallback Intent', fallback);
    intentMap.set('random',random);
    intentMap.set('booktickets',booktickets);
    // intentMap.set('your intent name here', yourFunctionHandler);
    // intentMap.set('your intent name here', googleAssistantHandler);
    agent.handleRequest(intentMap);
    });