Data filling in a table using a third-party web hook

Hello. Could you please help me with the issue of filling a table with data using a webhook of a third-party service imonnit.com?

imonnit is a service for monitoring sensors and collecting data from them, with each sensor event, the imonnit service sends data in the form of JSON via a web hook to a url in the form of a post request.
Here is an example of a request sent by a webhook:

{
           "gatewayMessage":{
           "gatewayID":"10000" ,
           "gatewayName":"ExampleGateway" ,
           "accountID":"xxxx" ,
           "networkID":"xxxx" ,
           "messageType":"0" ,
           "power":"0",
           "batteryLevel": "101" ,
           "date": 2022-4-28 21:47:01",
           "count":"3",
           "signalStrength": "29",
           "pendingChange": "False" 
       },
       "sensorMessages":[
          {
              "sensorID":"10001" ,
              "sensorName":"Temp1" ,
              "applicationID":"2" ,
              "networkID":"xxxx" ,
              "dataMessageGUID":"78642056-CBD8-43B0-9A4B-247E58D3B6CB",
              "state": "0" ,
              "messageDate": 2022-4-28 21:48:01",
              "rawData":"23.7",
              "dataType": "TemperatureData",
              "dataValue": "23.7",
              "plotValues": "74.66",
              "plotLabels": "Fahrenheit",                           
              "batteryLevel": "100",
              "signalStrength": "100",
              "pendingChange": "False",
              "voltage": "3.24" 
          }
        ],
        "locationMessages":[
          {
              "deviceID":"10000" ,
              "locationMessageGUID":"5badfa3d-f110-4058-81cd-1ade90983adb",
              "locationDate":"2022-4-28 21:47:11",
              "state":"0",
              "latitude":"40.6977500915527",
              "longitude": "-111.894149780273",
              "altitude": "1302",
              "speed":"0",
              "course": "0",
              "fixTime": "24",
              "sateliteCount": "6",
              "uncertainty": "1"
          }
        ]
     }

I have a table: “messages” for which there is an api in the system through which I can fill it with /data/bulk/messages, but the problem is that the webhook sends me an object with unnecessary data and I only need the structure of the sensorMessages array from the data of this webhook.
Is there any way to add some additional handler that will get the sensorMessages array from the object and then use the /data/bulk/messages url to fill the table? If you need more details, I will write.
Please advise something, I will be very grateful.

Hi Serhii,

Are you doing it with code or Codeless?

Mark

Hello Mark,
Thank you for your reply. Yes, I tried to make a service to which the json will come and try to filter it there and then load it into the table itself via the standard REST API of the table. At the moment, I don’t understand how to get json from the request to process it later.
Or maybe I misunderstand how the services work. Maybe you can tell me another way to do it.
Thank you.

The logic should do the following:

  1. Run an external request
  2. Get the sensorMessages list
  3. Go through the list to transform it so it has only the data you need. Transformation can be done using the Map List codeless block: https://www.youtube.com/watch?v=oiiSRcd-TMg
  4. Use the Bulk Create Codeless block to insert the data into the database.

Give it a try and if you need further help, please share the logic you created.

Hope this helps.

Regards,
Mark

1 Like

Okay, thank you very much for your detailed answer. I’ll try to do it now.

Regards,
Serhii

Hello @Serhii_Khomenko,

Do I understand correctly:

  1. You have created a “Messages” table in Backendless
  2. Imonnit service makes requests for the URL that you passed to it to write data - in your case, you passed it the URL for the bulk creation in the “Messages” table
  3. But Imonnit sends extra data and you need to “intercept” it before saving in order to modify data before saving it into the table.

Is everything right?

If yes, then I would suggest you create a custom API Service, where you can implement your own endpoint for the Immonit, modify retrieved data from it and save it into Backendless table.
Here is a documentation for Node JS API Services: Developing Backendless Server Code with node.js

Basically your service would look like this:

class ImonnitService {
  /**
   * @route POST /messages
   * @param {Object} data - object that Imonnit will send, you can name it whatever you want
   * @returns {Promise<void>}
   */
  async createMessages(data) {
    const { sensorMessages } = data
    
    // do whatever you need to do with sensorMessages, filter, transform etc
    
    await Backendless.Data.of('Messages').bulkCreate(sensorMessages)
  }
}

Backendless.ServerCode.addService(ImonnitService)

The route would be POST https://your-domain.backendless.app/api/services/ImonnitService/messages

There is also Event Handlers API which might be useful in your case also, but API service is a more powerful and extendable feature which you might want to use for other scenarios in your app.

Note: all this can be also implemented using Codeless block (no-code). I don’t know what you’re using but assumed you’re familiar with JS.

Hope it helps.

Regards,
Stanislaw

1 Like

Thank you very much Stanislaw, this is exactly what I was asking about and did not understand how to do.

Regards,
Serhii