UNPKG

node-red-contrib-chatbot

Version:

REDBot a Chat bot for a full featured chat bot for Telegram, Facebook Messenger and Slack. Almost no coding skills required

38 lines (34 loc) 2.62 kB
<script type="text/javascript"> $.RedBot.registerType('chatbot-authorized', { category: $.RedBot.config.name + ' Flow', color: '#FFCC66', defaults: { }, inputs: 1, outputs: 2, paletteLabel: 'Authorized', icon: 'chatbot-authorized.png', label: function() { return 'Authorized?'; } }); </script> <script type="text/x-red" data-help-name="chatbot-authorized"><p>Check if the current chat is authorized, in that case, the message will be forwarded to the first output, otherwise the second output.</p> <p><img src="https://s3.us-west-2.amazonaws.com/secure.notion-static.com/8f24f336-2ebf-48bb-86b5-b5e6af17e145/example-authorized.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=AKIAT73L2G45EIPT3X45%2F20230218%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Date=20230218T124909Z&X-Amz-Expires=3600&X-Amz-Signature=40e6f451c73e0e85e07fde7586412b68263381fdf86788bad05ede8ae218a3fb&X-Amz-SignedHeaders=host&x-id=GetObject" alt="Authorized node"></p> <p>The authorization scheme is very simple: a flag <code>authorized</code> in the <a href="https://www.notion.so/3460c588cf234344974936acd05f8c16">Chat Context</a> is set if the <em>chatId</em> 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 <code>authorized</code> flag.</p> <p>For example, the same check can be obtained with a <a href="https://www.notion.so/4113636f565d4ff4af08bc61a644206b">Rules node</a> or a <code>Function node</code></p> <pre><code class="language-javascript">const chat = msg.chat();Promise.resolve(chat.get(&#39;authorized&#39;)) .then(authorized =&gt; { node.send(authorized ? [msg, null] : [null, msg]); }); </code></pre> <p>In order to obtain a custom authorization scheme, use the <a href="https://www.notion.so/04668c7a415547bc9f34be57dd063db2">Extend node</a> , for example</p> <pre><code class="language-javascript">// this middleware will be execute before any &#39;in&#39; middleware node.chat.use(function(message) { const chat = message.chat(); // always return a promise, this ensure it&#39;s a promise even if using the // sync memory context return Promise.resolve(chat.get(&#39;chatId&#39;)) .then(chatId =&gt; Promise.resolve(myAuthScheme(chatId))) .then(authorized =&gt; Promise.resolve(chat.set(&#39;authorized&#39;, authorized))); }); </code></pre> <p>In that case the custom function <code>myAuthScheme</code> can also be an asynchronous call to an external service.</p> </script>