Edits history of script submission #11458 for ' MQTT trigger script template (windmill)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    /**
     * General Trigger Preprocessor
     * 
     * ⚠️ This function runs BEFORE the main function.
     * 
     * It processes raw trigger data (e.g., MQTT, HTTP, SQS) before passing it to `main()`.
     * Common tasks:
     * - Convert binary payloads to string/JSON
     * - Extract metadata
     * - Filter messages
     * - Add timestamps/context
     * 
     * The returned object determines `main()` parameters:
     * - `{a: 1, b: 2}` → `main(a, b)`
     * - `{payload}` → `main(payload)`
     * 
     * @param event - Trigger data (e.g., MQTT, HTTP)
     * @returns Processed data for `main()`
     */
    export async function preprocessor(
      event: {
        kind: 'mqtt',
        payload: string, // base 64 encoded payload
        topic: string,
        retain: boolean,
        pkid: number,
        qos: number,
        v5?: {
          payload_format_indicator?: number,
          topic_alias?: number,
          response_topic?: string,
          correlation_data?: Array<number>,
          user_properties?: Array<[string, string]>,
          subscription_identifiers?: Array<number>,
          content_type?: string
        }
      },
    ) {
      if (event.kind === 'mqtt') {
        const payloadAsString = atob(event.payload);
    
        return {
          contentType: event.v5?.content_type,
          payload: event.payload,
          payloadAsString
        };
      }
      // We assume the script is triggered by an MQTT message, which is why an error is thrown for other trigger kinds.
      // If the script is intended to support other triggers, update this logic to handle the respective trigger kind.
      throw new Error(`Expected mqtt trigger kind got: ${event.kind}`)
    }
    
    /**
     * Main Function - Handles processed trigger events
     * 
     * ⚠️ Called AFTER `preprocessor()`, with its return values.
     * 
     * @param payload - Raw base64 encoded payload
     * @param payloadAsString - Decoded string payload
     * @param contentType - MQTT v5 content type (if available)
     */
    export async function main(payload: string, payloadAsString: string, contentType?: string) {
    
    }

    Submitted by hugo697 369 days ago

  • bun
    /**
     * General Trigger Preprocessor
     * 
     * ⚠️ This function runs BEFORE the main function.
     * 
     * It processes raw trigger data (e.g., MQTT, HTTP, SQS) before passing it to `main()`.
     * Common tasks:
     * - Convert binary payloads to string/JSON
     * - Extract metadata
     * - Filter messages
     * - Add timestamps/context
     * 
     * The returned object determines `main()` parameters:
     * - `{a: 1, b: 2}` → `main(a, b)`
     * - `{payload}` → `main(payload)`
     * 
     * @param wm_trigger - Trigger details (e.g., MQTT, HTTP)
     * @param payload - Raw trigger data (format varies by trigger type)
     * @returns Processed data for `main()`
     */
    export async function preprocessor(
      wm_trigger: {
        kind: 'http' | 'email' | 'webhook' | 'websocket' | 'kafka' | 'nats' | 'postgres' | 'sqs' | 'mqtt',
        mqtt?: {
          topic: string,
          retain: boolean,
          pkid: number,
          qos: number,
          v5?: {
            payload_format_indicator?: number,
            topic_alias?: number,
            response_topic?: string,
            correlation_data?: Array<number>,
            user_properties?: Array<[string, string]>,
            subscription_identifiers?: Array<number>,
            content_type?: string
          }
        }
      },
      payload: Array<number>,
    ) {
      if (wm_trigger.kind === 'mqtt' && wm_trigger.mqtt) {
        const uint8Payload = new Uint8Array(payload);
        const payloadAsString = new TextDecoder().decode(uint8Payload);
    
        return {
          contentType: wm_trigger.mqtt.v5?.content_type,
          payload: uint8Payload,
          payloadAsString
        };
      }
      // We assume the script is triggered by an MQTT message, which is why an error is thrown for other trigger kinds.
      // If the script is intended to support other triggers, update this logic to handle the respective trigger kind.
      throw new Error(`Expected mqtt trigger kind got: ${wm_trigger.kind}`)
    }
    
    /**
     * Main Function - Handles processed trigger events
     * 
     * ⚠️ Called AFTER `preprocessor()`, with its return values.
     * 
     * @param payload - Raw binary payload
     * @param payloadAsString - Decoded string payload
     * @param contentType - MQTT v5 content type (if available)
     */
    export async function main(payload: Uint8Array, payloadAsString: string, contentType?: string) {
    
    }

    Submitted by dieriba.pro916 438 days ago