line

Calculate and send real-time alerts

This example shows a workflow for a delivery or car service with real-time data calculations and a vehicle tracking process as well as triggering notifications (emails or text messages) to the customer based on different conditions.

Visual of Workflow

This flowchart shows a visual representation of the workflow logic.
line

Workflow Code

This workflow is the code that orchestrates tasks (through the Zenaton workflow engine) and executes them on your servers.

In this Workflow, the CalculateTimeToArrivalTask task calculates both the duration and the ETA by comparing real-time data: the GPS position of the car, the actual traffic density, the distance of the trip, etc.

const twilioConnectorId = "";
const BEFORE = 3600;
const PRECISION = 120;
const UPDATE = 1200;

module.exports = {
  *handle(tripId, user) {
    let duration;
    let eta;
    let eta2;

    // Calculate the initial duration in seconds and estimated time of arrival (ETA)
    [duration, eta] = yield this.run.task("CalculateTimeToArrivalTask", tripId);

    // As long as the remaining duration is more than 1 hour and 2 minutes
    while (duration > BEFORE + PRECISION) {
      // Wait for half of the time of duration minus one hour
      yield this.wait.for((duration - BEFORE) / 2);

      // Re-calculate both the duration and the ETA.
      [duration, eta] = yield this.run.task("CalculateTimeToArrivalTask", tripId);
    }

    // Send a notification to the customer with updated ETA
    yield this.run.task("InformUserOfEtaTask", user, eta);

    // While it remains more than 20 minutes
    while (duration > PRECISION) {

      // Wait for 20 minutes
      yield this.wait.for(UPDATE);

      // Re-calculate both the duration and the ETA
      [duration, eta2] = yield this.run.task("CalculateTimeToArrivalTask", tripId);

      // If the new ETA has changed more than 20 minutes
      const diff = Math.abs(eta2 - eta)
      if (diff >= UPDATE) {
        eta = eta2;

        // Send an SMS notification to the customer via Twilio API connector with the new ETA
        yield* this.sendSms(user.phone, diff);
      }
    }
  },
  *sendSms(phone, late) {
    // Connect to Twilio with Zenaton API connector.
    const twilio_sms = this.connector("twilio_sms", twilioConnectorId);

    const message = `... will have ${late} minutes late ...`;
    return yield twilio_sms.post("/Messages.json", {body: {To: phone, Body: message}});
  }

Workflow Steps

  • Run a simple algorithm that calculates the duration and estimated time of arrival (ETA)
  • As long as the remaining duration is more than 1 hour, continue calculating the ETA and remaining duration at every interval (interval equals to half of the remaining duration minus one hour)
  • Once the remaining duration is less than one hour, send a message to the user saying that the vehicle will arrive in 1 hour
  • Wait for 20 minutes and recalculate remaining duration and ETA then compare it with the previous one
  • If the ETA has changed by more than 20 minutes, then send an alert to the user letting them know that there is a delay and include the new ETA
  • If the ETA has not changed by more than 20 minutes, send a notification to the user 2 minutes before arrival
  • As long as the remaining duration is 2 minutes before the updated ETA, recalculate the remaining duration and ETA and compare it with the previous one

Workflow Executions

View real-time execution data for each workflow step on the dashboard. Drill down to see event data, task output and error messages and log files.
line