The Universal Connector node can be used to use any system with the RedBot nodes.

Let’s see for example how to create a chatbot able to answer to emails, good knowledge of JavaScript and understanding of [[Universal Connector node|Universal-Connector-node]] are prerequisites.

This is a simple flow that takes the emails from the Node-RED Email node and push them through the Universal Receiver node that convertes them in the RedBot format. At this point it’s possible to use any RedBot’s node (for example Dialog Flow node to classify the content of the email). Let’s keep this example simple and just add a simple answer with a Message node which will be pushed to the Universal Sender node that will covert back the message to an appropriate format: in this case the payload to be sent to Node-RED mail out node.

https://github.com/guidone/node-red-contrib-chatbot/blob/master/docs/images/example-universal-1.png

Email responder flow

Basically all nodes between the receiver and sender are universal and can be used (with some expections) by all platforms, sender and receiver nodes takes care of this translation.

Nothing comes for free of course, the translation is implemented in the Extend Chat Server node (with the Universal Connector selected as platform) and takes into account of:

  1. Extracting a chatId from the email payload, this is a unique identifier of the chat, in this case we are going to use the sender email
  2. Extracting a messageId, this is unique identifier for the email
  3. Translate the incoming email message into an appropriate message type, in this example we are going to handle textual emails, the type will be message
  4. Prepare the payload for the Node-RED email sender for the message of type message

Extracting chatId and mesaggeId is trivial

node.chat.onChatId(msg => {  return msg.header !== null ? msg.header.from : null;});node.chat.onMessageId(msg => {  return msg.msgid != null ? msg.msgid : null;});

This is the middleware that listen for everthing is pushed through input of the Universal Receiver node and capture a text email

node.chat.in(message => {  return new Promise((resolve, reject) => {    // email node put the content on the .payload, perhaps additional check if the .payload is a string    if (message.originalMessage.payload !== null) {      message.payload.type = 'message';      message.payload.content = message.originalMessage.payload;      if (message.originalMessage.header !== null) {        message.payload.subject = message.originalMessage.header.subject;      }    }    resolve(message);  });});

The subject of the email is stored as well in the payload, RedBot’s nodes will not use it, it will be useful later. As soon as the incoming message is detected and assigned to a type (in this case message) the chain of middlewares stops and the current message is passed to the output. The message originally sent to the Universal Receiver node is stored in originalMessage, for further details about everything inside message.originalMessage.* read the documentation of Email node of Node-RED.

The middleware that translate a RedBot’s message into something that the Email node is able to understand is

node.chat.out('message', message => {  var chat = message.chat();  return new Promise((resolve, reject) => {     message.topic = message.originalMessage.subject;     message.payload = message.payload.content;     message.to = chat.get('chatId');  // here is the email     resolve(message);  });});

The Email node requires the key topic, payload, to in the message. This will send the answer email with the same subject of the inbound email (in this way the answer will be in a thread with the original email in the receiver’s client). In this example the unique identifier of the chat (_chatId) is the email and it’s used to fill the key to.

In this example we want to use the Node-RED Email node to send emails, in this the middleware just prepares the payload for the email node: for this reason the Universal Sender node has the pass through option enabled