Assets
Bundle and load images, SVG, Lottie, JSON, YAML, text, and binary resources.
Assets
Declare a resource directory in refmap.yml:
Example tree:
Then import the plugin-bound asset collection:
assets exists only when the plugin has a valid asset directory. If assets
are optional, inspect the environment:
Find an asset
There are three equivalent access styles:
The extension may be included:
Nested paths are supported:
Attribute lookup uses a normalized filename stem. Characters other than ASCII
letters and digits become _:
Avoid ambiguous stems
Files such as logo.svg and logo.png have the same normalized stem
logo. Access them by full filename or give them distinct stems.
Assets
An Assets object represents one directory.
Important members:
| Member | Result |
|---|---|
get(name) | Asset for a file or Assets for a directory |
obj[name] | Alias for get(name) |
obj.name | Attribute-style lookup |
parent | Assets for the parent directory |
dir_path | Resource directory as pathlib.Path |
len(obj) | Number of direct children |
name in obj | Whether a direct child or normalized stem exists |
Missing items raise AssetNotFoundException.
Asset
An Asset represents one file.
Path members:
| Member | Type | Meaning |
|---|---|---|
name | str | Normalized logical name |
filename | str | Actual filename |
ext | str | Last filename extension without . |
path | pathlib.Path | Absolute or resolved Python path |
path_str | str | String form of path |
java_file | java.io.File | Java file wrapper |
Read content
content():
- parses
.json,.yaml, and.ymlinto a dictionary - otherwise tries UTF-8 text
- falls back to bytes when text decoding fails
Use the explicit method when the expected type matters.
Android image helpers
Regular drawables
to_drawable()uses Android'sDrawable.createFromPathto_image_location()creates a TelegramImageLocation
Bitmap drawable
Width and height are bitmap pixels. Use this with bitmap-compatible files.
SVG
Passing both dimensions to to_svg_drawable returns a scaled
BitmapDrawable:
Create a themed SVG thumbnail:
Lottie
Lottie dimensions are converted through Android density helpers.
Create an asset from another path
Asset.from_path also accepts pathlib.Path and java.io.File.
For a temporary downloaded resource:
This is synchronous network and file I/O. Run it on a background queue, never directly in a UI callback or high-frequency hook.
Errors
AssetNotFoundExceptionmeans the requested file does not existAssetsDirNotFoundExceptionmeans an asset root no longer existsValueErrormeans a supplied path is not an allowed asset directory
Bundled asset paths are read-only from the plugin's point of view. Store mutable user data in the app data/cache helpers described in File Utilities.