The unidirectional file synchronization interface allows you to implement 1-way sync in your integration to import files from an external service to Botpress.
Filesystem abstraction
The file synchronization interface provides a filesystem-like abstraction that works with any kind of data source. The external service doesn’t need to provide an actual filesystem - your integration just needs to represent the external data as files and folders. For example:- If you are building a website crawler, individual pages could be folders and HTML contents and assets like images or stylesheets could be files.
- For a note-taking platform, notebooks could be folders with individual notes being files.
- For an email provider, mailboxes or labels could be folders and individual emails could be files.
Terminology
Throughout this document, we will use the following terms:- Integration
- The code that connects Botpress to an external service.
- External service
- The service from which you want to import files. This could be a cloud storage service, a file server, or any other type of external service that stores files.
- File synchronization interface
- The interface that defines the contract for implementing unidirectional file synchronization in your integration. This interface specifies the actions and events that your integration must implement to support file synchronization.
- File synchronizer plugin
- The Botpress plugin that orchestrates file synchronization. This plugin is responsible for managing the synchronization process, including scheduling, error handling, and reporting.
- File
- A file is a single unit of data that can be synchronized from the external service to Botpress. Files can contain any type of data, such as text, images, or binary data. Files can’t contain other files or folders.
- Folder
- A folder is a container for files. Folders can contain other folders and files, allowing for a hierarchical organization of data.
- Real-time synchronization
- A synchronization mode where changes in the external service are immediately reflected in Botpress. This is typically achieved through webhooks or other push mechanisms. Integrations aren’t required to support this mode, but it’s recommended for better user experience.
External service requirements
The external service providing file synchronization functionality must support the following:- An API that allows listing all files and folders in a folder.
- Must support pagination. This means that the API should return a limited number of items at a time, along with a token that can be used to retrieve the next set of items.
- An API that allows downloading files.
- Webhooks that can notify your integration of the following events:
Updating your package.json
file
Finding the current interface version
The current version of thefiles-readonly
interface is:
You will need this version number for the next steps.
Adding the interface as a dependency
Once you have the file synchronization interface version, you can add it as a dependency to your integration:Open the package.json file
package.json
file.Add the dependencies section
bpDependencies
section in your integration‘s package.json
file, create one:Add the interface as a dependency
bpDependencies
section, add the file synchronization interface as a dependency. For example, for version 0.2.0
, you would add the following:"<interface-name>": "interface:<interface-name>@<version>"
.Save the package.json file
package.json
file.Install the interface
bp add
command to install it. This command will:- Download the interface from Botpress.
- Install it in a directory named
bp_modules
in your integration‘s root directory.
Adding a helper build script
To keep your integration up to date, we recommend adding a helper build script to yourpackage.json
file:
Open the package.json file
package.json
file.Add the build script
scripts
section, add the following script:build
script already exists in your package.json
file, please replace it.Save the package.json file
package.json
file.npm run build
, it will automatically install the file synchronization interface and build your integration.
Editing your integration definition file
Adding the interface to your integration definition file
Now that the file synchronization interface is installed, you must add it your integration definition file in order to implement it.Open the integration.definition.ts file
integration.definition.ts
file.Import the interface
Extend your definition
.extend()
function at the end of your new IntegrationDefinition()
statement:.extend()
will be explained in the next section.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
filesReadonly
. - The second argument is a configuration object. Using this object, you can override interface defaults with custom names, titles, and descriptions.
Renaming actions
The file synchronization interface defines two actions that are used to interact with the external service:listItemsInFolder
- Used by the file synchronizer plugin to request a list of all files and folders in a folder.transferFileToBotpress
- Used by the file synchronizer plugin to request that a file be downloaded from the external service and uploaded to Botpress.
listItemsInFolder
to crawlFolder
, you can do it like this:
listItemsInFolder
to listNotebooksAndPages
and transferFileToBotpress
to downloadPage
. This way, the action names reflect the specific context of the note-taking platform, making your integration clearer and easier to understand.Renaming events
The file synchronization interface interface defines these events to notify the plugin of changes in the external service:fileCreated
- Emitted by your integration to notify the file synchronizer plugin that a new file has been created in the external service.fileUpdated
- Emitted by your integration to notify the file synchronizer plugin that a file has been updated in the external service.fileDeleted
- Emitted by your integration to notify the file synchronizer plugin that a file has been deleted in the external service.folderDeletedRecursive
- Emitted by your integration to notify the file synchronizer plugin that a folder and all of its contents have been deleted in the external service.
aggregateFileChanges
event, which contains all the changes in a single event.fileCreated
to pageCreated
, you can do it like this:
Implementing the interface
Implementing the actions
Implementing listItemsInFolder
The listItemsInFolder
action is used by the file synchronizer plugin to request a list of all files and folders in a folder.
listItemsInFolder
in the “Configuring the interface” section, please use the new name instead of listItemsInFolder
.Get the folder ID
input.folderId
. When this value is undefined
, it means the file synchronizer plugin is requesting a list of all items in the root directory of the external service. For root directory requests, please refer to the documentation of the external service to determine the correct root identifier - this is typically an empty string, a slash character (/
), or a special value defined by the service.Get the list of items
input.nextToken
), use it to get the next page of items. The external service should return a new pagination token in the response, which you should return with the action’s response.Map each items to the expected schema
Yield control back to the plugin
nextToken
field. The file synchronizer plugin will use this token to request the next page of items. Otherwise, return undefined
.Implementing transferFileToBotpress
The transferFileToBotpress
action is used by the file synchronizer plugin to request that a file be downloaded from the external service and uploaded to Botpress.
transferFileToBotpress
in the “Configuring the interface” section, please use the new name instead of transferFileToBotpress
.Get the file ID
input.file.id
. This is the identifier of the file to be downloaded from the external service.Download the file from the external service
Upload the file to Botpress
client.uploadFile
method. This method expects both the file’s content and a file key, which is provided by the file synchronizer plugin as input.fileKey
.Yield control back to the plugin
Implementing real-time sync
The file synchronizer plugin can be configured to use real-time synchronization. This means that changes in the external service are immediately reflected in Botpress. To enable this functionality, the external service must support webhooks that can notify your integration of changes in the filesystem.Implementing fileCreated
Add a webhook handler
Map the file to the expected schema
Emit the event
fileCreated
event with the mapped file as the payload. The file synchronizer plugin will then handle the rest of the synchronization process.Implementing fileUpdated
The logic is identical to the fileCreated
event, but you should emit the fileUpdated
event instead.
Implementing fileDeleted
The logic is identical to the fileCreated
event, but you should emit the fileDeleted
event instead.
Implementing folderDeletedRecursive
Add a webhook handler
Map the folder to the expected schema
Emit the event
folderDeletedRecursive
event with the mapped folder as the payload. The file synchronizer plugin will then handle the rest of the synchronization process.Implementing aggregateFileChanges
The logic is identical to the fileCreated
, fileUpdated
, fileDeleted
, or folderDeletedRecursive
events, but you should emit the aggregateFileChanges
event instead:
aggregateFileChanges
event. This is more efficient and faster to process for the file synchronizer plugin.