VoiceOverBot is a Telegram bot that transcribes voice messages and video notes (video circles) sent to it using Google's Generative AI (Gemini). It's built with Node.js, TypeScript, and the node-telegram-bot-api library.
Author: Gemini (via Google)
- Receives voice messages and video notes (video circles) in Telegram chats.
- Downloads voice messages directly or extracts audio from video notes using FFmpeg.
- Transcribes the audio using Google's Gemini Pro model (specifically
gemini-2.5-pro-preview-05-06by default) via the Vercel AI SDK. - Replies to the original voice message or video note with the transcribed text.
- Supports various audio file formats sent as documents (MP3, M4A, OGG, WAV, AAC).
- Handles chat member updates: greets when added to a new chat and informs about the need for admin rights to read messages.
- Includes basic error handling and retry mechanisms.
/
├── dist/ # Compiled JavaScript files
├── src/
│ └── index.ts # Main application logic
├── .env # Environment variables (create this file)
├── .gitignore # Git ignore file
├── package.json # Project dependencies and scripts
├── README.md # This file
└── tsconfig.json # TypeScript compiler options
- Node.js (v18 or higher recommended)
- pnpm (or npm/yarn)
- A Telegram Bot Token
- A Google Generative AI API Key
- FFmpeg (for video note audio extraction) - Install from https://ffmpeg.org/
-
Clone the repository (or set up your existing project):
# If you have a git repo already, skip this git clone https://github.com/hormold/voiceoverbot.git cd voiceoverbot
-
Install dependencies:
pnpm install
-
Create a
.envfile in the root of the project and add your API keys and bot token:BOT_TOKEN=YOUR_TELEGRAM_BOT_TOKEN GOOGLE_GENERATIVE_AI_API_KEY=YOUR_GOOGLE_GENERATIVE_AI_API_KEY # Optional: Specify a Gemini model ID (defaults to gemini-2.5-pro-preview-05-06 in the code) # GEMINI_MODEL_ID=gemini-2.5-pro-preview-05-06
- Replace
YOUR_TELEGRAM_BOT_TOKENwith your actual Telegram bot token. - Replace
YOUR_GOOGLE_GENERATIVE_AI_API_KEYwith your Google AI API key.
- Replace
-
Build the project (compile TypeScript to JavaScript):
pnpm build
-
To start the bot for development (with auto-reloading via nodemon):
pnpm dev
This command uses
nodemonto watch for changes insrc/index.tsand automatically restarts the bot. -
To start the bot for production:
pnpm start
This command runs the compiled JavaScript from the
distdirectory.
- The bot connects to Telegram using the
node-telegram-bot-api. - When a voice message is received, the bot downloads the audio file into a buffer.
- When a video note (video circle) is received, the bot:
- Downloads the MP4 video file
- Uses FFmpeg via
fluent-ffmpegto extract the audio track from the video - Converts the audio to MP3 format for compatibility
- For audio documents (MP3, M4A, OGG, WAV, AAC), the bot downloads the file directly.
- The audio data is structured as a
CoreMessagepart withtype: "file", appropriatemimeType, and the audioBuffer. This, along with a text prompt, is sent to the specified Google Gemini model using thegenerateTextfunction from the Vercel AI SDK (aipackage) with the@ai-sdk/googleprovider. - A system prompt instructs the AI on how to behave: transcribe accurately, preserve the original language, apply proper formatting, avoid extraneous content, and strictly use the
outputTranscriptiontool for its response. - The AI is forced (via
toolChoice) to use theoutputTranscriptiontool. This tool is defined with a Zod schema ensuring the AI provides the transcribed text in the expected string format. - When the AI calls the tool, the
executefunction within the tool definition resolves with the transcribed text. - The bot then sends this text back to the Telegram chat as a reply to the original voice message, video note, or audio document.
- The bot also handles being added to new chats by sending a welcome message and mentioning the need for admin permissions to function correctly.
node-telegram-bot-api: For interacting with the Telegram Bot API.ai: Vercel AI SDK for streamlined access to AI models.@ai-sdk/google: Google provider for the Vercel AI SDK.dotenv: For loading environment variables from a.envfile.zod: For schema validation (used for defining the AI tool's parameters).fluent-ffmpeg: For extracting audio from video notes (video circles).
typescript: For TypeScript language support.ts-node: To run TypeScript files directly.nodemon: To automatically restart the application during development.@types/*: Type definitions for various libraries.
Contributions are welcome! If you have suggestions or improvements, feel free to open an issue or submit a pull request.
This project was generated with assistance from Gemini.