简体   繁体   English

Dialogflow webhook 实现参数在 post API 中不起作用

[英]Dialogflow webhook fulfillment parameter is not working in post API

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.我在 webhook 中编写了一个代码,我想调用 POST API 并且我想调用那个 api 我必须传递一些参数,但是每当我试图传递来自 dialogflow 的参数时,它都会给出错误。 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先感谢您

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.显然body是一个字符串,可能是因为服务器没有为响应设置正确的Content-Type ,并且request忽略了json: true选项。 So you should use JSON.parse on it, and then access the saleInfo所以你应该对它使用JSON.parse ,然后访问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:除了该body: JSON.parse(sdata)不需要调用中的body: JSON.parse(sdata) ,您将data字符串sdata以再次解析它,只需直接使用data

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Dialogflow webhook 实现参数无法访问 - Dialogflow webhook fulfillment parameter not accessible 如何制作多语言Dialogflow Webhook /实现? - How to make a multiple language Dialogflow webhook/fulfillment? 如何使用 nodejs 在 Dialogflow 中处理 webhook 实现 - How to handle webhook fulfillment in Dialogflow using nodejs Dialogflow Fulfillment Webhook - 请求有效负载始终为空 - Dialogflow Fulfillment Webhook - Request Payload is always blank 获取原始参数输入-Dialogflow实现 - Get original parameter input - Dialogflow Fulfillment Dialogflow履行不适用于Mongoose以连接到MongoDB - Dialogflow fulfillment is not working with Mongoose to connect to MongoDB 如何为所选实体提供动态答案作为对话流实现中的参数 - How to provide dynamic answer for the selected entity as parameter in dialogflow fulfillment 在Dialogflow Fulfillment中使用第三方API - Using 3rd party API within Dialogflow Fulfillment 使用Dialogflow实现的简单HTTP get API请求 - Simple HTTP get request to API using Dialogflow fulfillment Dialogflow NodeJs V2-Webhook方法调用在完成回调之前结束 - Dialogflow NodeJs Fulfillment V2 - webhook method call ends before completing callback
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM