Chat Context
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.
To get the chat context in a Function node
:
var 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 chat context is not persisted, it means that if Node-RED is restarted the context is lost. In the future releases there will be support for an external storage.
- .get(key): Get a value for a specific key
- .set(key, value): Set the value for a specific key
- .remove(key): Remove a key from the chat context
- .clear(): Clear all the chat context
- .dump(): Dump in console the chat context
- .all(): Get all keys/values
Some most of the nodes like Message node
evaluates the specified text with a very simple template system (Handlebars-like syntax).
For example to say hello to the user
Hi, {{firstName}}!
There are some predefined variables in each chat context
- chatId - The chat id (same for a specific user)
- messageId - The message id of the last sent message
- userId - The user id
- firstName - First name, when available (for example in Telegram must me specified in preferences)
- lastName - Last name, when available (for example in Telegram must me specified in preferences)
- authorized - boolean, whether the user is authorized or not
- transport - the current transport, could be telegram, facebook, slack
- message - the current message from the user in string format