没有Firebase的Dialogflow Node.js实现

时间:2019-02-11 11:25:57

标签: node.js azure dialogflow

我试图使用nodejs而不使用firebase构建我的webhooks。 我找到了资源,并像这样构建了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.js package.json 组成。 我做了一个npm install,然后将两个文件和node_modules ftp到了我的azure Web应用程序中。当我测试我的google action应用程序时,由于无法通信而收到了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"
  }
}

1 个答案:

答案 0 :(得分:0)

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

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

或者,尝试通过ngrok转发您的本地主机,并将其用作临时实现网络挂钩。

这是我的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));
})

希望有帮助