Throughout this document, we will use the following terms:
Integration
The code that connects Botpress to an external service.
External service
The service that provides HITL functionality. This could be a help desk system like Zendesk, or any other system that allows human agents to send and receive messages to end users.
Human agent
A person who interacts with end users through the external service. This could be a support agent, a sales representative, or any other type of human agent.
End user
A person who interacts with your bot through Botpress. This could be a customer, a user, an employee, or any other type of end user.
External user
A representation of an end user within the external service. This is typically created when a HITL session is started.
HITL session
A conversation between an end user and a human agent. This is typically represented as a ticket in the external service.
HITL interface
The interface that defines the contract for implementing HITL functionality in your integration. This interface specifies the actions, events, and channels that your integration must implement to support HITL.
HITL plugin
The Botpress plugin that manages HITL sessions and relays messages between end users and your integration. Installing this plugin in a bot enables HITL functionality using the selected integration.
Entity
In the context of the HITL interface and plugin, an entity can be provided in order to support extra parameters in the startHitl action. This is useful to provide extra information about the HITL session, such as a ticket priority or customer identification number.
In your new IntegrationDefinition() statement, add an empty entity:
integration.definition.ts
Report incorrect code
Copy
export default new sdk.IntegrationDefinition({ entities: { ticket: { // <= name of the entity schema: sdk.z.object({}) }, },})
The name of the entity isn’t important, but it’s recommended to use a name that matches the terminology of the external service. For example, if the external service uses the term “ticket” to refer to a HITL session, you could name the entity ticket or hitlTicket.
4
Extend your definition
Use the .extend() function at the end of your new IntegrationDefinition() statement:
integration.definition.ts
Report incorrect code
Copy
export default new sdk.IntegrationDefinition({ entities: { ticket: { // <= name of the entity schema: sdk.z.object({}) }, },}) .extend(hitl, (self) => ({ entities: { hitlSession: self.entities.ticket, // <= name of the entity }, }))
The exact syntax of .extend() will be explained in the next section.
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.
Whilst renaming actions, events and channels is optional, it’s highly recommended to rename these to match the terminology of the external service. This will help you avoid confusion and make your integration easier to understand.
If you want to rename these actions, you can do so in the configuration object. For example, if you want to rename createUser to hitlCreateUser, you can do it like this:
For example, if you’re using a help desk system like Zendesk, Jira Service Desk, or Freshdesk for HITL functionality, you might rename startHitl to createTicket and stopHitl to closeTicket. These systems use tickets to represent help requests, so renaming actions to match their terminology makes your integration clearer and easier to understand.
If you want to rename these events, you can do so in the configuration object. For example, if you want to rename hitlAssigned to agentAssigned, you can do it like this:
hitl - Used by the HITL plugin to send and receive messages from the external service. This represents the communication channel for the HITL session, like a support ticket on Zendesk or a direct message thread on Slack.
If you want to rename this channel, you can do so in the configuration object. For example, if you want to rename hitl to supportTicket, you can do it like this:
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.
Please refer to the expected input and output schemas for the action:
interface.definition.ts line 85.This action should implement the following logic:
1
Create a Botpress user
Create a Botpress user using the Botpress client by calling the client.createUser() method.
Update the Botpress user to map it to the external user. This is typically done by setting a tag on the Botpress user with the external user‘s ID.
5
Yield control back to the plugin
Yield control back to the plugin by returning an object containing the Botpress user’s ID.
As reference, here’s how this logic is implemented in the Zendesk integration:
src/index.ts
Report incorrect code
Copy
export default new bp.Integration({ actions: { async createUser({ ctx, input, client }) { // Create a Botpress user: const { name, email, pictureUrl } = input const { user } = await client.createUser({ name, pictureUrl, tags: { email, role: 'end-user', }, }) // Create an external user on Zendesk: const zendeskClient = getZendeskClient(ctx.configuration) const zendeskUser = await zendeskClient.createOrUpdateUser({ role: 'end-user', external_id: user.id, // <= map to the Botpress user ID name, email, remote_photo_url: pictureUrl, }) // Map the Botpress user to the external user: await client.updateUser({ id: user.id, tags: { id: zendeskUser.id.toString(), // <= map to the external user ID }, }) // Yield control back to the plugin and return the user ID: return { userId: user.id, // <= return the Botpress user ID } }, },})
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.
Please refer to the expected input and output schemas for the action:
interface.definition.ts line 109.This action should implement the following logic:
1
Fetch the Botpress user
Fetch the Botpress user with ID input.userId that was passed in the input parameters.
2
Retrieve the external user's ID
From the Botpress user’s tags, retrieve the external user‘s ID.
3
Create a Botpress conversation
Create a Botpress conversation using the Botpress client by calling the client.getOrCreateConversation() method.
Yield control back to the plugin by returning an object containing the Botpress conversation’s ID.
As reference, here’s how this logic is implemented in the Zendesk integration:
src/index.ts
Report incorrect code
Copy
export default new bp.Integration({ actions: { async startHitl({ ctx, input, client }) { // Fetch the Botpress user that was passed in the input parameters: const { user } = await client.getUser({ id: input.userId, }) // From the user's tags, retrieve the external user's id: const zendeskAuthorId = user.tags.id if (!zendeskAuthorId) { throw new sdk.RuntimeError( `User ${user.id} isn't linked to a Zendesk user` ) } // Create a new ticket on Zendesk: const zendeskClient = getZendeskClient(ctx.configuration) const ticketTitle = input.title ?? 'Untitled Ticket' const ticketBody = 'A user created a support ticket' const createdZendeskTicket = await zendeskClient .createTicket(ticketTitle, ticketBody, { id: zendeskAuthorId, // <= map the ticket to the external user ID }) // Create a Botpress conversation and map it to the Zendesk ticket: const { conversation } = await client.getOrCreateConversation({ channel: 'hitl', tags: { id: createdZendeskTicket.id.toString(), // <= map to the ticket ID }, }) // Map the Zendesk ticket to the Botpress conversation: await zendeskClient.updateTicket(createdZendeskTicket.id, { external_id: conversation.id, // <= map to the Botpress conversation ID }) // Yield control back to the plugin and return the conversation ID: return { conversationId: conversation.id, // <= return the Botpress conversation ID } }, },})
The input parameters of the startHitl action contain a messageHistory parameter. This parameter contains the conversation history that should be relayed to the external service to provide the human agent with context about the conversation. This parameter is an array of every message that was sent in the conversation prior to the HITL session being started.
Show TypeScript schema for the messageHistory parameter
If you decide to relay the conversation history to the external service, you can do so by iterating over the messageHistory array and sending each message to the external service using its API or SDK. However, doing so might cause a significant number of notifications being sent to the external service. 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:
Report incorrect code
Copy
## User1 said:> Hello, I need help with my order.## Bot replied:> I have escalated this conversation to a human agent. Please wait while I connect you.
export default new sdk.IntegrationDefinition({ entities: { ticket: { // <= name of your entity schema: sdk.z.object({ priority: sdk.z.string().optional(), // <= add the extra parameter here }) }, },})
Doing so will display the 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
Report incorrect code
Copy
export default new bp.Integration({ actions: { async startHitl({ ctx, input, client }) { // Retrieve the extra parameter: const priority = input.hitlSession?.priority ?? 'normal' // Then use it to create the HITL session: const createdZendeskTicket = await zendeskClient .createTicket(ticketTitle, ticketBody, { priority, // <= pass the extra parameter to the external service }) }, },})
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.
Please refer to the expected input and output schemas for the action:
interface.definition.ts line 162.This action should implement the following logic:
1
Fetch the Botpress conversation
Fetch the Botpress conversation with ID input.conversationId that was passed in the input parameters.
2
Retrieve the HITL session's ID
From the Botpress conversation’s tags, retrieve the HITL session‘s ID.
Yield control back to the plugin by returning an empty object.
The input parameters contain an unused reason parameter. Please ignore it. This parameter will be removed in future versions of the HITL interface.
As reference, here’s how this logic is implemented in the Zendesk integration:
src/index.ts
Report incorrect code
Copy
export default new bp.Integration({ actions: { async stopHitl({ ctx, input, client }) { // Fetch the Botpress conversation that was passed in the input parameters: const { conversation } = await client.getConversation({ id: input.conversationId, }) // From the conversation's tags, retrieve the Zendesk ticket's id: const ticketId: string | undefined = conversation.tags.id if (!ticketId) { return {} } const zendeskClient = getZendeskClient(ctx.configuration) // Close the ticket on Zendesk: await zendeskClient.updateTicket(ticketId, { status: 'closed', }) // Yield control back to the plugin: return {} }, },})
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.
This channel handler should implement the following logic:
1
Retrieve the HITL session's ID
From the Botpress conversation’s tags, retrieve the HITL session‘s ID.
2
Retrieve the external user's ID
If the payload contains a userId parameter, the message has been sent by the end user. Retrieve the external user‘s ID from the tags of the Botpress user payload.userId.
If the payload doesn’t contain a userId parameter, the message has been sent by the bot. Retrieve the external user‘s ID from the tags of the attached Botpress user.
3
Send the message to the HITL session
Using the external service‘s API or SDK, send the message to the HITL session. This is typically a comment in a ticket.
As reference, here’s how this logic is implemented in the Zendesk integration:
src/index.ts
Report incorrect code
Copy
export default new bp.Integration({ channels: { hitl: { messages: { async text({ client, conversation, ctx, payload, user }) { // Retrieve the ticket id from the conversation's tags: const zendeskTicketId = conversation.tags.id // Retrieve the external user: let zendeskAuthorId = user.tags.id if (payload.userId) { const { user: botpressUser } = await client.getUser({ id: payload.userId }) zendeskAuthorId = botpressUser.tags.id } // Send the message to Zendesk: return await getZendeskClient(ctx.configuration) .createComment(zendeskTicketId, zendeskAuthorId, payload.text) }, }, }, }})
When notified by the external service that a new message has been added to the HITL session, you should relay the message to the Botpress conversation:
1
Retrieve the Botpress conversation's ID
Retrieve the Botpress conversation’s ID from the HITL session‘s metadata.
Using the Botpress client, emit the hitlAssigned event by calling the client.createEvent() method.
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.
Using the Botpress client, emit the hitlStopped event by calling the client.createEvent() method.
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.