Turn PDF books into chapter-by-chapter audiobooks directly inside Telegram.
Built with Python ยท Aiogram ยท Google Gemini ยท Deepgram Aura ยท Microsoft Edge-TTS ยท FFmpeg ยท PyMuPDF
Telegram AI Audiobook Bot is a Python-based Telegram bot that converts PDF books into MP3 audiobooks.
The bot extracts text from PDF files, detects chapters, splits long chapters into manageable chunks, generates speech using multiple text-to-speech engines, combines the resulting audio, and sends the finished audiobook back through Telegram.
The system is designed around a multi-engine fallback architecture, allowing it to continue processing when one TTS provider or API key becomes unavailable.
The bot supports multiple TTS engines:
-
Google Gemini TTS
- Natural-sounding AI narration.
- Supports configurable voice selection.
- Can be used with narration/style instructions where supported by the selected Gemini model.
- Multiple API keys can be configured.
-
Deepgram Aura
- Fast neural text-to-speech generation.
- Supports Aura-2 voices such as
aura-2-thalia-en. - Multiple API keys can be configured for fallback.
-
Microsoft Edge-TTS
- Used as an additional fallback engine.
- Provides access to Microsoft neural voices.
- Does not require a paid API key.
The exact capabilities of each engine depend on the model, API, account, and configuration being used.
The bot can use multiple API keys for supported providers.
For example:
Gemini Key 1
โ
Gemini Key 2
โ
Gemini Key 3
โ
Deepgram Key 1
โ
Deepgram Key 2
โ
Edge-TTS
If an API request fails because of a supported quota, rate-limit, or provider error, the bot can move to another configured key or TTS engine according to the application's fallback logic.
Important: Multiple API keys do not automatically create unlimited quota. Each key's actual limits depend on the provider and the projects/accounts associated with those keys.
The bot uses PyMuPDF for PDF processing and text extraction.
It can:
- ๐ Extract text from PDF pages.
- ๐งน Clean extracted text.
- ๐ข Remove unnecessary page-number patterns where detected.
- ๐ Detect chapters using configured chapter-heading patterns.
- ๐ Use PDF document structure/bookmarks where supported by the implementation.
- โ๏ธ Split large chapters into smaller speech-generation chunks.
- ๐ Preserve the logical order of chapter content.
PyMuPDF is a Python library for PDF/document extraction and manipulation.
Long chapters can be divided into smaller chunks.
Example:
Chapter 1
โ
โโโ Chunk 1 โโโบ TTS
โโโ Chunk 2 โโโบ TTS
โโโ Chunk 3 โโโบ TTS
โโโ Chunk 4 โโโบ TTS
โ
โผ
FFmpeg Merge
โ
โผ
Chapter 1.mp3
This allows large chapters to be processed without sending one enormous request to a TTS provider.
The resulting audio chunks can then be combined using FFmpeg.
The project uses SQLite to keep track of audiobook processing.
This allows the bot to maintain information such as:
- ๐ Current book
- ๐ Current chapter
- ๐ง Completed chapters
- โณ Processing state
- โ Failed processing jobs
- ๐ Resume information
If processing is interrupted, the bot can use the stored state to continue according to the application's resume logic rather than starting the entire book from the beginning.
| Technology | Purpose |
|---|---|
| ๐ Python | Main programming language |
| ๐ค Aiogram 3 | Telegram bot framework |
| โจ Google Gemini | AI text-to-speech |
| ๐๏ธ Deepgram Aura | Neural text-to-speech |
| ๐ Microsoft Edge-TTS | Additional TTS fallback |
| ๐ฌ FFmpeg | Audio conversion and merging |
| ๐ PyMuPDF | PDF parsing and text extraction |
| ๐๏ธ SQLite | Persistent job/progress storage |
Aiogram is an asynchronous Python framework for building Telegram bots using asyncio and aiohttp.
Google's Gemini API provides the API interface used to access Gemini models.
Deepgram's current documentation identifies Aura-2 as its broad-language TTS model family, with model identifiers such as aura-2-thalia-en.
Before running the bot, install the following.
Python 3.10 or newer is recommended.
Check your version:
python --versionFFmpeg is required for audio processing and merging.
sudo apt update
sudo apt install ffmpegUsing Homebrew:
brew install ffmpegUsing Winget:
winget install Gyan.FFmpegVerify the installation:
ffmpeg -versionCreate a Telegram bot using @BotFather and obtain your bot token.
Your token should be kept private and should never be committed to GitHub.
git clone https://github.com/your-username/telegram-audiobook-bot.git
cd telegram-audiobook-botpython -m venv venv
venv\Scripts\activatepython3 -m venv venv
source venv/bin/activatepip install -r requirements.txtCreate a .env file in the project root.
Example:
# =========================================================
# Telegram
# =========================================================
BOT_TOKEN=YOUR_TELEGRAM_BOT_TOKEN
# =========================================================
# Google Gemini
# =========================================================
# Multiple Gemini API keys can be separated by commas.
GEMINI_API_KEYS=YOUR_GEMINI_KEY_1,YOUR_GEMINI_KEY_2,YOUR_GEMINI_KEY_3
# Gemini TTS model configured by the project.
GEMINI_TTS_MODEL=YOUR_GEMINI_TTS_MODEL
# Gemini voice configured by the project.
GEMINI_VOICE=YOUR_GEMINI_VOICE
# Optional narration/style instruction.
GEMINI_STYLE=calm, clear audiobook narrator
# =========================================================
# Deepgram
# =========================================================
# Multiple Deepgram API keys can be separated by commas.
DEEPGRAM_API_KEYS=YOUR_DEEPGRAM_KEY_1,YOUR_DEEPGRAM_KEY_2
# Example Aura-2 model:
DEEPGRAM_MODEL=aura-2-thalia-en
# =========================================================
# Microsoft Edge-TTS
# =========================================================
EDGE_VOICE=en-US-AriaNeural
# =========================================================
# Optional Access Control
# =========================================================
# Restrict the bot to specific Telegram user IDs.
# Leave empty if the bot should be publicly accessible.
ALLOWED_USER_IDS=Never upload your real API keys to GitHub.
Add this to .gitignore:
# Environment variables
.env
.env.*
!.env.example
# Python
__pycache__/
*.py[cod]
*.pyo
*.pyd
# Virtual environment
venv/
.venv/
# Local database
*.db
*.sqlite
*.sqlite3
# Generated audio
*.mp3
*.wav
*.ogg
*.m4a
# Temporary files
tmp/
temp/
cache/
output/After configuring .env:
python bot.pyIf your system uses python3:
python3 bot.pyThe bot should then connect to Telegram and begin listening for updates.
| Command | Description |
|---|---|
/start |
Start the bot and display the main menu |
/engine |
Select the preferred TTS engine |
/style |
Configure the Gemini narration/style instruction |
/sample |
Generate a short audio sample using the current voice configuration |
/status |
View the current audiobook processing status |
/cancel |
Stop the active audiobook conversion |
/resume |
Continue a previously interrupted conversion |
The exact commands and behavior depend on the implementation in
bot.py.
The bot can be configured to use different processing strategies.
Gemini
โ
Deepgram
โ
Edge-TTS
The bot attempts the configured primary provider first and falls back according to the application's failure-handling logic.
Uses the configured fast TTS provider.
Uses Microsoft Edge-TTS directly.
A simplified view of the fallback system:
โโโโโโโโโโโโโโโโโโโ
โ Text Chunk โ
โโโโโโโโโโฌโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโ
โ Gemini Key #1 โ
โโโโโโโโโโโฌโโโโโโโโโโ
โ
Failure / Limit
โ
โผ
โโโโโโโโโโโโโโโโโโโโโ
โ Gemini Key #2 โ
โโโโโโโโโโโฌโโโโโโโโโโ
โ
Failure / Limit
โ
โผ
โโโโโโโโโโโโโโโโโโโโโ
โ Gemini Key #3 โ
โโโโโโโโโโโฌโโโโโโโโโโ
โ
Failure / Limit
โ
โผ
โโโโโโโโโโโโโโโโโโโโโ
โ Deepgram Key #1 โ
โโโโโโโโโโโฌโโโโโโโโโโ
โ
Failure / Limit
โ
โผ
โโโโโโโโโโโโโโโโโโโโโ
โ Deepgram Key #2 โ
โโโโโโโโโโโฌโโโโโโโโโโ
โ
Failure / Limit
โ
โผ
โโโโโโโโโโโโโโโโโโโโโ
โ Edge-TTS โ
โโโโโโโโโโโฌโโโโโโโโโโ
โ
โผ
Generated Audio
Deepgram's Aura API accepts a model such as aura-2-thalia-en through its /v1/speak endpoint.
The complete processing flow is approximately:
๐ PDF Book
โ
โผ
โโโโโโโโโโโโโโโโ
โ PyMuPDF โ
โ Text Extract โ
โโโโโโโโฌโโโโโโโโ
โ
โผ
๐งน Text Cleaning
โ
โผ
๐ Chapter Detection
โ
โผ
โ๏ธ Text Chunking
โ
โผ
๐๏ธ TTS Generation
โ
โผ
๐ง Audio Chunks
โ
โผ
๐ฌ FFmpeg Merge
โ
โผ
๐ Chapter MP3
โ
โผ
๐ค Telegram Delivery
telegram-audiobook-bot/
โ
โโโ bot.py
โ โโโ Aiogram handlers, Telegram interaction,
โ queues, commands, and audio delivery
โ
โโโ config.py
โ โโโ Environment configuration, API keys,
โ directories, and application settings
โ
โโโ db.py
โ โโโ SQLite database, progress tracking,
โ jobs, chapters, and resume state
โ
โโโ pdf_utils.py
โ โโโ PDF extraction, text cleaning,
โ chapter detection, and chunk splitting
โ
โโโ tts.py
โ โโโ TTS providers, API-key rotation,
โ fallback handling, audio generation,
โ and FFmpeg processing
โ
โโโ requirements.txt
โ โโโ Python dependencies
โ
โโโ .env
โ โโโ Private API keys and configuration
โ
โโโ .gitignore
โ โโโ Files excluded from Git
โ
โโโ README.md
โโโ Project documentation
API keys are sensitive credentials.
Never commit these values to Git:
BOT_TOKEN
GEMINI_API_KEYS
DEEPGRAM_API_KEYS
If a key is accidentally exposed publicly:
- Revoke the exposed key.
- Generate a new key.
- Replace it in
.env. - Check your Git history if the key was committed.
For development, it is recommended to use a virtual environment:
python -m venv venvActivate it:
venv\Scripts\activatesource venv/bin/activateThen install dependencies:
pip install -r requirements.txtMultiple Gemini keys can be configured, but each key is still subject to the quota and restrictions of the project/account associated with it. Google documents that Gemini API keys are associated with Google Cloud projects, which manage billing, permissions, and quota.
The example configuration uses:
aura-2-thalia-en
Deepgram currently documents Aura-2 as a TTS family with multiple voices and languages.
PyMuPDF provides PDF text extraction and supports working with several document formats.
FFmpeg is used by the project for audio processing and merging.
MIT License.
You are free to use, modify, distribute, and build upon this project according to the terms of the MIT License.
Made with ๐ Python ยท ๐ค Telegram ยท โจ Gemini ยท ๐๏ธ Deepgram ยท ๐ Edge-TTS ยท ๐ฌ FFmpeg