The Botpress API allows you to access your bot’s conversations from an external service. This is very useful if you need to manage or analyze the conversations somehow. In this tutorial we give steps to use the API for this purpose and steps to format the response data. Let’s get started!Documentation Index
Fetch the complete documentation index at: https://botpress.com/docs/llms.txt
Use this file to discover all available pages before exploring further.
NoteAccess the API documentation to verify the schema/structure of the Conversations and Messages and see if the data is useful for your use case.
NoteYou can find your personal access token in your Admin Dashboard. You can find your bot ID in the URL of your bot’s dashboard, it’s the letters and numbers right after
chatbots/1. Getting the Conversations list from the API
Using Axios
Send aGET request to https://api.botpress.cloud/v1/chat/conversations adding the following headers: Authorization: Bearer <your-personal-access-token> and x-bot-id: <your-bot-id>.
This is how the request would look like using Axios in a JavaScript application (e.g in a Execute Code in Botpress):
Using the Botpress Client library
The same logic to get conversations but using the Botpress Client library in a Node.js application:InfoThe API returns a maximum of 20 conversations per request. You will need to use the
nextToken property to fetch the next batch of conversations. The nextToken property is returned in the meta object of the response.2. Getting all Conversations from the API
Now that you know the methods of getting the first conversations from the API, let’s see how to get them. You can get them gradually or all at once, but you need to use the pagination token regardless.Getting conversations gradually
Create a function that will fetch the next batch of conversations when called. You could use a button to trigger it or trigger it automatically with infinite scroll. Pass thenextToken property retrieved from the first request as a parameter to the function, then update it on subsequent calls.
Getting all conversations
Use a loop to fetch all conversations at once using the above logic. This isn’t recommended for bots with a large number of conversations as it may take a long time to complete and may cause timeouts. The use of a library likep-retry to retry requests automatically is recommended.
3. Getting the Messages list
Once you have gone through the list of conversations and found out the id of the desired conversation to export, you can then use the API to list the actual messages. In the example below we’re using the Botpress Client library and assume the client is set up already:This is how the
allMessages array is gonna look like:
NoteIn this example the bot sends a Multiple Choice card, then the user answers by clicking one of the options, and then the bot sends an Image card. Notice how the message payload changes according to the message type, so be mindful of that when reading the
payload property.4. Formatting the Messages list
Now that you have the list of messages of a conversation it’s time to format it so it becomes readable. We’re going to use a custom function for that:history string will end up looking like this:
NoteYou can customize the
formatMessage function to have different formatting or to return more information of the messages. Now use the history variable however you like - send it via email, add it to a Google Sheets document, send it to Botpress to do further processing, etc.