Skip to content

TelegramBot Tool to Interact with Google's Gemini AI Chatbot

Notifications You must be signed in to change notification settings

sudoAlireza/GeminiBot

Repository files navigation

GeminiBot - Personalized Telegram Bot

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.

Set-up Tutorial on Medium

Getting Started

Prerequisites

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 .env file to restrict GeminiBot to your account.

BotPreview.mp4

Installation

  1. Clone the repository:

    git clone https://github.com/sudoAlireza/GeminiBot.git
  2. Navigate to the project directory:

    cd GeminiBot
  3. Install the required dependencies:

    pip install -r requirements.txt

Environment Variables

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 to ru.
  • LOG_LEVEL: The logging level for the bot (e.g., DEBUG, INFO, WARNING, ERROR). Defaults to INFO.

Local Development

For local development, it's recommended to use a Python virtual environment to manage dependencies.

Steps:

  1. Create and activate a virtual environment:

    python3 -m venv venv
    source venv/bin/activate
  2. Install dependencies:

    pip install -r requirements.txt
  3. 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)
  4. Run the bot:

    python main.py

Deployment with Docker

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 --build

The bot will now be running in the background. To view the logs, you can use the following command:

docker-compose logs -f

To stop the bot, use the following command:

docker-compose down

Features

  • 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.

Internationalization (i18n)

The bot supports multiple languages using gettext and Babel.

Adding a New Language

To add a new language (e.g., Spanish - es):

  1. Initialize the new language catalog:

    venv/bin/pybabel init -i locales/messages.pot -d locales -l es
  2. Translate the strings: Edit the newly created locales/es/LC_MESSAGES/messages.po file and translate the msgid strings into Spanish.

  3. Compile the translations:

    venv/bin/pybabel compile -d locales

Updating Existing Translations

If you add new translatable strings to the code:

  1. Extract new strings to the POT file:

    venv/bin/pybabel extract -F babel.cfg -o locales/messages.pot .
  2. 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)
  3. Translate new strings: Edit the .po files for each language and translate the new msgid entries.

  4. Compile the translations:

    venv/bin/pybabel compile -d locales

To-Do

  • Removing Specific Conversation from History
  • Add Conversation Feature to Images Part
  • Handle Long Responses in Multiple Messages
  • Add Tests and Easy Deployment

Documentation

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.

Security

Ensure the security of your API keys and sensitive information. Follow best practices for securing API keys and tokens.

Contributing

Contributions to GeminiBot are encouraged. Feel free to submit issues and pull requests.

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •