无法在AWS Lambda

时间:2018-05-25 13:58:26

标签: aws-lambda dialogflow

根据DialogFLow Fulfillment docs,WebhookClient构造函数需要Express HTTP请求和响应对象。 但是,在Lambda函数中,我只收到event(请求)。如何创建Express请求和响应对象?

到目前为止我已尝试过这个:

const {WebhookClient} = require('dialogflow-fulfillment');

exports.dialogflowFulfillment = async (event) => {
  let response = {};
  const agent = new WebhookClient({ event, response });

  function sayNiceThings(agent) {
    agent.add(`Nice to meet you!`);
  }

  let intentMap = new Map();
  intentMap.set('Say Nice Things', sayNiceThings);
  agent.handleRequest(intentMap);
};

1 个答案:

答案 0 :(得分:1)

创建NodeJS Express应用

安装无服务器http软件包以添加AWS Lambda网桥

安装对话流程实现和Google npm动作包。

  npm init -f
  npm install --save express serverless-http
  npm install dialogflow-fulfillment
  npm install actions-on-google

创建index.js文件:

index.js:
=========
const serverless = require('serverless-http');
const bodyParser = require('body-parser');
const express = require('express');

const app = express();
app.use(bodyParser.json({ strict: false }));

const {WebhookClient, Card, Suggestion} = require('dialogflow-fulfillment');
const request = require('request');



app.get('/', function (req, res) {

  res.send('Hello World !!!\n');
  console.log("Testing express lambda\n");

})



app.post('/', function (req, res) {


    const agent = new WebhookClient({request: req, response: res});

    function test_handler(agent) {
      agent.add(`Welcome to my agent on AWS Lambda!`);
    }

    // Run the proper function handler based on the matched Dialogflow intent name
    let intentMap = new Map();
    intentMap.set('test-intent', test_handler);

    agent.handleRequest(intentMap);


})


module.exports.handler = serverless(app);

在serverless.yml中添加配置:

serverless.yml
================

service: example-express-lambda

provider:
  name: aws
  runtime: nodejs8.10
  stage: dev
  region: ap-southeast-1

functions:
  app:
    handler: index.handler
    events:
      - http: ANY /
      - http: 'ANY {proxy+}'

然后部署lambda函数。 将端点URL添加到Dialogflow实现Webhook URL中。

参考: https://serverless.com/blog/serverless-express-rest-api/