Message node supports basic Handlebars-like templates, sometimes this is not enough and a proper message needs to be prepared in an upstream node. Every message node (Text, Image, etc) accepts strings, images, etc. as input, preparing a message in an upstream node it’s very simple, for example:

Prepare text in upstream node

Prepare text in upstream node

The Function node looks like

console.log(msg);
const chat = msg.chat();
msg.payload = 'This is a special message ' + chat.get('firstName');

This only works if the message parameter is left blank.

It’s important to understand how RedBot keeps track of the current conversation, in the above node the console log will looks like

{
  originalMessage: {
    ...
  },
  payload: {
    type: 'text',
    ...
  }
}

This is how Node-RED works: for every incoming event a message (the msg object) is generated and it’s sent across the flow based on the wiring. The payload key contains the real content of the message (could be a incoming text message or a buffer for an image, etc) while the originalMessage contains the tracking of the conversation.

Inside a Function node you’re free to modify the payload of a message and everything will still work as long as the originalMessage is kept intact and passed through the next node.

For every message node it’s possible to prepare the message in the upstream node, for an image for example

Prepare text in upstream node

Prepare text in upstream node

Here the File node puts the loaded file into the payload as a Buffer (keeping the rest of the msg object intact), the Image node reads it and prepare the proper payload for the sender node.

Generally every message node (like Text, Image, etc) should be wired to a sender.