Specifications of message handler (deluge script)

Specifications of message handler (deluge script)

A message handler custom function can be used to enrich the data sent by the connected device. The custom function is written using the deluge scripting language. In this document, we provide a sample code to demonstrate how a message handler operates, along with a detailed breakdown of the code's functionality.
  1.    headerMap = header.toMap();
  2.   deviceid = headerMap.get("deviceid");
  3.   receivedtime = headerMap.get("occurred_timestamp");
  4.   topic = headerMap.get("topic");
  5.   communication_status_flag = headerMap.get("data_internal");
  6.   state_memory = headerMap.get("state_memory");
  7.   if(state_memory == null)
         {
              state_memoryMap = Map();
         }

  8.   else
         {
              state_memoryMap = state_memory.toMap();
         }

  9.   if(communication_status_flag == false && topic == "telemetry")
         {
              payload = message.toMap();
             if(payload.containKey("data"))
                   {
                        deviceMessage = payload.get("data");
                   }
             else
                 {
                        deviceMessage = payload;
                 }
         }
  10.   else if(communication_status_flag == true)
         {
                   deviceMessage = Map();
                   deviceMessage.put("header", headerMap);
         }
  11.   output = Map();
  12.   output.put("device_message",deviceMessage);
  13.   return output;
This code is provided by default when you create a message handler custom function. Refer to this document to learn how to execute and test the code.

Code Breakdown

The messages sent by connected devices are stored and accessible via the header and message arguments. To process these messages, necessary data must be extracted from these arguments. The header argument contains metadata such as Device ID, communication status flag, Status Type, Message Type, timestamp, and State Memory (if enabled when creating the message handler custom function).

In the Deluge script, data is handled using Map objects. In the code snippet below, data from the header argument is assigned to a variable named headerMap, which is initialized as a Map object. Key data elements such as device id, received time, topic, and communication_status_flag are then extracted and stored in corresponding variables.
  1.   headerMap = header.toMap();
  2.   deviceid = headerMap.get("deviceid");
  3.   receivedtime = headerMap.get("occurred_timestamp");
  4.   topic = headerMap.get("topic");
  5.   communication_status_flag = headerMap.get("data_internal");
Next, if state memory is enabled for the message handler, the state_memory variable must be initialized and stored as a map object. The following block of code accomplishes this. In the code block below, the state memory is saved to a variable named state_memoryMap.
  1.   state_memory = headerMap.get("state_memory");
  2.   if(state_memory == null) { state_memoryMap = Map(); }
  3.   else { state_memoryMap = state_memory.toMap(); }
Once the basic values are extracted and the state memory is initialized, the next step involves determining the message type of the received message. This is done by checking the value of the communication_status_flag variable. If the value is true, the message pertains to the device's connection status, indicating whether the device is connected or disconnected. If the value is false, the message is a Device Message, which means it contains the payload sent by the device. The following block of code identifies this distinction.
 
First, the message type is confirmed. If it is a payload message, the data from the message argument is extracted and stored as a map object in a variable called payload. From this payload, the actual data is then extracted and stored in a variable called deviceMessage.
 
Conversely, if it is a connection status message, the deviceMessage
variable is assigned as an empty Map object. This step is necessary because the code must return a Map object, regardless of the message type being processed.
  1. if(communication_status_flag == false && topic == "telemetry") { payload = message.toMap(); if(payload.containKey("data")) { deviceMessage = payload.get("data"); } else { deviceMessage = payload; } }
  2. else if(communication_status_flag == true) { deviceMessage = Map();      
         }
With the deviceMessage values handled, the final message to be returned is assigned to a variable called output. It's important to note that the returned output
must be a Map object to conform to the custom function's requirement.
  1. output = Map();
  2. output.put("device_message",deviceMessage);
  3. return output;
While executing the code described above, it will either output the message value as received by the application or return a null value if the message is related to connection status. Here’s a block of code that takes the received message, adds 5 to it, and outputs the value.

Add the below line of code before the code line output = Map().
  1. deviceMessage = deviceMessage.get('value')+5;
