A highly customizable, realistic AI Telegram bot powered by Google's Gemini 2.0 Flash. This bot is designed to mimic human interaction patterns, including realistic typing delays and natural message splitting.
The main goal of this project is to create a bot that doesn't feel like a bot. It achieves this through two core mechanisms:
Unlike standard bots that send a single wall of text, this bot understands how to break down thoughts into multiple messages.
How it works:
If you instruct the bot in the system_prompt.txt to "split messages with newlines," it will generate a response like:
Hey!
I was just thinking about that.
Let me check for you.
The bot's handler detects these newlines and sends them as three separate messages, just like a human would text.
It doesn't just blast messages instantly. The bot calculates a "typing delay" based on the length of each message chunk.
- Dynamic Calculation: It estimates how long a fast typer (e.g., a teenager) would take to type the message.
- Visual Feedback: It displays the "typing..." status in Telegram while it "types" the message.
- Random Variance: Adds a slight random factor to the delay so it never feels robotic or perfectly consistent.
The bot is built with Python, python-telegram-bot, and Google GenAI.
- Message Reception: Listens for messages in private chats or mentions in groups.
- AI Generation: We use the Google GenAI api to generate a response. Sends the conversation history and user input to Gemini 2.0 Flash (or the model you choose).
- Processing:
- The AI generates a response (potentially multi-line).
- The bot splits the response by newlines.
- Delivery Loop:
- For each message part:
- Calculates delay:
len(text) / chars_per_second * random_variance. - Sends "Typing..." action.
- Waits for the calculated delay.
- Sends the message.
- Calculates delay:
- For each message part:
-
Clone the repository:
git clone https://github.com/emqnuele/humanlike-telegram-bot.git cd humanlike-telegram-bot -
Install dependencies:
pip install -r requirements.txt
-
Set up environment variables:
- Rename
.env.exampleto.env. - Add your API keys:
TELEGRAM_BOT_TOKEN=your_telegram_token GOOGLE_API_KEY=your_google_api_key GOOGLE_MODEL_NAME=gemini-2.0-flash # Optional, defaults to gemini-2.0-flash
- Rename
This is the brain of the bot. You can define its personality, tone, and behavior here.
To enable the realistic splitting feature, include instructions like:
"Write as if you are texting a friend. Use short messages. Separate distinct thoughts with newlines."
Chat histories are automatically saved in data/histories/ as JSON files, ensuring the bot remembers context across conversations.
Run the bot with:
python main.pyThe bot will start polling for Telegram messages. It also starts a lightweight Flask server on port 5000 (useful for keeping the bot alive on some hosting platforms).
Created by Emanuele Faraci.
Check out my portfolio: emanuelefaraci.com