creating dialogflow v2 project with serverless

2019-08-29 09:34发布

问题:

Having issues trying to run a new AWS/Serverless/Dialogflow project. I am sure it is something simple that I am just not seeing.

Steps

  • Created initial project using: serverless create --template aws-nodejs-typescript

  • moved handler.js to src/ & updated serverless.yml

  • npm installed actions-on-google
  • followed the actions-on-google example and updated src/handler.js

    import { dialogflow, Image } from 'actions-on-google';
    
    const app = dialogflow({debug: true});
    
    app.intent("test.intent", (conv) => {
      conv.ask("Hi, how is it going?");
      conv.ask("Here is a picture of a cat!");
      conv.ask(new Image({
        url: "https://developers.google.com/web/fundamentals/accessibility/semantics-builtin/imgs/160204193356-01-cat-500.jpg",
        alt: "A fluffy cat!"
      }));
    });
    
    exports.fulfillment = app;
    
  • also updated tsconfig.json to match another Typescript project

    {
      "compilerOptions": {
        "sourceMap": true,
        "target": "es6",
        "allowJs": true,
        "module": "commonjs"
      },
      "exclude": [
        "node_modules"
      ],
      "include": [
        "./src/**/*"
      ]
    }
    

For thoroughness here is my serverless.yml. (I manually created the API Gateway because serverless creates the lambda-proxy and I haven't looked into the other config.)

service:
  name: test-lambda

# Add the serverless-webpack plugin
plugins:
  - serverless-webpack

provider:
  name: aws
  runtime: nodejs6.10

functions:
  fulfillment:
    handler: src/handler.fulfillment
    # events:
    #   - http:
    #       method: get
    #       path: hello

Error

The project compiles and deploys successfully but when the lambda is called I keep getting

(node:1) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: Cannot convert undefined or null to object

P.S. the example source chose to use a cat!

回答1:

Finally had some time to circle back to this problem and came across this git issue.

Essentially the dialogflow instance needed to be encapsulated by the lambda.

exports.fulfillment = function(event, context, callback) {
  app.handler(event, {})
    .then((res) => {
      if (res.status != 200) {
        callback(null, {
          "fulfillmentText": `I got status code: ${res.status}`
        });
      } else {
        callback(null, res.body);
      }
    }).catch((e) => {
      callback(null, {
        "fulfillmentText": `There was an error\n${e}`
      });
    });
};