我试着在Dialogflow中用履行(nodejs)发送自定义的payload。
我的动机是发送链接和文本作为对网页的响应。
我的示例代码供参考。
const response = {
messages: [
{
payload: {
messages: [
{
speech: 'here are some quick links for your convenience.',
linkmessage: [{
message: 'google',
link: 'www.google.com'
}, {
message: 'yahoo',
link: 'www.yahoo.co.in'
}],
button: [{
buttonname: 'more page'
}]
}
]
}
}
]
};
agent.add(new Payload(agent.UNSPECIFIED, response, { rawPayload: true, sendAsMessage: true}));
这里我得到的响应是:
{
"fulfillment": {
"speech": "",
"messages": [
{
"lang": "en",
"type": 0,
"speech": ""
}
],
"data": {
"null": {
"messages": [
{
"payload": {
"messages": [
{
"speech": "here are some quick links for your convenience.",
"linkmessage": [{
"message": "google",
"link": "www.google.com"
}, {
"message": "yahoo",
"link": "www.yahoo.co.in"
}],
"button": [{
"buttonname": "more page"
}]
}
]
}
}
]
}
}
}
在这里,你可以看到在响应中,我得到"无效"对象被附加在响应的数据对象里面。
有谁能帮我删除这个 无效 对话流响应中的对象,以及其他任何从履行nodejs中发送自定义payload的选项。
function update() {
const response = {
messages: [
{
payload: {
messages: [
{
speech: 'here are some quick links for your convenience.',
linkmessage: [{
message: 'google',
link: 'www.google.com'
}, {
message: 'yahoo',
link: 'www.yahoo.co.in'
}],
button: [{
buttonname: 'more page'
}]
}
]
}
}
]
};
agent.add(new Payload(agent.UNSPECIFIED, response, { rawPayload: true, sendAsMessage: true}));
}
在这个函数中需要添加参数为agent,这样你就可以得到非 "null "的对象。
需要更新参考编码。
function update(agent) {
const response = {
messages: [
{
payload: {
messages: [
{
speech: 'here are some quick links for your convenience.',
linkmessage: [{
message: 'google',
link: 'www.google.com'
}, {
message: 'yahoo',
link: 'www.yahoo.co.in'
}],
button: [{
buttonname: 'more page'
}]
}
]
}
}
]
};
agent.add(new Payload(agent.UNSPECIFIED, response, { rawPayload: true, sendAsMessage: true}));
}