背景:我设置了一个具有两个意图的简单DialogFlow。设置为api v1并将webhook设置为故障服务器。我使用glitch.com托管了一些js来收集信息(用户要求)。
切换到api的v2可以解决此问题。我发现的所有示例都使用Firebase而不是您自己的服务器来托管Java脚本,这就是为什么我被卡住了,希望有人可以指出正确的方向,以便在v2中将数据发送到Google Assistant或从Google Assistant返回数据火力地堡。
请参见下面的代码
// 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()));
});
答案 0 :(得分:0)
您是否有任何特定原因要使用自己的服务器?
Firebase会自动处理请求,如果您打算公开发布“行动”,则Google的奖励计划应向您的帐户提供每月200美元的Cloud信用额度,以抵消个人应计费用。
您可以在此处了解有关Google的奖励和社区计划的更多信息:https://developers.google.com/actions/community/