这个错误类似于我问的here,但这次是NodeJs客户端。
我正试图找到一个位置的方向。一旦在我的webhook上触发了意图,我就会使用GoogleMapAPI计算方向。但是在它完成并发送响应之前,我在操作控制台上收到错误。
我检查了总响应时间,并且不到2秒,谷歌超时不到5秒。 br />
我哪里错了???
将express.js与Action-on-Google Node Client一起使用
'use strict';
const express = require('express');
const bodyParser = require('body-parser');
const intentHandler = require('./intent_handler')
const app = express();
app.use(bodyParser.json());
const ApiAiAssistant = require('actions-on-google').ApiAiAssistant;
// Create functions to handle requests here
....
....
const DIRECTION_INTENT = 'action_direction';
function MyAssistant(req, res) {
const assistant = new ApiAiAssistant({request: req, response: res});
assistant.handleRequest(responseHandler(assistant));
}
function responseHandler (assistant) {
// intent contains the name of the intent you defined in the Actions area of API.AI
let intent = assistant.getIntent();
switch (intent) {
case WELCOME_INTENT:
...
break;
case WELCOME_FALLBACK_PERMISSION_INTENT:
...
break;
case DIRECTION_INTENT:
console.log(">>>>>>>DIRECTION_INTENT<<<<<<<");
intentHandler.directionIntent(assistant);
break;
}
}
app.post('/', function (req, res) {
MyAssistant(req, res);
});
app.listen(8080, function () {
console.log('app listening on port 8080!')
});
处理者代码
'use strict';
const speech = require("./speech_template");
const direction = require("./directionModule");
const intent_handler = {
'welcomeIntent': function (assistant) {
.....
},
'welcomeFallbackPermissionIntent': function (assistant) {
.....
},
'directionIntent':function (assistant) {
console.log('direction intent');
direction.getDirectionWithSavedAddress(function (response) {
assistant.ask(response);
});
}
};
module.exports = intent_handler;
方向提取--- ERROR在此操作完成之前出现在操作控制台上
'use strict';
const striptags = require('striptags');
const speech = require("./speech_template");
let googleMapsClient = require('@google/maps').createClient({
key: global.GOOGLE_DIRECTION_KEY
});
const directionModule = {
'getDirectionWithSavedAddress': function (eventCallback) {
let myAdd = <From Saved Data>;
if (myAdd === undefined) {
console.log("error......");
}
let destination = <From Saved Data>;
this.getDirectionWithAddress(myAdd, destination, function (dir) {
....
if(SUCCESS){
eventCallback(`<speak> ${steps} </speak>`);
}else{
eventCallback(`<speak> ${speech.ERROR_DIRECTIONS} </speak>`);
}
});
},
'getDirectionWithAddress': function (add1, add2, eventCallback) {
let dir = {};
googleMapsClient.directions({
origin: add1,
destination: add2,
mode: "driving",
departure_time: "now"
}, function (err, response) {
if (!err) {
console.log(response.json.routes[0]);
....
....
....
} else {
console.log(`Error --> ${err.toString()}`);
....
}
eventCallback(dir);
});
}
};
module.exports = directionModule;
更新 我通过WebStorm在本地运行代码,并使用ngrok通过端口转发公开webhook。
UPDATE2
不好请求400
{
"originalRequest": {
"source": "google",
"version": "2",
"data": {
"isInSandbox": true,
"surface": {
"capabilities": [
{
"name": "actions.capability.AUDIO_OUTPUT"
}
]
},
"inputs": [
{
"rawInputs": [
{
"query": "get me there",
"inputType": "VOICE"
}
],
"arguments": [
{
"rawText": "get me there",
"textValue": "get me there",
"name": "text"
}
],
"intent": "actions.intent.TEXT"
}
],
"user": {
"locale": "en-US",
"userId": "<uID>"
},
"device": {},
"conversation": {
"conversationId": "<cID>",
"type": "ACTIVE",
"conversationToken": "[\"_actions_on_google_\",\"defaultwelcomeintent-followup\"]"
}
}
},
"id": "<ID>",
"timestamp": "2017-09-12T17:08:10.321Z",
"lang": "en",
"result": {
"source": "agent",
"resolvedQuery": "get me there",
"speech": "",
"action": "action_direction",
"actionIncomplete": false,
"parameters": {},
"contexts": [
{
"name": "_actions_on_google_",
"parameters": {},
"lifespan": 99
},
{
"name": "google_assistant_input_type_voice",
"parameters": {},
"lifespan": 0
},
{
"name": "actions_capability_audio_output",
"parameters": {},
"lifespan": 0
},
{
"name": "defaultwelcomeintent-followup",
"parameters": {},
"lifespan": 4
}
],
"metadata": {
"intentId": "<iID>",
"webhookUsed": "true",
"webhookForSlotFillingUsed": "false",
"nluResponseTime": 15,
"intentName": "DirectionIntent"
},
"fulfillment": {
"speech": "",
"messages": [
{
"type": 0,
"speech": ""
}
]
},
"score": 1
},
"status": {
"code": 200,
"errorType": "success"
},
"sessionId": "<sID>"
}
这看起来像我的回调完成之前,我的webhook正在向Google Actions发送空回复 为什么会发生这种情况以及如何解决??????
答案 0 :(得分:0)
问题在于您的directionIntent()
函数如何调用并处理getDirectionWithSavedAddress()
函数的结果。它希望getDirectionWithSavedAddress()
返回一个函数,而不是。相反,getDirectionWithSavedAddress()
期望将其结果发送到回调。
因此,在调用getDirectionWithAddress()
后,函数结束,不返回任何内容。这&#34;没什么&#34;发送到assistant.ask()
,然后将其返回给Google的服务器。这是一个无效的回复,因此您收到错误。
解决这个问题应该很简单。您需要使用回调函数调用getDirectionWithSavedAddress()
。在此函数中,您应该使用发送给回调的值调用assistant.ask()
。
所以directionIntent()
可能看起来像
'directionIntent':function (assistant) {
console.log('direction intent');
direction.getDirectionWithSavedAddress( function( msg ){
assistant.ask( msg );
} );
}
<强>更新强>
这条线毫无意义:
assistant.handleRequest(responseHandler(assistant));
assistant.handleRequest()
函数应该被传递给Intent名称的Map到要调用来处理事件的函数。您在responseHandler()
功能中手动执行此操作,但未返回地图。由于您没有返回Map,因此在尝试执行handleRequest()
时会失败并生成错误&#34;操作错误:请求处理程序不能为空&#34;。
您可以通过调用responseHandler(assistant)
而不是处理handleRequest()
来解决此问题。或者您可以创建handleRequest()
期待的地图并完全摆脱responseHandler()
。