Node-RED has two variable context global and flow, the first is available everywhere in the app, the second just in the executed flow.

RedBot introduces the chat context where is possible to store information related to the specific user. The Receiver node store here some default information like chatId, username, authorized, firstName, lastName, etc. but it’s possible to store any kind of information.

Two chat context providers are available - Memory (default): it’s a very fast and volatile storage, means that when the server is restarted all the stored data is lost. It’ the default one (good for experimenting) and all the methods are synchronous - Plain file: it stores data in a plain json file, all the methods are asynchronous. - SQLite: it stores context in a local *.sqlite file

To get the chat context in a Function node (using the Memory context provider):

const chat = msg.chat();
console.log(chat.get('authorized')); 
// is the user authorized
console.log(chat.get('username')); // guidone72
chat.set('my_stuff', 'remember that');

The same with Plain file on SQLite context provider

const chat = msg.chat();
chat.get('authorized')  
  .then(authorized => {    
    console.log(authorized); // is the user authorized    
    node.send(msg);  
  });

since it’s asynchronous all methods return a Promise. It’s also possibile to combine multiple calls

const chat = msg.chat();
chat.get('authorized', 'username')  
  .then(variables => {    
    console.log(variables.authorized); // is the user authorized    
    console.log(variables.username); // guidone72    
    return chat.set('my_stuff', 'remember that');  
  })  
  .then(() => node.send(msg));  // pass thru when the chat.set is over

Context keys

Platform keys

Platform keys are pre-populated by RedBot: chatId (a unique identifier for a user in a specific platform), userId (a unique identifier of a user through different platforms) and transport. Values in the chat context are meant to be persisted over the time and different platforms, for this reason these keys are read-only and cannot be modified. They can be accessed by javascript msg.chat().get('chatId') or in templates {{chatId}} for retro-compatibility purposes, by they really don’t belong to the chat context.

Flow keys

Some keys are pre-populated by RedBot: firstName, lastName, language, authorized, pending. These are specific to the flow and the used platform and can be modified or deleted by the msg.chat().* methods. Not all the chat platforms provide such informations and/or may be different (unless a Mission Control [[MC Store|MC-Store]] node is used to reconcile the data): for example a user can have the Telegram client in the Italian language and the Messenger client in English language. The {{message}} key is the current inbound message (from the user) and the {{messageId}} is the platform-dependent unique id (generally used to edit/delete already sent messages)

Global keys