首页 > 解决方案 > 没有firebase的Dialogflow nodejs实现

问题描述

我正在尝试使用 nodejs 而不使用 firebase 来构建我的 webhook。我找到了一个资源并像这样构建了我的 index.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;

我的NodeJs应用程序只包含index.jspackage.json。我做了一个npm install然后将这两个文件和 node_modules 传输到我的 azure Web 应用程序。当我测试我的 google 操作应用程序时,它收到一个 webhook 错误,因为它无法通信。

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

PS:这是我的package.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"
  }
}

标签: node.jsazuredialogflow-es

解决方案


尝试创建一个简单的 get-route 来测试问题是否与您的服务器设置或您初始化应用程序的方式有关。

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

或者,尝试通过ngrok转发您的 localhost并将其用作临时履行 webhook。

这是我的 index.js 的灵感:

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));
})

希望能帮助到你


推荐阅读