问题
i would like to send an email for an operation in my program i make in Dialogflow.
I wanna use NodeMailer but when i deploy my project i have this error message:
The deployment of your Cloud Function failed: Function load error: Code in file index.js can't be loaded. Did you list all required modules in the package.json dependencies? Detailed stack trace: Error: Cannot find module 'nodemailer'
Is this possible to use NodeMailer in DialogFlow ?
Thanks
回答1:
It sounds like you're using the built-in Fulfillment Editor in Dialogflow.
If so, you have two issues with using NodeMailer
- You need to include it in your package.json file
- By default, your code will run in a Firebase Cloud Function that has limited network access.
Both are fixable.
Remember, however, that you don't need to use the Dialogflow editor. You are welcome to run fulfillment on any publicly accessible HTTPS server.
Adding NodeMailer to your package.json
On the Fulfillment page, in the Inline Editor section, you'll click on the tab labeled package.json. You'll see some JSON in there, including a section titled "dependencies". You need to add lines to that section indicating what other npm modules are necessary - in your case, the "nodemailer" package.
Your new "dependencies" section might look something like this:
"dependencies": {
"actions-on-google": "^1.5.x",
"firebase-admin": "^4.2.1",
"firebase-functions": "^0.5.7",
"nodemailer": "^4.4.2",
"apiai": "^4.0.3"
}
Test this out to make sure it deploys correctly. But once it deploys, you'll probably run into another problem.
Limited Network Access
For some configurations of nodemailer, it seems likely that it will want to connect to a mail server somewhere. The base level of Firebase Cloud Functions limits you to connect to Google network services only. If you're connecting to GMail - you may be fine. But if not, you'll need to upgrade.
You can do this by going to the Firebase Console and selecting the same project that you're using for Dialogflow and then upgrading to the Blaze Plan. Although this requires you to enter a credit card, and is subject to usage billing, there is still a free tier that is sufficient for testing and experimentation.
来源:https://stackoverflow.com/questions/48398663/use-nodemailer-in-dialogflow