我正试图实现一个对话流网络书来连接到wordpress api,但收到了一个错误:没有为平台定义响应:null



这是我的代码。如何配置它以返回对Google Assistant集成的响应?我想在Dialogflow内联代码编辑器中使用这些代码,或者将其部署到firebase函数中。

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 getProjects(agent){
const api = 'https://playhacker.com/wp-json/wp/v2';
const tag = agent.parameters.tag;
//let url = `${api}/posts?tags=${tag}&_embed`;

let getPosts = (tag, callback) => {
let url = `${api}/posts?tags=${tag}&_embed`;
request({url}, (err, res, body) => {
if (err) {
callback('Sorry, there was an error getting posts from our blog', err);
return;
} else {
let posts = JSON.parse(body);
if (posts.length === 0) {
callback(`It does not seem like there is any content available on this topic`);
return;
} else {
let formattedPosts = posts.map((post) => {
return {
"payload": {
"google": {
"expectUserResponse": true,
"richResponse": {
"items": [
{
"simpleResponse": {
"textToSpeech": "Here is a project we found for you"
}
},
{
"basicCard": {
"title": post.title.rendered,
"subtitle": "Project",
"formattedText": post.excerpt.rendered.replace(/<(?:.|\n)*?>/gm, '').replace(/&[^\s]*/, ''),
"image": {
"url": post._embedded['wp:featuredmedia'][0].media_details.sizes.listing.source_url,
"accessibilityText": "featured image"
},
"buttons": [
{
"title": "Read more",
"openUrlAction": {
"url": post.link
}
}
],
"imageDisplayOptions": "CROPPED"
}
}
]
}
}
}
};
});
formattedPosts.unshift({
type: 0,
platform: 'google',
speech: 'Sure, here are some helpful projects'
});
callback(undefined, formattedPosts);
return;
}
}
});
};

}
let intentMap = new Map();
intentMap.set('getProjects',getProjects);
agent.handleRequest(intentMap);
});

当我在Dialogflow在线编辑器中运行它时,我得到了以下错误:

{
insertId: "000001-ec48afd6-c286-47e4-8c75-43a2c6f1fc08"  
labels: {…}  
logName: "projects/apologetic-robot-fiajxe/logs/cloudfunctions.googleapis.com%2Fcloud-functions"  
receiveTimestamp: "2020-05-03T06:38:00.862369842Z"  
resource: {
labels: {…}   
type: "cloud_function"   
}
severity: "ERROR"  
textPayload: "Error: No responses defined for platform: null
at V2Agent.sendResponses_ (/srv/node_modules/dialogflow-fulfillment/src/v2-agent.js:243:13)
at WebhookClient.send_ (/srv/node_modules/dialogflow-fulfillment/src/dialogflow-fulfillment.js:505:17)
at promise.then (/srv/node_modules/dialogflow-fulfillment/src/dialogflow-fulfillment.js:316:38)
at <anonymous>
at process._tickDomainCallback (internal/process/next_tick.js:229:7)"  
timestamp: "2020-05-03T06:37:59.833Z"  
trace: "projects/apologetic-robot-fiajxe/traces/29d9241c46088463269b00ecf3558974"  
}

错误Error: No responses defined意味着您没有定义任何要发送到对话流的响应,这些响应可以显示给聊天机器人用户。

例如,当代码流到达函数getProjects时,在从函数返回之前,应该有对dialogflow的响应,类似

agent.add(`There seems to be some error`);

其将依次向用户显示该字符串。

另外,在您上面的例子中,我看不到您的getPosts函数在任何地方被调用。

相关内容

  • 没有找到相关文章

最新更新