代码之家  ›  专栏  ›  技术社区  ›  r3plica

Dialogflow节点在没有firebase的情况下实现

  •  0
  • r3plica  · 技术社区  · 6 年前

    我正在尝试使用nodejs构建我的webhook,而不使用firebase。 我找到了一个资源并建立了索引。js文件如下:

    const express = require('express');
    const bodyParser = require('body-parser');
    const {
        dialogflow
    } = require('actions-on-google');
    
    const PORT = process.env.PORT || 5000;
    const app = dialogflow({
        debug: true
    });
    const server = express()
        .use(bodyParser.urlencoded({
            extended: true
        }))
        .use(bodyParser.json(), app)
        .listen(PORT, () => console.log(`Listening on ${ PORT }`));
    
    // Handle the Dialogflow intent named 'Default Welcome Intent'.
    app.intent('Default Welcome Intent', (conv) => {
        const name = conv.user.storage.userName;
        if (!name) {
          // Asks the user's permission to know their name, for personalization.
          conv.ask(new Permission({
            context: 'Hi there, to get to know you better',
            permissions: 'NAME',
          }));
        } else {
          conv.ask(`Hi again, ${name}. What are you looking for?`);
        }
      });
    
      // Handle the Dialogflow intent named 'actions_intent_PERMISSION'. If user
      // agreed to PERMISSION prompt, then boolean value 'permissionGranted' is true.
      app.intent('actions_intent_PERMISSION', (conv, params, permissionGranted) => {
        if (!permissionGranted) {
          // If the user denied our request, go ahead with the conversation.
          conv.ask(`OK, no worries. What are you looking for?`);
          conv.ask(new Suggestions('Blue', 'Red', 'Green'));
        } else {
          // If the user accepted our request, store their name in
          // the 'conv.user.storage' object for future conversations.
          conv.user.storage.userName = conv.user.name.display;
          conv.ask(`Thanks, ${conv.user.storage.userName}. ` +
            `What are you looking for?`);
          conv.ask(new Suggestions('Blue', 'Red', 'Green'));
        }
      });
    
      // Handle the Dialogflow NO_INPUT intent.
      // Triggered when the user doesn't provide input to the Action
      app.intent('actions_intent_NO_INPUT', (conv) => {
        // Use the number of reprompts to vary response
        const repromptCount = parseInt(conv.arguments.get('REPROMPT_COUNT'));
        if (repromptCount === 0) {
          conv.ask('Which color would you like to hear about?');
        } else if (repromptCount === 1) {
          conv.ask(`Please say the name of a color.`);
        } else if (conv.arguments.get('IS_FINAL_REPROMPT')) {
          conv.close(`Sorry we're having trouble. Let's ` +
            `try this again later. Goodbye.`);
        }
      });    
    
    module.exports = server;
    

    我的 诺德斯 应用程序只包括 指数js 包裹json . 我做了一个 npm install 然后将这两个文件和节点模块ftp到我的azure web应用程序中。当我测试我的google actions应用程序时,它出现了一个webhook错误,因为它无法通信。

    有人能告诉我需要做什么才能让它工作吗?

    包裹json :

    {
      "name": "pyb-actions",
      "description": "Actions on Google for PYB",
      "author": "r3plica",
      "private": true,
      "scripts": {
        "lint": "eslint .",
        "start": "npm run shell"
      },
      "dependencies": {
        "actions-on-google": "^2.0.0",
        "express": "^4.16.4",
        "i18n": "^0.8.3"
      },
      "devDependencies": {
        "eslint": "^4.19.0",
        "eslint-config-google": "^0.9.1"
      }
    }
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   theisof    6 年前

    尝试创建simpel get路由,以测试问题是否与服务器设置或应用程序初始化方式有关。

    app.get('/', function(request,response) {
      response.json({
        "response": "The server is runnning"
      });
    });
    

    或者,尝试通过 ngrok 并将其用作临时实现webhook。

    这是我的索引。灵感来源:

    const express = require('express');
    const bodyParser = require('body-parser')
    const { dialogflow } = require('actions-on-google')
    const intents = require('./intents')
    
    const expressApp = express().use(bodyParser.json())
    const app = dialogflow()
    
    app.intent('Welcome', intents.welcome)
    
    expressApp.post('/fulfill', (req, resp) => app)
    
    expressApp.listen(process.env.PORT || 3000, '0.0.0.0', () => {
      console.log("server starting " + (process.env.PORT || 3000));
    })
    

    希望有帮助

    推荐文章