Skip to content

About

Transform PDFs into chapter-by-chapter audiobooks via Telegram using Gemini, Deepgram, and Edge-TTS with multi-key quota rotation and instant failover.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

ย 

History

8 Commits

Folders and files

NameName
Last commit message
Last commit date
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ“š Telegram AI Audiobook Bot

Turn PDF books into chapter-by-chapter audiobooks directly inside Telegram.

Built with Python ยท Aiogram ยท Google Gemini ยท Deepgram Aura ยท Microsoft Edge-TTS ยท FFmpeg ยท PyMuPDF


๐Ÿง  About

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.


โœจ Features

๐ŸŽ™๏ธ Multi-Engine Text-to-Speech

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.


๐Ÿ”„ Multi-Key Failover

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.


๐Ÿ“– Smart PDF Processing

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.


โšก Parallel Audio Processing

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.


๐Ÿ’พ Resume & Progress Tracking

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 Stack

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.


๐Ÿงฐ Prerequisites

Before running the bot, install the following.

1. Python

Python 3.10 or newer is recommended.

Check your version:

python --version

2. FFmpeg

FFmpeg is required for audio processing and merging.

Ubuntu / Debian

sudo apt update
sudo apt install ffmpeg

macOS

Using Homebrew:

brew install ffmpeg

Windows

Using Winget:

winget install Gyan.FFmpeg

Verify the installation:

ffmpeg -version

3. Telegram Bot

Create a Telegram bot using @BotFather and obtain your bot token.

Your token should be kept private and should never be committed to GitHub.


๐Ÿš€ Installation

1. Clone the repository

git clone https://github.com/your-username/telegram-audiobook-bot.git
cd telegram-audiobook-bot

2. Create a virtual environment

Windows

python -m venv venv
venv\Scripts\activate

Linux / macOS

python3 -m venv venv
source venv/bin/activate

3. Install dependencies

pip install -r requirements.txt

๐Ÿ” Environment Configuration

Create 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=

โš ๏ธ Keep your .env private

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/

โ–ถ๏ธ Running the Bot

After configuring .env:

python bot.py

If your system uses python3:

python3 bot.py

The bot should then connect to Telegram and begin listening for updates.


๐ŸŽฎ Bot Commands

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.


๐ŸŽ™๏ธ TTS Engine Modes

The bot can be configured to use different processing strategies.

Auto

Gemini
   โ†“
Deepgram
   โ†“
Edge-TTS

The bot attempts the configured primary provider first and falls back according to the application's failure-handling logic.

Fast

Uses the configured fast TTS provider.

Edge

Uses Microsoft Edge-TTS directly.


๐Ÿ” Fallback Architecture

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.


๐Ÿ“š Audiobook Processing Pipeline

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

๐Ÿ“ฆ Project Structure

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

๐Ÿ”’ Security

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:

  1. Revoke the exposed key.
  2. Generate a new key.
  3. Replace it in .env.
  4. Check your Git history if the key was committed.

๐Ÿงช Development

For development, it is recommended to use a virtual environment:

python -m venv venv

Activate it:

Windows

venv\Scripts\activate

Linux / macOS

source venv/bin/activate

Then install dependencies:

pip install -r requirements.txt

๐Ÿ“ Notes

Gemini API Keys

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

Deepgram

The example configuration uses:

aura-2-thalia-en

Deepgram currently documents Aura-2 as a TTS family with multiple voices and languages.

PDF Extraction

PyMuPDF provides PDF text extraction and supports working with several document formats.

Audio Processing

FFmpeg is used by the project for audio processing and merging.


๐Ÿ“œ License

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

About

Transform PDFs into chapter-by-chapter audiobooks via Telegram using Gemini, Deepgram, and Edge-TTS with multi-key quota rotation and instant failover.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages