1

I have tested my nodemailer nodejs code and it works. The problem I am facing now is I realize when I change my Dialogflow Fulfillment version from 0.5.0 to 0.6.1 for Firebase db, the nodemailer will crash. However, when I switch it back to 0.5.0, it works.

Can I know how to I tackle this? Do I need to add in additional code for ver 0.6.1 or what is the difference?

My code as following:

// 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"
  }
}

ERROR:

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.
Wytrzymały Wiktor
  • 11,492
  • 5
  • 29
  • 37
  • Hello, are you using the inline editor to run this code? or this code is just running externally making use of the library [dialogflow-fulfillment-nodejs](https://github.com/dialogflow/dialogflow-fulfillment-nodejs) if so, your issue made be due to the obsolescing of the library. As you can see in the link, this library is no longer maintained. You can try running your code in the [inline editor with cloud functions](https://cloud.google.com/dialogflow/es/docs/fulfillment-inline-editor). – Betjens Nov 11 '21 at 15:18
  • Hi @Betjens , Yes, I am using the inline editor to run the code. but it doesnt seems to work for 0.6.1. What could be the reason? – user15458141 Nov 17 '21 at 09:28
  • Ok I have replicated the issue on my side and got the same output for the mail intent but the others intents are fine. So I don't think its an issue of reaching the server side, but using the mail function. looks like this [case](https://stackoverflow.com/questions/19877246/nodemailer-with-gmail-and-nodejs). – Betjens Nov 17 '21 at 12:13

1 Answers1

0

Ok, so I was able to send the mail, here is my updated code to your sendEmail function based on the block you provide:

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!");
}

I recommend using try catch to catch this kind of issues:

try {
  // code to check
}
catch (e) {
  console.log("entering catch block");
  console.log(e);
  console.log("leaving catch block");
}

Useful Tips

  • You can check the logs on by going to cloud functions -> my_deployed_ fullfillment -> logs
  • Latest results are always show at the end.
  • If no console message reach the log, make sure your function executes properly
  • Based on the code you use, it looks like you are using an insecure way to deliver the mail. As a reminder be warn that your sample mail needs to implement this answer.
  • If you are looking for something more secure there are samples on the web like this case.

Useful Links

Betjens
  • 1,353
  • 2
  • 4
  • 13