MongoDB / README.md
Zok213
Update Dockerfile to streamline application file copying and create README if missing; revise README.md for clarity on Hugging Face deployment.
95ccedb
---
title: MongoDB
emoji: 🚀
colorFrom: gray
colorTo: indigo
sdk: docker
pinned: false
---
# Solona SuperTeam Telegram Bot
The project consists of 3 main components:
1. Backend API - Handles database connection and stores chat sessions
2. User Telegram Bot - Bot interacts with users
3. Admin Telegram Bot - Bot notifies administrators
## Installation
1. Clone repository:
```bash
git clone <repository-url>
cd <repository-name>
```
2. Create and activate virtual environment:
```bash
python -m venv venv
# Windows
venv\Scripts\activate
# Linux/Mac
source venv/bin/activate
```
3. Install dependencies:
```bash
pip install -r requirements.txt
```
4. Create .env file and configure environment variables:
```bash
# Windows
copy .env.example .env
# Linux/Mac
cp .env.example .env
```
5. Edit the .env file with your configuration settings
## Running the Project
1. Run Backend API:
```bash
cd backend
python app.py
```
2. Run User Telegram Bot (open a new terminal):
```bash
cd telebot
python bot.py
```
3. Run Admin Telegram Bot (open a new terminal):
```bash
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 database
- `GET /unanswered-questions` - Fetch sessions where RAG responded with "I don't know"
- `WebSocket /ws` - WebSocket endpoint for real-time notifications
- `GET /health` - Health check endpoint
- `GET /` - Root endpoint with API information
## Environment Variables
The application uses the following environment variables:
- `MONGODB_URI` - MongoDB connection string
- `MONGODB_DB` - MongoDB database name
- `MONGODB_COLLECTION` - MongoDB collection name
- `ADMIN_TELEGRAM_BOT_TOKEN` (optional) - Telegram bot token for sending notifications
- `ADMIN_GROUP_CHAT_ID` (optional) - Telegram chat ID to send notifications to
- `NOTIFICATION_WEBHOOK_URL` (optional) - Webhook URL for sending notifications
- `PORT` (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:
1. Install the dependencies: `pip install -r requirements.txt`
2. Set up environment variables (create a `.env` file)
3. Run the application: `python app.py`
## Docker
You can also build and run this application using Docker:
```bash
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
```