eran
eran

Reputation: 15136

Running an ApiAi node app on AWS Lambda

I want to set up my fullfilment in AWS Lambda for my Google Assistant. I am using the actions-on-google npm package. To create an ApiAiApp({req,res} I need an http request and response objects.

However, the lambda callback provides a different set of parameters:

exports.handler = function(event, context, callback) {
    const apiAiApp = new ApiAiApp({req:<REQUEST>, res:<RESPONSE>});
}

How do I translate event, context, callback to the <REQUEST> and <RESPONSE>?

(I don't believe I need the context here)

Upvotes: 1

Views: 922

Answers (1)

Michael K
Michael K

Reputation: 11

I was facing the same issue, finally I solved it, via the project "claudia.js" that provide some code to generate a simple proxy that allows to host express apps on AWS Lambda. https://github.com/claudiajs/example-projects/tree/master/express-app-lambda

and https://claudiajs.com/tutorials/serverless-express.html

hint: you end up with two "apps", e.g. if you use the firebase example from the fullfillment in Dialogflow, so you need to rename one, to avoid conflicts.

'use strict';

const googleAssistantRequest = 'google'; // Constant to identify Google Assistant requests
const express = require('express');
const app = express(); 
const DialogflowApp = require('actions-on-google').DialogflowApp; // Google Assistant helper library
const bodyParser = require('body-parser');
var endpoint = "..."; // name of AWS endpoint aka as "Resource path" in API Gateway Trigger setup

...

const urlencodedParser = bodyParser.json({ extended: false });
app.post('/'+ endpoint, urlencodedParser,  (request, response) => {
    console.log('Request headers: ' + JSON.stringify(request.headers));
    console.log('Request body: ' + JSON.stringify(request.body));

    // An action is a string used to identify what needs to be done in fulfillment
    let action = request.body.result.action; // https://dialogflow.com/docs/actions-and-parameters


    ...

    const appDialogFlow = new DialogflowApp({request: request, response: response});

    // Create handlers for Dialogflow actions as well as a 'default' handler
    const actionHandlers = {
        ...
    };

    ...
    // If undefined or unknown action use the default handler
    if (!actionHandlers[action]) {
        action = 'default';
    }

    // Run the proper handler function to handle the request from Dialogflow
    actionHandlers[action]();

    ... some helper functions, etc ...

});

//app.listen(3000) // <-- comment this line out from your app 
module.exports = app;  // <-- link to the proxy that is created viy claudia.js

Upvotes: 1

Related Questions