dialogflow / dialogflow-fulfillment-nodejs

Dialogflow agent fulfillment library supporting v1&v2, 8 platforms, and text, card, image, suggestion, custom responses
Apache License 2.0
598 stars 281 forks source link

Unhandled promise rejections are deprecated #321

Open XNinety9 opened 4 years ago

XNinety9 commented 4 years ago

Hi there.

I've been using dialogflow-fulfillment-nodejs library for a few days now, so far, so good.

But when trying to request data from an external API within the fulfillment handler, I've been facing a tenacious error:

(node:67018) UnhandledPromiseRejectionWarning: Error: No responses defined for platform: DIALOGFLOW_CONSOLE
    at V2Agent.sendResponses_ (/Users/x99/Documents/Repositories/PROJECT/functions/node_modules/dialogflow-fulfillment/src/v2-agent.js:243:13)
    at WebhookClient.send_ (/Users/x99/Documents/Repositories/PROJECT/functions/node_modules/dialogflow-fulfillment/src/dialogflow-fulfillment.js:505:17)
    at /Users/x99/Documents/Repositories/PROJECT/functions/node_modules/dialogflow-fulfillment/src/dialogflow-fulfillment.js:316:38
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at emitUnhandledRejectionWarning (internal/process/promises.js:168:15)
    at processPromiseRejections (internal/process/promises.js:247:11)
    at processTicksAndRejections (internal/process/task_queues.js:94:32)

Here's the code:

const { response } = require('express');
var https = require ('https');

function httpRequest(params, postData) {
    return new Promise(function(resolve, reject) {
        var req = https.request(params, function(res) {
            // reject on bad status
            if (res.statusCode < 200 || res.statusCode >= 300) {
                return reject(new Error('statusCode=' + res.statusCode));
            }
            // cumulate data
            var body = [];
            res.on('data', function(chunk) {
                body.push(chunk);
            });
            // resolve on end
            res.on('end', function() {
                try {
                    body = JSON.parse(Buffer.concat(body).toString());
                } catch(e) {
                    reject(e);
                }
                resolve(body);
            });
        });

        // reject on request error
        req.on('error', function(err) {
            // This is not a "Second reject", just a different sort of failure
            reject(err);
        });

        if (postData) {
            req.write(postData);
        }

        // IMPORTANT
        req.end();
    })
    .catch(function (error) {
        console.error("Promise rejected");
    });
}

exports.howManyTasks = function(agent) {
    console.log("[INFO] howManyTasks");

    const host = "myapihost.com"
    const path = "/api/users/myusertoken/task_lists";
    const bearerToken = "mybearertoken";

    var params = {        
        hostname: host,
        port: 443,
        path: path,
        method: 'GET',
        headers:{
            Authorization: ' Bearer ' + bearerToken            
       }
    };

    httpRequest(params).then(function(body) {
        console.log(body);
    })
    .catch(function () {
        console.error("Promise Rejected");
    });

    console.log("[INFO] I'm out!");
}

From what I see in the logs, the error comes from the library itself? Knowing that beyond the error, my code successfully retrieves the needed data from the API.

DIALQGUE commented 2 years ago

I have this error too, my code worked before when I deploy it. But after few hours (or other trigger, I don't know for sure) Dialogflow fulfillment's requests have changed and now it come with "originalDetectIntentRequest" field. And I stuck here for 3 days.

Are you found any solution for this? Or alternative library to use with dialogflow fulfillment? Since this library is deprecated.