Zok213
Update Dockerfile to streamline application file copying and create README if missing; revise README.md for clarity on Hugging Face deployment.
95ccedb
metadata
title: MongoDB
emoji: 🚀
colorFrom: gray
colorTo: indigo
sdk: docker
pinned: false
Solona SuperTeam Telegram Bot
The project consists of 3 main components:
- Backend API - Handles database connection and stores chat sessions
- User Telegram Bot - Bot interacts with users
- Admin Telegram Bot - Bot notifies administrators
Installation
- Clone repository:
git clone <repository-url>
cd <repository-name>
- Create and activate virtual environment:
python -m venv venv
# Windows
venv\Scripts\activate
# Linux/Mac
source venv/bin/activate
- Install dependencies:
pip install -r requirements.txt
- Create .env file and configure environment variables:
# Windows
copy .env.example .env
# Linux/Mac
cp .env.example .env
- Edit the .env file with your configuration settings
Running the Project
- Run Backend API:
cd backend
python app.py
- Run User Telegram Bot (open a new terminal):
cd telebot
python bot.py
- Run Admin Telegram Bot (open a new terminal):
cd teleadmin
python bot.py
Project Structure
.
├── backend/ # Backend API
│ ├── app.py # API endpoints
│ └── database.py # Database connection and handling
├── telebot/ # User Bot
│ ├── bot.py # Initialize and run bot
│ └── handlers.py # Handle bot events
├── teleadmin/ # Admin Bot
│ ├── bot.py # Initialize and run admin bot
│ └── handlers.py # Handle admin notifications
├── requirements.txt # Required libraries
├── .env # Environment variables
└── .env.example # Environment variables template
Features
User Bot
- /start command with buttons
- Knowledge Portal: Returns information about Solona Superteam
- Event A: Returns information about Event A
- Events: Displays events from API
- Access SolonaSuperTeam: Link to website
- Assistant: Displays support information from API
- FAQ: Displays frequently asked questions from API as interactive buttons
- Free interaction with RAG API
Admin Bot
- Listens for and notifies about sessions with factor "RAG" and messages starting with "I don't know"
- Continuous monitoring and reporting of issues to admin group
Backend
- MongoDB connection
- Chat session storage
- API endpoints for saving and retrieving sessions
Common Issues
MongoDB Connection Issues
- Check MongoDB connection information in .env file
- Ensure your IP is whitelisted in MongoDB Atlas
Bot Running Issues
- Use the
application.run_polling()method to run the bot instead of manual async/await approach - If you encounter asyncio errors, ensure you have a compatible python-telegram-bot version (v20.7+ recommended)
- On Windows, you may need to install additional package:
pip install pypiwin32
Admin Notification Issues
- Check ADMIN_GROUP_CHAT_ID in .env file
- Ensure admin bot has been added to the group chat
- Check bot permissions in group chat
- Add print statements to bot.py and handlers.py to debug and monitor session checking process
API Communication Issues
- Ensure API_BASE_URL is correct and accessible
- Check if backend API is running
- Use try/except to catch errors during API calls and print debugging messages
Maintenance and Updates
- Check backend and bot logs regularly
- Update libraries when needed
- Back up database periodically
- If API changes, ensure both bots are updated
MongoDB Telegram Bot API
A FastAPI-based service that connects to MongoDB and provides an API for a Telegram bot to store and retrieve chat sessions.
Features
- Save chat sessions to MongoDB
- Detect and notify about unanswered questions (RAG responses with "I don't know")
- WebSocket support for real-time notifications
- Rate limiting to prevent abuse
- Response caching for improved performance
API Endpoints
POST /session- Save a new session chat to the databaseGET /unanswered-questions- Fetch sessions where RAG responded with "I don't know"WebSocket /ws- WebSocket endpoint for real-time notificationsGET /health- Health check endpointGET /- Root endpoint with API information
Environment Variables
The application uses the following environment variables:
MONGODB_URI- MongoDB connection stringMONGODB_DB- MongoDB database nameMONGODB_COLLECTION- MongoDB collection nameADMIN_TELEGRAM_BOT_TOKEN(optional) - Telegram bot token for sending notificationsADMIN_GROUP_CHAT_ID(optional) - Telegram chat ID to send notifications toNOTIFICATION_WEBHOOK_URL(optional) - Webhook URL for sending notificationsPORT(optional) - Port to run the API on (defaults to 7860 for Hugging Face)
Deployment on Hugging Face
This application is configured for deployment on Hugging Face Spaces with Docker.
Local Development
To run the application locally:
- Install the dependencies:
pip install -r requirements.txt - Set up environment variables (create a
.envfile) - Run the application:
python app.py
Docker
You can also build and run this application using Docker:
docker build -t mongodb-telegram-bot-api .
docker run -p 7860:7860 -e MONGODB_URI=your_mongodb_uri -e MONGODB_DB=your_db -e MONGODB_COLLECTION=your_collection mongodb-telegram-bot-api