A feature-rich Telegram chatbot powered by Google’s Gemini AI, designed to engage users with insightful and thought-provoking conversations. This bot leverages Flask for handling webhooks, SQLite for maintaining chat history, and includes robust rate limiting and error handling to ensure a smooth user experience.
- AI-Powered Conversations: Utilizes Google’s Gemini 1.5-flash model to generate intelligent and engaging responses.
- Persistent Chat History: Stores user interactions in a SQLite database for context-aware conversations.
- Rate Limiting: Ensures fair usage by limiting the number of requests per user.
- Multithreading: Handles multiple user requests concurrently for improved performance.
- Webhook Integration: Uses Flask to manage Telegram webhooks securely.
- Error Handling & Logging: Comprehensive logging for monitoring and troubleshooting.
- Customizable AI Persona: Configured to behave as Albert, a distinguished astrophysicist with a unique communication style.
- Python 3.7 or higher
- A Telegram account
- Telegram Bot Token
- Google Gemini API Key
- Hosting service (e.g., Heroku, AWS, etc.) to run the Flask app and expose a public URL
-
Clone the Repository
-
Create a Virtual Environment
python3 -m venv venv source venv/bin/activate # On Windows, use `venv\Scripts\activate`
-
Install Dependencies
pip install -r requirements.txt
-
Set Up Environment Variables
Create a
.env
file in the root directory and add the following variables:TELEGRAM_BOT_TOKEN=your_telegram_bot_token GEMINI_API_KEY=your_gemini_api_key WEBHOOK_URL_BASE=https://your-domain.com PORT=5000 # Or any other port you prefer
The bot requires several environment variables to function correctly. Ensure you have the following variables set in your .env
file:
- TELEGRAM_BOT_TOKEN: Your Telegram bot token obtained from BotFather.
- GEMINI_API_KEY: Your API key for Google’s Gemini AI.
- WEBHOOK_URL_BASE: The base URL where your Flask app is hosted (e.g.,
https://your-domain.com
). - PORT: The port on which the Flask app will run (default is
5000
).
Example .env
file:
TELEGRAM_BOT_TOKEN=123456789:ABCDEFGHIJKLMNOPQRSTUVWXYZ
GEMINI_API_KEY=your_gemini_api_key
WEBHOOK_URL_BASE=https://your-domain.com
PORT=5000
-
Initialize the Database
The database is automatically initialized when you run the bot for the first time. It creates a
chat_history.db
SQLite database to store user interactions. -
Run the Bot
python your_bot_script.py
Replace
your_bot_script.py
with the name of your Python script containing the bot code. -
Set Up Webhook
The bot automatically sets up the webhook using the
WEBHOOK_URL_BASE
andBOT_TOKEN
. Ensure that your hosting service correctly points to the webhook URL. -
Interact with the Bot
Open Telegram, find your bot by its username, and start interacting!
- Flask App: Handles incoming webhook requests from Telegram.
- Telebot: Manages Telegram bot interactions and messaging.
- SQLite Database: Stores chat history for each user to maintain context.
- Google Generative AI (Gemini): Generates AI-powered responses based on user input and chat history.
- Rate Limiter: Controls the number of requests a user can make within a specific timeframe to prevent abuse.
- Thread Pool Executor: Manages concurrent processing of user messages.
- Logging: Captures errors and warnings for monitoring and debugging.
- Webhook Reception: Telegram sends updates to the Flask webhook route.
- Message Queuing: Incoming messages are placed in a thread-safe queue.
- Worker Threads: Multiple worker threads process messages concurrently.
- Input Validation: Validates user input for length and content.
- Rate Limiting: Checks if the user has exceeded the request limit.
- AI Response Generation: Fetches chat history and generates a response using Gemini AI.
- Response Delivery: Sends the formatted response back to the user via Telegram.
The bot is configured to log errors and warnings to a file named bot_errors.log
. This helps in monitoring the bot’s performance and troubleshooting issues.
logging.basicConfig(filename='bot_errors.log', level=logging.ERROR)
This project is licensed under the MIT License.
Developed with ❤️ by Jacob