GeminiBot is a Telegram bot tailored to chat with Google's Gemini AI chatbot. Leveraging the official Gemini Bot on the Telegram platform, it engages users in dynamic conversations.
Before deploying the bot, ensure you have the following:
- Python 3.10 installed on your system
- Obtain a Telegram API token from BotFather
- Acquire a Gemini API key from the Google Gemini website
- Get your Telegram Account id from Show Json Bot. Account id is different than Account username and you should set it in
.envfile to restrict GeminiBot to your account.
BotPreview.mp4
-
Clone the repository:
git clone https://github.com/sudoAlireza/GeminiBot.git
-
Navigate to the project directory:
cd GeminiBot -
Install the required dependencies:
pip install -r requirements.txt
The bot is configured using environment variables. The following variables are required:
TELEGRAM_BOT_TOKEN: Your Telegram bot token from BotFather.GEMINI_API_TOKEN: Your Gemini API key from Google AI Studio.GEMINI_MODEL: The Gemini model to use (e.g.,gemini-flash-latest).AUTHORIZED_USER: A comma-separated list of your Telegram user IDs to restrict bot access.LANGUAGE: The language code for the bot's interface (e.g.,en,ru). Defaults toru.LOG_LEVEL: The logging level for the bot (e.g.,DEBUG,INFO,WARNING,ERROR). Defaults toINFO.
For local development, it's recommended to use a Python virtual environment to manage dependencies.
Steps:
-
Create and activate a virtual environment:
python3 -m venv venv source venv/bin/activate -
Install dependencies:
pip install -r requirements.txt
-
Set environment variables: Set the required environment variables in your active shell session. Replace the placeholder values with your actual tokens and IDs.
export TELEGRAM_BOT_TOKEN=<Your Telegram Bot Token> export GEMINI_API_TOKEN=<Your Gemini API key> export GEMINI_MODEL=<Your Gemini Model> # e.g., gemini-flash-latest export AUTHORIZED_USER="<your_user_id_1>,<your_user_id_2>" export LANGUAGE=ru # Set the desired language (e.g., en, ru)
-
Run the bot:
python main.py
The recommended way to deploy the bot is using Docker and Docker Compose.
Data Persistence:
The bot stores conversation data in the data directory. This directory is mounted as a volume in the Docker Compose configuration to ensure that your data is preserved even if the container is removed.
Configuration:
When deploying with Docker, you should provide the required environment variables to the container. The recommended way to do this is through your orchestration platform (e.g., Portainer, Kubernetes) by setting the environment variables in the container's configuration.
Build and Run:
docker-compose up -d --buildThe bot will now be running in the background. To view the logs, you can use the following command:
docker-compose logs -fTo stop the bot, use the following command:
docker-compose down- Engage in online conversations with Google's Gemini AI chatbot
- Maintain conversation history for continuing or initiating new discussions
- Send images with captions to receive responses based on the image content. For example, the bot can read text within images and convert it to text.
The bot supports multiple languages using gettext and Babel.
To add a new language (e.g., Spanish - es):
-
Initialize the new language catalog:
venv/bin/pybabel init -i locales/messages.pot -d locales -l es
-
Translate the strings: Edit the newly created
locales/es/LC_MESSAGES/messages.pofile and translate themsgidstrings into Spanish. -
Compile the translations:
venv/bin/pybabel compile -d locales
If you add new translatable strings to the code:
-
Extract new strings to the POT file:
venv/bin/pybabel extract -F babel.cfg -o locales/messages.pot . -
Update existing language catalogs:
venv/bin/pybabel update -i locales/messages.pot -d locales -l en # For English venv/bin/pybabel update -i locales/messages.pot -d locales -l es # For Spanish (or other languages)
-
Translate new strings: Edit the
.pofiles for each language and translate the newmsgidentries. -
Compile the translations:
venv/bin/pybabel compile -d locales
- Removing Specific Conversation from History
- Add Conversation Feature to Images Part
- Handle Long Responses in Multiple Messages
- Add Tests and Easy Deployment
For detailed instructions on using Telegram bots, refer to the Telegram Bots Documentation.
To begin with Gemini, refer to the Gemini API: Quickstart with Python.
Ensure the security of your API keys and sensitive information. Follow best practices for securing API keys and tokens.
Contributions to GeminiBot are encouraged. Feel free to submit issues and pull requests.