0

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()));
            });
Les
  • 33
  • 5

1 Answers1

0

Is there any particular reason you want to use your own server?

Firebase handles requests automatically, and if you intend to publish your Action to the public, Google's rewards program should offer a $200 / monthly Cloud credit towards your account, offsetting personally accrued costs.

You can learn more about Google's rewards & community programs here: https://developers.google.com/actions/community/

Max Wiederholt
  • 668
  • 7
  • 12
  • My code, applied to firebase didn't return the results, using node.js it worked well. The ultimate version would be to have proper authorization setup so through a website each person's account would be linked however I don't have the resources to complete that. So for now the code will be shared and anyone who wishes to use it would need to use their own hosting (Node.js or otherwise) I've completed the code now, will post soon. – Les Nov 12 '18 at 17:06