Background: I setup a simple DialogFlow to with two intents. Set as api v1 and webhook to a glitch server. I used glitch.com to host some js to gather information (requested by the user).
Switching to v2 of the api breaks this. All the examples I've found use firebase as opposed to your own server to host the java script which is why I'm stuck and hoping someone can point me in the right direction for getting/returning data to/from google assistant in v2 without firebase.
See code below
// 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()));
});