Building a Discord/Telegram bot with long-term memory using OpenClaw + MemoClaw
Here’s a thing that happens with every chat bot: someone talks to it for an hour, shares their preferences, explains their project, corrects a misconception. Then the session resets and the bot greets them like a stranger.
“Hi! How can I help you today?”
Nobody wants that. Your users don’t want it. You don’t want it. The whole point of having a persistent bot is that it persists.
This tutorial walks through building an OpenClaw agent that runs in Discord or Telegram and actually remembers things. User preferences, past conversations, ongoing projects, corrections. All stored in MemoClaw, all recalled automatically when someone shows up again.
What you’re building
By the end of this, your bot will:
- Remember each user’s preferences and past context
- Greet returning users with relevant history (“Hey, last time you were debugging the auth flow — did that get sorted?”)
- Track ongoing conversations across sessions
- Handle group chats without mixing up who said what
- Stay within a reasonable cost budget
The architecture: OpenClaw agent connected to Discord/Telegram + memoclaw-hooks for automatic memory lifecycle + per-user namespaces for isolation.
Prerequisites
- OpenClaw installed and running
- A Discord or Telegram channel connected to OpenClaw
- Node.js 18+
- About 10 minutes
If you haven’t set up a channel yet, check the OpenClaw docs for Discord or Telegram setup.
Step 1: install MemoClaw and create a wallet
npm install -g memoclaw
memoclaw init
This generates a wallet that acts as your bot’s identity. No accounts, no API keys. The wallet address is how MemoClaw knows who’s storing and recalling memories.
You’ll get 100 free API calls to start.
Step 2: install the hooks
openclaw hooks install memoclaw-hooks
openclaw hooks enable memoclaw
Set your wallet key:
# Add to your OpenClaw env config
env:
MEMOCLAW_PRIVATE_KEY: "0xYourPrivateKey"
Restart the gateway:
openclaw gateway restart
The hooks now fire on session start (recall), session end (extract), context compaction (preserve), and periodically (consolidate). Your bot has memory without writing any memory logic.
Step 3: configure per-user namespaces
This is where chat bots differ from single-user agents. You’re talking to multiple people. Their memories shouldn’t bleed into each other.
Set up namespace routing in your OpenClaw config:
env:
MEMOCLAW_PRIVATE_KEY: "0xYourPrivateKey"
MEMOCLAW_NAMESPACE_MODE: "per-user"
With per-user mode, the hooks automatically create namespaces like user-alice, user-bob, etc. When Alice starts a session, the hook recalls from her namespace. When Bob does, he gets his.
Same wallet, different namespaces. Each user’s memory is isolated.
When to use a shared namespace instead
Sometimes you want shared context — a team bot where everyone should benefit from the same knowledge base. Set the namespace explicitly:
env:
MEMOCLAW_NAMESPACE: "team-project-x"
Now everyone’s memories go into one pool. The bot recalls from the shared namespace for every user. Good for project bots. Bad for personal assistants.
You can also do both: per-user namespaces for personal preferences, plus a shared namespace for project knowledge. The hooks support a MEMOCLAW_SHARED_NAMESPACE env var for this:
env:
MEMOCLAW_NAMESPACE_MODE: "per-user"
MEMOCLAW_SHARED_NAMESPACE: "team-shared"
On recall, the hook searches both namespaces and merges results.
Step 4: teach your agent to use memory
The hooks handle the background lifecycle, but your agent’s personality matters too. Update your agent’s SOUL.md to be memory-aware:
## Memory
You have persistent memory via MemoClaw. You remember past conversations.
When a user returns:
- Reference what you remember about them naturally
- Don't list everything you recall — use it contextually
- If you remember something but aren't sure it's still current, ask
When users share preferences or correct you:
- These are high-importance memories, they'll be stored automatically
- Acknowledge the correction: "Got it, I'll remember that"
When users share project context:
- Use tags to organize: project, preference, correction, decision
- Namespace keeps their stuff separate from other users
Don't be creepy about it. "I remember you mentioned..." is fine.
"I have 47 stored memories about you" is not.
Step 5: handling group chats
Group chats add complexity. Multiple people talking, and you need to track who said what.
The hooks handle this if you use per-user namespace mode — each person’s contributions get stored under their namespace. But in a group context, your agent sees messages from everyone.
Add to your SOUL.md:
## Group chats
In group conversations, attribute context to the right person.
Use tags to track who said what:
- Tag user-specific facts with their name: --tags alice,preference
- Project-level facts shared by anyone go to the shared namespace
- Don't store casual banter or social noise
For manual stores in group contexts:
# Alice's preference
memoclaw store "Alice prefers TypeScript over JavaScript" \
--importance 0.7 --tags alice,preference \
--namespace user-alice
# Team decision (shared)
memoclaw store "Team decided to use Railway for staging" \
--importance 0.8 --tags decision,infrastructure \
--namespace team-shared
What this looks like in practice
After a few sessions, here’s how a conversation might go:
First time user shows up:
User: Hey, I'm working on a Next.js app and having trouble with SSR
Bot: What kind of SSR issue? Are you using the App Router or Pages Router?
Normal. No memory to work with yet. The hooks store the conversation when the session ends.
User comes back three days later:
User: Hey, quick question about my app
Bot: Sure — is this the Next.js project you were working on? Last time
you were dealing with SSR issues. Did that get resolved?
User: Yeah I fixed that. Now I'm trying to set up auth with NextAuth
Bot: Got it. Are you still on the App Router? That affects how
NextAuth sessions work.
The bot remembered the Next.js context, the SSR issue, and the App Router detail. It didn’t dump all its memories — just used them naturally in conversation.
User corrects the bot:
User: Actually we switched to Clerk, not NextAuth
Bot: Noted — Clerk for auth. I'll remember that going forward.
This correction gets stored with high importance (0.9+). Next session, the bot knows it’s Clerk, not NextAuth.
Privacy: what to store and what to skip
Your bot will encounter sensitive information. Set boundaries.
Add to your OpenClaw config or SOUL.md:
## Memory boundaries
NEVER store:
- Passwords, API keys, tokens, secrets
- Personal health information
- Financial details (account numbers, card numbers)
- Anything the user explicitly asks you to forget
ALWAYS store:
- Technical preferences (language, framework, tools)
- Project context (stack, architecture decisions)
- Corrections (high importance, these prevent repeat mistakes)
- Communication preferences (verbose vs. terse, timezone)
MemoClaw memories are not encrypted at rest. Treat them like notes in a shared doc — useful context, not a vault.
If a user asks you to forget something:
memoclaw search "the thing they want forgotten" --namespace user-alice
# Find the memory ID, then:
memoclaw delete MEMORY_ID
Cost projection
Here’s what a typical chat bot costs with MemoClaw:
| Activity | Frequency | Cost |
|---|---|---|
| Session start (recall) | Per user, per session | $0.005 |
| Session end (extract) | Per session | $0.01 |
| Consolidation | Every 6 hours | $0.01 |
Say you have 20 active users doing 2 sessions each per day:
- 40 recalls: $0.20
- 40 extracts: $0.40
- 4 consolidations: $0.04
- Daily total: ~$0.64
- Monthly: ~$19
For a bot with 5 active users, you’re under $5/month. The free tier (100 calls) covers about 2-3 days of a 20-user bot, or a couple weeks for a smaller one.
All list, search, delete, and stats calls are free and don’t count toward the quota.
Troubleshooting
Bot doesn’t remember anything between sessions
Check that the hooks are installed and enabled:
openclaw hooks list --verbose
Check that MEMOCLAW_PRIVATE_KEY is set in the environment where the gateway runs.
Bot recalls memories from the wrong user
Namespace routing might not be set. Verify MEMOCLAW_NAMESPACE_MODE is per-user in your config.
Memories pile up and recall gets noisy
Run consolidation more frequently, or adjust the recall limit:
export MEMOCLAW_HOOK_RECALL_LIMIT=5 # Default is 10
Fewer results means tighter context. You can also use memoclaw consolidate --namespace user-alice to merge duplicates manually.
Bot is being weird about what it remembers
Check what’s stored:
memoclaw list --namespace user-alice --limit 20
Delete anything that shouldn’t be there:
memoclaw delete MEMORY_ID
Going further
Once your bot has memory working, some ideas:
- Core memories: Pin critical facts (user name, timezone, key project details) so they always appear in recall. See Core Memories: Pinning Facts Your Agent Must Never Lose.
- Cross-agent memory: If you run multiple bots (e.g., a support bot and a project bot), they can share memory through the same wallet + namespace. User context flows between them.
- Memory-driven personality: Over time, your bot builds a model of each user. It can adapt tone, detail level, and topic suggestions based on what it knows.
- Session summaries: At the end of each day, run a cron job that summarizes the day’s conversations into a few high-level memories. Keeps things compact.
The hooks do the heavy lifting. Your job is teaching the agent how to use what it remembers, and setting boundaries on what it should store.
Resources: