Adapters

An adapter is a function that has access to the Chatbot object. The Chatbot object is passed as the only argument of the function.

function myAdapter(bot) {
  // subscribe middlewares
  bot.subscriptions.onDisplayUserMessage(myCallback);
}

You must provide the adapters in the configuration of the SDK build method. Add a property called adapters, which has to be an array. Adapters will be executed in the same order in the adapters array. There is no limit of how many adapters you want to use.

function myAdapter(bot) {
  // adapter stuff
}

const configuration = {
  // other configuration, like labels, environment...
  adapters: [
    myAdapter,
    otherAdapter
  ]
}
Note

The purpose of adapters is to create middleware chains with the subscribed middlewares before the Chatbot is created. It is not possible to subscribe or unsubscribe any middleware after the Chatbot creation, so the order of middlewares will be the same during the Chatbot runtime (i.e. after its creation). This way, the developer customizing the Chatbot can know the order of middlewares by checking the order of adapters.

Important considerations

Actions

As described above, you can subscribe middlewares to certain actions. With all middlewares subscribed for each action, the Chatbot creates a middleware chain that makes a function composition of the original action. This is done after the last adapter is executed. In order to make sure that all middlewares are in the middleware chain for any given action, actions will throw an error if they are called in the main scope of an adapter.

function myAdapter(bot) {
  bot.subscriptions.onSendMessage(
    function(messageData, next) {
      // This function scope is not the main scope of an adapter, so actions will work
      bot.actions.displaySystemMessage({message: 'Sending a message to the Inbenta API...'});

      return next(messageData);
    }
  );

  setTimeout(function() {
    // This function scope is not the main scope of an adapter, so actions will work
    bot.actions.displaySystemMessage({message: 'System message with 2 sec of delay'});
  }, 2000);

  // This scope is the main scope of an adapter, so the following line will throw an error
  bot.actions.displaySystemMessage({message: "This message won't be displayed, an error will be thrown instead"});
}
Subscriptions

In order to create the middleware chains only once during the build, adding subscriptions after the build will not add middleware in the middleware chain. For this reason, subscriptions only work in the main scope of an adapter.

function myAdapter(bot) {
  // In this scope (myAdapter main scope) subscriptions will work

  setTimeout(function() {
    // In this scope, which will be executed after finishing build process, subscriptions won't have effect.
    bot.subscriptions.onDisplayUserMessage(function(messageData, next) {
      messageData.message = 'This modification will not have effect, so the original message will be displayed';
      return next(messageData);
    });
  }, 1);
}