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:External service requirements
The 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 of the following events:
- A was created.
- A was updated.
- A was deleted.
- A was deleted.
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 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 0.2.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.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 defines two actions that are used to interact with the :listItemsInFolder- Used by the to request a list of all files and folders in a folder.transferFileToBotpress- Used by the to request that a file be downloaded from the and uploaded to Botpress.
listItemsInFolder to crawlFolder, you can do it like this:
integration.definition.ts
Renaming events
The interface defines these events to notify the plugin of changes in the :fileCreated- Emitted by your to notify the that a new has been created in the .fileUpdated- Emitted by your to notify the that a has been updated in the .fileDeleted- Emitted by your to notify the that a has been deleted in the .folderDeletedRecursive- Emitted by your to notify the that a and all of its contents have been deleted in the .
fileCreated to pageCreated, you can do it like this:
integration.definition.ts
Implementing the interface
Implementing the actions
Implementing listItemsInFolder
The listItemsInFolder action is used by the to request a list of all files and folders in a folder.
If you opted to rename the action to something else to
listItemsInFolder in the “Configuring the interface” section, please use the new name instead of listItemsInFolder.Get the folder ID
Get the folder identifier from
input.folderId. When this value is undefined, it means the is requesting a list of all items in the root directory of the . For root directory requests, please refer to the documentation of the 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
Use the ‘s API to get the list of items in the folder. If the supports filtering by item type (file or folder), by maximum file size, or by modification date, please use these filters to limit the number of items returned. This will help reduce the amount of data transferred and improve performance.
If a pagination token is provided (
input.nextToken), use it to get the next page of items. The 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
Map each item to the expected schema. The expects the following schemas:
Yield control back to the plugin
Yield control back to the by returning the list of items. The will then handle the rest of the synchronization process.
If the indicates it has more items, return the pagination token in the
nextToken field. The will use this token to request the next page of items. Otherwise, return undefined.src/index.ts
Implementing transferFileToBotpress
The transferFileToBotpress action is used by the to request that a file be downloaded from the and uploaded to Botpress.
If you opted to rename the action to something else to
transferFileToBotpress in the “Configuring the interface” section, please use the new name instead of transferFileToBotpress.Get the file ID
Get the file identifier from
input.file.id. This is the identifier of the file to be downloaded from the .Upload the file to Botpress
Upload the file to Botpress using the
client.uploadFile method. This method expects both the file’s content and a file key, which is provided by the as input.fileKey.src/index.ts
Implementing real-time sync
The can be configured to use real-time synchronization. This means that changes in the are immediately reflected in Botpress. To enable this functionality, the must support webhooks that can notify your of changes in the filesystem.Implementing fileCreated
Add a webhook handler
In your , add a webhook handler that can receive file change notifications from the .
Map the file to the expected schema
In your handler, map the file to the expected schema. The expects the following schema:
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
In your , add a webhook handler that can receive file change notifications from the .
Map the folder to the expected schema
In your handler, map the folder to the expected schema. The expects the following schema:
Implementing aggregateFileChanges
The logic is identical to the fileCreated, fileUpdated, fileDeleted, or folderDeletedRecursive events, but you should emit the aggregateFileChanges event instead: