MQTT trigger script template

Script windmill

by dieriba.pro916 · 2/24/2025

  • Submitted by dieriba.pro916 Bun
    Created 442 days ago
    1
    /**
    2
     * MQTT Preprocessor Function
    3
     * 
    4
     * This function is called when an MQTT message is received, BEFORE your main handler function.
    5
     * It allows you to transform or validate the incoming data before processing.
    6
     * 
    7
     * Flow: MQTT message → preprocessor function → main function
    8
     * 
    9
     * IMPORTANT: The object you return from this preprocessor determines the parameters passed to your main function:
    10
     * - If you return {a: 1, b: 2}, then main will be called as main(a, b)
    11
     * - If you return {payload}, then main will be called as main(payload)
    12
     * - If you return nothing or {}, then main will only receive the raw payload as main(payload)
    13
     * 
    14
     * @param wm_trigger - Information about the trigger source (MQTT in this case)
    15
     * @param payload - The raw bytes of the MQTT message (as Uint8Array)
    16
     * @returns An object with the arguments to pass to the main function
    17
     */
    18
    export async function preprocessor(
    19
      wm_trigger: {
    20
        kind: 'http' | 'email' | 'webhook' | 'websocket' | 'kafka' | 'nats' | 'postgres' | 'sqs' | 'mqtt',
    21
        mqtt: {
    22
          topic: string,
    23
          retain: boolean,
    24
          pkid: number,
    25
          qos: number,
    26
          v5?: {
    27
            payload_format_indicator?: number,
    28
            topic_alias?: number,
    29
            response_topic?: string,
    30
            correlation_data?: Array<number>,
    31
            user_properties?: Array<[string, string]>,
    32
            subscription_identifiers?: Array<number>,
    33
            content_type?: string
    34
          }
    35
        }
    36
      },
    37
      /**
    38
       * The raw message payload from the MQTT broker as bytes
    39
       * This preprocessor function is called before your main function
    40
       * The object you return from this function will be passed as arguments to your main function
    41
       * You can transform the payload here before it reaches your main handler
    42
       */
    43
      payload: Uint8Array,
    44
    ) {
    45
      return {
    46
        content_type: wm_trigger.mqtt.v5?.content_type,
    47
        payload,
    48
        payloadAsString: new TextDecoder().decode(payload)
    49
      }
    50
    }
    51
    
    
    52
    /**
    53
     * Main Function - Handles MQTT Messages
    54
     * 
    55
     * This function receives arguments based on what was returned by the preprocessor:
    56
     * - If preprocessor returns {payload, content_type}, main is called as main(payload, content_type)
    57
     * - If preprocessor returns {payload, someValue}, main is called as main(payload, someValue)
    58
     * - If preprocessor returns {}, main is called as main(payload) with the raw payload only
    59
     * 
    60
     * IMPORTANT: 
    61
     * - The 'payload' parameter will always be available to the main function
    62
     * - Any other parameters only exist if explicitly returned from the preprocessor
    63
     * - There are no "special" parameters - all additional parameter names come directly 
    64
     *   from what you return in the preprocessor
    65
     * 
    66
     * @param content_type - The content type from MQTT v5 properties if available
    67
     * @param payload - The raw binary payload from MQTT
    68
     * @param payloadAsString - The payload converted to a string
    69
     * @returns The result of processing the MQTT message
    70
     */
    71
    export async function main(payload: string, content_type?: string, payloadAsString: string) {
    72
    
    
    73
    }