One of the most powerful features of Botpress Integrations is the ability to connect to external messaging channels. Without an integration, your bot cannot receive or send messages—every message must go through an Integration.
Here’s a high-level overview of how messaging works in Botpress:
Here’s a quick breakdown of the sequence:
webhook.botpress.cloud/$webhookId
). Every integration has a unique way of providing the webhook URL to the external service. For example, Telegram does it using the API, while Slack requires you to set up a webhook URL in the Slack app settings. The webhook ID path parameter is a string that uniquely identifies the pair of an integration and a bot. This ID is different if you install the same integration in multiple different bots.api.botpress.cloud/v1/chat
.The Runtime API requires authentication as either a Bot or an Integration.
If Botpress doesn’t yet provide an integration for your preferred messaging service, you can create your own using the webhook-message
template.
Follow the Getting Started guide and select the webhook-message
template instead of hello-world
.
After initializing your project, you’ll see the following integration definition:
Let’s break down this data structure:
configuration
: Defines the settings bot developers must provide when installing the integration in the Studio. The configuration schema is used to display a configuration form. Here, only a webhookUrl
is required to receive outgoing messages.channels
: Specifies supported messaging channels. This example uses a single webhook
channel, but you can define multiple. For example, Github allows conversing with your bot on issues and pull requests. Each channel can have its own set of tags and message types.channels["$CHANNEL_NAME"].conversation.tags
: Defines available tags for the conversation. In this case, we are defining a single tag called id
that identifies the conversation in the external service. This allows binding the botpress conversation to the external service conversation. When sending outgoing message response, this tag will be used to properly route the message to the external service.user.tags
: Defines available tags for the user. Here, we define a single tag called id
that identifies the user in the external service. This allows binding the botpress user to the external service user.messages
: Defines available message types. In this case, we are defining a single message type called text
that contains a single field called text
. This allows sending text messages to the external service. You could define multiple message types, for example, image
, video
, etc. The SDK exports a record of default message types that are supported in most messaging channels.The integration implementation looks something like this:
Let’s break down this code:
The handler
function is where the incoming request is handled. When you or an external service calls webhook.botpress.cloud/$webhookId
this is where Botpress forwards the request to.
The first thing done in this handler is always to parse the HTTP request. Once the request is parsed and validated you can see three consecutive calls made to the runtime API, respectively getOrCreateConversation
, getOrCreateUser
, and createMessage
. This is a very common pattern in Botpress integrations that integrate with a messaging channel.
The getOrCreateConversation
and getOrCreateUser
operations first check if a conversation or user already exist for the provided tags (here the external ids). If they do, the operations return the existing conversation and user. If they don’t, the operations create a new conversation and user with the provided tags. This ensures that subsequent messages from the same user and conversation are routed to the same conversation and user in Botpress.
The createMessage
operation creates a new message in Botpress with the provided payload. This message is then forwarded to the bot for processing.
The channels["$CHANNEL_NAME"]
property (here the channel name is webhook
) is where you define the outgoing message handlers; i.e. the logic executed when the bot sends a message to the user. In this case, we are defining a single handler for the text
message type. Here we call the configured webhook with the conversation that was created in the incoming request. It is important here not to confuse the id tag and the conversation id. The id tag is the external id of the conversation in the external service that was provided in the incoming request.
Still unsure how integration messaging works? Here are some helpful resources: