节点红色JSON对象

时间:2017-12-28 20:00:59

标签: json telegram-bot node-red

我有节点红色的对象消息,如下所示:

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

我想解析这条消息并写下如下函数:

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

我试过这些: json parse-1

json-parse-2

json-parse-3

这是节点红色中的函数:

msg=msg.payload.chatId
return msg

我想在输出中看到chatid:

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

和msg是这个功能的输出:

[{"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"]]}]

我向电报机器人发送消息,我想将chatid与我的聊天ID进行比较。

1 个答案:

答案 0 :(得分:0)

您应始终从函数节点返回整个消息对象。你试图只返回一个字符串,这是行不通的。

如果您只想阻止传递与ID不匹配的邮件,您可以使用以下两种方式之一。

  1. 使用带有以下代码的函数节点:

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

    如果chatId111111111

  2. ,这只会允许邮件通过
  3. 使用具有以下内容的切换模式:

    enter image description here