All articles

Telegram Knowledge-Base Bot

AI practiceRead in Chinese

Telegram Knowledge-Base Bot

Current Status

The MVP was deployed. In a Telegram group, a user could mention the bot with a question; the bot retrieved relevant knowledge-base material and generated an answer with an LLM. Mentioning it without a question returned site navigation.

Deployment

Pushing to main triggered GitHub Actions to:

  1. Upload knowledge-base Markdown to /srv/knowledge_base/.
  2. Upload bot/ to /srv/kb_bot/.
  3. Write /srv/kb_bot/bot/.env.
  4. Rebuild the complete RAG vector index with python3 retriever.py.
  5. Restart the kb_bot systemd service.
  6. Add the Nginx location idempotently on the first deployment.

Production secrets were stored under GitHub Actions → Settings → Environments → prod → Secrets.

Environment Variables

VariablePurpose
TELEGRAM_BOT_TOKENToken issued by BotFather
BOT_USERNAMEBot username without @, used to filter group mentions
COMPATIBLE_BASE_URLBase URL for an LLM-compatible API
COMPATIBLE_API_KEYAPI key for that endpoint
OPENROUTER_API_KEYOpenRouter key used for embeddings
KB_PATHMarkdown directory, default /srv/knowledge_base
INDEX_PATHChromaDB index directory, default /srv/kb_index
GITHUB_TOKENFine-grained read-only token for private repository commits in /recent

Using the Bot

In a group, mention the bot and include a question, for example @my_kb_bot What is RAG?. A mention without a question opens navigation. In a private chat, send the question directly.

CommandBehavior
/start, /helpFeature introduction and usage
/navSite navigation with page links
/siteSite entry points
/categoriesBrowse knowledge-base categories and articles
/todayCurrent date
/statusNumber of loaded documents
/recentSummarize recent GitHub commits and pending work, then suggest next steps
/saveRun knowledge-capture steps 1–3 on the current session and preview a write decision
/save <text>Capture supplied text from step 2 onward and preview a write decision

Session Isolation

Conversation history was keyed by {chat_id}:{user_id}:{session_key}. The chat ID identified a group or private chat, the user ID isolated participants, and the session key combined the date with a sequence number. Thirty minutes of inactivity started a new session. For example, two users in the same group had different histories, and one user's later session also had a different key.

Limits and Follow-Up Work

  • Session memory was added with a seven-day Redis TTL.
  • Keyword matching was replaced with RAG vector retrieval.
  • /save supported extraction, routing, and a preview of the write decision. Confirmation and the actual write step were still pending.
  • A hard-coded chat_id allowlist restricted which groups could use the bot.
  • Messages from any bot account were ignored to prevent loops between bots.
Telegram Knowledge-Base Bot | Xuhuan / Gelatoni