首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Dialogflow Fulfillment 0.6.1中使用Nodemailer

在Dialogflow Fulfillment 0.6.1中使用Nodemailer
EN

Stack Overflow用户
提问于 2021-11-11 07:11:47
回答 1查看 70关注 0票数 1

我测试了我的nodemailer nodejs代码,它工作正常。我现在面临的问题是,当我将对话流实现版本从0.5.0更改为Firebase数据库的0.6.1时,nodemailer将崩溃。但是,当我将它切换回0.5.0时,它就可以工作了。

我能知道如何解决这个问题吗?我是否需要为ver 0.6.1添加额外的代码,或者有什么不同?

我的代码如下:

代码语言:javascript
复制
// 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"
  }
}

错误:

代码语言:javascript
复制
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.
EN

回答 1

Stack Overflow用户

发布于 2021-11-18 17:12:06

好了,我可以发送邮件了,这是我根据你提供的代码块更新后的sendEmail函数代码:

代码语言:javascript
复制
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来捕获此类问题:

代码语言:javascript
复制
try {
  // code to check
}
catch (e) {
  console.log("entering catch block");
  console.log(e);
  console.log("leaving catch block");
}

有用提示

  • 您可以通过转到cloud functions -> my_deployed_ fullfillment -> logs
  • 来检查日志。
  • 如果没有控制台消息到达日志,请确保您的函数在您使用的代码上执行cloud fullfillment,看起来您正在使用不安全的方式传递邮件。提醒一下,您的示例邮件需要实现此answer.
  • If,您正在寻找更安全的东西。web上有类似以下case.

的示例

有用链接

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69924429

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档