简体   繁体   English

Google行动上项目实现在Node.js中实现isRequestFromAssistant

[英]Implementing isRequestFromAssistant in Node.js on actions-on-google project fulfillment

I am having trouble implementing the isRequestFromAssistant method to verify requests to my fulfillment webhook. 我在实现isRequestFromAssistant方法以验证对实现Webhook的请求时遇到麻烦。 Using Node.js, I instantiate the following variables at the start of my index.js file: 使用Node.js,我在index.js文件的开头实例化了以下变量:

const App = require('actions-on-google').ApiAiApp;
const app = new App({ request, response });

I then use "app" with the .ask and .tell and other methods throughout my functions. 然后,在整个函数中,我将.app和.tell等“ app”与其他方法一起使用。

The code I see in the docs for implementing isRequestFromAssistant is: 我在文档中看到的用于实现isRequestFromAssistant的代码是:

const app = new ActionsSdkApp({request, response});
app.isRequestFromAssistant('my-project-id')
  .then(() => {
    app.ask('Hey there, thanks for stopping by!');
  })
  .catch(err => {
    response.status(400).send();
  });

If I leave out the first line and use my existing app variable, created with the .ApiAi method instead of the .ActionsSdkApp method, it doesn't work. 如果我省略第一行并使用通过.ApiAi方法而不是.ActionsSdkApp方法创建的现有应用程序变量,则该方法无效。 If I create a new variable App1 and app1 using the .ActionsSdkApp method and change the above code to be app1.isRequestFromAssistant, it also doesn't work. 如果我使用.ActionsSdkApp方法创建新的变量App1和app1,并将上面的代码更改为app1.isRequestFromAssistant,它也将不起作用。 I have tried other variations with no luck. 我没有运气尝试过其他变体。

When I say it doesn't work, I mean I receive a 500 Internal Server Error when I call it. 当我说它不起作用时,我的意思是我在调用它时收到500 Internal Server Error。 I am hosting it with NGROK currently. 我目前在NGROK上托管。 I am still a beginner with Node.js, although I have managed to get the other 700 lines of code working just fine, learning mostly from Google searches and reading these forums. 我仍然是Node.js的初学者,尽管我设法使其他700行代码正常运行,主要是从Google搜索中学习并阅读了这些论坛。

You have a few things going on here which, individually or separately, may be causing the problem. 您在此处进行的某些操作可能会单独或单独导致此问题。

First - make sure you have the most recent version of the actions-on-google library. 首先 -确保您拥有最新版本的Google动作库。 The isRequestFromAssistant() function was added in version 1.6.0, I believe. 我相信isRequestFromAssistant()函数是在1.6.0版中添加的。

Second - Make sure you're creating the right kind of App instance. 其次 -确保您正在创建正确类型的App实例。 If you're using Dialogflow (formerly API.AI), you should be creating it with something like 如果您使用的是Dialogflow(以前称为API.AI),则应使用类似以下内容创建它

const App = require('actions-on-google').DialogflowApp;
const app = new App( {request, response} );

or 要么

const { DialogflowApp } = require('actions-on-google');
const app = new DialogflowApp( {request, response} );

(They both do the same thing, but you'll see both forms in documentation.) You should switch to DialogflowApp from ApiAiApp (which your example uses) to reflect the new name, but the old form has been retained. (它们都做同样的事情,但是您会在文档中看到这两种形式。)您应该从ApiAiApp(您的示例使用的是)切换到DialogflowApp,以反映新名称,但是保留了旧形式。

If you're using the Actions SDK directly ( not using Dialogflow / API.AI), then you should be using the ActionsSdkApp object, something like 如果您直接使用Actions SDK( 而不是使用Dialogflow / API.AI),则应该使用ActionsSdkApp对象,例如

const { ActionsSdkApp } = require('actions-on-google');
const app = new ActionsSdkApp({request: request, response: response});

(Again, you'll see variants on this, but they're all fundamentally the same.) (同样,您会看到一些变体,但它们基本上是相同的。)

Third - Make sure you're using the right function that matches the object you're using. 第三 -确保您使用的功能与您使用的对象相匹配。 The isRequestFromAssistant() function is only if you are using the Actions SDK. 在使用Actions SDK时, 使用isRequestFromAssistant()函数。

If you are using Dialogflow, the corresponding function is isRequestFromDialogflow() . 如果使用的是Dialogflow,则对应的函数为isRequestFromDialogflow() The parameters are different, however, since it requires you to set confirmation information as part of your Dialogflow configuration. 但是,参数有所不同,因为它要求您将确认信息设置为Dialogflow配置的一部分。

Finally - If you're getting a 500 error, then check your logs (or the output from stderr) for the node.js server that is running. 最终 -如果遇到500错误,请检查正在运行的node.js服务器的日志(或stderr的输出)。 Typically there will be an error message there that points you in the right direction. 通常,那里会出现一条错误消息,指出正确的方向。 If not - posting that error message as part of your StackOverflow question is always helpful. 如果不是这样-将错误消息作为StackOverflow问题的一部分发布总是有帮助的。

Set the secure (randomly generated) auth header & key values in the dialogflow Fulfillment page, then in nodejs: 在dialogflow实现页面中,然后在nodejs中,设置安全(随机生成的)身份验证标头和密钥值:

if (app.isRequestFromDialogflow("replace_with_key", "replace_with_value")) {
  console.log("Request came from dialogflow!");
  // rest of bot
} else {
  console.log("Request did not come from dialogflow!");
  response.status(400).send();
}

Also see: https://developers.google.com/actions/reference/nodejs/DialogflowApp#isRequestFromDialogflow 另请参阅: https : //developers.google.com/actions/reference/nodejs/DialogflowApp#isRequestFromDialogflow

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

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