Skip to content

Instantly share code, notes, and snippets.

@Defozo
Last active April 14, 2024 17:20
Show Gist options
  • Save Defozo/4fd2f1c363c5e8b6a3c7a03b6e9fb26f to your computer and use it in GitHub Desktop.
Save Defozo/4fd2f1c363c5e8b6a3c7a03b6e9fb26f to your computer and use it in GitHub Desktop.
This Pipedream workflow is designed to integrate with Toggl and Habitica. For every 1 minute logged in Toggl, it scores a specified task in Habitica.
/*
This Pipedream workflow is designed to integrate with Toggl and Habitica.
For every 1 minute logged in Toggl, it scores a specified task in Habitica.
The workflow is structured as follows:
1. Toggl is used as the trigger.
2. The `get_record_or_create` step retrieves or creates a record.
- Uses the "Data Store" feature in Pipedream.
- The key for the data store is the ID from the Toggl payload (`steps.trigger.event.payload.id`).
- The value stored is the duration from the Toggl payload (`steps.trigger.event.payload.duration`).
3. The `add_update_record` step adds or updates the record.
- Uses the "Data Store" feature in Pipedream.
- The key for the data store is the ID from the Toggl payload (`steps.trigger.event.payload.id`).
- The value stored is the duration from the Toggl payload (`steps.trigger.event.payload.duration`).
4. The `habitica` step (Node.js) contains the logic to score the Habitica task based on the duration from Toggl.
Remember to replace taskId.
Author: Defozo
Date: 2023-08-13 CET
*/
import { axios } from "@pipedream/platform"
export default defineComponent({
props: {
habitica: {
type: "app",
app: "habitica",
}
},
async run({steps, $}) {
const taskId = "cf515xxx-xxxx-xxxx-xxxx-21000370xxxx"; // Replace with your actual task ID
let previousDuration;
if (steps.get_record_or_create.$summary.startsWith("Successfully added a new record with the key")) {
previousDuration = 0; // Assume the stored value was 0
} else {
previousDuration = steps.get_record_or_create.$return_value / 60; // Convert to minutes
}
const currentDuration = steps.trigger.event.payload.duration / 60; // Convert to minutes
const difference = Math.floor(Math.abs(currentDuration - previousDuration));
// Function to add a delay
const delay = ms => new Promise(res => setTimeout(res, ms));
// Decide the direction to score based on the comparison
let direction;
if (previousDuration < currentDuration) {
direction = "up";
} else if (previousDuration > currentDuration) {
direction = "down";
} else {
return { message: "No change in duration, no action taken." };
}
// Loop and make the API call to score the task
for (let i = 0; i < difference; i++) {
await axios($, {
method: "POST",
url: `https://habitica.com/api/v3/tasks/${taskId}/score/${direction}`,
headers: {
"x-client": `${this.habitica.$auth.user_id}-Pipedream`,
"x-api-user": `${this.habitica.$auth.user_id}`,
"x-api-key": `${this.habitica.$auth.api_token}`,
},
});
// Wait for 1 second before the next request
if (i < difference - 1) { // No need to delay after the last request
await delay(100);
}
}
return { message: `Task scored ${direction} ${difference} times.` };
},
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment