An intelligent Telegram chatbot for answering customs-related questions in Azerbaijani. The bot uses OpenAI embeddings for semantic search and supports both text and voice messages with multiple speech-to-text engines.
- Text Message Support: Ask questions via text and receive categorized answers
- Voice Message Support: Send voice messages in Azerbaijani
- Multi-Engine Speech Recognition:
- ElevenLabs Speech-to-Text (primary)
- OpenAI Whisper (fallback)
- Google Speech Recognition (secondary fallback)
- Smart Category Classification: Automatically categorizes questions using semantic similarity
- Semantic Search: Uses OpenAI embeddings and FAISS for fast, accurate answer retrieval
- Context Awareness: Remembers last category for better follow-up question handling
- Python 3.x
- OpenAI API: Text embeddings (text-embedding-3-large)
- FAISS: Vector similarity search
- Telegram Bot API: User interface
- ElevenLabs API: Azerbaijani speech-to-text
- Pandas & NumPy: Data processing
- scikit-learn: Cosine similarity calculations
git clone https://github.com/valiyyaddin/E-custom-chatbot.git
cd E-custom-chatbotpip install -r requirements.txtCreate a .env file in the root directory:
TELEGRAM_TOKEN=your_telegram_bot_token
ELEVEN_API_KEY=your_elevenlabs_api_key
API_KEY=your_openai_api_keyPlace your customs FAQ CSV file in the project directory. The CSV should have the following columns:
Sual: QuestionsCavab: AnswersKateqoriya: Categories
Update the CSV_PATH in main.py to point to your CSV file.
python main.py- Open Telegram and search for your bot
- Send
/startto begin - Ask questions in text or send voice messages
- Receive categorized answers instantly
Text Message:
User: GΓΆmrΓΌk rΓΌsumu necΙ ΓΆdΙnilir?
Bot: π Kateqoriya: ΓdΙniΕ
π¬ Cavab: [Detailed answer about customs payment]
Voice Message:
User: [Sends voice message]
Bot: π€ Sual: GΓΆmrΓΌk rΓΌsumu necΙ ΓΆdΙnilir?
π Kateqoriya: ΓdΙniΕ
π¬ Cavab: [Detailed answer]
Adjust these in main.py:
CATEGORY_THRESHOLD = 0.56 # Strictness for category classification
RETRIEVAL_THRESHOLD = 0.37 # Leniency for answer retrievalCSV_PATH = 'path/to/your/customs_faq.csv'
EMBEDDINGS_FILE = 'embeddings_save.pkl' # Cached embeddings
CATEGORY_FILE = 'cat.txt' # Last category cache-
Embedding System:
- Generates OpenAI embeddings for all questions
- Caches embeddings for performance
- Uses FAISS index for fast similarity search
-
Question Processing Pipeline:
User Question β Clean Text β Generate Embedding β Classify Category β Search Similar Questions β Return Best Answer -
Speech Recognition Chain:
Voice Message β ElevenLabs STT β [If fails] β Whisper STT β [If fails] β Google SR β Text -
Category Memory:
- Saves last successfully classified category
- Uses for context in ambiguous questions
- Falls back to full search if needed
E-custom-chatbot/
βββ main.py # Main bot application
βββ requirements.txt # Python dependencies
βββ customs_faq - customs_faq.csv.csv # FAQ dataset
βββ .gitignore # Git ignore rules
βββ .env # Environment variables (create this)
βββ embeddings_save.pkl # Cached embeddings (auto-generated)
βββ cat.txt # Last category cache (auto-generated)
βββ config/ # Configuration files
βββ services/ # Additional services
βββ vosk.py # Vosk STT service
βββ whisper.py # Whisper STT service
βββ filesUtils.py # File utilities
- Telegram Bot Token: Get from @BotFather
- OpenAI API Key: Get from OpenAI Platform
- ElevenLabs API Key: Get from ElevenLabs
- Embeddings are cached in
embeddings_save.pklto avoid regeneration - FAISS index enables fast similarity search (O(log n))
- Category memory reduces search space for follow-up questions
- Multiple STT engines ensure high transcription success rate
The bot includes comprehensive error handling:
- Graceful fallbacks for STT failures
- Category classification fallbacks
- User-friendly error messages in Azerbaijani
- Detailed logging for debugging
- CSV file path is currently hardcoded
- Embeddings regeneration required for data updates
- Voice messages require adequate audio quality
- Azerbaijani language optimization for speech recognition
- Support for multiple languages
- Admin panel for FAQ management
- Analytics dashboard
- Conversation history
- Feedback system
- Docker containerization
- Database integration (replace CSV)
Contributions are welcome! Please feel free to submit issues or pull requests.
This project is open source and available under the MIT License.
For questions or support, please open an issue in the GitHub repository.