Categories:
Init middleware
Description of init-middleware, the very first one to be executed once the HTTP request arrives at the server
Introduction
The init-middleware is the first to be executed once the HTTP request arrives at the server to the POST /api/messages endpoint and the Direct Line authentication executed by the Adapter is successful.
It is a Turn middleware, so it is executed during turn initialization.
It launches the execution of all the following middlewares, in the order declared during aura-bot initialization. It is only executed if the type of the activity is message.
The source code of this middleware is included in Aura Bot Platform middlewares - Github repository.
init-middleware flowchart
The orderly steps for the management of this middleware are detailed below:
- Set a proper correlator in the
TurnContext, that will be used during the whole processing of the activity throughout aura-bot.
This means that:
- If a correlator is received in the
channelDataof the activity, this one is used. - If the channel does not send a correlator, a new one is generated at this point, to be used the same way.
ContextUtils.setCorrelator(context, ContextUtils.getCorrelatorFromChannelData(context));
/**
* Get correlator from channel data, if exist there, or creates a new one.
* @param context Context where the channel data will be taken from
*/
static getCorrelatorFromChannelData(context) {
if (!context.activity.channelData || !context.activity.channelData.correlator) {
context.activity.channelData = { ...context.activity.channelData || {}, correlator: uuid.v4() };
}
return context.activity.channelData.correlator;
}
-
Push the
auraId, received in thefrom.idof the activity, to theTurnContext, to make it available in all the activity execution steps. ThegetAuraIdmethod sets theauraIdbased on the channel type, if the channel is integrated. Then, theauraIdis prefixed with the channel prefix.This is necessary because the
auraIdis the key to the user cache and, if it is not formatted properly, it will fail to fetch the user from the cache and will re-request the user’s data from the services.ContextUtils.setAuraId(context, ChannelDataUtils.getAuraId(context, AuraChannelsConfiguration.instance)); -
Initialize the data accessors of the conversational context in the
TurnContext:conversationState,userStateandauthenticationState.ContextUtils.setAuraPersistentDataAccesors(context, this.dataAccessors); -
Initialize the
conversationState. This is necessary to avoid errors when a user accesses through several channels simultaneously.ContextUtils.setConversationState(context, this.conversationState); -
Set activity locale with the default value.
context.activity.locale = ConfigurationManager.instance.environmentConfiguration.AURA_DEFAULT_LOCALE;
At this stage, two scenarios can happen:
-
If the activity processing is executed properly, the following middleware is executed.
-
In the happening of an unexpected error during the activity processing, the error makes all its way back to this initialization middleware error handling, in order to return the user a response activity including the most appropriate text related to the error.
One of the possible errors landing in this middleware during the activity processing, happens when the size of the response is bigger than the ones allowed by Direct Line protocol, that must not exceed 256K characters, the response is bypassed and a generic text explaining that there are too many results is returned to the user: (errors:error.message.oversize).