Newbie help – split text and store in variables

Using node-red, I receive messages from Telegram which are formatted like:

{
  "payload": {
    "chatId": 12345,
    "messageId": 1,
    "type": "message",
    "content": "Hi_._MainChat"
  },
  "originalMessage": {
    "message_id": 1,
    "from": {
      "id": 1234567890,
      "is_bot": false,
      "first_name": "John",
      "username": "JohnDoe",
      "language_code": "en"
    },
    "chat": {
      "id": 12345,
      "first_name": "John",
      "username": "JohnDoe",
      "type": "private"
    },
    "date": 1590504750,
    "text": "Hi_._MainChat"
  },
  "_msgid": "3be22fdd.3c9f5"
}

I want to split the content at '_._' and store the left ("Hi") and the right ("MainChat") part in two variables for later use. Could one be so kind and assist me how to do this?

I tried a function like

// Split the message content to extract text and origin
var text = {};
text.payload = (msg.originalMessage.text.split("_._"))[0];
text.type = "text";

var origin = {};
origin.payload = (msg.originalMessage.text.split("_._"))[1];
origin.type = "origin";

msg.payload.text = text.payload;
msg.payload.origin = origin.payload;

return [text,origin];

but failed miserably.

a little bit of pointless code but not too far off.

// Split the message content to extract text and origin
var parts = msg.originalMessage.text.split("_._");
msg.payload.text = parts[0];
msg.payload.origin = parts[1];
return msg;//return the updated msg for the next node to access msg.payload.text & msg.payload.origin

//// or if you REALLY REALLY want two messages to be sent from the function then...
//var msg1 = {payload: parts[0]};
//var msg2 = {payload: parts[1]};
//return[msg1,msg2]
1 Like

Thanks a lot!

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