2017-09-12 92 views
0

这个错误与我询问here的错误类似,但这次是使用NodeJs客户端。

我正在尝试查找指向某个位置的路线。只要意图在我的webhook上触发,我正在使用GoogleMapAPI计算路线。但是在它完成并发送响应之前,我在我的操作控制台上收到错误消息。

我检查了总响应时间,并且小于2秒,这是小于5秒超时谷歌。

我在哪里错了?
Google上的API.ai操作 - 无法解析带有'INVALID_ARGUMENT'错误的JSON响应字符串:“:无法找到字段。”

我API.ai意向
enter image description here enter image description here

使用与操作上,谷歌节点客户端express.js

'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; 


方向提取---错误出现在行动控制台在此之前拿完

'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; 

UPDATE 我通过WebStorm本地运行的代码,并使用ngrok通过端口转发揭露网络挂接。

UPDATE2
错误的请求400

enter image description here

{ 
    "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>" 
} 

这看起来像之前我的回调结束后,我的网络挂接被发送给谷歌操作空响应。
为什么会发生这种情况,以及如何解决它?

+0

由于您使用ngrok,还可以发布HTTP响应** **那ngrok记录?你应该可以去ngrok的控制台URL查看这个。 – Prisoner

+0

更新...有400错误! –

+0

即使在回调完成之前,我的webhook响应正在触发......为什么? –

回答

0

问题在于你的directionIntent()函数如何调用,并处理你的getDirectionWithSavedAddress()函数的结果。它期望getDirectionWithSavedAddress()返回函数,当它不。相反,getDirectionWithSavedAddress()预计将其结果发送到回调

所以在拨打电话getDirectionWithAddress()后,函数结束,不返回任何内容。这个“无”发送到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()函数将会被传递意图名称的地图以调用处理事件的函数。您在responseHandler()函数中手动执行此操作,并且您没有返回地图。既然你不返回一个地图,它没有试图做的handleRequest()时,并产生错误“操作错误:请求处理程序不能为空”。

您可以通过只调用responseHandler(assistant),而不是与handleRequest()处理在所有解决这个问题。或者你可以创建handleRequest()所期待的地图,并完全摆脱responseHandler()

+0

遗憾我错过了在编辑...我通过回调返回它只 –

+0

现在更新...错误是一样 –

+0

与附加应答 – Prisoner