0

这个错误与我在这里问的类似,但这次是 NodeJs 客户端。

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

我检查了总响应时间,它不到 2 秒,Google 的超时时间不到 5 秒。

我哪里错了???

我的 API.ai 意图
在此处输入图像描述 在此处输入图像描述

将 express.js 与 Action-on-Google 节点客户端一起使用

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

更新 我正在通过 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 发送空响应。
为什么会发生这种情况以及如何解决????

4

1 回答 1

0

问题在于您的directionIntent()函数如何调用和处理函数的结果getDirectionWithSavedAddress()。它期望getDirectionWithSavedAddress() 返回一个函数,而实际上它没有。相反,getDirectionWithSavedAddress()期望将其结果发送到回调

因此,在它调用 之后getDirectionWithAddress(),函数结束,什么也不返回。这个“什么都没有”被发送到assistant.ask(),然后返回到谷歌的服务器。这是一个无效的响应,所以你得到了错误。

解决这个问题应该很简单。您需要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()并生成错误“操作错误:请求处理程序不能为空”。

你可以通过打电话responseHandler(assistant)而不是处理来解决这个问题handleRequest()。或者您可以创建handleRequest()预期的地图并完全摆脱responseHandler()

于 2017-09-12T16:16:51.893 回答