To test this operation, provide the input {'value': 6} in the Messages field when executing the custom function. This will adjust the value in deviceMessage to 11, and will be sent as the output of the message handler function.
 
 
Info
Learn more about testing a message handler custom function and using state memory in a message handler.


    Access your files securely from anywhere

      Zoho CRM Training Programs

      Learn how to use the best tools for sales force automation and better customer engagement from Zoho's implementation specialists.

      Zoho CRM Training
        Redefine the way you work
        with Zoho Workplace

          Zoho DataPrep Personalized Demo

          If you'd like a personalized walk-through of our data preparation tool, please request a demo and we'll be happy to show you how to get the best out of Zoho DataPrep.

          Zoho CRM Training

            Create, share, and deliver

            beautiful slides from anywhere.

            Get Started Now


              Zoho Sign now offers specialized one-on-one training for both administrators and developers.

              BOOK A SESSION





                          Quick Links Workflow Automation Data Collection
                          Web Forms Enterprise Begin Data Collection
                          Interactive Forms Workplace Data Collection App
                          CRM Forms Customer Service Accessible Forms
                          Digital Forms Marketing Forms for Small Business
                          HTML Forms Education Forms for Enterprise
                          Contact Forms E-commerce Forms for any business
                          Lead Generation Forms Healthcare Forms for Startups
                          Wordpress Forms Customer onboarding Order Forms for Small Business
                          No Code Forms Construction RSVP tool for holidays
                          Free Forms Travel
                          Prefill Forms Non-Profit

                          Intake Forms Legal
                          Mobile App
                          Form Designer HR
                          Mobile Forms
                          Card Forms Food Offline Forms
                          Assign Forms Photography
                          Mobile Forms Features
                          Translate Forms Real Estate Kiosk in Mobile Forms
                          Electronic Forms

                          Notification Emails for Forms Alternatives Security & Compliance
                          Holiday Forms Google Forms alternative  GDPR
                          Form to PDF Jotform alternative HIPAA Forms
                          Email Forms
                          Encrypted Forms
                          Embeddable Forms
                          Secure Forms
                          Drag and Drop form builder
                          WCAG


                                            You are currently viewing the help pages of Qntrl’s earlier version. Click here to view our latest version—Qntrl 3.0's help articles.




                                                Manage your brands on social media

                                                  Zoho Desk Resources

                                                  • Desk Community Learning Series


                                                  • Digest


                                                  • Functions


                                                  • Meetups


                                                  • Kbase


                                                  • Resources


                                                  • Glossary


                                                  • Desk Marketplace


                                                  • MVP Corner


                                                  • Word of the Day


                                                    Zoho Marketing Automation

                                                      Zoho Sheet Resources

                                                       

                                                          Zoho Forms Resources


                                                            Secure your business
                                                            communication with Zoho Mail


                                                            Mail on the move with
                                                            Zoho Mail mobile application

                                                              Stay on top of your schedule
                                                              at all times


                                                              Carry your calendar with you
                                                              Anytime, anywhere




                                                                    Zoho Sign Resources

                                                                      Sign, Paperless!

                                                                      Sign and send business documents on the go!

                                                                      Get Started Now




                                                                              Zoho TeamInbox Resources



                                                                                      Zoho DataPrep Resources



                                                                                        Zoho DataPrep Demo

                                                                                        Get a personalized demo or POC

                                                                                        REGISTER NOW


                                                                                          Design. Discuss. Deliver.

                                                                                          Create visually engaging stories with Zoho Show.

                                                                                          Get Started Now









                                                                                                              • Related Articles

                                                                                                              • Creating a Message Handler

                                                                                                                A message handler can be created in a Zoho IoT application by selecting "Message Handler" in the Category field while creating a custom function. Configuring the fields while creating a message handler Language: Deluge is the supported language for ...
                                                                                                              • Adding a Message Handler through a Device Instance

                                                                                                                A message handler custom function can be added through a device instance in the end application. To add a message handler through a device, Access the End Application. Select Devices > Devices in the left pane. Click on the name of the device for ...
                                                                                                              • Understanding message formatter and message handler

                                                                                                                In the Zoho IoT application, data sent from the gateway/smart device can be enhanced further. This includes decoding raw payload into a usable JSON format and performing additional computations and enrich by calling external API's. This is achieved ...
                                                                                                              • Testing a Message Handler Custom Function

                                                                                                                While creating a message handler custom function, you can test the function to see if the desired output is received. For testing, you can provide the input value and see if the expected output value is obtained. For the message handler custom ...
                                                                                                              • Testing State Memory Enabled Message Handler Custom Function

                                                                                                                With state memory enabled, you can store values that can be used in successive messages to handle the incoming messages. We will go through a sample code to understand how state memory works while creating a custom function. headerMap = ...
                                                                                                                Wherever you are is as good as
                                                                                                                your workplace

                                                                                                                  Resources

                                                                                                                  Videos

                                                                                                                  Watch comprehensive videos on features and other important topics that will help you master Zoho CRM.



                                                                                                                  eBooks

                                                                                                                  Download free eBooks and access a range of topics to get deeper insight on successfully using Zoho CRM.



                                                                                                                  Webinars

                                                                                                                  Sign up for our webinars and learn the Zoho CRM basics, from customization to sales force automation and more.



                                                                                                                  CRM Tips

                                                                                                                  Make the most of Zoho CRM with these useful tips.



                                                                                                                    Zoho Show Resources