File Utilities
Work with Telegram directories, read and write files, and register custom file open handlers.
The file_utils module has two main responsibilities:
- filesystem helpers for common plugin tasks
FilesController, which lets you intercept file opening by extension and optionally provide custom file icons
Standard Directories
These helpers return absolute paths to useful Telegram and plugin directories.
Available directory helpers:
get_plugins_dir()get_cache_dir()get_files_dir()get_images_dir()get_videos_dir()get_audios_dir()get_documents_dir()
Directory Operations
ensure_dir_exists(path: str)
Creates the directory if it does not exist yet, including missing parent directories.
list_dir(...)
Lists files and or directories with optional recursion and extension filtering.
Parameters:
recursive=False: walk child directories tooinclude_files=True: include files in the resultinclude_dirs=False: include directories in the resultextensions=None: optional suffix filter such as[".json", ".txt"]
File Operations
write_file(path: str, content: str)
Writes text to a file and overwrites any existing content.
read_file(path: str)
Reads the whole file as text.
On failure it returns None and logs the exception.
write_file_bytes(path: str, content: bytes)
Writes binary data to a file.
read_file_bytes(path: str)
Reads the whole file as bytes.
On failure it returns None and logs the exception.
delete_file(path: str)
Deletes a file and returns:
Trueif the file was deletedFalseif the file did not exist or deletion failed
FilesController
FilesController lets you register handlers for specific file extensions.
When a user opens a file of a registered type, your callback runs instead of the default open flow.
Typical use cases:
- open custom file types
- handle plugin-specific archives
- launch a custom viewer
- provide a custom icon for a file extension
Secrets are required to unregister
FilesController.register(...) returns a secret string. Keep it somewhere safe if you want to remove the handler later with unregister(...).
FilesController.SUPPORT_ICONS
FilesController.SUPPORT_ICONS is True when the current client version supports custom file icons.
FilesController.Place
This enum describes where the file open request came from.
Available values:
FilesController.Place.UNKNOWNFilesController.Place.ChatActivityFilesController.Place.FilteredSearchViewFilesController.Place.SharedMediaLayoutFilesController.Place.SearchDownloadsContainerFilesController.Place.ChannelAdminLogActivity
FilesController.FileInfo
Use FileInfo to describe a file extension handler.
Fields:
ext: stron_click: Callable[[FilesController.OnClickArgs], None]whitelist_places: list[FilesController.Place] = []blacklist_places: list[FilesController.Place] = []get_icon: Optional[Callable[[], Drawable]] = None
Rules:
- you cannot use
whitelist_placesandblacklist_placestogether get_iconrequiresFilesController.SUPPORT_ICONS == True
FilesController.OnClickArgs
This object is passed to your on_click callback.
Fields:
placefilefile_namemessageactivityparent_fragment
Registering a File Handler
Use FilesController.register(file_info) to install a handler.
It returns a secret string which is required for unregister(...).
Restricting a Handler to Certain Places
Use whitelist_places or blacklist_places to control where the handler is active.
Registering a Custom Icon
If icon support is available, you can provide a Drawable factory with get_icon.
Unregistering a File Handler
Use the extension and the secret returned by register(...).
Exceptions
FilesController can raise these exceptions:
FilesController.ExtensionAlreadyRegisteredFilesController.ExtensionNotRegisteredFilesController.SecretInvalid