line

Triggering An Email After 3 Days of Cold Weather

This example shows how to create a workflow that triggers a promotional email about a tropical vacation after 3 days of cold weather. It can be useful for an agency or anyone who wants to trigger notification based on data from an 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

This workflow is the code that orchestrates tasks (through the Zenaton workflow engine) and executes them on your servers. Tasks will be dispatched as soon as three days of cold weather occurs.

const { workflow, 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 Maichimp
// to visit a warm weather destination. Otherwise send a regular email campaign.
module.exports = workflow('TemperatureCampaignWorkflow', {
 *handle(days, minTemp, minRep, city, emailRecipients) {
 
   // You can get your connectors id here: https://app.zenaton.com/connectors
   const openweathermap = this.connector('openweathermap', 'your-connector-id')
   const mailchimp = this.connector('mailchimp', 'your-connector-id')
   
   let repCount = 0
   do {
     //Check the temperature of Paris via openweathermap API.
     response = yield openweathermap.get(`/weather?q=${city}&units=imperial`)
     //If the temperature is under minTemp degrees (42 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)
   
   //trigger an email campaign on mailchimp based on the weather.
   if (repCount === minRep) {
     yield mailchimp.post(`/campaigns/campaign_${city}_hot/actions/send`)
   } else {
     yield mailchimp.post(`/campaigns/campaign_${city}_cold/actions/send`)
   }
}})