actions-on-google / actions-on-google-nodejs

Node.js client library for Actions on Google
https://actions-on-google.github.io/actions-on-google-nodejs
Apache License 2.0
900 stars 197 forks source link

"DialogflowApp is not a constructor" #355

Open glittle opened 4 years ago

glittle commented 4 years ago

I'm upgrading from v1, and am getting

TypeError: DialogflowApp is not a constructor

My package.json file includes:

"actions-on-google": "^2.12.0",

and the code includes:

const DialogflowApp = require('actions-on-google').DialogflowApp;

and this line, which is failing:

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

This follows the documentation exactly (as shown in the VS Code IntelliSense). However, it is not the same as the intro documentation in github.

Fleker commented 4 years ago

I don't know which VS Code Intellisense documentation you're using. However, you should consider the GitHub README and documentation as the source of truth. The code you're using is outdated. It should be:

const {dialogflow} = require('actions-on-google')
const app = dialogflow()
Canain commented 4 years ago

I think you might have installed @types/actions-on-google which is outdated and is only for v1.

The v2 library includes its own types that are updated.

You should be able to remove that dependency to get the correct types.

glittle commented 4 years ago

@Canain Here's my complete package.json file. Also, the node_modules folder does not have a @types/actions-on-google folder.

"dependencies": {
    "actions-on-google": "^2.12.0",
    "body-parser": "^1.19.0",
    "dotenv": "^8.1.0",
    "express": "^4.17.1",
    "firebase-admin": "^8.5.0",
    "moment": "^2.24.0",
    "moment-timezone": "^0.5.26",
    "node-rest-client": "^3.1.0"
},
"devDependencies": {
    "semistandard": "^14.1.0"
}
glittle commented 4 years ago

Thanks @Fleker. I'm finding the actions on google documentation to be quite extensive, but almost impossible to use.

The old code always passed request and response into the constructor, but the sample you refer to does not, and I've not found a good explanation of how to tell the app how to tap into the request and response. Can you point me to the right documentation for that? And while you are at it, where can I find the equivalent for the app.getArgument('x') and app.getIntent() from the old SDK?

Fleker commented 4 years ago

The app object directly handles the request/response types. As per our README, there are ways to directly ingest the types. Like Firebase Functions:

const functions = require('firebase-functions')

// ... app code here

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

The intent names are defined as part of your app handlers. Parameters are provided as a second argument to each intent handler.

app.intent('My Custom Intent Name', (conv, params) => {
    const x = params.x
})

There's extensive documentation on using the Node.js library.

glittle commented 4 years ago

Thanks, @Fleker. I'll take a look at that. In all my searching and following links from the DialogFlow pages, I'd not stumbled across that particular set of documentation. The example for "Self-hosted Express server (multiple routes)" should get me past my current roadblock.

glittle commented 4 years ago

It is still frustrating...

This page (https://developers.google.com/actions/reference/nodejsv2/overview) links to a "migration guide" page.

However, the link redirects to (https://actions-on-google.github.io/actions-on-google-nodejs/2.12.0/index.html) which does not talk about migration at all.

The "Actions on Google docs" link there goes to a page with no useful documentation.

I finally found this page (https://dialogflow.com/docs/reference/v1-v2-migration-guide-fulfillment) which seems to be more complete, but still doesn't help much.

I still haven't found the replacement for something as simple as ApiAiApp's getUser().

glittle commented 4 years ago

Maybe I need a reference for how to migrate/convert code using require('actions-on-google').ApiAiApp to DialogFlow V2. Is there such a reference?

glittle commented 4 years ago

Another question... is this documentation (which talks about dialogflow V2) anything to do with the current version of DialogFlow? https://googleapis.dev/nodejs/dialogflow/latest/v2.IntentsClient.html

The sample code there seems totally different...

const dialogflow = require('dialogflow');
const client = new dialogflow.v2.IntentsClient();
client.updateIntent(request)
  .then(responses => {
    const response = responses[0];
    // doThingsWith(response)
  })
  .catch(err => {
    console.error(err);
  })

The documentation is not dated, so there's no way to know how current it is.

Fleker commented 4 years ago

require('actions-on-google').ApiAiApp

is now

const { dialogflow } = require('actions-on-google')
const app = dialogflow()

From there, follow the Node.js guide linked above.

To get the user, run

app.intent(INTENT_NAME, conv => {
    conv.user
})