Dialogflow webhook fulfillment parameter is not working in post API

爱⌒轻易说出口 提交于 2019-12-25 01:46:54

问题


I make a code in webhook were i want invoke POST API and i want to invoke that api for that i have to pass some parameter but whenever i am trying to pass parameter coming from dialogflow its gives error. My code is like that

//Self Hosted Express Server

const bodyParser = require('body-parser')
var request = require('request-promise-native');
const { dialogflow } = require('actions-on-google');
const assistant = dialogflow({
  clientId: "305xxxxxx7-rv9kocdq2xxxxouuq8f9ul2eg.apps.googleusercontent.com"
});


module.exports = (app) => {
  const logger = console;

assistant.intent('Sales',(conv, params) => {
 var  pcode = params.myproduct;

// console.log(pcode)

    const token = '3369708919812376';
    const serviceID = '502';
    const P_STATE_CD = 'ALL';
    const P_FO_CD = 'ALL';
    const P_DISTT_CD = 'ALL';
    const P_DATE = '16/12/2019';
    const  P_PRD_GROUP = pcode;
    const P_PERSONAL_NO = '106296';
        var data = {"token" : token,"serviceID" : serviceID,"P_STATE_CD" : P_STATE_CD,"P_FO_CD" : P_FO_CD,"P_DISTT_CD" : P_DISTT_CD,"P_DATE" : P_DATE,"P_PRD_GROUP" : P_PRD_GROUP ,"P_PERSONAL_NO" : P_PERSONAL_NO };
        var sdata = JSON.stringify(data);

                    const options = {
                        method: 'POST',
                        uri: 'http://chatbotWebservice/resources/webservice/service' ,
                        body: JSON.parse(sdata) ,
                        json: true
                    }
        return request(options)
            .then( body => {
                 var unit = body
                unit.intent = "Sales"
                unit.value1 = unit.saleInfo[0].QMTD
                unit.value2 = unit.saleInfo[0].QYTD
                unit.value3 = unit.saleInfo[0].O_UOM
                unit.value4 = null
                unit.value5 = null

                delete unit.saleInfo
                var unit2 = JSON.stringify(unit)
                console.log(unit2)

          conv.ask(unit2);
              })
              .catch( err => {
               console.error( err );
               conv.ask('Something went wrong. What should I do now?');
                 });
  })

And the error like this

TypeError: Cannot read property '0' of undefined
    at request.then.body (/home/dbalounge/GoogleDF/service.js:40:44)
    at process._tickCallback (internal/process/next_tick.js:68:7)

Please help me out this. Thank You in Advance


回答1:


Apparently body is coming as a string, probably because the server is not setting the correct Content-Type to the response, and request is ignoring json: true option. So you should use JSON.parse on it, and then access the saleInfo

return request(options)
   .then( body => {
    var unit = JSON.parse(body)
    unit.intent = "Sales"
    unit.value1 = unit.saleInfo[0].QMTD
    /* ... */
 });

Aside from that body: JSON.parse(sdata) in your call is not needed, you're stringifying data to sdata to parse it back again, just use data directly:

const options = {
    method: 'POST',
    uri: 'http://chatbotWebservice/resources/webservice/service' ,
    body: data,
    json: true
}


来源:https://stackoverflow.com/questions/59446872/dialogflow-webhook-fulfillment-parameter-is-not-working-in-post-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!