超时和ENOTFOUND错误导致HTTP请求失败

时间:2018-01-15 02:21:02

标签: node.js http firebase google-cloud-functions dialogflow

我对Dialogflow的履行中的http请求有疑问。 我想从Dialogflow向另一个Web数据库服务发出http POST请求。

但发生了2次错误。如果你知道解决方案,请告诉我吗?

仅供参考,我在Postman和Node.js本地文件上测试了http post请求,然后请求成功完成。

  1. 请求超时错误

    
    
      "status": {
        "code": 206,
        "errorType": "partial_content",
        "errorDetails": "Webhook call failed. Error: Request timeout.",
        "webhookTimedOut": true
      }
    
    
    

  2. ENOTFOUND错误

  3. 
    
    { Error: getaddrinfo ENOTFOUND <service url> <service url>:443
        at errnoException (dns.js:28:10)
        at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:76:26)
      code: 'ENOTFOUND',
      errno: 'ENOTFOUND',
      syscall: 'getaddrinfo',
      hostname: '<service url>',
      host: '<service url>',
      port: 443 
    }
    &#13;
    &#13;
    &#13;

    我在Fulfillment&#39;内联编辑器中编辑的代码&#39;在下面。

    &#13;
    &#13;
    (function() {
        'use strict';
    
        // Cloud Functions for Firebase library
        const functions = require('firebase-functions');
        // Google Assistant helper library
        const DialogflowApp = require('actions-on-google').DialogflowApp;
        const googleAssistantRequest = 'google';
        const req = require('request');
    
        function addTask(request, response) {
    
            let action = request.body.result.action;
            let parameters = request.body.result.parameters;
            let inputContexts = request.body.result.contexts;
            let requestSource = (request.body.originalRequest) ? request.body.originalRequest.source : undefined;
            const app = new DialogflowApp({request: request, response: response});
    
            // Function to send correctly formatted Google Assistant responses to Dialogflow which are then sent to the user
            function sendGoogleResponse(responseToUser) {
                if (typeof responseToUser === 'string') {
                    app.ask(responseToUser);
                } else {
                    // If speech or displayText is defined use it to respond
                    let googleResponse = app.buildRichResponse().addSimpleResponse({
                        speech: responseToUser.speech || responseToUser.displayText,
                        displayText: responseToUser.displayText || responseToUser.speech
                    });
    
                    if (responseToUser.googleRichResponse) {
                        googleResponse = responseToUser.googleRichResponse;
                    }
    
                    if (responseToUser.googleOutputContexts) {
                        app.setContext(...responseToUser.googleOutputContexts);
                    }
    
                    // Send response to Dialogflow and Google Assistant
                    app.ask(googleResponse);
                }
            }
    
            // Function to send correctly formatted responses to Dialogflow which are then sent to the user
            function sendResponse(responseToUser) {
                // if the response is a string send it as a response to the user
                if (typeof responseToUser === 'string') {
                    let responseJson = {};
                    responseJson.speech = responseToUser;
                    responseJson.displayText = responseToUser;
                    // Send response to Dialogflow
                    response.json(responseJson);
                } else {
                    // If the response to the user includes rich responses or contexts send them to Dialogflow
                    let responseJson = {};
    
                    // If speech or displayText is defined, use it to respond (if one isn't defined use the other's value)
                    responseJson.speech = responseToUser.speech || responseToUser.displayText;
                    responseJson.displayText = responseToUser.displayText || responseToUser.speech;
                    responseJson.data = responseToUser.richResponses;
                    responseJson.contextOut = responseToUser.outputContexts;
    
                    // Send response to Dialogflow
                    response.json(responseJson);
                }
            }
    
            const actionHandlers = {
                'input.firstquestion': () => {
                    const headers = {
                        'Content-Type': 'application/json'
                    };
    
                    const form = {
                        'app': 503,
                        'record': {
                            'todo': {
                                'value': parameters.task
                            },
                            'duedate': {
                                'value': parameters.date
                            }
                        }
                    };
    
                    const options = {
                        url: '<service url>',
                        method: 'POST',
                        headers: headers,
                        json: form
                    };
    
                    req(options, function(error, resp, body) {
                        if (body) {
                            console.log(body);
                        } else if (error) {
                            console.log(error);
                        }
                    });
                },
                'default': () => {
                    // Use the Actions on Google lib to respond to Google requests; for other requests use JSON
                    if (requestSource === googleAssistantRequest) {
                        let responseToUser = {
                            speech: 'これはDialogflowからのメッセージです',
                            text: 'これはDialogflowからのメッセージです'
                        };
                        sendGoogleResponse(responseToUser);
                    } else {
                        let responseToUser = {
                            speech: 'これはDialogflowからのメッセージです',
                            text: 'これはDialogflowからのメッセージです'
                        };
                        sendResponse(responseToUser);
                    }
                }
            };
    
            if (!actionHandlers[action]) {
                action = 'default';
            }
    
            actionHandlers[action]();
        }
    
        exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
            console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers));
            console.log('Dialogflow Request body: ' + JSON.stringify(request.body));
            if (request.body.result) {
                addTask(request, response);
            } else {
                console.log('不正なリクエストです');
                return response.status(400).end('不正なWebhookのリクエストです。v1かv2のWebhookリクエストを利用してください。');
            }
        });
    })();
    &#13;
    &#13;
    &#13;

0 个答案:

没有答案