2025-11-23 09:19:06 -03:00
|
|
|
# SET BOT MEMORY
|
2025-10-25 15:59:06 -03:00
|
|
|
|
2025-11-23 09:19:06 -03:00
|
|
|
Store persistent key-value data at the bot level that persists across all conversations.
|
2025-11-03 20:42:38 -03:00
|
|
|
|
|
|
|
|
## Syntax
|
|
|
|
|
|
|
|
|
|
```basic
|
2025-11-23 09:19:06 -03:00
|
|
|
SET BOT MEMORY key, value
|
2025-10-25 15:59:06 -03:00
|
|
|
```
|
|
|
|
|
|
2025-11-03 20:42:38 -03:00
|
|
|
## Parameters
|
|
|
|
|
|
2025-11-23 09:19:06 -03:00
|
|
|
| Parameter | Type | Description |
|
|
|
|
|
|-----------|------|-------------|
|
|
|
|
|
| `key` | String | Unique identifier for the memory item |
|
|
|
|
|
| `value` | String | Value to store (can be any string data) |
|
2025-11-03 20:42:38 -03:00
|
|
|
|
|
|
|
|
## Description
|
2025-10-25 15:59:06 -03:00
|
|
|
|
2025-11-23 09:19:06 -03:00
|
|
|
The `SET BOT MEMORY` keyword stores data that is:
|
|
|
|
|
- Persistent across all user sessions
|
|
|
|
|
- Shared between all users of the same bot
|
|
|
|
|
- Stored in the database permanently
|
|
|
|
|
- Available until explicitly updated or cleared
|
2025-10-25 15:59:06 -03:00
|
|
|
|
2025-11-23 09:19:06 -03:00
|
|
|
Bot memory is useful for:
|
|
|
|
|
- Configuration settings
|
|
|
|
|
- Global counters and statistics
|
|
|
|
|
- Shared state between users
|
|
|
|
|
- Bot-wide preferences
|
|
|
|
|
- Cached data that applies to all conversations
|
2025-10-25 15:59:06 -03:00
|
|
|
|
2025-11-23 09:19:06 -03:00
|
|
|
## Examples
|
2025-10-25 15:59:06 -03:00
|
|
|
|
2025-11-23 09:19:06 -03:00
|
|
|
### Store Simple Values
|
2025-10-25 15:59:06 -03:00
|
|
|
```basic
|
2025-11-23 09:19:06 -03:00
|
|
|
SET BOT MEMORY "welcome_message", "Hello! Welcome to our service."
|
|
|
|
|
SET BOT MEMORY "support_email", "support@example.com"
|
|
|
|
|
SET BOT MEMORY "business_hours", "9 AM - 5 PM EST"
|
|
|
|
|
```
|
2025-11-03 20:42:38 -03:00
|
|
|
|
2025-11-23 09:19:06 -03:00
|
|
|
### Store Counters
|
|
|
|
|
```basic
|
|
|
|
|
current_count = GET BOT MEMORY "visitor_count"
|
|
|
|
|
IF current_count = "" THEN
|
|
|
|
|
current_count = "0"
|
|
|
|
|
END IF
|
|
|
|
|
new_count = VAL(current_count) + 1
|
|
|
|
|
SET BOT MEMORY "visitor_count", STR(new_count)
|
|
|
|
|
TALK "You are visitor number " + STR(new_count)
|
2025-10-25 15:59:06 -03:00
|
|
|
```
|
|
|
|
|
|
2025-11-23 09:19:06 -03:00
|
|
|
### Store JSON Data
|
|
|
|
|
```basic
|
|
|
|
|
user_data = '{"name": "John", "level": 5, "points": 1200}'
|
|
|
|
|
SET BOT MEMORY "user_profile", user_data
|
|
|
|
|
```
|
2025-11-03 20:42:38 -03:00
|
|
|
|
2025-11-23 09:19:06 -03:00
|
|
|
### Dynamic Keys
|
|
|
|
|
```basic
|
|
|
|
|
today = FORMAT(NOW(), "YYYY-MM-DD")
|
|
|
|
|
daily_key = "stats_" + today
|
|
|
|
|
SET BOT MEMORY daily_key, "25"
|
|
|
|
|
```
|
2025-11-03 20:42:38 -03:00
|
|
|
|
2025-11-23 09:19:06 -03:00
|
|
|
### Configuration Management
|
|
|
|
|
```basic
|
|
|
|
|
' Store bot configuration
|
|
|
|
|
SET BOT MEMORY "max_retries", "3"
|
|
|
|
|
SET BOT MEMORY "timeout_seconds", "30"
|
|
|
|
|
SET BOT MEMORY "api_version", "v2"
|
|
|
|
|
|
|
|
|
|
' Later, read configuration
|
|
|
|
|
max_retries = GET BOT MEMORY "max_retries"
|
|
|
|
|
timeout = GET BOT MEMORY "timeout_seconds"
|
|
|
|
|
```
|
2025-11-03 20:42:38 -03:00
|
|
|
|
2025-11-23 09:19:06 -03:00
|
|
|
## Database Storage
|
|
|
|
|
|
|
|
|
|
Bot memories are stored in the `bot_memories` table:
|
|
|
|
|
- `id`: UUID primary key
|
|
|
|
|
- `bot_id`: Reference to the bot
|
|
|
|
|
- `key`: Memory key (indexed for fast lookup)
|
|
|
|
|
- `value`: Memory value (text)
|
|
|
|
|
- `created_at`: Timestamp of creation
|
|
|
|
|
- `updated_at`: Timestamp of last update
|
|
|
|
|
|
|
|
|
|
## Performance Considerations
|
|
|
|
|
|
|
|
|
|
- Keys are indexed for fast retrieval
|
|
|
|
|
- Values are stored as text (no size limit in PostgreSQL)
|
|
|
|
|
- Updates are asynchronous to avoid blocking
|
|
|
|
|
- Consider using structured keys for organization
|
|
|
|
|
|
|
|
|
|
## Best Practices
|
|
|
|
|
|
|
|
|
|
1. **Use Descriptive Keys**: Make keys self-documenting
|
|
|
|
|
```basic
|
|
|
|
|
SET BOT MEMORY "config:email:smtp_server", "mail.example.com"
|
|
|
|
|
SET BOT MEMORY "stats:daily:2024-01-15", "150"
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
2. **Handle Missing Values**: Always check if memory exists
|
|
|
|
|
```basic
|
|
|
|
|
value = GET BOT MEMORY "some_key"
|
|
|
|
|
IF value = "" THEN
|
|
|
|
|
' Initialize with default
|
|
|
|
|
SET BOT MEMORY "some_key", "default_value"
|
|
|
|
|
value = "default_value"
|
|
|
|
|
END IF
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
3. **Avoid Sensitive Data**: Don't store passwords or tokens
|
|
|
|
|
```basic
|
|
|
|
|
' BAD: Don't do this
|
|
|
|
|
' SET BOT MEMORY "admin_password", "secret123"
|
|
|
|
|
|
|
|
|
|
' GOOD: Store non-sensitive config
|
|
|
|
|
SET BOT MEMORY "admin_email", "admin@example.com"
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
4. **Structure Complex Data**: Use JSON for complex structures
|
|
|
|
|
```basic
|
|
|
|
|
settings = '{"theme": "dark", "language": "en", "notifications": true}'
|
|
|
|
|
SET BOT MEMORY "user_preferences", settings
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
5. **Clean Up Old Data**: Remove unused memories periodically
|
|
|
|
|
```basic
|
|
|
|
|
' Remove old daily stats
|
|
|
|
|
old_date = FORMAT(DATE_ADD(NOW(), -30, "days"), "YYYY-MM-DD")
|
|
|
|
|
SET BOT MEMORY "stats_" + old_date, ""
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## Differences from User Memory
|
|
|
|
|
|
|
|
|
|
| Aspect | Bot Memory | User Memory |
|
|
|
|
|
|--------|------------|-------------|
|
|
|
|
|
| Scope | All users of the bot | Single user |
|
|
|
|
|
| Persistence | Permanent | Session or permanent |
|
|
|
|
|
| Use Case | Global settings | Personal data |
|
|
|
|
|
| Access | Any conversation | User's conversations only |
|
|
|
|
|
|
|
|
|
|
## Error Handling
|
|
|
|
|
|
|
|
|
|
- If database connection fails, operation is logged but doesn't crash
|
|
|
|
|
- Invalid bot IDs are logged as errors
|
|
|
|
|
- Duplicate keys update existing values
|
|
|
|
|
- Empty keys are not allowed
|
2025-11-03 20:42:38 -03:00
|
|
|
|
|
|
|
|
## Related Keywords
|
|
|
|
|
|
2025-11-23 09:19:06 -03:00
|
|
|
- [GET BOT MEMORY](./keyword-get-bot-memory.md) - Retrieve stored bot memory
|
|
|
|
|
- [SET](./keyword-set.md) - Set user-scoped variables
|
|
|
|
|
- [REMEMBER](./keyword-remember.md) - Store user-specific memories
|
2025-11-03 20:42:38 -03:00
|
|
|
|
2025-11-23 09:19:06 -03:00
|
|
|
## Implementation
|
2025-10-25 15:59:06 -03:00
|
|
|
|
2025-11-23 09:19:06 -03:00
|
|
|
Located in `src/basic/keywords/bot_memory.rs`
|
2025-10-25 15:59:06 -03:00
|
|
|
|
2025-11-23 09:19:06 -03:00
|
|
|
The implementation:
|
|
|
|
|
- Uses async database operations
|
|
|
|
|
- Handles updates atomically with transactions
|
|
|
|
|
- Validates bot ID format
|
|
|
|
|
- Logs all operations for debugging
|