Basic User Operations (Deprecated)¶
Deprecated — current user only
Use db.user for all new code. User only supports the current user who triggered the command. No cross-user reads or writes.
This page documents the legacy User API for existing bots. New projects should skip straight to db.user.
How User works¶
Four steps, one caveat:
- At command start, the current user's props are preloaded into
botState.user_props User.getreads from that in-memory cache — instant, synchronousUser.set/User.delupdate memory immediately, then persist in the background- If persistence fails, the in-memory value is rolled back (logged server-side)
Because writes are fire-and-forget, you cannot reliably set then get a new value in the same command and assume it persisted — use db.user when you need confirmed writes.
User.get(key)¶
Read a value for the current user.
Returns null if the key does not exist.
db.user replacement:
User.set(key, value, type?, ttl?)¶
Write a value for the current user. Updates cache immediately; persists asynchronously.
| Parameter | Description |
|---|---|
key | Storage key |
value | Value to store |
type | Optional type hint (string, integer, boolean, …) |
ttl | Optional TTL in seconds |
Returns { success: true, key, user_id, cached: true } — does not mean persistence succeeded.
Alias: User.setProperty(...) — same behavior.
Object syntax with key / value / type / ttl is supported. user_id in object form is not supported for bot use — use db.user for other users.
db.user replacement:
await db.user.set("language", "en")
await db.user.set("otp", "482910", { ttl: 300, type: "string" })
User.del(key)¶
Delete one key for the current user.
Object form: User.del({ key: "draft" }) — no user_id.
Alias: User.delProperty(...)
db.user replacement:
User.has(key)¶
Check if a key exists for the current user.
db.user replacement:
User.getAll()¶
Return a shallow copy of all cached keys for the current user.
db.user replacement:
User.delAll()¶
Clear all cached keys for the current user and trigger background delete.
Aliases: User.delAllProperty(...), User.clear(...)
db.user replacement:
Helper methods¶
| Method | Returns |
|---|---|
User.keys() | Key names for current user |
User.values() | Values for current user |
User.size() | Number of keys |
User.getUserId() | Current user's Telegram ID string |
User.getOwnerId() | Owner account ID |
User.isOwnerValid() | Whether owner ID is configured |
Try it — legacy vs modern¶
Save language preference (legacy)¶
User.set("language", user.language_code || "en")
let lang = User.get("language")
Bot.sendMessage("Language: " + lang)
Modern equivalent¶
await db.user.set("language", user.language_code || "en")
let lang = await db.user.get("language", "en")
Bot.sendMessage("Language: " + lang)
Limitations¶
- Current user only — no cross-user access
- No
incr/decr/push/pull— usedb.user - No
mgetbatch reads — usedb.user - Writes not awaited — race conditions in the same command
- Not available in webapp, global webhook, or broadcast