Lecca.io LogoLecca.io
TriggersWebhook strategy

Create App Webhook Trigger

We are working on a CLI to make this easier

Create triggers folder

Locate the your app folder in the apps directory and add a new triggers folder if it doesn't exist already.

new-trigger.trigger.ts
your-new-app.app.ts

Create trigger file

Create a new file within your app's trigger folder. Call it something like new-trigger.trigger.ts. Make sure it ends with .trigger.ts.

Add your own values. We provided mock data to help initialize the process.

import { createAppWebhookTrigger } from "@lecca-io/toolkit";
 
import { shared } from "../shared/slack.shared";
 
export const newMessage = createAppWebhookTrigger({
  id: "your-new-app_trigger_new-trigger",
  name: "",
  description: "Triggered when a new message is sent in a channel.",
  inputConfig: [
    //Add a configuration to display the form
  ],
  run: async ({ inputData, configValue }) => {
    //the config value to transform the inputData (webhook body) before returning it as an array.
    return [inputData];
  },
  mockRun: async () => {
    return [
      //Return mock data, as array
    ];
  },
  eventType: "", //your app file needs to define a `parseWebhookEventType` property.
  webhookPayloadMatchesIdentifier: ({ webhookBody, connectionMetadata }) => {
    //This is to check what workflows & connections this webhook is for.
    //Look at the connection metadata when a creation is made. Then look at the webhook body.
    //See if there are unique identifiers that match.
 
    // For example, slack webhook trigger does this check.
    // if (webhookBody.team_id !== connectionMetadata.team?.id) {
    //   return false;
    // } else {
    //   return true;
    // }
 
    return false;
  },
});
  1. export const newTrigger = createTrigger

    • replace newTrigger with the name of your trigger.
  2. Add an id: your-new-app_trigger_new-trigger

    • Make sure the id follows the format <app-id>_trigger_<trigger-name-in-kebab-case>
  3. Add a name for your trigger

  4. Add a brief one sentence description

  5. Add an Input Config

    This is used to generate the form for the UI to configure this action.

    Read more about the Input Config

  6. Define the run function.

    This function will run every time the trigger polls.

    The common arguments used by this function are configValue and connection.

    1. configValue represents the Input Config that was used when the user configured this trigger.

    2. connection is the decrypted connection properties of the connection selected by the user when configuring this trigger.

      If using an oauth2 connection, do not worry about handling refresh tokens, that is handled by the engine. Just make the api call assuming the access token is valid.

    Use the input config values to transform, filter, or modify the inputData then return the inputData within an array.

    All triggers must always return array values.
  7. Define the mock function.

    The mock function is used by users when they want to generate a mock output instead of making an real api call to their third party integration.

    Ouput data is used to map to other nodes in the workflow builder. This is done by clicking Save & Test in the configuration form.

  8. Define the eventType property.

    A trigger may only trigger if the event payload matches some sort of event type property. A webhook trigger like slack may send multiple different types of webhooks, but we want to filter them to a certain type of webhook.

    In your <your-app>.app.ts app file, you will need to define a verifyWebhookRequest and parseWebhookEventType argument in the createApp function.

    You can look at an example of this implementation in the slack.app.ts file

  9. Define the webhookPayloadMatchesIdentifier method

    Each webhook body payload should have some sort of identifier that you can match to the connection metadata.

    new-message.trigger.ts
    //Slack's webhook trigger webhookPayloadMatchesIdentifier
     
    webhookPayloadMatchesIdentifier({
      webhookBody,
      connectionMetadata,
    }: {
      webhookBody: WebhookBody;
      connectionMetadata: { team?: { id: string } };
    }): boolean {
      if (webhookBody.team_id !== connectionMetadata.team?.id) {
        return false;
      } else {
        return true;
      }
    }

    Mind that we have only created one app webhook trigger at the time of writing this documentation so there may be webhook body's that won't work with this method. If you run into this, please open an issue.

Add new trigger to the app's triggers property

your-new-app.app.ts
import { createApp } from "@lecca-io/toolkit";
 
export const yourNewApp = createApp({
  ...
  actions: [],
  triggers: [newTrigger], 
  connections: [],
});

On this page