简体   繁体   中英

Dialogflow Fulfillment Webhook - Request Payload is always blank

I have tried automated Fulfillment Webhook and Coded in Node in the inline editor, but I can't seem to get the PAYLOAD populated. I successfully connect to my API, but it is looking for the orderNumber in the PAYLOAD which is empty? What is needed to get the Payload to contain my parameters?

Intent Image Intent Pic 1

Intent Pic 2 Intent Pic 2

Below info from the Diagnostic Info

{
  "responseId": "7b8c877b-fc06-405a-8428-1959493f870d-a14fa99c",
  "queryResult": {
    "queryText": "10620054",
    "parameters": {
      "orderNumber": "10620054"
    },
    "allRequiredParamsPresent": true,
    "outputContexts": [
      {
        "name": "projects/rrd-order-bot-cxniiq/agent/sessions/199d2c42-4d42-15b6-6329-b116a51991e9/contexts/order",
        "lifespanCount": 5,
        "parameters": {
          "orderNumber": "10620054",
          "orderNumber.original": "10620054"
        }
      },
      {
        "name": "projects/rrd-order-bot-cxniiq/agent/sessions/199d2c42-4d42-15b6-6329-b116a51991e9/contexts/status",
        "lifespanCount": 5,
        "parameters": {
          "orderNumber": "10620054",
          "orderNumber.original": "10620054"
        }
      },
      {
        "name": "projects/rrd-order-bot-cxniiq/agent/sessions/199d2c42-4d42-15b6-6329-b116a51991e9/contexts/status",
        "lifespanCount": 5,
        "parameters": {
          "orderNumber": "10620054",
          "orderNumber.original": "10620054"
        }
      }
    ],
    "intent": {
      "name": "projects/rrd-order-bot-cxniiq/agent/intents/9a5f61e5-e2cc-44c9-8cb3-53a5d43ec0fc",
      "displayName": "Order information"
    },
    "intentDetectionConfidence": 0.01,
    "languageCode": "en"
  },
  "originalDetectIntentRequest": {
    "payload": {}
  },
  "session": "projects/rrd-order-bot-cxniiq/agent/sessions/199d2c42-4d42-15b6-6329-b116a51991e9"
}

Below is the Inline editor code I have tried in addition to the automatic Webhook.

// See https://github.com/dialogflow/dialogflow-fulfillment-nodejs
// for Dialogflow fulfillment library docs, samples, and to report issues
'use strict';

const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
//var querystring = require('querystring');

process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements

exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
  const agent = new WebhookClient({ request, response });
  console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers));
  console.log('Dialogflow Request body: ' + JSON.stringify(request.body));

  function welcome(agent) {
    agent.add(`Welcome to my agent!`);
  }

  function fallback(agent) {
    agent.add(`I didn't understand`);
    agent.add(`I'm sorry, can you try again?`);
  }

  function orderHandler(agent) {
    const order = agent.parameters.orderNumber;
    const https = require('https');
    const querystring = require('querystring');

    const parameters = {orderNumber: order};

    const post_data = querystring.stringify(parameters);

    const options = {
      hostname: 'xxxxxxxxxxxx', (removed for question)
      port: 443,
      path: '/RRD_OrderStatusWeb/v1/getRRDOrderStatus',
      method: 'POST' 
    };

const request = https.request(options, (response)=>{
    let chunks_of_data = [];

    response.on('data', (fragments) => {
        chunks_of_data.push(fragments);
    });

    response.on('end', () => {
        let response_body = Buffer.concat(chunks_of_data);
        console.log(response_body.toString());
    });

    response.on('error', (error) => {
        console.log(error);
    });
});

request.on('error', (error) => {
    console.log('Error Code: ' + error.code);
    console.log('Error Message: ' + error.message);
});

request.write(post_data);
request.end();    


    //agent.add('intent called: ' + order);
}

  // Run the proper function handler based on the matched Dialogflow intent name
  let intentMap = new Map();
  intentMap.set('Default Welcome Intent', welcome);
  intentMap.set('Default Fallback Intent', fallback);
  intentMap.set('Order information', orderHandler);
  agent.handleRequest(intentMap);
});

Correct options for my JASON API call are here. Currently only chunking response lengths here...

// See https://github.com/dialogflow/dialogflow-fulfillment-nodejs
// for Dialogflow fulfillment library docs, samples, and to report issues
'use strict';

const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');

process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements

exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
  const agent = new WebhookClient({ request, response });
  console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers));
  console.log('Dialogflow Request body: ' + JSON.stringify(request.body));

  function welcome(agent) {
    agent.add(`Welcome to my agent!`);
  }

  function fallback(agent) {
    agent.add(`I didn't understand`);
    agent.add(`I'm sorry, can you try again?`);
  }

  function orderHandler(agent) {
    const order = agent.parameters.orderNumber;
    const https = require('https');
//    const querystring = require('JSON');
//    const parameters = {orderNumber: order};
    var auth = "Basic " + new Buffer("userid here" + ":" + "password here").toString("base64");

    const post_data = JSON.stringify({
      orderNumber: order
    });

    const options = {
      hostname: 'host url here',
      port: 443,
      path: 'path here',
      method: 'POST',
      headers: {
        'Authorization': auth,
        'Content-Type': 'application/json',
        'Content-Length': post_data.length
    }
    };

const request = https.request(options, (response)=>{
var chunks = '';

  response.on("data", function (chunk) {
    console.log(chunk.length);
    chunks += chunk;
  });

  response.on("end", function () {
    const object = JSON.parse(chunks);
    console.log(object.length);
    console.log(Buffer.byteLength(chunks, 'utf8') / 1024 + " kbytes");
  });
});

request.on('error', (error) => {
    console.log('Error Code: ' + error.code);
    console.log('Error Message: ' + error.message);
});

//agent.add('request data ' + request + post_data);    

request.write(post_data);
request.end();    

}

  // Run the proper function handler based on the matched Dialogflow intent name
  let intentMap = new Map();
  intentMap.set('Default Welcome Intent', welcome);
  intentMap.set('Default Fallback Intent', fallback);
  intentMap.set('Order information', orderHandler);
  agent.handleRequest(intentMap);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM