首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Google Actions & Dialogflow v2 - webhooks

Google Actions & Dialogflow v2 - webhooks
EN

Stack Overflow用户
提问于 2018-10-27 01:33:10
回答 1查看 290关注 0票数 0

背景:我设置了一个简单的DialogFlow来实现两个目的。设置为v1接口并将webhook设置为故障服务器。我使用glitch.com托管一些js来收集信息(由用户请求)。

切换到api的v2打破了这一点。我发现的所有例子都是使用firebase,而不是你自己的服务器来托管java脚本,这就是为什么我卡住了,希望有人能指引我在没有firebase的情况下在v2中从google assistant获取/返回数据的正确方向。

请参阅下面的代码

代码语言:javascript
复制
            // 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()));
            });
EN

回答 1

Stack Overflow用户

发布于 2018-11-10 06:25:49

你有什么特别的理由想要使用你自己的服务器吗?

Firebase会自动处理请求,如果您打算向公众发布您的操作,Google的奖励计划应向您的账户提供200美元/月的云积分,以抵消个人累积的成本。

你可以在这里了解更多关于谷歌奖励和社区计划的信息:https://developers.google.com/actions/community/

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53013717

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档