Actions on google 如何调用restapi在googleassistant上执行操作
我已经看到了一些答案,但对于如何调用restapi在googleassistant上执行操作仍然有点困惑 这是我的密码:Actions on google 如何调用restapi在googleassistant上执行操作,actions-on-google,Actions On Google,我已经看到了一些答案,但对于如何调用restapi在googleassistant上执行操作仍然有点困惑 这是我的密码: 'use strict'; // Import the Dialogflow module from the Actions on Google client library. const {dialogflow} = require('actions-on-google'); // Import the firebase-functions package for de
'use strict';
// Import the Dialogflow module from the Actions on Google client library.
const {dialogflow} = require('actions-on-google');
// Import the firebase-functions package for deployment.
const functions = require('firebase-functions');
const http = require('https');
// Instantiate the Dialogflow client.
const app = dialogflow({debug: true});
// Handle the Dialogflow intent named 'favorite color'.
// The intent collects a parameter named 'color'.
app.intent('favorite color', (conv, {any}) => {
//const luckyNumber = any.length;
// Respond with the user's lucky number and end the conversation.
//conv.close('Your lucky number is ' + luckyNumber);
return callApi(any).then((output) => {
console.log(output);
conv.close(`I found ${output.length} items!!`);
}).catch(() => {
conv.close('Error occurred while trying to get vehicles. Please try again later.');
});
});
function callApi (any){
return new Promise((resolve, reject) => {
// Create the path for the HTTP request to get the vehicle
//Make the HTTP request to get the vehicle
http.get({host: 'hawking.sv.cmu.edu', port: 9023, path: '/dataset/temporary'}, (res) => {
let body = ''; // var to store the response chunks
res.on('data', (d) => { body += d; }); // store each response chunk
res.on('end', () => {
// After all the data has been received parse the JSON for desired data
let response = JSON.parse(body);
let output = {};
//
// //copy required response attributes to output here
//
console.log(response.length.toString());
resolve(output);
// callback(output);
});
res.on('error', (error) => {
console.log(`Error calling the API: ${error}`)
reject();
});
}); //http.get
// let output = {};
// resolve(output);
}); //promise
}
// Set the DialogflowApp object to handle the HTTPS POST request.
exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app);
它总是表明:
畸形反应
必须设置“最终响应”
似乎http.get是异步的,在发出http请求之前,它已经返回了结果,而不必等待请求完成
我在app.intent函数中添加了“return”,但仍然不起作用
有什么建议吗
谢谢 你在使用Firebase的免费层吗?在免费层,出站URL连接被禁用:不…我正在使用付费连接。。。我不知道为什么我不能点击应用程序上的url。意图。。。今天我尝试了另一种方法。您是否正在记录承诺的结果,以查看它是否被正确调用?可能是您的逻辑中有一个错误导致webhook代码崩溃。我今天尝试了这个,它表明在模拟器中:无信息返回IsOutside如果我创建一个nodejs文件并在我的机器上运行相同的代码,一切都正常。我确信url没有问题
app.intent('demo14mata', (conv, {any}) => {
var html = ""
http.get(url,(res)=>{
res.on("data",(data)=>{
html+=data
})
res.on("end",()=>{
console.log(html)
conv.close('return info is' + html);
})
}).on("error",(e)=>{
console.log('something wrong')
conv.close('no info return');
})
conv.close('no info return is(outside)' + html);
});