简体   繁体   中英

Response from Webhook using NodeJS Client

I built a custom webhook as a fulfillment endpoint for a Dialogflow intent. It works fine when I respond with raw JSON, ie: {'fullfillmentText':'hi'} , but does not seem to work using the "actions-on-google" library.

The code from their website implies this should work:

app.intent('myintent', (conv) => {
    conv.close('See you later!');
});

But it does not. Google Home just says my app isn't responding. It might be that as it stands my function (using Fn Project) has to return JSON and if I return JSON as a response that it isn't expecting it fails. Maybe someone can shed some light?

Edit 1: I'm using a custom webhook using the Fn Project open source functions as a service. Demonstrating how to use the project is my purpose here so I don't want to use inline editor or Google Cloud Functions or firebase or any other default option.

Here's the rest of the code

const fdk = require('@fnproject/fdk');
const request = require('request');

const dialogflow = require('actions-on-google');
const app = dialogflow({
  debug: true,
});

fdk.handle(function (input) {
  app.intent('myintent', (conv) => {
    conv.ask('I could not understand. Can you say that again?');
  });

  return {'fulfillmentText': 'response from webhook'}
});

Although you are creating the app object, which does the Intent handler processing and such, and registering a handler with it via app.intent() , you're not doing anything to "export" it so app 's methods are called when the webhook is triggered. When called, it gets the body of the request and will format the JSON for the response.

If, for example, you were using Firebase functions, you would connect app to be handled through the functions with something like

exports.fulfillment = functions.https.onRequest(app);

But you're not. You're using a different framework.

The library comes with a number of frameworks that are supported out of the box, but the Fn Project isn't one of them. In theory, you can create your own Framework object which will do this for you (the "Frameworks" section of this article discusses it briefly, but doesn't go into details about how to do so).

As you surmise, it may be easiest for you to just read the JSON request and generate the JSON response yourself without using the actions-on-google library. Or you can look into a library such as multivocal to see if it would be easier to leverage its multi-framework support.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM