This flowchart shows a visual representation of the workflow tasks.
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
});
}
};
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"
}
}
);
};
Sample Projects
Start building workflows
Sign-up and run a sample project Learn more