我的功能是在收到params.finalemail后不发送电子邮件

时间:2019-08-30 00:33:54

标签: node.js ibm-watson watson-assistant ibm-cloud-functions

我在IBM云函数中创建了一个函数,用于计算两个日期之间的夜晚并发送电子邮件。夜晚的计算效果很好。Watson发送params.checkout和params.checkin并返回总夜晚数。

当Watson发送params.finalemail时,参数将发送到云功能,但不会发送电子邮件。

我在Watson中进行设置的方式是2个节点,第一个节点发送签入/签出,它运行完美。第二个节点发送params.finalemail。但是电子邮件没有发送。

/**
  *
  * main() will be invoked when you Run This Action.
  *
  * @param Cloud Functions actions accept a single parameter,
  *        which must be a JSON object.
  * 
  * @return which must be a JSON object.
  * 
*/
// using SendGrid's v3 Node.js Library
// https://github.com/sendgrid/sendgrid-nodejs
const sgMail = require('@sendgrid/mail');

function sendmail(params) {


  /*    Replace YOUR-SENDGRID-API-KEY-GOES-HERE with
        the API Key you get from SendGrid.
  */
  sgMail.setApiKey('api')

  let msg = {}
  msg.to = 'test@test.com'
  msg.from = 'test@me.com'
  msg.subject = 'Your Reservation'
  msg.html = params.finalemail

  sgMail.send(msg,(error, json) => {
    if (error) {
      console.log(error)
    }
  })
  return { sent: 1 }
}

/**
  * For nights(), the params variable will look like:
  *     { "checkin": "YYYY-MM-DD", "checkout": "YYYY-MM-DD" }
  *     Example: { "checkin": "2019-12-01", "checkout": 2020-01-31" }
  *     date1 should always be the earliest date.
  * 
  * The date params are assumed to be valid dates. It is the
  * responsibility of the caller to pass in valid dates.
  * This routine does not error check the dates.
  * 
  * @return which must be a JSON object.
  *     It will be the output of this action.
  *     The number of nights between checkin and checkout.
  *     { nights: x }
  *
*/
function parseDate(str) {
  let ymd = str.split('-');
  return new Date(ymd[0], ymd[1]-1, ymd[2]);
}

function datediff(first, second) {
  // Take the difference between the dates and divide by milliseconds per day.
  // Round to nearest whole number to deal with DST.
  return Math.round((second-first)/(1000*60*60*24));
}

function nights(params) {
  let nights = 0;
  if (params.checkin && params.checkout) {
    nights = datediff(parseDate(params.checkin), parseDate(params.checkout))
    if (nights < 0) {
        nights = 0
    }
  }
  return { nights };
}

function main(params) {
  if (params['finalemail']) {
    return sendmail(params)
  }

  if (params['checkin'] && params['checkout']) {
    return nights(params)
  }

  return {}
}

exports.main = main

0 个答案:

没有答案