【问题标题】:Using Nodemailer with Dialogflow Fulfillment 0.6.1将 Nodemailer 与 Dialogflow Fulfillment 0.6.1 一起使用
【发布时间】:2021-12-23 17:03:16
【问题描述】:

我已经测试了我的 nodemailer nodejs 代码并且它可以工作。我现在面临的问题是,当我将 Firebase db 的 Dialogflow Fulfillment 版本从 0.5.0 更改为 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.

【问题讨论】:

  • 您好,您是使用内联编辑器运行这段代码吗?或者这个代码只是在外部运行,使用库dialogflow-fulfillment-nodejs 如果是这样,你的问题是由于库的过时造成的。正如您在链接中看到的,该库不再维护。您可以尝试在inline editor with cloud functions 中运行您的代码。
  • 嗨@Betjens,是的,我正在使用内联编辑器来运行代码。但它似乎不适用于 0.6.1。可能是什么原因?
  • 好的,我已经复制了我这边的问题,并为邮件意图获得了相同的输出,但其他意图很好。所以我认为它不是到达服务器端的问题,而是使用邮件功能。看起来像这样case

标签: node.js email dialogflow-es nodemailer dialogflow-es-fulfillment


【解决方案1】:

好的,所以我可以发送邮件了,这是我根据您提供的块更新到您的 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");
}

实用提示

  • 您可以通过转到 云功能 -> my_deployed_ fullfillment -> 日志来检查日志
  • 最新结果总是显示在最后。
  • 如果没有控制台消息到达日志,请确保您的函数正确执行
  • 根据您使用的代码,您似乎使用了不安全的方式来传递邮件。提醒一下,您的示例邮件需要实现此answer
  • 如果您正在寻找更安全的东西,网上有类似 case 的示例。

有用的链接

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-04
相关资源
最近更新 更多