简体   繁体   中英

How can I make Dialogflow agent greet user if they have used the action before?

I'm using Actions On Google / Dialogflow, and I'm trying to make a function that will greet a user by their name if they've used the action before, and if not, will ask for their name. I have tried to map this to the "Welcome intent" through fulfillment, but whenever I try to run the action on the simulator, I get this error:

Error 206: Webhook Error

Which Initially would make sense if this was mapped to another intent, but I'm wondering if I'm getting this error because you can't have a fulfillment with the welcome intent?

Here's the code I'm using in the inline editor which may be the problem:

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


  function welcome(conv) {
      if (conv.user.last.seen) {
          conv.ask(`Welcome back ${name}!`);
    } else {
          conv.ask('Welcome to The app! My name is Atlas, could I get your name?');
   }}

  let intentMap = new Map();
  intentMap.set('Welcome Intent', welcome);
  agent.handleRequest(intentMap);

How come this isn't working? Do I need to implement user login? Do I need to use a function that would write to a firestore databbase?

Thanks for the help or suggestions!

Let's clear a few things up to start:

  • You can have fulfillment with your welcome intent.
  • You do not need user login. Although using Google Sign In for Assistant can certainly be used, it doesn't fundamentally change your problem here.
  • You do not need to use a function that writes to the firestore database. Again, you could use it, but this doesn't change your problems.

The specific reason this isn't working is because the conv parameter in this case contains a Dialogflow WebhookClient rather than an actions-on-google Conversation object.

To get the Conversation object with the parameter you have, you can call conv.getConv() , which will give you an object that has a user parameter. So this may look something like

function welcome(conv) {
  let aog = conv.getConv();
  if (aog.user.last.seen) {
      conv.ask(`Welcome back ${name}!`);
  } else {
      conv.ask('Welcome to The app! My name is Atlas, could I get your name?');
}}

There are, however, still some issues with this. Most notably, it isn't clear where name will come from. I assume you will get it out of the user store object, but you don't seem to have done this.

For anyone who comes across this question in the future and just wants a straight forward answer without having to search through ambiguous answers / documentation, here is what to do step by step:

note: I ended up using the Google Sign in method, but even if this isn't your goal, i'll post the link to the alternative method.

1) Import the actions on google module. What people / tutorials don't to show is you have to import the library like this (for user login):

const {
   dialogflow,
   Permission,
   SignIn
} = require('actions-on-google')

instead of

const dialogflow = require('actions-on-google')

2) Use this code:

const app = dialogflow({
      clientId: '<YOUR CLIENT ID from Actions on Google>',
 });

 app.intent('Start Signin', conv => {
      conv.ask(new SignIn('To get your account details'));
 });


  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?`);
   }
        });

This function will ask the user for a login, and next time you invoke the intent, it will say "Welcome back name", because google automatically saves it.

Here's the link to the alternative method :

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