首页 > 解决方案 > 我的功能是在收到 params.finalemail 后不发送电子邮件

问题描述

我在 IBM 云函数中创建了一个函数来计算两个日期之间的夜数并发送电子邮件。夜数计算完美。Watson 发送 params.checkout 和 params.checkin 并返回总夜数。

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

我在 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

标签: node.jsibm-watsonwatson-assistantibm-cloud-functions

解决方案


推荐阅读