How to get fulfillment for both sets of code?












1















I'm making a chatbot on Dialogflow that can do various things (I've basically followed a few tutorials that Dialogflow has put out, re-formatted the code slightly and made some adjustments to it for my own).



The code is long, so I'll put the link to a Github below. Also, I'm using the inline editor on dialogflow



The problem/ I'm not really sure what to do is I have a user login section (lines 33 to 51) where a user can login to their google account via voice.



In the tutorials, in the fulfillment line they have (I'm almost certain this is the reason it isn't working):



exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app)


But the problem , to fulfill my other functions I need to have:



exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
..... Functions under }


I had attempted to write variations thinking that this might work:



exports.dialogflowFirebaseFulfillment = functions.https.onRequest((app, request, response)

exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app(request, response)
// Syntax error


How could I get the two halves of the code working together? Is it possible to do this or do I have to re-format this again, or do I just have to do something a little different with the fulfillment line?



Thanks for the help!



code here



Note: In the code on Github, I did not change the fulfillment line, I just left it in the form that my other functions would work. The code has been tested for the login and WORKS, it does NOT WORK with the other functions I have written.










share|improve this question



























    1















    I'm making a chatbot on Dialogflow that can do various things (I've basically followed a few tutorials that Dialogflow has put out, re-formatted the code slightly and made some adjustments to it for my own).



    The code is long, so I'll put the link to a Github below. Also, I'm using the inline editor on dialogflow



    The problem/ I'm not really sure what to do is I have a user login section (lines 33 to 51) where a user can login to their google account via voice.



    In the tutorials, in the fulfillment line they have (I'm almost certain this is the reason it isn't working):



    exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app)


    But the problem , to fulfill my other functions I need to have:



    exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
    ..... Functions under }


    I had attempted to write variations thinking that this might work:



    exports.dialogflowFirebaseFulfillment = functions.https.onRequest((app, request, response)

    exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app(request, response)
    // Syntax error


    How could I get the two halves of the code working together? Is it possible to do this or do I have to re-format this again, or do I just have to do something a little different with the fulfillment line?



    Thanks for the help!



    code here



    Note: In the code on Github, I did not change the fulfillment line, I just left it in the form that my other functions would work. The code has been tested for the login and WORKS, it does NOT WORK with the other functions I have written.










    share|improve this question

























      1












      1








      1








      I'm making a chatbot on Dialogflow that can do various things (I've basically followed a few tutorials that Dialogflow has put out, re-formatted the code slightly and made some adjustments to it for my own).



      The code is long, so I'll put the link to a Github below. Also, I'm using the inline editor on dialogflow



      The problem/ I'm not really sure what to do is I have a user login section (lines 33 to 51) where a user can login to their google account via voice.



      In the tutorials, in the fulfillment line they have (I'm almost certain this is the reason it isn't working):



      exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app)


      But the problem , to fulfill my other functions I need to have:



      exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
      ..... Functions under }


      I had attempted to write variations thinking that this might work:



      exports.dialogflowFirebaseFulfillment = functions.https.onRequest((app, request, response)

      exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app(request, response)
      // Syntax error


      How could I get the two halves of the code working together? Is it possible to do this or do I have to re-format this again, or do I just have to do something a little different with the fulfillment line?



      Thanks for the help!



      code here



      Note: In the code on Github, I did not change the fulfillment line, I just left it in the form that my other functions would work. The code has been tested for the login and WORKS, it does NOT WORK with the other functions I have written.










      share|improve this question














      I'm making a chatbot on Dialogflow that can do various things (I've basically followed a few tutorials that Dialogflow has put out, re-formatted the code slightly and made some adjustments to it for my own).



      The code is long, so I'll put the link to a Github below. Also, I'm using the inline editor on dialogflow



      The problem/ I'm not really sure what to do is I have a user login section (lines 33 to 51) where a user can login to their google account via voice.



      In the tutorials, in the fulfillment line they have (I'm almost certain this is the reason it isn't working):



      exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app)


      But the problem , to fulfill my other functions I need to have:



      exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
      ..... Functions under }


      I had attempted to write variations thinking that this might work:



      exports.dialogflowFirebaseFulfillment = functions.https.onRequest((app, request, response)

      exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app(request, response)
      // Syntax error


      How could I get the two halves of the code working together? Is it possible to do this or do I have to re-format this again, or do I just have to do something a little different with the fulfillment line?



      Thanks for the help!



      code here



      Note: In the code on Github, I did not change the fulfillment line, I just left it in the form that my other functions would work. The code has been tested for the login and WORKS, it does NOT WORK with the other functions I have written.







      javascript node.js dialogflow actions-on-google






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 27 '18 at 1:45









      Landon GLandon G

      11312




      11312
























          2 Answers
          2






          active

          oldest

          votes


















          2














          The issue is that the two libraries have different ways of setting up the listener that handles the request/response objects and setting up the intent handler registration. You can't mix the two (as you've discovered). You need to pick one and convert the functions registered the other way.



          To save myself a lot of typing, I'm going to refer to the actions-on-google library as a-o-g and the dialogflow-fulfillment library as d-f.



          Using the actions-on-google library



          You would initialize the listener with something like:



          const app = dialogflow({
          // REPLACE THE PLACEHOLDER WITH THE CLIENT_ID OF YOUR ACTIONS PROJECT
          clientId: '<CLIENT ID>',
          });

          // Intent handler declarations go here

          exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app);


          Converting the dialogflow-fulfillment-style handlers is relatively straightforward. The a-o-g conv object is fairly similar to the d-f agent object. Both, for example, have an add() method that behave the same way when dealing with Actions.



          The conv object also has a parameters property, although using the second argument in the function call is preferred, it contains the same things. Similarly, it has an arguments property which contains the same thing as the third argument passed to the handler.



          It is also worth noting that app.intent() doesn't have to have its function as an arrow function or even specified inline like that. You can write the function separately and just pass it as a parameter.



          So your makeAppointment() function might be re-written and registered as something like



            function makeAppointment (conv) {
          // Calculate appointment start and end datetimes (end = +1hr from start)
          const dateTimeStart = new Date(Date.parse(conv.parameters.date.split('T')[0] + 'T' + agent.parameters.time.split('T')[1].split('-')[0] + timeZoneOffset));
          const dateTimeEnd = new Date(new Date(dateTimeStart).setHours(dateTimeStart.getHours() + 1));
          const appointmentTimeString = dateTimeStart.toLocaleString(
          'en-US',
          { month: 'long', day: 'numeric', hour: 'numeric', timeZone: timeZone }
          );

          // Check the availibility of the time, and make an appointment if there is time on the calendar
          return createCalendarEvent(dateTimeStart, dateTimeEnd).then(() => {
          conv.add(`Okay, I have you booked for ${appointmentTimeString}!`);
          }).catch(() => {
          conv.add(`I'm sorry, there are no slots available for ${appointmentTimeString}.`);
          });
          }
          app.intent( 'Make Appointment', makeAppointment );


          Using the dialogflow-fulfillment library



          You already have the listener and handler setup this way



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

          // Declare your functions here

          let intentMap = new Map();
          // Map intent name to functions here
          agent.handleRequest(intentMap);
          });


          so the question is how to turn the handlers, which appear to have a-o-g specific information in the function arguments, into d-f compatible handlers.



          The d-f agent object has a way to get the (nearly) equivalent conv object. Unsurprisingly, it is agent.getConv(). Really. You can then use the parameters and arguments properties as noted above to get the equivalent of the second and third function arguments. You'll use agent.add() to add a message (you can use conv as well, but its a little more complicated).



          It might look something like this for the "Get Signin" handler:



          function getSignin (agent){
          let conv = agent.getConv();
          let params = conv.parameters;
          let signin = conv.arguments;
          if (signin.status === 'OK') {
          const payload = conv.user.profile.payload;
          agent.ask(`Welcome back ${payload.name}. What do you want to do next?`);
          } else {
          agent.ask(`I won't be able to save your data, but what do you want to do next?`);
          }
          }


          and then make sure you register the handler in the appropriate place



          intentMap.set('Get Signin', getSignin);





          share|improve this answer































            2














            It appears you're mixing 2 different versions of the Actions on Google library. It's recommended you stick with V2, which means changing the Intent-map to intent handlers instead.



            // Intent that starts the account linking flow.
            app.intent('Start Signin', conv => {
            conv.ask(new SignIn('To get your account details'));
            });

            // Create a Dialogflow intent with the `actions_intent_SIGN_IN` event.
            app.intent('Get Signin', (conv, params, signin) => {
            if (signin.status === 'OK') {
            const payload = conv.user.profile.payload;
            conv.ask(`Welcome back ${payload.name}. What do you want to do next?`);
            } else {
            conv.ask(`I won't be able to save your data, but what do you want to do next?`);
            }
            });

            app.intent('Make Appointment', (conv) => {
            /* some code here */
            });

            app.intent('ReadFromFirestore', (conv) => {
            // Get the database collection 'dialogflow' and document 'agent'
            const dialogflowAgentDoc = db.collection('dialogflow').doc('agent');

            // Get the value of 'entry' in the document and send it to the user
            return dialogflowAgentDoc.get().then(doc => {
            if (!doc.exists) {
            conv.tell('No data found in the database!');
            } else {
            conv.ask(doc.data().entry);
            }
            }).catch(() => {
            conv.ask('Error reading entry from the Firestore database. Please add a entry to the database first by saying, "Write <your phrase> to the database"');
            });
            });

            app.intent('WriteToFirestore', (conv) => {
            /* some code here */
            });

            exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app);


            (methods omitted)



            The "app" (dialogflow) object already wraps the request and response arguments, so there isn't anything you should do to interfere with those.



            If you want separate endpoints, outside of dialogflow, to do stuff: you can add additional functions like so:



            exports.someOtherFunction = functions.https.onRequest((request, response) => {
            /* do something with the request and response here */
            });


            These won't directly have much to do with Dialogflow, however.






            share|improve this answer

























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53491592%2fhow-to-get-fulfillment-for-both-sets-of-code%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              2














              The issue is that the two libraries have different ways of setting up the listener that handles the request/response objects and setting up the intent handler registration. You can't mix the two (as you've discovered). You need to pick one and convert the functions registered the other way.



              To save myself a lot of typing, I'm going to refer to the actions-on-google library as a-o-g and the dialogflow-fulfillment library as d-f.



              Using the actions-on-google library



              You would initialize the listener with something like:



              const app = dialogflow({
              // REPLACE THE PLACEHOLDER WITH THE CLIENT_ID OF YOUR ACTIONS PROJECT
              clientId: '<CLIENT ID>',
              });

              // Intent handler declarations go here

              exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app);


              Converting the dialogflow-fulfillment-style handlers is relatively straightforward. The a-o-g conv object is fairly similar to the d-f agent object. Both, for example, have an add() method that behave the same way when dealing with Actions.



              The conv object also has a parameters property, although using the second argument in the function call is preferred, it contains the same things. Similarly, it has an arguments property which contains the same thing as the third argument passed to the handler.



              It is also worth noting that app.intent() doesn't have to have its function as an arrow function or even specified inline like that. You can write the function separately and just pass it as a parameter.



              So your makeAppointment() function might be re-written and registered as something like



                function makeAppointment (conv) {
              // Calculate appointment start and end datetimes (end = +1hr from start)
              const dateTimeStart = new Date(Date.parse(conv.parameters.date.split('T')[0] + 'T' + agent.parameters.time.split('T')[1].split('-')[0] + timeZoneOffset));
              const dateTimeEnd = new Date(new Date(dateTimeStart).setHours(dateTimeStart.getHours() + 1));
              const appointmentTimeString = dateTimeStart.toLocaleString(
              'en-US',
              { month: 'long', day: 'numeric', hour: 'numeric', timeZone: timeZone }
              );

              // Check the availibility of the time, and make an appointment if there is time on the calendar
              return createCalendarEvent(dateTimeStart, dateTimeEnd).then(() => {
              conv.add(`Okay, I have you booked for ${appointmentTimeString}!`);
              }).catch(() => {
              conv.add(`I'm sorry, there are no slots available for ${appointmentTimeString}.`);
              });
              }
              app.intent( 'Make Appointment', makeAppointment );


              Using the dialogflow-fulfillment library



              You already have the listener and handler setup this way



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

              // Declare your functions here

              let intentMap = new Map();
              // Map intent name to functions here
              agent.handleRequest(intentMap);
              });


              so the question is how to turn the handlers, which appear to have a-o-g specific information in the function arguments, into d-f compatible handlers.



              The d-f agent object has a way to get the (nearly) equivalent conv object. Unsurprisingly, it is agent.getConv(). Really. You can then use the parameters and arguments properties as noted above to get the equivalent of the second and third function arguments. You'll use agent.add() to add a message (you can use conv as well, but its a little more complicated).



              It might look something like this for the "Get Signin" handler:



              function getSignin (agent){
              let conv = agent.getConv();
              let params = conv.parameters;
              let signin = conv.arguments;
              if (signin.status === 'OK') {
              const payload = conv.user.profile.payload;
              agent.ask(`Welcome back ${payload.name}. What do you want to do next?`);
              } else {
              agent.ask(`I won't be able to save your data, but what do you want to do next?`);
              }
              }


              and then make sure you register the handler in the appropriate place



              intentMap.set('Get Signin', getSignin);





              share|improve this answer




























                2














                The issue is that the two libraries have different ways of setting up the listener that handles the request/response objects and setting up the intent handler registration. You can't mix the two (as you've discovered). You need to pick one and convert the functions registered the other way.



                To save myself a lot of typing, I'm going to refer to the actions-on-google library as a-o-g and the dialogflow-fulfillment library as d-f.



                Using the actions-on-google library



                You would initialize the listener with something like:



                const app = dialogflow({
                // REPLACE THE PLACEHOLDER WITH THE CLIENT_ID OF YOUR ACTIONS PROJECT
                clientId: '<CLIENT ID>',
                });

                // Intent handler declarations go here

                exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app);


                Converting the dialogflow-fulfillment-style handlers is relatively straightforward. The a-o-g conv object is fairly similar to the d-f agent object. Both, for example, have an add() method that behave the same way when dealing with Actions.



                The conv object also has a parameters property, although using the second argument in the function call is preferred, it contains the same things. Similarly, it has an arguments property which contains the same thing as the third argument passed to the handler.



                It is also worth noting that app.intent() doesn't have to have its function as an arrow function or even specified inline like that. You can write the function separately and just pass it as a parameter.



                So your makeAppointment() function might be re-written and registered as something like



                  function makeAppointment (conv) {
                // Calculate appointment start and end datetimes (end = +1hr from start)
                const dateTimeStart = new Date(Date.parse(conv.parameters.date.split('T')[0] + 'T' + agent.parameters.time.split('T')[1].split('-')[0] + timeZoneOffset));
                const dateTimeEnd = new Date(new Date(dateTimeStart).setHours(dateTimeStart.getHours() + 1));
                const appointmentTimeString = dateTimeStart.toLocaleString(
                'en-US',
                { month: 'long', day: 'numeric', hour: 'numeric', timeZone: timeZone }
                );

                // Check the availibility of the time, and make an appointment if there is time on the calendar
                return createCalendarEvent(dateTimeStart, dateTimeEnd).then(() => {
                conv.add(`Okay, I have you booked for ${appointmentTimeString}!`);
                }).catch(() => {
                conv.add(`I'm sorry, there are no slots available for ${appointmentTimeString}.`);
                });
                }
                app.intent( 'Make Appointment', makeAppointment );


                Using the dialogflow-fulfillment library



                You already have the listener and handler setup this way



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

                // Declare your functions here

                let intentMap = new Map();
                // Map intent name to functions here
                agent.handleRequest(intentMap);
                });


                so the question is how to turn the handlers, which appear to have a-o-g specific information in the function arguments, into d-f compatible handlers.



                The d-f agent object has a way to get the (nearly) equivalent conv object. Unsurprisingly, it is agent.getConv(). Really. You can then use the parameters and arguments properties as noted above to get the equivalent of the second and third function arguments. You'll use agent.add() to add a message (you can use conv as well, but its a little more complicated).



                It might look something like this for the "Get Signin" handler:



                function getSignin (agent){
                let conv = agent.getConv();
                let params = conv.parameters;
                let signin = conv.arguments;
                if (signin.status === 'OK') {
                const payload = conv.user.profile.payload;
                agent.ask(`Welcome back ${payload.name}. What do you want to do next?`);
                } else {
                agent.ask(`I won't be able to save your data, but what do you want to do next?`);
                }
                }


                and then make sure you register the handler in the appropriate place



                intentMap.set('Get Signin', getSignin);





                share|improve this answer


























                  2












                  2








                  2







                  The issue is that the two libraries have different ways of setting up the listener that handles the request/response objects and setting up the intent handler registration. You can't mix the two (as you've discovered). You need to pick one and convert the functions registered the other way.



                  To save myself a lot of typing, I'm going to refer to the actions-on-google library as a-o-g and the dialogflow-fulfillment library as d-f.



                  Using the actions-on-google library



                  You would initialize the listener with something like:



                  const app = dialogflow({
                  // REPLACE THE PLACEHOLDER WITH THE CLIENT_ID OF YOUR ACTIONS PROJECT
                  clientId: '<CLIENT ID>',
                  });

                  // Intent handler declarations go here

                  exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app);


                  Converting the dialogflow-fulfillment-style handlers is relatively straightforward. The a-o-g conv object is fairly similar to the d-f agent object. Both, for example, have an add() method that behave the same way when dealing with Actions.



                  The conv object also has a parameters property, although using the second argument in the function call is preferred, it contains the same things. Similarly, it has an arguments property which contains the same thing as the third argument passed to the handler.



                  It is also worth noting that app.intent() doesn't have to have its function as an arrow function or even specified inline like that. You can write the function separately and just pass it as a parameter.



                  So your makeAppointment() function might be re-written and registered as something like



                    function makeAppointment (conv) {
                  // Calculate appointment start and end datetimes (end = +1hr from start)
                  const dateTimeStart = new Date(Date.parse(conv.parameters.date.split('T')[0] + 'T' + agent.parameters.time.split('T')[1].split('-')[0] + timeZoneOffset));
                  const dateTimeEnd = new Date(new Date(dateTimeStart).setHours(dateTimeStart.getHours() + 1));
                  const appointmentTimeString = dateTimeStart.toLocaleString(
                  'en-US',
                  { month: 'long', day: 'numeric', hour: 'numeric', timeZone: timeZone }
                  );

                  // Check the availibility of the time, and make an appointment if there is time on the calendar
                  return createCalendarEvent(dateTimeStart, dateTimeEnd).then(() => {
                  conv.add(`Okay, I have you booked for ${appointmentTimeString}!`);
                  }).catch(() => {
                  conv.add(`I'm sorry, there are no slots available for ${appointmentTimeString}.`);
                  });
                  }
                  app.intent( 'Make Appointment', makeAppointment );


                  Using the dialogflow-fulfillment library



                  You already have the listener and handler setup this way



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

                  // Declare your functions here

                  let intentMap = new Map();
                  // Map intent name to functions here
                  agent.handleRequest(intentMap);
                  });


                  so the question is how to turn the handlers, which appear to have a-o-g specific information in the function arguments, into d-f compatible handlers.



                  The d-f agent object has a way to get the (nearly) equivalent conv object. Unsurprisingly, it is agent.getConv(). Really. You can then use the parameters and arguments properties as noted above to get the equivalent of the second and third function arguments. You'll use agent.add() to add a message (you can use conv as well, but its a little more complicated).



                  It might look something like this for the "Get Signin" handler:



                  function getSignin (agent){
                  let conv = agent.getConv();
                  let params = conv.parameters;
                  let signin = conv.arguments;
                  if (signin.status === 'OK') {
                  const payload = conv.user.profile.payload;
                  agent.ask(`Welcome back ${payload.name}. What do you want to do next?`);
                  } else {
                  agent.ask(`I won't be able to save your data, but what do you want to do next?`);
                  }
                  }


                  and then make sure you register the handler in the appropriate place



                  intentMap.set('Get Signin', getSignin);





                  share|improve this answer













                  The issue is that the two libraries have different ways of setting up the listener that handles the request/response objects and setting up the intent handler registration. You can't mix the two (as you've discovered). You need to pick one and convert the functions registered the other way.



                  To save myself a lot of typing, I'm going to refer to the actions-on-google library as a-o-g and the dialogflow-fulfillment library as d-f.



                  Using the actions-on-google library



                  You would initialize the listener with something like:



                  const app = dialogflow({
                  // REPLACE THE PLACEHOLDER WITH THE CLIENT_ID OF YOUR ACTIONS PROJECT
                  clientId: '<CLIENT ID>',
                  });

                  // Intent handler declarations go here

                  exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app);


                  Converting the dialogflow-fulfillment-style handlers is relatively straightforward. The a-o-g conv object is fairly similar to the d-f agent object. Both, for example, have an add() method that behave the same way when dealing with Actions.



                  The conv object also has a parameters property, although using the second argument in the function call is preferred, it contains the same things. Similarly, it has an arguments property which contains the same thing as the third argument passed to the handler.



                  It is also worth noting that app.intent() doesn't have to have its function as an arrow function or even specified inline like that. You can write the function separately and just pass it as a parameter.



                  So your makeAppointment() function might be re-written and registered as something like



                    function makeAppointment (conv) {
                  // Calculate appointment start and end datetimes (end = +1hr from start)
                  const dateTimeStart = new Date(Date.parse(conv.parameters.date.split('T')[0] + 'T' + agent.parameters.time.split('T')[1].split('-')[0] + timeZoneOffset));
                  const dateTimeEnd = new Date(new Date(dateTimeStart).setHours(dateTimeStart.getHours() + 1));
                  const appointmentTimeString = dateTimeStart.toLocaleString(
                  'en-US',
                  { month: 'long', day: 'numeric', hour: 'numeric', timeZone: timeZone }
                  );

                  // Check the availibility of the time, and make an appointment if there is time on the calendar
                  return createCalendarEvent(dateTimeStart, dateTimeEnd).then(() => {
                  conv.add(`Okay, I have you booked for ${appointmentTimeString}!`);
                  }).catch(() => {
                  conv.add(`I'm sorry, there are no slots available for ${appointmentTimeString}.`);
                  });
                  }
                  app.intent( 'Make Appointment', makeAppointment );


                  Using the dialogflow-fulfillment library



                  You already have the listener and handler setup this way



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

                  // Declare your functions here

                  let intentMap = new Map();
                  // Map intent name to functions here
                  agent.handleRequest(intentMap);
                  });


                  so the question is how to turn the handlers, which appear to have a-o-g specific information in the function arguments, into d-f compatible handlers.



                  The d-f agent object has a way to get the (nearly) equivalent conv object. Unsurprisingly, it is agent.getConv(). Really. You can then use the parameters and arguments properties as noted above to get the equivalent of the second and third function arguments. You'll use agent.add() to add a message (you can use conv as well, but its a little more complicated).



                  It might look something like this for the "Get Signin" handler:



                  function getSignin (agent){
                  let conv = agent.getConv();
                  let params = conv.parameters;
                  let signin = conv.arguments;
                  if (signin.status === 'OK') {
                  const payload = conv.user.profile.payload;
                  agent.ask(`Welcome back ${payload.name}. What do you want to do next?`);
                  } else {
                  agent.ask(`I won't be able to save your data, but what do you want to do next?`);
                  }
                  }


                  and then make sure you register the handler in the appropriate place



                  intentMap.set('Get Signin', getSignin);






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 27 '18 at 3:13









                  PrisonerPrisoner

                  35.1k33258




                  35.1k33258

























                      2














                      It appears you're mixing 2 different versions of the Actions on Google library. It's recommended you stick with V2, which means changing the Intent-map to intent handlers instead.



                      // Intent that starts the account linking flow.
                      app.intent('Start Signin', conv => {
                      conv.ask(new SignIn('To get your account details'));
                      });

                      // Create a Dialogflow intent with the `actions_intent_SIGN_IN` event.
                      app.intent('Get Signin', (conv, params, signin) => {
                      if (signin.status === 'OK') {
                      const payload = conv.user.profile.payload;
                      conv.ask(`Welcome back ${payload.name}. What do you want to do next?`);
                      } else {
                      conv.ask(`I won't be able to save your data, but what do you want to do next?`);
                      }
                      });

                      app.intent('Make Appointment', (conv) => {
                      /* some code here */
                      });

                      app.intent('ReadFromFirestore', (conv) => {
                      // Get the database collection 'dialogflow' and document 'agent'
                      const dialogflowAgentDoc = db.collection('dialogflow').doc('agent');

                      // Get the value of 'entry' in the document and send it to the user
                      return dialogflowAgentDoc.get().then(doc => {
                      if (!doc.exists) {
                      conv.tell('No data found in the database!');
                      } else {
                      conv.ask(doc.data().entry);
                      }
                      }).catch(() => {
                      conv.ask('Error reading entry from the Firestore database. Please add a entry to the database first by saying, "Write <your phrase> to the database"');
                      });
                      });

                      app.intent('WriteToFirestore', (conv) => {
                      /* some code here */
                      });

                      exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app);


                      (methods omitted)



                      The "app" (dialogflow) object already wraps the request and response arguments, so there isn't anything you should do to interfere with those.



                      If you want separate endpoints, outside of dialogflow, to do stuff: you can add additional functions like so:



                      exports.someOtherFunction = functions.https.onRequest((request, response) => {
                      /* do something with the request and response here */
                      });


                      These won't directly have much to do with Dialogflow, however.






                      share|improve this answer






























                        2














                        It appears you're mixing 2 different versions of the Actions on Google library. It's recommended you stick with V2, which means changing the Intent-map to intent handlers instead.



                        // Intent that starts the account linking flow.
                        app.intent('Start Signin', conv => {
                        conv.ask(new SignIn('To get your account details'));
                        });

                        // Create a Dialogflow intent with the `actions_intent_SIGN_IN` event.
                        app.intent('Get Signin', (conv, params, signin) => {
                        if (signin.status === 'OK') {
                        const payload = conv.user.profile.payload;
                        conv.ask(`Welcome back ${payload.name}. What do you want to do next?`);
                        } else {
                        conv.ask(`I won't be able to save your data, but what do you want to do next?`);
                        }
                        });

                        app.intent('Make Appointment', (conv) => {
                        /* some code here */
                        });

                        app.intent('ReadFromFirestore', (conv) => {
                        // Get the database collection 'dialogflow' and document 'agent'
                        const dialogflowAgentDoc = db.collection('dialogflow').doc('agent');

                        // Get the value of 'entry' in the document and send it to the user
                        return dialogflowAgentDoc.get().then(doc => {
                        if (!doc.exists) {
                        conv.tell('No data found in the database!');
                        } else {
                        conv.ask(doc.data().entry);
                        }
                        }).catch(() => {
                        conv.ask('Error reading entry from the Firestore database. Please add a entry to the database first by saying, "Write <your phrase> to the database"');
                        });
                        });

                        app.intent('WriteToFirestore', (conv) => {
                        /* some code here */
                        });

                        exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app);


                        (methods omitted)



                        The "app" (dialogflow) object already wraps the request and response arguments, so there isn't anything you should do to interfere with those.



                        If you want separate endpoints, outside of dialogflow, to do stuff: you can add additional functions like so:



                        exports.someOtherFunction = functions.https.onRequest((request, response) => {
                        /* do something with the request and response here */
                        });


                        These won't directly have much to do with Dialogflow, however.






                        share|improve this answer




























                          2












                          2








                          2







                          It appears you're mixing 2 different versions of the Actions on Google library. It's recommended you stick with V2, which means changing the Intent-map to intent handlers instead.



                          // Intent that starts the account linking flow.
                          app.intent('Start Signin', conv => {
                          conv.ask(new SignIn('To get your account details'));
                          });

                          // Create a Dialogflow intent with the `actions_intent_SIGN_IN` event.
                          app.intent('Get Signin', (conv, params, signin) => {
                          if (signin.status === 'OK') {
                          const payload = conv.user.profile.payload;
                          conv.ask(`Welcome back ${payload.name}. What do you want to do next?`);
                          } else {
                          conv.ask(`I won't be able to save your data, but what do you want to do next?`);
                          }
                          });

                          app.intent('Make Appointment', (conv) => {
                          /* some code here */
                          });

                          app.intent('ReadFromFirestore', (conv) => {
                          // Get the database collection 'dialogflow' and document 'agent'
                          const dialogflowAgentDoc = db.collection('dialogflow').doc('agent');

                          // Get the value of 'entry' in the document and send it to the user
                          return dialogflowAgentDoc.get().then(doc => {
                          if (!doc.exists) {
                          conv.tell('No data found in the database!');
                          } else {
                          conv.ask(doc.data().entry);
                          }
                          }).catch(() => {
                          conv.ask('Error reading entry from the Firestore database. Please add a entry to the database first by saying, "Write <your phrase> to the database"');
                          });
                          });

                          app.intent('WriteToFirestore', (conv) => {
                          /* some code here */
                          });

                          exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app);


                          (methods omitted)



                          The "app" (dialogflow) object already wraps the request and response arguments, so there isn't anything you should do to interfere with those.



                          If you want separate endpoints, outside of dialogflow, to do stuff: you can add additional functions like so:



                          exports.someOtherFunction = functions.https.onRequest((request, response) => {
                          /* do something with the request and response here */
                          });


                          These won't directly have much to do with Dialogflow, however.






                          share|improve this answer















                          It appears you're mixing 2 different versions of the Actions on Google library. It's recommended you stick with V2, which means changing the Intent-map to intent handlers instead.



                          // Intent that starts the account linking flow.
                          app.intent('Start Signin', conv => {
                          conv.ask(new SignIn('To get your account details'));
                          });

                          // Create a Dialogflow intent with the `actions_intent_SIGN_IN` event.
                          app.intent('Get Signin', (conv, params, signin) => {
                          if (signin.status === 'OK') {
                          const payload = conv.user.profile.payload;
                          conv.ask(`Welcome back ${payload.name}. What do you want to do next?`);
                          } else {
                          conv.ask(`I won't be able to save your data, but what do you want to do next?`);
                          }
                          });

                          app.intent('Make Appointment', (conv) => {
                          /* some code here */
                          });

                          app.intent('ReadFromFirestore', (conv) => {
                          // Get the database collection 'dialogflow' and document 'agent'
                          const dialogflowAgentDoc = db.collection('dialogflow').doc('agent');

                          // Get the value of 'entry' in the document and send it to the user
                          return dialogflowAgentDoc.get().then(doc => {
                          if (!doc.exists) {
                          conv.tell('No data found in the database!');
                          } else {
                          conv.ask(doc.data().entry);
                          }
                          }).catch(() => {
                          conv.ask('Error reading entry from the Firestore database. Please add a entry to the database first by saying, "Write <your phrase> to the database"');
                          });
                          });

                          app.intent('WriteToFirestore', (conv) => {
                          /* some code here */
                          });

                          exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app);


                          (methods omitted)



                          The "app" (dialogflow) object already wraps the request and response arguments, so there isn't anything you should do to interfere with those.



                          If you want separate endpoints, outside of dialogflow, to do stuff: you can add additional functions like so:



                          exports.someOtherFunction = functions.https.onRequest((request, response) => {
                          /* do something with the request and response here */
                          });


                          These won't directly have much to do with Dialogflow, however.







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Nov 27 '18 at 3:18

























                          answered Nov 27 '18 at 3:10









                          ArtArt

                          47239




                          47239






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53491592%2fhow-to-get-fulfillment-for-both-sets-of-code%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Contact image not getting when fetch all contact list from iPhone by CNContact

                              count number of partitions of a set with n elements into k subsets

                              A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks