Help with msg.payload from object from db

Hi All,

Looking for some assistance on how to formulate or prep an object that i retrieved from a db query and post to Telegram

So, basically you want to extract a string from inside an object and this object is itself an element of an array. Seems dauting to find the path to recover this string but you can use a resource in the debug node to make it easier. Here an example to illustrate.

r-01 .

If you click on the path icon you will get the location of the property in the object. In my example I got payload[0].sla_detail.

Hi Andrei, thank you for your reply.

I got the same path.

payload[0].sla_detail

unsure of how to format it

{
    msg.payload = {};
    msg.payload.type = 'message';
    msg.payload.content = + msg.payload[0];
    msg.payload.chatId = -xxxxxxxx
    return msg;
}

Put that in the payload.content

    var text = msg.payload[0].sla_detail;
    msg.payload = {};
    msg.payload.type = 'message';
    msg.payload.content =  text;
    msg.payload.chatId = -xxxxxxxx
    return msg;

1 Like

Thank you again Steve, thank you for educating me. forever grateful

The reason it works is, we store the value from the database in a temporary variable (text) before we overwrite the msg.payload with a new empty object {} - at which point we destroy the databases payload

1 Like

Afternoon Steve, I trust that you are doing well.

Thank you for all your assistance and your great code.

With your assistance I am now able to to read from a database all the IP addresses that will be ping at a time interval. The idea was to get a Telegram notification when a device goes offline via ping. That all is working great.

The problem or challenge now comes in when a lot goes down at the same time Telegram is overwhelmed by all the messages, can you assist with maybe a delay between lets say after pinging every 5 addresses rest for 5 seconds and continue where it left off?

const chatId = -437570160; // << update this

if (!msg.ping.host) return null; //ensure host has a value

var pingMonitor = flow.get("pingMonitor") || {};//get lookup object ||or|| a new empty object
var pingPrev = pingMonitor[msg.ping.host] || {};//this ping monitor object ||or|| a new empty object
var sendTelegram = false;

//if ping is good, you get a number in payload...
if (typeof msg.payload == "number" && pingPrev.status === "bad" ) {
    pingPrev.status = "good";
    msg.payload = {};
    msg.payload.type = 'message';
    msg.payload.content = `${msg.ping.name} (${msg.ping.host}) is now back online & operational!`;
    msg.payload.chatId = chatId;
    sendTelegram = true;
} else if (msg.payload === false && (pingPrev.status === "good" || !pingPrev.status)) {
    pingPrev.status = "bad";
    msg.payload = {};
    msg.payload.type = 'message';
    msg.payload.content = `${msg.ping.name} (${msg.ping.host}) seems to be OFFLINE`;
    msg.payload.chatId = chatId;
    sendTelegram = true;
}

//update context
pingPrev.datetime = new Date();
pingPrev.host = msg.ping.host;
pingPrev.name = msg.ping.name;
pingMonitor[msg.ping.host] = pingPrev;
flow.set("pingMonitor", pingMonitor);

if (sendTelegram) return msg;

return null;

I have also uploaded the flow for you. Thank you for looking.
Anton

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.