Generic boilerplate for RedBot nodes

const _ = require('underscore');
const RegisterType = require('../lib/node-installer');
const { ChatExpress } = require('chat-platform');
const { 
  enrichFilePayload, 
  isValidMessage, 
  getChatId, 
  getMessageId, 
  getTransport, 
  extractValue,
  appendPayload 
} = require('../lib/helpers/utils');
const MessageTemplate = require('../lib/message-template-async');

module.exports = function(RED) {
  const registerType = RegisterType(RED);

  function ChatBotMyNode(config) {    
    RED.nodes.createNode(this, config);
    const node = this;
    this.my_config = config.my_config;

    this.on('input', function(msg, send, done) {
      // send/done compatibility for node-red < 1.0
      send = send || function() { node.send.apply(node, arguments) };
      done = done || function(error) { node.error.call(node, error, msg) };
      const sendPayload = appendPayload(send, msg);
      // check if valid message
      if (!isValidMessage(msg, node)) {
        done('Invalid input message');
        return;
      }      
      // get RedBot values
      const chatId = getChatId(msg);
      const messageId = getMessageId(msg);
      const template = MessageTemplate(msg, node);
      const transport = getTransport(msg);      
      // check transport compatibility
      if (!ChatExpress.isSupported(transport, 'chatbot-my-node')) {
        done(`Node "chatbot-my-node" is not supported by ${transport} transport`);
        return;
      }
      // get vars
      let my_config = extractValue('string', 'my_config', node, msg)
      
      // do something

      template(content)
        .then(content => {
          sendPayload({
            type: 'my-type',
            content: content,
            chatId: chatId,
            messageId: messageId,
            inbound: false
          });
          done();
        });
    });
  }
  
  registerType('chatbot-my-node', ChatBotMyNode);
};