Jump to content
  • 0

How do I create my own trigger?


Micke
 Share

Question

2 answers to this question

Recommended Posts

  • 0

Hello Micke! Thank you for your question.

Under the Examples account that you should have access to you can take a look at the HTTP Trigger example. It is placed under the Functions tab not the App tab. To create a similar one you have to create a function under your Functions tab that you later can use by importing it to you app/apps. Hope this helps!

Link to comment
Share on other sites

  • 0

Hi Micke, like Linnea mentioned above you can look at example triggers to get a feeling of how you can build your own. To break it down though, you can use the produceEvent attribute to produce your own events like so. 

// Code in the 'Create Event' Node

exports.setup = async ctx => {
  const { produceEvent } = ctx;
  produceEvent({});
};

exports.handler = async (ev, ctx) => {
  const { route } = ctx;
  route(ev);
};

This would create one event and then route it to the next node in the exports.handler. I.e. the 'Do Something' node would fire once. See what this would look like visually below. 

1239581666_Screenshot2022-02-15at18_55_35.thumb.png.e1900d86bd4f3d0dc6ed7b5ea35f9059.png

This would however only create one event. To have it fire multiple times you could wrap it in a setInterval like the code below. 

// Alternative code to have it run every 5 seconds 

exports.setup = async ctx => {
  const { produceEvent } = ctx;
  setInterval(produceEvent({}), 5000);
};

exports.handler = async (ev, ctx) => {
  const { route } = ctx;
  route(ev);
};

This would fire the 'Do Something' node every 5 seconds. 

Another choice is to have a cron schedule wrap the produceEvent({}) to fire off your functions on a set custom schedule. I.e. every Monday at 5 pm. See the example code below. 

// Create events on a schedule with cron
const cron = require('node-cron');

exports.setup = async ctx => {
  const { produceEvent } = ctx;

  cron.schedule('0 0 17 ? * MON *', () => {
    produceEvent({});
  });
};

exports.handler = async (ev, ctx) => {
  const { route } = ctx;
  route(ev);
}

Just remember to import the node-cron module in the app or your library function if you are using this example. 

Tip: you can generate a specific cron schedule here: https://www.freeformatter.com/cron-expression-generator-quartz.html

Link to comment
Share on other sites

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...