node.js - API.ai Actions on Google - Failed to parse JSON response string with 'INVALID_ARGUMENT' error: ": Cannot find field." -


this error similar asked here, time it's nodejs client.

trying find directions location. intent triggered on webhook, calculating directions using googlemapapi. before can finish , send response, receive error on actions console.

checked total response time , less 2 seconds less 5 seconds timeout google.

where wrong???

my api.ai intent
enter image description here enter image description here

using 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 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 name of intent defined in 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!') }); 


handler code

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


direction extraction --- error comes on action console before finished

'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 running code locally via webstorm , exposing webhook via port forwarding using ngrok.

update2
bad request 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>" } 

this looks before callback finished, webhook sending empty response google actions.
why happening , how resolve it?????

the problem lies in how directionintent() function calls, , handles result of, getdirectionwithsavedaddress() function. expects getdirectionwithsavedaddress() returns function, when not. instead, getdirectionwithsavedaddress() expects send results callback.

so after makes call getdirectionwithaddress(), function ends, returning nothing. "nothing" sent assistant.ask(), returns google's server. invalid response, you're getting error.

fixing should straightforward. need call getdirectionwithsavedaddress() callback function. inside function should call assistant.ask() value sent callback.

so directionintent() might like

    'directionintent':function (assistant) {       console.log('direction intent');       direction.getdirectionwithsavedaddress( function( msg ){         assistant.ask( msg );       } );     } 

updated

this line makes no sense:

assistant.handlerequest(responsehandler(assistant)); 

the assistant.handlerequest() function supposed passed map of intent names functions call handle event. you're doing manually in responsehandler() function , you're not returning map. since you're not returning map, fails when trying handlerequest() , generates error "action error: request handler can not empty".

you can fix calling responsehandler(assistant) , not dealing handlerequest() @ all. or can create map handlerequest() expecting , rid of responsehandler() completely.


Comments

Popular posts from this blog

neo4j - finding mutual friends in a cypher statement starting with three or more persons -

php - How to remove letter in front of the word laravel -

minify - Minimizing css files -