The Human in the loop (HITL) interface allows you to implement human agent intervention in your integration.
Terminology
Throughout this document, we will use the following terms:External service requirements
The providing HITL functionality must support the following:- An API that allows creating .
- An API that allows creating .
- An API that allows adding messages to .
- An API that allows closing .
- Webhooks that can notify your of the following events:
- closure.
- assignment.
- reply.
Updating your package.json file
Finding the current interface version
The current version of thehitl interface is:
You will need this version number for the next steps.
Adding the interface as a dependency
Once you have the version, you can add it as a dependency to your :Add the dependencies section
If there is no
bpDependencies section in your ‘s package.json file, create one:package.json
Add the interface as a dependency
In the
bpDependencies section, add the as a dependency. For example, for version 2.0.0, you would add the following:package.json
Install the interface
Now that you have added the as a dependency, you can run the
bp add command to install it. This command will:- Download the interface from Botpress.
- Install it in a directory named
bp_modulesin your ‘s root directory.
Adding a helper build script
To keep your up to date, we recommend adding a helper build script to yourpackage.json file:
Add the build script
In the
scripts section, add the following script:package.json
If the
build script already exists in your package.json file, please replace it.npm run build, it will automatically install the and build your .
Editing your integration definition file
Adding the interface to your integration definition file
Now that the is installed, you must add it your integration definition file in order to implement it.Add an empty entity
In your
new IntegrationDefinition() statement, add an empty entity:integration.definition.ts
The name of the entity isn’t important, but it’s recommended to use a name that matches the terminology of the . For example, if the uses the term “ticket” to refer to a , you could name the entity
ticket or hitlTicket.Configuring the interface
The.extend() function takes two arguments:
- The first argument is a reference to the interface you want to implement. In this case, it’s
hitl. - The second argument is a configuration object. Using this object, you can override interface defaults with custom names, titles, and descriptions.
Renaming actions
Thehitl interface defines three actions that are used to interact with the :
createUser- Used by the to request the creation of a user in the and on Botpress.startHitl- Used by the to request the creation of a in the .stopHitl- Used by the to request the closure of a in the .
createUser to hitlCreateUser, you can do it like this:
integration.definition.ts
Renaming events
Thehitl interface defines these events to notify the plugin of changes in the external service:
hitlAssigned- Emitted by your to notify the that a has been assigned to a .hitlStopped- Emitted by your to notify the that a has been closed.
hitlAssigned to agentAssigned, you can do it like this:
integration.definition.ts
Renaming channels
Thehitl interface defines these channels:
hitl- Used by the to send and receive messages from the . This represents the communication channel for the , like a support ticket on Zendesk or a direct message thread on Slack.
hitl to supportTicket, you can do it like this:
integration.definition.ts
Implementing the interface
Implementing the actions
Implementing createUser
The createUser action is used by the to request the creation of an (a requester) in the .
If you opted to rename the action to something else than
createUser in the “Configuring the interface” section, please use the new name instead of createUser.Create a Botpress user
Create a Botpress user using the Botpress client by calling the
client.createUser() method.Map the external user to the Botpress user
Update the on the to map it to the Botpress user. Please refer to the ‘s documentation to know how to set extra metadata for the . The must be able at any time to query the in order to retrieve the Botpress user ID from the .
Map the Botpress user to the external user
Update the Botpress user to map it to the . This is typically done by setting a tag on the Botpress user with the ‘s ID.
src/index.ts
Implementing startHitl
The startHitl action is used by the to request the creation of a (typically a ticket) in the .
If you opted to rename the action to something else than
startHitl in the “Configuring the interface” section, please use the new name instead of startHitl.Fetch the Botpress user
Fetch the Botpress user with ID
input.userId that was passed in the input parameters.Create a Botpress conversation
Create a Botpress conversation using the Botpress client by calling the
client.getOrCreateConversation() method.Map the Botpress conversation to the HITL session
Update the Botpress conversation to map it to the . This is typically achieved by setting a
ticketId tag on the Botpress conversation.Map the HITL session to the Botpress conversation
Update the on the to map it to the Botpress conversation. Please refer to the ‘s documentation to know how to set extra metadata for the (typically a ticket). The must be able at any time to query the in order to retrieve the Botpress conversation ID from the .
src/index.ts
Relaying the conversation history
The input parameters of thestartHitl action contain a messageHistory parameter. This parameter contains the conversation history that should be relayed to the to provide the with context about the conversation. This parameter is an array of every message that was sent in the conversation prior to the being started.
If you decide to relay the conversation history to the , you can do so by iterating over the messageHistory array and sending each message to the using its API or SDK. However, doing so might cause a significant number of notifications being sent to the . To alleviate this, you can choose to send only the last few messages in the conversation history, or to concatenate the messages into a single message. For example you could combine messages like this:
Adding extra parameters to the startHitl action
If the requires extra parameters when starting a , you can add them to the hitlSession entity, or whichever name you chose for the entity in the Adding the interface to your integration definition file section. For example, if the requires a priority parameter, you can add it like this:
integration.definition.ts
priority parameter in the “Start HITL” card within the Botpress Studio, allowing the bot authors to set it. The value of this parameter will then be passed to the startHitl action as part of the hitlSession input parameter:
src/index.ts
Implementing stopHitl
The stopHitl action is used by the to request the closure of a (typically a ticket) in the .
If you opted to rename the action to something else than
stopHitl in the “Configuring the interface” section, please use the new name instead of stopHitl.Fetch the Botpress conversation
Fetch the Botpress conversation with ID
input.conversationId that was passed in the input parameters.Close the HITL session
On the , close the . This is typically involves resolving or closing a ticket in the .
The input parameters contain an unused
reason parameter. Please ignore it. This parameter will be removed in future versions of the .src/index.ts
Implementing the channel
Thehitl channel is used by the relay messages to the , which is usually a ticket or thread in the .
If you opted to rename the channel to something else than
hitl in the “Configuring the interface” section, please use the new name instead of hitl.Retrieve the external user's ID
- If the payload contains a
userIdparameter, the message has been sent by the . Retrieve the ‘s ID from the tags of the Botpress userpayload.userId. - If the payload doesn’t contain a
userIdparameter, the message has been sent by the bot. Retrieve the ‘s ID from the tags of the attached Botpress user.
src/index.ts
Implementing the events
You should set up webhooks so that the receives notifications about these events:- A new message is added to the (usually a ticket).
- A has been assigned to the .
- The was closed.
Incoming messages
When notified by the that a new message has been added to the , you should relay the message to the Botpress conversation:Retrieve the Botpress conversation's ID
Retrieve the Botpress conversation’s ID from the ‘s metadata.
Implementing hitlAssigned
When notified by the that a has been assigned to the , you should notify the by emitting the hitlAssigned event:
Retrieve the Botpress conversation's ID
Retrieve the Botpress conversation’s ID from the ‘s metadata.
If you opted to rename the event to something else than
hitlAssigned in the “Configuring the interface” section, please use the new name instead of hitlAssigned.Implementing hitlStopped
When notified by the that the was closed, you should notify the by emitting the hitlStopped event:
Retrieve the Botpress conversation's ID
Retrieve the Botpress conversation’s ID from the ‘s metadata.
If you opted to rename the event to something else than
hitlStopped in the “Configuring the interface” section, please use the new name instead of hitlStopped.