简体   繁体   中英

End conversation using Dialogflow Fulfillment Library v2 on firebase?

How do i set the "end conversation" flag using the Dialogflow Fulfillment Library WebhookClient class? I'm using the inline editor powered by Cloud Functions for Firebase, in case that matters.

The situation is that a particular intent cannot be visited more than 3 times. On the first and second visits you get a response, and it allows you to say something and continue; on the third visit, it should give a response then end the conversation/close the mic/kill the app (whatever the proper term is for this)

For non-ending responses i'm using WebhookClient.add(), and i'm using a mix of rich responses, and strings for Text to Speech.

From what I read on github ( https://github.com/dialogflow/dialogflow-fulfillment-nodejs ), i assumed WebhookClient.end() would be what i wanted. But when i use that, the sript crashes and i get nothing. The following is all inside exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {...}

 const agent = new WebhookClient({ request, response });
 const fbContext = 'fallbackcount';

 function fallback(agent) {
      //all fallbacks use this fulfillment, we give up after 3 bunk responses
      //check if we've been here before - we will find a context with a counter if we have
      let countContext = agent.getContext(fbContext);
      let fbcount = 0;
      if (countContext)
      {
           if (typeof countContext.parameters !== "undefined" )
           {
                if(typeof countContext.parameters.count !== "undefined" )
                {
                     fbcount = countContext.parameters.count;
                }
           }
      }

      console.log("current tracking" + fbcount.toString());
      switch(fbcount) {
           case 0:
           {
                agent.add(`Fallback response 1");
                break;
           }
           case 1:
           {
                agent.add("Fallback response 2");
                break;
           }
           default:
           {
                //intention: die on third bunk response
                //reality: following line causes a problem and i get no response, and the app doesn't close
                agent.end("Fallback response 3 - Goodbye!"); 
           }
      }

      let newcount = fbcount + 1;
      console.log("new tracking " + newcount.toString());
      agent.setContext({
        name: fbContext,
        lifespan: 1,
        parameters:{count: newcount}
      });
 }

What am i doing wrong?

Please note that this is NOT a duplicate of Actions on Google: Unable to Close Convo in DialogFlow Fulfillment as he is asking regarding the actions-on-google library, wheras I am using dialogflow-fulfillment-nodejs

I have also seen Dialogflow API V2 "End of conversation" flag in webhook request and that appears to be dealing in raw json, which i believe should be avoidable from what i've seen in docs

I don't know if this is the correct way to do it in this case, but I guess you could use a payload response to close the conversation, in which you set the "expectedUserResponse" to "false":

const googlePayloadJson = {
        "expectUserResponse": false
    };
    let payload = new Payload(agent.ACTIONS_ON_GOOGLE, {});
    payload.setPayload(googlePayloadJson);
    agent.add(payload);

This worked for me:

function close(message){
    let conv = agent.conv();
    conv.close(message); 
    agent.add(conv);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM