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}});
}
Real-world Examples
Start building workflows
Sign-up and run a sample project Learn more