botbook/src/chapter-06-gbdialog/keyword-set-user-memory.md

4.1 KiB

SET USER MEMORY

Persists data at the user level, accessible across sessions and bots. Unlike SET BOT MEMORY which stores data per-bot, user memory follows the user wherever they go.

Syntax

SET USER MEMORY "key", value

Parameters

Parameter Type Description
key String Unique identifier for the stored value
value Any The value to store (string, number, object)

Description

SET USER MEMORY stores persistent data associated with a specific user. This data:

  • Persists across sessions - Available when user returns days/weeks later
  • Persists across bots - Accessible from any bot the user interacts with
  • Survives restarts - Stored in the database, not just memory
  • Supports TTL - Optional time-to-live for automatic expiration

This is ideal for user preferences, profile data, and cross-bot personalization.

Examples

Basic Usage

' Store user preferences
SET USER MEMORY "language", "pt-BR"
SET USER MEMORY "timezone", "America/Sao_Paulo"
SET USER MEMORY "theme", "dark"

TALK "Preferences saved!"

Store Complex Objects

' Store user profile
profile = #{ 
    name: username,
    email: useremail,
    plan: "premium",
    signupDate: NOW()
}
SET USER MEMORY "profile", profile

TALK "Profile updated successfully!"

Cross-Bot Data Sharing

' In sales-bot: Store purchase history
purchase = #{
    orderId: orderid,
    amount: total,
    date: NOW()
}
SET USER MEMORY "lastPurchase", purchase

' In support-bot: Access the same data
lastPurchase = GET USER MEMORY("lastPurchase")
TALK "I see your last order was #" + lastPurchase.orderId

User Preferences for Personalization

' Check if returning user
name = GET USER MEMORY("name")

IF name = "" THEN
    TALK "Welcome! What's your name?"
    HEAR name
    SET USER MEMORY "name", name
    TALK "Nice to meet you, " + name + "!"
ELSE
    TALK "Welcome back, " + name + "!"
END IF

Store User Facts

' Store facts about the user for AI context
SET USER MEMORY "fact_occupation", "software engineer"
SET USER MEMORY "fact_interests", "AI, automation, productivity"
SET USER MEMORY "fact_company", "Acme Corp"

' These can be used to personalize AI responses
Keyword Description
GET USER MEMORY Retrieve user-level persisted data
SET BOT MEMORY Store data at bot level
GET BOT MEMORY Retrieve bot-level data
USER FACTS Get all stored user facts

Database Storage

User memory is stored in the user_memory table with the following structure:

Column Description
user_id The user's unique identifier
key The memory key
value JSON-encoded value
memory_type Type classification (preference, fact, context)
ttl Optional expiration timestamp
created_at When the memory was created
updated_at Last modification time

Config.csv Options

name,value
user-memory-enabled,true
user-memory-max-keys,1000
user-memory-default-ttl,0
Option Default Description
user-memory-enabled true Enable/disable user memory
user-memory-max-keys 1000 Maximum keys per user
user-memory-default-ttl 0 Default TTL in seconds (0 = no expiry)

Best Practices

  1. Use descriptive keys - user_language not lang
  2. Prefix related keys - pref_theme, pref_language, fact_name
  3. Don't store sensitive data - No passwords or tokens
  4. Consider TTL for temporary data - Session-specific data should expire
  5. Keep values reasonable size - Don't store large files or blobs

See Also