我测试了我的nodemailer nodejs代码,它工作正常。我现在面临的问题是,当我将对话流实现版本从0.5.0更改为Firebase数据库的0.6.1时,nodemailer将崩溃。但是,当我将它切换回0.5.0时,它就可以工作了。
我能知道如何解决这个问题吗?我是否需要为ver 0.6.1添加额外的代码,或者有什么不同?
我的代码如下:
// See https://github.com/dialogflow/dialogflow-fulfillment-nodejs
// for Dialogflow fulfillment library docs, samples, and to report issues
'use strict';
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
const nodemailer = require('nodemailer');
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'EMAIL',
pass: 'APP PASSWORD'
}
});
process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements
exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
const agent = new WebhookClient({ request, response });
console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers));
console.log('Dialogflow Request body: ' + JSON.stringify(request.body));
function welcome(agent) {
agent.add(`Welcome to my agent!`);
}
function fallback(agent) {
agent.add(`I didn't understand`);
agent.add(`I'm sorry, can you try again?`);
}
function sendEmail(agent){
const email = agent.parameters.email;
const name = agent.parameters.name;
const cls = agent.parameters.cls;
const message = agent.parameters.message;
const mailOptions = {
from: 'SENDER EMAIL',
to: 'EMAIL',
cc: email,
subject: "ABC",
text: "MESSAGE:" +
'\n\n Name: ' + name +
'\n\n Class: ' + cls +
'\n\n Email: ' + email +
'\n\n Message: ' + message
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
}
else {
console.log('Email sent: ' + info.response);
}
});
}
// Run the proper function handler based on the matched Dialogflow intent name
let intentMap = new Map();
intentMap.set('Default Welcome Intent', welcome);
intentMap.set('Default Fallback Intent', fallback);
intentMap.set('Email Enquiry', sendEmail);
agent.handleRequest(intentMap);
});
{
"name": "dialogflowFirebaseFulfillment",
"description": "This is the default fulfillment for a Dialogflow agents using Cloud Functions for Firebase",
"version": "0.0.1",
"private": true,
"license": "Apache Version 2.0",
"author": "Google Inc.",
"engines": {
"node": "10"
},
"scripts": {
"start": "firebase serve --only functions:dialogflowFirebaseFulfillment",
"deploy": "firebase deploy --only functions:dialogflowFirebaseFulfillment"
},
"dependencies": {
"actions-on-google": "^2.2.0",
"firebase-admin": "^5.13.1",
"firebase-functions": "^2.0.2",
"dialogflow": "^0.6.0",
"dialogflow-fulfillment": "^0.6.1",
"nodemailer": "^6.6.3"
}
}错误:
Function execution took 150 ms, finished with status: 'crash'
Dialogflow fulfillment error : Webhook call failed. Error: UNAVAILABLE, State: URL_UNREACHABLE, Reason: UNREACHABLE_5xx, HTTP status code: 500.发布于 2021-11-18 17:12:06
好了,我可以发送邮件了,这是我根据你提供的代码块更新后的sendEmail函数代码:
function sendEmail(agent){
// parameters must exist else it will be show as undefined
const email = agent.parameters.email;
const name = agent.parameters.name;
const cls = agent.parameters.cls;
const message = agent.parameters.message;
// to avoid the issue with \n using plain text i switch to html with '<br>'
const mailOptions = {
from: 'email from',
to: 'email to',
cc: email,
subject: "ABC",
html: "MESSAGE:" +
'<br> Name: ' + name +
'<br> Class: ' + cls +
'<br> Email: ' + email +
'<br> Message: ' + message
};
// show in the log your mail options
console.log(mailOptions);
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
}
else {
console.log('Email sent: ' + info.response);
}
});
agent.add("I send the mail!");
}我建议使用try catch来捕获此类问题:
try {
// code to check
}
catch (e) {
console.log("entering catch block");
console.log(e);
console.log("leaving catch block");
}有用提示
的示例
有用链接
https://stackoverflow.com/questions/69924429
复制相似问题