Skip to main content Skip to footer

Search Crosser Knowledge Base

Use AI tools to create custom code

Did you know that you can use external AI tools like ChatGPT to create code for Crossers’ custom code modules? Low code is great but eventually you might run into limitations and need to enhance the process with custom code. If you feed tools like ChatGPT with reference code from the Crosser examples, you can ask these tools to create code specified for your requirements, executable in Crosser.

C# Example:

ChatGPT prompt

Here is some C# code examples in Crosser from the C# module documentation:

// just return a new object 
return new {num = 123, str = "ABC"};
// return the data part from the object coming in 
return msg.data

Create C# code that adds a GUID on the property data.guid. Also return the entire object 'data'.

ChatGPT Output

// Ensure the incoming message has a data property and initialize it if necessary
if (msg.data == null)
{
    msg.data = new { };
}
// Add a GUID to the data property
msg.data.guid = Guid.NewGuid().ToString();

// Return the entire data object
return msg.data;

Debug message in Crosser

{
  "crosser": {
    "success": true
  },
  "guid": "54e3ec1f-ccef-467f-b4ea-bf6be59d3dad",
  "pressure": 268,
  "temp": 27
}

Python Bridge Example:

ChatGPT prompt

Here is a Python code examples in Crosser:

import json # feel free to alter/add, but do not remove the definition msg_handler 
def msg_handler(msg, module): 
      print(json.dumps(msg)) 
      # pass the message to the next module 
      module.next(msg) 
      
Create Python code that adds a GUID on the property data.guid. Also return the entire object 'data'.

ChatGPT Output

import json
import uuid

# feel free to alter/add, but do not remove the definition `msg_handler`
def msg_handler(msg, module):
    # Check if 'data' exists in the message
    if 'data' in msg:
        # Add a GUID to the 'data' property
        msg['data']['guid'] = str(uuid.uuid4())
    
    # Print the modified message for debugging purposes
    print(json.dumps(msg))
    
    # Pass the modified message to the next module
    module.next(msg)

Debug message in Crosser

{
  "crosser": {
    "success": true
  },
  "data": {
    "guid": "ca0f2053-093e-4763-9fb1-a9eabd4afbb7",
    "pressure": 487,
    "temp": 89
  },
  "id": 10,
  "name": "machine"
}

Note: In this case ChatGPT created code that returns the entire message (msg) and not just the property 'data'.

Tip: Make sure to ask ChatGPT to put the code in try/except clause to ensure stability. The more you work in the chat the better the results will be over time.

About the author

David Nienhaus | Senior Solution Engineer

David is a Senior Solution Engineer at Crosser. He has over 10 years experience working with software integration and digitization projects for critical infrastructure.
His engineering background gives him the understanding and focus needed to solve customer use cases in the most efficient and successful way.