How jasurkhon/self-service-bot works
Project Type and Purpose
• This is a specialized Telegram chatbot (“bot.py”), enhanced with two‐factor authentication and an embedded machine‐learning retrieval algorithm.
• Its primary use case: authenticated users send free‐form queries via Telegram, the bot locates semantically similar entries in a preprocessed knowledge base (CSV + vector file), and returns the best match.
File‐Structure Analysis
Top‐level files and their roles:
• bot.py – Core chatbot logic: receives Telegram updates, enforces 2FA, invokes ML retrieval, sends replies.
• app.py – Orchestration script (e.g., launch routines, config loader, or local testing harness).
• NewVec.npy – NumPy array of precomputed embeddings for the KB.
• preprocessed_kb_base.csv – Tabular knowledge base with matched text & metadata.
• np.txt – Temporary storage for closest matches (logging or quick-lookup).
• consumer.sqlite – SQLite database for runtime user‐session data (2FA tokens, conversation state).
• customer.sql – SQL dump of registered users & credentials.
• requirements.txt – Lists frameworks: pyTelegramBotAPI, numpy, pandas, torch, transformers, scikit-learn, seaborn.
Key Components
a. Telegram Client (External Service)
– Users interact via Telegram’s API.
b. Bot Logic Layer (bot.py)
– Subcomponents:
• 2FA Module (generates/verifies codes, stores tokens in consumer.sqlite)
• Query Processor (prepares user text, calls embedding+search)
• Response Formatter (packages selected KB entry back to user)
c. ML Retrieval Engine
– Preprocessing & vector model (uses torch + transformers)
– Vector Store (NewVec.npy) + Metadata Store (preprocessed_kb_base.csv)
– Similarity Search (e.g., sklearn or custom cosine computation)
d. Persistence Layer
– consumer.sqlite: session/2FA state
– customer.sql → relational DB of user credentials (pre‐loaded)
e. Auxiliary Files
– np.txt: ephemeral cache/log of recent similarity results
f. Orchestration Script (app.py)
– Configuration loader for paths, credentials, external model dir (deeppavlov-rubert)
– Launches bot.py or performs initial data checks
Component Interactions & Data Flows
1. User → Telegram API → bot.py (Webhook or Polling)
2. bot.py → 2FA Module → consumer.sqlite (read/write OTP state)
3. On authenticated query: bot.py → Query Processor → loads embedding model → loads NewVec.npy + preprocessed_kb_base.csv → computes similarity → writes top results to np.txt (optional)
4. ML Engine → returns best match → bot.py formats message → Telegram API → User
5. On startup: app.py reads requirements, ensures model folder (deeppavlov-rubertbasecased) is in place, then spawns bot.py
Architectural Patterns & Design Principles
• Separation of Concerns: Authentication logic, ML retrieval, and messaging handled in discrete modules.
• Data locality: Precompute embeddings (NewVec.npy) to avoid recomputing at runtime.
• Single‐process, file‐based persistence (SQLite) for lightweight state management.
• External dependency isolation: requirements.txt pins all libraries.