nealrs
nealrs

Reputation: 445

Get body request parameters using express in V2 Dialogflow

I'm migrating my Google Action from v1 => v2 using an express app, and in the past, I've been able to get url params & initialize my action map like this:

// INITIALIZE EXPRESS APPLICATION & ENDPOINTS
app.use(bodyParser.json({strict: false}));

// POST [TYPE] [PLATFORM] [PUBLISHER] PARAMS => PASS TO FULFILLMENT
app.post('/:platform/:type/:publisher', function(req, res) {
  debugRequest(req);
  console.log(`SENDING TO ${TYPE} => ${PLATFORM} => ${PUBLISHER} FULFILLMENT`);
  fulfillment.fulfillment(req, res);
});

```

With v2, instead of using a .post route with express, I just need to use .use e.g. express().use(bodyParser.json(), app). However, I don't understand how to get the body params (req/res) using this method [still kind of a node newbie] from body parser.

I need the full URL path (type, platform, publisher) from the request in order to fulfill some app logic later on, within various intents.

If someone has a more built out express / v2 Dialogflow example, that'd be very helpful. I have all this working with v1, but times are a changing!

Upvotes: 0

Views: 756

Answers (2)

Shuyang Chen
Shuyang Chen

Reputation: 448

You should be able to get this data now with the new Framework Metadata feature added in 2.2.0. See this GitHub comment for more details.

An object containing framework metadata is now present as the 2nd parameter in the middleware function.

Now you can do something like:

app.middleware((conv, framework) => {
  if (framework.express) {
    conv.expressParams = framework.express.request.expressParams;
  }
});

app.intent('some intent', conv => {
  conv.ask(`Params sent was ${JSON.stringify(conv.expressParams)}`);
});

Upvotes: 1

Nick Felker
Nick Felker

Reputation: 11978

From a similar question in this GitHub issue:

You can retrieve the raw JSON data from conv.request for the core Actions on Google data and conv.body for the entire JSON body.

Upvotes: 0

Related Questions