背景:我设置了一个简单的DialogFlow,有两个意图 . 设置为api v1和webhook到故障服务器 . 我使用glitch.com来托管一些js来收集信息(由用户请求) .
切换到api的v2打破了这一点 . 我发现的所有示例都使用firebase而不是你自己的服务器来托管java脚本,这就是为什么我被困住了,希望有人可以指出我在v2中获取/返回谷歌助手数据的正确方向火力点 .
见下面的代码
// init project pkgs
const express = require('express');
const ApiAiAssistant = require('actions-on-google').ApiAiAssistant;
const bodyParser = require('body-parser');
const request = require('request');
const app = express();
const Map = require('es6-map');
// Pretty JSON output for logs
const prettyjson = require('prettyjson');
const toSentence = require('underscore.string/toSentence');
app.use(bodyParser.json({type: 'application/json'}));
app.use(express.static('public'));
// http://expressjs.com/en/starter/basic-routing.html
app.get("/", function (request, response) {
response.sendFile(__dirname + '/views/index.html');
});
// Handle webhook requests
app.post('/', function(req, res, next) {
// Log the request headers and body to help in debugging.
// Check the webhook requests coming from Dialogflow by clicking the Logs button the sidebar.
logObject('Request headers: ', req.headers);
logObject('Request body: ', req.body);
// Instantiate a new API.AI assistant object.
const assistant = new ApiAiAssistant({request: req, response: res});
// Declare our action parameters
const PRICE_ACTION = 'price';
// Create functions to handle price query
function getPrice(assistant) {
console.log('** Handling action: ' + PRICE_ACTION);
let requestURL = 'https://blockchain.info/q/24hrprice';
request(requestURL, function(error, response) {
if(error) {
console.log("got an error: " + error);
next(error);
} else {
let price = response.body;
logObject('the current bitcoin price: ' , price);
// Respond to the user with the current temperature.
assistant.tell("The current bitcoin price is " + price);
}
});
}
// Add handler functions to the action router.
let actionRouter = new Map();
actionRouter.set(PRICE_ACTION, getPrice);
actionRouter.set(STATUS_ACTION, getStatus);
// Route requests to the proper handler functions via the action router.
assistant.handleRequest(actionRouter);
});
// Handle errors
app.use(function (err, req, res, next) {
console.error(err.stack);
res.status(500).send('Oppss... could not check the bitcoin price');
})
// Pretty print objects for logging
function logObject(message, object, options) {
console.log(message);
console.log(prettyjson.render(object, options));
}
// Listen for requests
let server = app.listen(process.env.PORT, function () {
console.log('--> Our Webhook is listening on ' + JSON.stringify(server.address()));
});
1 回答
你想使用自己的服务器有什么特别的原因吗?
Firebase会自动处理请求,如果您打算向公众发布您的操作,Google的奖励计划应为您的帐户提供每月200美元的Cloud积分,抵消个人应计费用 .
您可以在此处详细了解Google的奖励和社区计划:https://developers.google.com/actions/community/