Dialogflow Webhook实现参数在Post API中不起作用 [英] Dialogflow webhook fulfillment parameter is not working in post API
问题描述
我在webhook中编写了一个代码,我想调用POST API,并且我想为此调用api,所以我必须传递一些参数,但是每当我尝试传递来自dialogflow的参数时,都会出错。我的代码就是这样
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?');
});
})
像这样的错误
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
推荐答案
显然, body
是字符串,可能是因为服务器未将正确的 Content-Type
设置为响应,并且 request
忽略了 json:true
选项。因此,您应该在其上使用 JSON.parse
,然后访问 saleInfo
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
/* ... */
});
除了那个 body:JSON.parse(sdata)$ c不需要通话中的$ c>,而是将
data
字符串化为 sdata
以便再次解析回去直接使用 data
:
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
}
这篇关于Dialogflow Webhook实现参数在Post API中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!