Search code examples
jsontelegram-botnode-red

Node red JSON object


I have object message in node red like this :

{ chatId: 111111111, messageId: 1111, type: "message", content: "VENT Auto", date: "2017-12-28T19:46:45.000Z",inbound: true }

I want to parse this message and write if function like this :

if (chatId==111111111){return msg;} 

I tried these : json parse-1

json-parse-2

json-parse-3

this is the function in node red :

msg=msg.payload.chatId
return msg

I want to see the chatid in the output :

if (msg.payload.chatid== 11111111){return msg)

and msg is the output of this funtion :

[{"id":"b068944.8218168","type":"chatbot-telegram-receive","z":"52d0aba5.3f0a3c","bot":"","x":170,"y":600,"wires":[["f479d424.fe9e3"]]},{"id":"f479d424.fe9e3","type":"function","z":"52d0aba5.3f0a3c","name":"","func":"\nreturn msg;","outputs":1,"noerr":0,"x":370,"y":620,"wires":[["c8f111dc.9ac8d"]]},{"id":"ba82cead.cdf808","type":"change","z":"52d0aba5.3f0a3c","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.content","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":580,"y":620,"wires":[["5f96e508.3d69cc"]]},{"id":"c8f111dc.9ac8d","type":"function","z":"52d0aba5.3f0a3c","name":"","func":"msg=msg.payload.chatId\nreturn msg\n","outputs":1,"noerr":0,"x":330,"y":700,"wires":[["ba82cead.cdf808"]]}]

I send message to telegram bot and I want to compare the chatid to my chat id.


Solution

  • You should always return a whole message object from a function node. You are trying to return just a string, this will not work.

    If you just want to prevent the passing of a message that does not match the id you can do it one of 2 ways.

    1. Use a function node with the following code:

      if (msg.payload.chatId == "111111111") {
        return msg;
      }
      

      This will only allow the message to pass if the chatId is 111111111

    2. Use a switch mode with the following:

      enter image description here