Check if the current chat is authorized, in that case, the message will be forwarded to the first output, otherwise the second output.
Authorized node
The authorization scheme is very simple: a flag authorized
in the Chat Context is set if the chatId is included in a whitelist defined in the bot configuration. All messages will go out of the receiver node, no matter if the user is authorized or not, it just affects the authorized
flag.
For example, the same check can be obtained with a Rules node or a Function node
const chat = msg.chat();Promise.resolve(chat.get('authorized')) .then(authorized => { node.send(authorized ? [msg, null] : [null, msg]); });
In order to obtain a custom authorization scheme, use the Extend node , for example
// this middleware will be execute before any 'in' middleware
node.chat.use(function(message) {
const chat = message.chat();
// always return a promise, this ensure it's a promise even if using the
// sync memory context
return Promise.resolve(chat.get('chatId'))
.then(chatId => Promise.resolve(myAuthScheme(chatId)))
.then(authorized => Promise.resolve(chat.set('authorized', authorized)));
});
In that case the custom function myAuthScheme
can also be an asynchronous call to an external service.