Telegram Bot with Persistent AI Memory

Build a Telegram bot that actually remembers your conversations. Using python-telegram-bot and REM Labs, this tutorial creates a bot with persistent memory -- it knows your preferences, recalls past discussions, and gets smarter with every message.

Setup

pip install python-telegram-bot remlabs openai

You will need a Telegram bot token from @BotFather, a REM Labs API key from remlabs.ai/console, and an OpenAI API key for generating responses.

The Bot Code

# bot.py import os from telegram import Update from telegram.ext import ( Application, CommandHandler, MessageHandler, filters, ContextTypes ) from remlabs import RemLabs from openai import OpenAI memory = RemLabs(api_key=os.environ["REMLABS_API_KEY"]) llm = OpenAI(api_key=os.environ["OPENAI_API_KEY"]) async def handle_message( update: Update, ctx: ContextTypes.DEFAULT_TYPE ): user_id = str(update.effective_user.id) text = update.message.text namespace = f"telegram-{user_id}" # Recall relevant memories for this user memories = memory.recall( query=text, namespace=namespace, limit=5 ) context = "\n".join([m["content"] for m in memories]) # Generate response with memory context response = llm.chat.completions.create( model="gpt-4o", messages=[ {"role": "system", "content": f"You are a helpful assistant. " f"User context:\n{context}"}, {"role": "user", "content": text} ] ) reply = response.choices[0].message.content await update.message.reply_text(reply) # Store this exchange memory.remember( content=f"User said: {text}\nBot replied: {reply}", namespace=namespace, tags=["telegram", "conversation"] ) async def remember_cmd( update: Update, ctx: ContextTypes.DEFAULT_TYPE ): user_id = str(update.effective_user.id) note = " ".join(ctx.args) if not note: await update.message.reply_text( "Usage: /remember your note here") return memory.remember( content=note, namespace=f"telegram-{user_id}", tags=["manual", "telegram"] ) await update.message.reply_text("Remembered.") async def recall_cmd( update: Update, ctx: ContextTypes.DEFAULT_TYPE ): user_id = str(update.effective_user.id) query = " ".join(ctx.args) or "recent memories" results = memory.recall( query=query, namespace=f"telegram-{user_id}", limit=5 ) if results: text = "\n\n".join( [f"- {m['content'][:200]}" for m in results]) await update.message.reply_text(text) else: await update.message.reply_text("No memories found.") app = Application.builder().token( os.environ["TELEGRAM_TOKEN"]).build() app.add_handler(CommandHandler("remember", remember_cmd)) app.add_handler(CommandHandler("recall", recall_cmd)) app.add_handler( MessageHandler(filters.TEXT, handle_message)) app.run_polling()

Bot Commands

Group Chat Memory

For group chats, scope memory to the group instead of the user:

# Group-scoped namespace namespace = f"telegram-group-{update.effective_chat.id}" # Or hybrid: group context + user preferences group_mem = memory.recall(query=text, namespace=f"tg-group-{chat_id}", limit=3) user_mem = memory.recall(query=text, namespace=f"tg-user-{user_id}", limit=3)

Privacy: Each user gets their own namespace. Memories from one user are never visible to another. REM Labs encrypts all data at rest and in transit.

Build a Telegram bot with memory

Free tier. Per-user memory isolation. Python SDK included.

Get started free →