0

We are using firebase cloud functions for our fulfillment and an external rest api for our crud operations.

We have one intent with a few followups and it looks like this

 - Create Note
 - - Create Note - fallback*
 - - - Create Note - fallback - yes
 - - - Create Note - fallback - no

* the fallback allows us to capture free text

In our webhook I have the following fulfillment

app.intent("Create Note", (conv) => {
    if (!conv.user.storage.note) conv.user.storage.note = {};
    conv.ask("Go ahead, talk to Talkatoo!");
});

app.intent("Create Note - fallback", (conv) => {
    conv.user.storage.input = conv.input.raw;

    let response = conv.user.storage.note
         ? conv.user.storage.note.body.concat(conv.input.raw)
         : conv.user.storage.input;

    conv.ask("So far this is what I heard you say, let me know if this is complete. ", response);
});

app.intent("Create Note - fallback - yes", (conv) => {
    // based on the conv.user.storage.note object
    // either make a call to create a note or update a note

    // make call to external api and based on the response 
    // set the value for conv.user.storage.note

   conv.ask("Great news, let me save that for you!");
});

app.intent("Create Note - fallback - no", (conv) => {
    // based on the conv.user.storage.note object
    // either make a call to create a note or update a note

    // make call to external api and based on the response 
    // set the value for conv.user.storage.note

    // Send the user back to Create Note to capture the rest of their input
   conv.followup("my_custom_event");
});

The issue is that conv.user.storage.note is getting set when I get the response from the API, but then it is getting reset to empty and so a new note is created each time. I'm trying to append the various inputs from the user to be one note

islalobo
  • 617
  • 2
  • 8
  • 22

2 Answers2

0

Based on ...

app.intent("Create Note - fallback - yes", (conv) => {
    // based on the conv.user.storage.note object
    // either make a call to create a note or update a note

    // make call to external api and based on the response 
    // set the value for conv.user.storage.note

   conv.ask("Great news, let me save that for you!");
});

it looks like either you haven't written the code for the API call yet, or you removed that code before you posted on StackOverflow. Is that correct?

Can you update your question to show where and how you send an HTTP request to your external REST API?

Max Wiederholt
  • 668
  • 7
  • 12
  • I send it using `request-promise`. What I included was pseudo code because you don't need to see the call; it's just a basic call. Based on this post https://stackoverflow.com/a/52450347/4122445 and a few other similar responses from user Prisoner, it seems there are some work arounds to deal with async calls and responding to the user. In our case, I realized the conversation doesn't rely on any of the data returned from the response so I can instead save the user's multiple inputs to the user.storage and then when they are complete save the full response. – islalobo Feb 25 '19 at 20:40
  • Also, the issue WAS that conv.user.storage.note was getting set, but it wasn't getting RESET, actually the response ie. conv.followup("my_custom_event"), was happening first. Which I should have caught on to just by looking at my code. – islalobo Feb 25 '19 at 20:45
0

For anyone who this might end up helping, I found a post and realized that dealing with responses to async calls might need more thought (to work in work arounds) How to make asynchronous calls from external services to actions on google?

For our use case we didn't need the response data ie, we didn't need to communicate any of the response data to the user, so instead we saved the user's multiple inputs in user.storage and when they were complete we save the full response.

app.intent("Intent - fallback", (conv) => {
    conv.user.storage.input = conv.user.storage.input
    ? conv.user.storage.input.concat(conv.input.raw)
    : conv.input.raw;

    conv.ask("Is that complete or would you like to continue?");
});

app.intent("Intent - fallback - yes", (conv) => {

    createNote(conv); <--- makes call to save

    conv.ask("Great news, let me save that for you!");
});

app.intent("Intent - fallback - no", (conv) => {
    conv.followup("my_custom_event");
});
islalobo
  • 617
  • 2
  • 8
  • 22