line

Trigger An Email After 3 Days of Cold Weather

A workflow that triggers an email about a tropical vacation after 3 days of cold weather. It's useful for an agency who wants to trigger notification based on data from external API and time factors.
  • Call a weather API to get the temperature of the current day during a specific period (ie. number of days)
  • If the temperature is less than 40°F for three days in a row, then send an email to users using gmail.
  • If not, then keep checking everyday until the end of the time period
  • If during the time period, the temperature hasn't dropped below 40°F for 3 days, then send a different promotional email.

Flowchart of the workflow

This flowchart shows a visual representation of the workflow tasks.

flowchart workflow

Workflow Code

View the Github Project where you can fork the project and deploy to Heroku in a few clicks.

const { duration } = require("zenaton");

// Check the temperature of a given city every day for n days.
// When the temperature drops below minTemp for minRep then send a promotional email via Sendgrid to visit a warm weather destination
module.exports.handle = function*({ city, days, minTemp, minRep, recipient }) {
  let repCount = 0;
  do {
    //Check the temperature of the city via openweathermap API.
    const response = yield this.run.task("getCityTemparature", city);
    //If the temperature is under minTemp degrees (45 degrees fahrenheit) for at least minRep(3 days),
    if (response.main.temp < minTemp) {
      repCount++;
    } else {
      repCount = 0;
    }
    // wait one day
    yield this.wait.for(duration.days(1));
    days--;
  } while (repCount < minRep && days > 0);

  if (repCount === minRep) {
    // Trigger an email campaign on Sendgrid based on the weather.
    yield this.run.task("SendWeatherCampaign", {
      city,
      recipient
    });
  }
};

Workflow Tasks

The GetCityTemperature gets the temperature for a given city.

const axios = require("axios");

// Get your API key here: https://openweathermap.org/appid
const { OPEN_WEATHER_API_KEY } = process.env;

module.exports.handle = async function(city) {
  const response = await axios.get(
    `https://api.openweathermap.org/data/2.5/weather?q=${city}&units=imperial&APPID=${OPEN_WEATHER_API_KEY}`
  );
  return response.data;
};

The SendWeatherCampaign task sends the promotional email based on the weather using Sendgrid.

const axios = require("axios");

// https://app.sendgrid.com/guide/integrate/langs/nodejs
const { SENDGRID_API_KEY } = process.env;

module.exports.handle = async function({ city, recipient }) {
  const body = `Hey Freezing in ${
    city.split(",")[0]
  }? It's the right time to plan vacations in Bahamas! \n`;

  await axios.post(
    "https://api.sendgrid.com/v3/mail/send",
    {
      personalizations: [{ to: [{ email: recipient }] }],
      content: [
        {
          type: "text/plain",
          value: body
        }
      ],
      subject: "50% OFF Bahamas!",
      from: { email: "zenaton-tutorial@zenaton.com" }
    },
    {
      headers: {
        Authorization: "Bearer " + SENDGRID_API_KEY,
        "Content-Type": "application/json"
      }
    }
  );
};