Skip to content

Anatomy of a Message

Guidone edited this page Feb 1, 2018 · 4 revisions

Messages exchanged by Node-RED nodes are JavaScript objects look like

{
  /* some keys */
  payload: 'some content from the previous node'
}

Generally each node put the result of its computation in the payload key of the message after cloning the incoming message, any other key except payload is left untouched.

All RedBot receivers work in this way: the incoming message of the different platforms (Telegram, Slack, etc.) is translated in a common format and stored in the payload key of the message and the information to keep track of the current chat in the originalMessage key. In this way every node between a receiver and a sender can make any kind of computation, since every node changes just the payload value, the sender node will always be able to handle a proper response using the information of originalMessage.

For an incoming text message "I am a message", this will be the incoming message

// the msg object in a function node
{
  originalMessage: {
    // internal values
  },
  payload: {
    transport: 'telegram',
    chatId: '123456',
    userId: '456789',
    type: 'message',
    inbound: true,
    ts: // instance of moment(),
    content: 'I am a message'
  }
}

The content key it's a String for plain text messages, Buffer for images and files, etc.

The msg object has also some methods:

Method Return Description
.chat() instance of ChatContext() Returns the current chat context, a persisted memory space related to the current user, see here for more information
.client() Object / Class Returns the underlying class used for a specific platform, depends on the platform: Telegram, Slack. Use this to get the wrapped class and access features of a specific platform not yet implemented in RedBot
.api() instance of ChatPlatform() ChatPlatform is a wrapper for third party platform libraries, it translates incoming messages into a common format suitable for RedBot, it's unlikely you'll need this
Clone this wiki locally