6

Kicking Off a Pipedream Workflow on a Full Moon (Because Why Not?)

 2 years ago
source link: https://www.raymondcamden.com/2022/05/16/kicking-off-a-pipedream-workflow-on-a-full-moon-because-why-not
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client


Raymond Camden

DevRel at Adobe, Star Wars nerd, Web/Serverless hacker, lover of good beer and good books. Oh, and cats.



May 16, 2022

Kicking Off a Pipedream Workflow on a Full Moon (Because Why Not?)

Kicking Off a Pipedream Workflow on a Full Moon (Because Why Not?)

File this under "You probably will never need it but...", did you know that Pipedream is flexible to the point of allowing you to define truly customized ways to kick off workflows? How flexible? What about the ability to fire workflows on a full moon?

This is the email I got yesterday, and yep, I can confirm this happened:

Sources in Pipedream are anything that can start a workflow. So for example:

  • "On a new file added in Google Drive..."
  • "On a new Tweet from a user..."
  • "On a particular schedule"

What's cool though is that this system is open to customized sources as well. Pipedream's docs describe how to create custom sources from either the UI or CLI. A few months ago I was bored and thinking about the problems werewolves may have on the dating scene and it occurred to me - a simple notification system for the full moon would probably be helpful!

Following the directions on building from the CLI, I built a CRON-based source that made use of the excellent API from visualcrossing. Their Timeline includes a lot of information but more importantly returns a moonphase value that goes from 0 (new moon), to 0.5 (full moon), and finally to 1 (the next new moon).

Here's the endpoint I ended up using - I had to ask for the moon phase and also asked it to filter the result set to just the moon phase (to make it a bit speedier):

https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/timeline/70508/today?key=${this.visualCrossingKey}&include=moonphase&elements=moonphase

So how does this look inside a Pipedream source? Here's the complete code:

import fetch from "node-fetch";

export default {
  name: "Full Moon",
  description: "I check the moon phase daily and emit on a full moon.",
  props: {
    timer: {
        type:"$.interface.timer",
        default: {
            cron: "0 0 * * *"
        }
    },
    visualCrossingKey:{
        type:"string",
        label:"Visual Crossing API Key",
        description:"Get your key from visualcrossing.com"
    }
  },
  async run() {
    const endpoint = `https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/timeline/70508/today?key=${this.visualCrossingKey}&include=moonphase&elements=moonphase`;
    let resp = await fetch(endpoint);
    let data = await resp.json();
    let moonphase = data.days[0].moonphase;
    // only emit on the full moon
    if(moonphase === 0.5) this.$emit({ fullmoon:true });
  },
};

The code begins with the metadata related to the source, including the name, and importantly, a props block where I can specify both how it works (a timer) and that it requires a key from visualcrossing. This lets me deploy the source and share it with others as my key isn't embedded in the code.

Once deployed, I made a new workflow, configured the source with my key, and ended it with an "email" me action built by Pipedream:

This is an older V1 workflow on Pipedream so I can share it here: https://pipedream.com/@raymondcamden/email-on-full-moon-p_V9CaRmP. While my specific example here isn't terribly useful, I hope it's a good demonstration of what's possible. Let me know what you think.

Share: Twitter Facebook LinkedIn

Support this Content!

If you like this content, please consider supporting me. You can become a Patron, visit my Amazon wishlist, or buy me a coffee! Any support helps!

Want to get a copy of every new post? Use the form below to sign up for my newsletter.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK