简体   繁体   English

dialogflow,助手的Google登录(包括Google Home)

[英]dialogflow, Google Sign-In for the Assistant (include Google Home)

I am developing an Action that needs to catch the name/email of the user after they consent. 我正在开发一项Action,需要在他们同意后捕获用户的名称/电子邮件。

I found an interesting tutorial ( https://medium.com/google-developer-experts/how-to-use-google-sign-in-for-the-assistant-b818f3de9211 ). 我找到了一个有趣的教程( https://medium.com/google-developer-experts/how-to-use-google-sign-in-for-the-assistant-b818f3de9211 )。 clientID is a "copy-paste" of clientID existing in Google Action Account Linking. clientID是Google Action帐户链接中现有的clientID的“复制粘贴”。 The intent is invoked successfully! 该意图已成功调用!

const app = dialogflow({
  clientId: 'my_client_id_from_google_action_ACCOUNT_LINKING.apps.googleusercontent.com'
});

app.intent("Hours", conv => {
  console.log('--- ok');
  const payload = conv.user.profile.payload;

  if (payload) {
    const name = payload.name;
    const email = payload.email;
    console.log('--- name: ' + name);
    console.log('--- email: ' + email);
  } else {
    conv.ask("Not signed in yet.");
    conv.ask(new Suggestion("want to sign in"));
  }
});

} }); }});

Actually, I am not able to get the user's email/name. 实际上,我无法获得用户的电子邮件/名称。

I would like to get the name and email address. 我想获取姓名和电子邮件地址。


I do really appreciate your help! 非常感谢您的帮助! Neither Payload nor conv.user.profile.token are populated. 有效载荷和conv.user.profile.token均未填充。 I am newbie in this programming language and concept, and will put the entire code. 我是该编程语言和概念的新手,并将介绍整个代码。 Thank you so much for your time and patience. 非常感谢您的时间和耐心。

Entire code index.js: 整个代码index.js:

'use strict';

const functions = require('firebase-functions');
const {google} = require('googleapis');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
const {dialogflow,SignIn} = require("actions-on-google");

process.env.DEBUG = 'dialogflow:*';

const app = dialogflow({
    clientId: '------------------sas6.apps.googleusercontent.com',
});

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 fallback(agent) {
        agent.add(`I didn't understand`);
    }

    function welcome(agent) {
        app.intent("Get Sign-In", conv => {
            agent.add('--- ok Get Sign-In');
            console.log('--- ok Get Sign-In');
            const payload = conv.user.profile.payload;

            if (payload) {
                console.log('--- payload if');
                console.log('--- name: ' + payload.name);
                console.log('--- email: ' + payload.email);
            } else {
                console.log('--- payload else');
                conv.ask("Not signed in yet.");
                conv.ask(new Suggestion("want to sign in"));
            }
        });
    }

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

package.json: 的package.json:

{
  "name": "dialogflowFirebaseFulfillment",
  "description": "my first agent",
  "version": "0.0.1",
  "private": true,
  "license": "Apache Version 2.0",
  "author": "Google Inc.",
  "engines": {
    "node": "6"
  },
  "scripts": {
    "lint": "semistandard --fix \"**/*.js\"",
    "start": "firebase serve --only functions",
    "deploy": "firebase deploy --only functions"
  },
  "dependencies": {
    "firebase-functions": "2.0.2",
    "firebase-admin": "^5.13.1",
    "actions-on-google": "2.2.0", 
    "googleapis": "^27.0.0",
    "dialogflow": "^0.6.0",
    "dialogflow-fulfillment": "0.5.0",
    "nodemailer": "^4.6.4"
  }
}

There are a few issues with your code that point to the fundamental problem. 您的代码中存在一些指向基本问题的问题。 Let's look at each one. 让我们看看每个。

Fundamental problem: You never ask the user to log in 基本问题:您永远不会要求用户登录

While you have code that appears to handle the response from a sign-in request, there doesn't seem to be any code that asks the user to sign in. 尽管您的代码似乎可以处理登录请求的响应,但似乎没有任何代码要求用户登录。

While there are other ways that the user can login (they could sign in on your website, for example), the typical way is to prompt them to do so using the Signin helper in the response to one of your Intents. 尽管用户可以通过其他方式登录(例如,他们可以在您的网站上登录),但典型的方法是使用登录助手在对您的一个Intent的响应中提示他们登录。 It might look something like this 它可能看起来像这样

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

if you're using the actions-on-google library. 如果您使用的是Google Actions库。 Which actually leads to a different problem. 这实际上导致了另一个问题。

You're mixing the dialogflow-fulfillment and actions-on-google library 您正在混搭对话框流程实现和Google动作库

And, worse, you're doing it in a way that is probably causing problems. 而且,更糟糕的是,您这样做的方式可能会引起问题。

It looks like you are primarily using the dialogflow-fulfillment library, since this is what is setup in the Firebase Function handler and what triggers the call to agent.handleRequest(intentMap); 看来您主要是在使用dialogflow-fulfillment库,因为这是Firebase Function处理程序中设置的,并且触发对agent.handleRequest(intentMap);的调用agent.handleRequest(intentMap); .

Although you setup a different handler with the call app.intent() , you do this inside your welcome intent (judging by its name), which you generally don't want to do. 尽管您通过调用app.intent()设置了其他处理程序,但app.intent()您不希望使用的欢迎意图(通过其名称进行判断)中进行此操作。 There is also nothing that is setup to call this handler. 也没有任何东西可以调用此处理程序。 Typically you'd set this up with a line such as 通常,您可以使用以下行来设置

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

Since this line is so much like the dialogflow-fulfillment one, it drives home the point that you should do fulfillment with one library or the other - not both. 由于这一行与dialogflow-fulfillment非常相似,因此可以说您应该使用一个或另一个库(而不是两者)来实现。 In general, if you're doing Actions on Google stuff, stick to the actions-on-google library. 通常,如果您要对Google内容执行“操作”,请坚持使用Google动作库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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